PSQt/src/GeoTree.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------
00002 // File and Version Information:
00003 //   $Id: GeoTree.cpp 11039 2015-12-01 01:18:58Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Author: Mikhail S. Dubrovin
00006 //---------------------------------------------------------------------
00007 
00008 //--------------------------
00009 
00010 #include "PSQt/GeoTree.h"
00011 #include "PSQt/QGUtils.h"
00012 #include "PSQt/Logger.h"
00013 
00014 //#include <string>
00015 //#include <iomanip>   // for setw, setfill
00016 //#include <math.h>
00017 //#include <stdio.h>
00018 
00019 #include <sstream>   // for stringstream
00020 #include <iostream>    // cout
00021 #include <fstream>    // ifstream(fname), ofstream
00022 
00023 //#include <dirent.h> // for DIR, dirent
00024 
00025 //using namespace std; // for cout without std::
00026 
00027 namespace PSQt {
00028 
00029 //--------------------------
00030 //--------------------------
00031 //--------------------------
00032 //--------------------------
00033 //--------------------------
00034 
00035   GeoTree::GeoTree(QWidget *parent, const std::string& gfname, const unsigned& pbits)
00036     : QTreeView(parent)
00037     , m_gfname(gfname)
00038     , m_pbits(pbits)
00039     , m_model(0)
00040     , m_geoacc(0)
00041 //  : Frame(parent)
00042 //  : QWidget(parent)
00043 {
00044   if (m_pbits & 1) MsgInLog(_name_(), INFO, "In c-tor");
00045 
00046   //setGeometry(100, 100, 200, 300);
00047   setWindowTitle("Geo selection tree");
00048  
00049   makeTreeModel();
00050 
00051   m_view = this;
00052   //m_view = new QTreeView();  
00053 
00054   m_view->setModel(m_model);
00055   m_view->setAnimated(true);
00056   m_view->setHeaderHidden(true);
00057   m_view->expandAll();
00058   //m_view->setMinimumWidth(200);
00059 
00060   //QVBoxLayout *vbox = new QVBoxLayout();
00061   //vbox -> addWidget(m_view);
00062   //setLayout(vbox);
00063   //move(100,50);  
00064 
00065 
00066   //if(m_pbits & 4) 
00067 
00068   //connect(this, SIGNAL(selectedText(const std::string&)), this, SLOT(testSignalString(const std::string&))); 
00069   connect(this, SIGNAL(geometryIsLoaded(PSCalib::GeometryAccess*)), this, SLOT(testSignalGeometryIsLoaded(PSCalib::GeometryAccess*))); 
00070   connect(this, SIGNAL(selectedGO(shpGO&)), this, SLOT(testSignalGO(shpGO&))); 
00071   //connect(this, SIGNAL(collapsed(const QModelIndex&)), this, SLOT(testSignalCollapsed(const QModelIndex&)));
00072   //connect(this, SIGNAL(expanded(const QModelIndex&)), this, SLOT(testSignalExpanded(const QModelIndex&)));
00073 }
00074 
00075 //--------------------------
00076 
00077 void 
00078 GeoTree::makeTreeModel()
00079 {
00080   if ( m_model == 0 ) {
00081     m_model = new QStandardItemModel();
00082     //m_model->setHorizontalHeaderLabels(QStringList(QString('o')));
00083     updateTreeModel(m_gfname);
00084   }
00085 }
00086 
00087 //--------------------------
00088 
00089 bool
00090 GeoTree::loadGeometry(const std::string& gfname)
00091 {
00092   if (!file_exists(gfname)) {
00093     stringstream ss; ss << "Geometry file \"" << gfname << "\" does not exist";
00094     MsgInLog(_name_(), WARNING, ss.str());
00095     return false;
00096   }
00097 
00098   if (! m_geoacc) delete m_geoacc;
00099   m_geoacc = new PSCalib::GeometryAccess(gfname);
00100   if (m_pbits & 2) m_geoacc->print_list_of_geos();
00101 
00102   emit geometryIsLoaded(m_geoacc);
00103   
00104   return true;
00105 }
00106 
00107 //--------------------------
00108 
00109 void 
00110 GeoTree::updateTreeModel(const std::string& gfname)
00111 {
00112   //if(m_pbits & 4) 
00113   //std::cout << "updateTreeModel for file: " << gfname << '\n';
00114   MsgInLog(_name_(), INFO, std::string("Update geometry-tree model for file: ") + gfname); 
00115 
00116   if(! loadGeometry(gfname)) return;
00117 
00118   m_model->clear();
00119   map_item_to_geo.clear();
00120   map_geo_to_item.clear();
00121 
00122   fillTreeModel(shpGO(),0,0,m_pbits);
00123   //fillTreeModelTest();
00124 
00125   //m_view->expandAll();
00126   expandAll();
00127 
00128 }
00129 
00130 //--------------------------
00131 
00132 void 
00133 GeoTree::fillTreeModel( shpGO geo_add
00134                       , QStandardItem* parent
00135                       , const unsigned& level
00136                       , const unsigned& pbits )
00137 {
00138    shpGO geo = (geo_add != shpGO()) ? geo_add : m_geoacc->get_top_geo();
00139 
00140    QStandardItem* item_parent = (parent) ? parent : m_model->invisibleRootItem();
00141 
00142    stringstream ss; ss << geo->get_geo_name() << "." << geo->get_geo_index();
00143    std::string iname = ss.str();
00144    QStandardItem* item_add = new QStandardItem(iname.c_str());
00145 
00146    item_parent->appendRow(item_add);
00147 
00148    map_item_to_geo[item_add] = geo;
00149    map_geo_to_item[geo_add] = item_add;
00150 
00151    std::vector<shpGO> list = geo->get_list_of_children();
00152 
00153    if(pbits & 1) {stringstream ss; ss << "==== Add item: \"" << iname << "\" number of children:" << list.size();}
00154       
00155    if (list.size()) {
00156        if(pbits & 1) ss << "    add as a group\n";
00157 
00158        //if (parent) item_add->setCheckable(true);
00159 
00160        for(std::vector<shpGO>::iterator it=list.begin(); it!= list.end(); ++ it) { 
00161          //if(pbits & 2) cout << " add child from the list: " << (*it)->get_geo_name() << '/n';
00162          fillTreeModel(*it, item_add, level+1, pbits);
00163        }
00164    }
00165    else {
00166        if(pbits & 1) {ss << "    add as a set\n";
00167        //item_add->setIcon(icon);
00168        //item_add->setCheckable(true);
00169          MsgInLog(_name_(), INFO, ss.str()); 
00170        }
00171        return;
00172    }
00173    if(pbits & 1) MsgInLog(_name_(), INFO, ss.str()); 
00174 }
00175 
00176 //--------------------------
00177 
00178 void 
00179 GeoTree::fillTreeModelTest()
00180 {
00181    QStandardItem* item_parent = m_model->invisibleRootItem();
00182 
00183    QStandardItem* item1 = new QStandardItem("item1");
00184    QStandardItem* item2 = new QStandardItem("item2");
00185    QStandardItem* item3 = new QStandardItem("item3");
00186 
00187    item_parent->appendRow(item1);
00188    item_parent->appendRow(item2);
00189    item_parent->appendRow(item3);
00190 
00191    QStandardItem* item31 = new QStandardItem("item31");
00192    QStandardItem* item32 = new QStandardItem("item32");
00193 
00194    item3->appendRow(item31);
00195    item3->appendRow(item32);
00196 }
00197 
00198 //--------------------------
00199 
00200 void 
00201 GeoTree::currentChanged(const QModelIndex & index, const QModelIndex & index_old)
00202 {
00203   //std::cout << "currentChanged:: new r:" << index.row() << " c:" << index.column() 
00204   //                      << "     old r:" << index_old.row() << " c:" << index_old.column() << '\n';
00205 
00206   QStandardItem *item = m_model->itemFromIndex(index);
00207 
00208   std::string str(item->text().toStdString());
00209 
00210   //if(m_pbits & 4) {
00211   //  stringstream ss; ss << "currentChanged::item"
00212   //          << " r:" << item->row() 
00213   //          << " c:" << item->column() 
00214   //        << " a:" << item->accessibleText().toStdString()
00215   //        << " t:" << str; }
00216 
00217   MsgInLog(_name_(), INFO, "Selected geometry object: " + str); 
00218 
00219   //emit selectedText(str);
00220   emit selectedGO(map_item_to_geo[item]);
00221 }
00222 
00223 //--------------------------
00224 
00225 void 
00226 GeoTree::setItemSelected(const QModelIndex& index)
00227 {
00228   this->selectionModel()->select(index, QItemSelectionModel::Select);
00229   currentChanged(index,index);
00230 }
00231 
00232 //--------------------------
00233 
00234 void 
00235 GeoTree::setItemSelected(const QStandardItem* item)
00236 {
00237   std::map<shpGO,QStandardItem*>::iterator it=map_geo_to_item.begin();
00238   const QStandardItem* item_sel = (item) ? item : it->second;
00239   this->setItemSelected(m_model->indexFromItem(item_sel));
00240 }
00241 //--------------------------
00242 
00243 void
00244 GeoTree::saveGeometryInFile(const std::string& ofname)
00245 {
00246   if (! m_geoacc) {
00247     MsgInLog(_name_(), WARNING, "Geometry object does not exist and can't be saved");
00248     return;
00249   }
00250 
00251   MsgInLog(_name_(), INFO, "Save geometry in file " + ofname);
00252   m_geoacc->save_pars_in_file(ofname);
00253 }
00254 
00255 //--------------------------
00256 //--------------------------
00257 //--------------------------
00258 //--------------------------
00259 void 
00260 GeoTree::testSignalGeometryIsLoaded(PSCalib::GeometryAccess*)
00261 {
00262   MsgInLog(_name_(), DEBUG, "testSignalGeometryIsLoaded"); 
00263 }
00264 
00265 //--------------------------
00266 
00267 void 
00268 GeoTree::testSignalString(const std::string& str)
00269 {
00270   MsgInLog(_name_(), DEBUG, "GeoTree::testSignalString(string): str = " + str); 
00271 }
00272 
00273 //--------------------------
00274 
00275 void 
00276 GeoTree::testSignalGO(shpGO& geo)
00277 {
00278   stringstream ss; ss << "GeoTree::testSignalGO(shpGO): "; // << geo.get()->get_geo_name() << ' ' << geo.get()->get_geo_index() << '\n';   // geo.get()->print_geo();
00279   ss << geo->string_geo();
00280   MsgInLog(_name_(), DEBUG, ss.str()); 
00281 }
00282 
00283 //--------------------------
00284 
00285 void 
00286 GeoTree::testSignalCollapsed(const QModelIndex& index)
00287 {
00288   stringstream ss; ss << "GeoTree::testSignalCollapsed(shpGO): row:" << index.row() << " col:" << index.column();
00289   MsgInLog(_name_(), DEBUG, ss.str()); 
00290 }
00291 
00292 //--------------------------
00293 
00294 void 
00295 GeoTree::testSignalExpanded(const QModelIndex& index)
00296 {
00297   stringstream ss; ss << "GeoTree::testSignalExpanded(shpGO): row:" << index.row() << " col:" << index.column();
00298   MsgInLog(_name_(), DEBUG, ss.str()); 
00299 }
00300 
00301 //--------------------------
00302 //--------------------------
00303 //--------------------------
00304 //--------------------------
00305 //--------------------------
00306 //--------------------------
00307 
00308 } // namespace PSQt
00309 
00310 //--------------------------

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7