CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Fireworks/Core/src/FWGeometryBrowser.cc

Go to the documentation of this file.
00001 #include "Fireworks/Core/interface/FWGeometryBrowser.h"
00002 #include "Fireworks/Core/interface/FWGUIManager.h"
00003 #include "Fireworks/TableWidget/interface/FWTableWidget.h"
00004 
00005 #include "Fireworks/Core/interface/FWParameterSetterBase.h"
00006 #include "Fireworks/Core/interface/FWGeometryTableManager.h"
00007 #include "Fireworks/Core/interface/fwLog.h"
00008 
00009 
00010 #include "TFile.h"
00011 #include "TGFileDialog.h"
00012 #include "TGeoNode.h"
00013 #include "TGStatusBar.h"
00014 #include "TGButton.h"
00015 #include "TGLabel.h"
00016 
00017 bool geodebug = 0;
00018 
00019 #include <iostream>
00020 FWGeometryBrowser::FWGeometryBrowser(FWGUIManager *guiManager)
00021    : TGMainFrame(gClient->GetRoot(), 600, 500),
00022      m_mode(this, "Mode:", 1l, 0l, 1l),
00023      m_filter(this,"Materials:",std::string()),
00024      m_autoExpand(this,"AutoExpand:", 5l, 0l, 1000l),
00025      m_maxDaughters(this,"MaxChildren:", 999l, 0l, 1000l), // debug
00026      m_guiManager(guiManager),
00027      m_tableManager(0),
00028      m_geometryFile(0),
00029      m_fileOpen(0),
00030      m_settersFrame(0)
00031 {
00032    m_mode.addEntry(0, "Node");
00033    m_mode.addEntry(1, "Volume");
00034    
00035    m_tableManager = new FWGeometryTableManager(this);
00036  
00037    TGTextButton* m_fileOpen = new TGTextButton (this, "Open Geometry File");
00038    this->AddFrame(m_fileOpen,  new TGLayoutHints( kLHintsExpandX , 4, 2, 2, 2));
00039    m_fileOpen->Connect("Clicked()","FWGeometryBrowser",this,"browse()");
00040 
00041 
00042    m_settersFrame = new TGHorizontalFrame(this);
00043    this->AddFrame( m_settersFrame,new TGLayoutHints(kLHintsExpandX, 2, 2, 2, 2));
00044    m_settersFrame->SetCleanup(kDeepCleanup);
00045 
00046    m_tableWidget = new FWTableWidget(m_tableManager, this); 
00047    AddFrame(m_tableWidget,new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,2,2,2,2));
00048    m_tableWidget->SetBackgroundColor(0xffffff);
00049    m_tableWidget->SetLineSeparatorColor(0x000000);
00050    m_tableWidget->SetHeaderBackgroundColor(0xececec);
00051    m_tableWidget->Connect("cellClicked(Int_t,Int_t,Int_t,Int_t,Int_t,Int_t)",
00052                           "FWGeometryBrowser",this,
00053                           "cellClicked(Int_t,Int_t,Int_t,Int_t,Int_t,Int_t)");
00054    m_tableWidget->disableGrowInWidth();
00055    resetSetters();
00056 
00057    m_statBar = new TGStatusBar(this, this->GetWidth(), 12);
00058    m_statBar->SetText("No simulation geomtery loaded.");
00059    AddFrame(m_statBar, new TGLayoutHints(kLHintsExpandX));
00060 
00061    SetWindowName("Geometry Browser");
00062    this->Connect("CloseWindow()","FWGeometryBrowser",this,"windowIsClosing()");
00063    Layout();
00064    MapSubwindows();
00065 
00066    gVirtualX->SelectInput(GetId(), kKeyPressMask | kKeyReleaseMask | kExposureMask |
00067                           kPointerMotionMask | kStructureNotifyMask | kFocusChangeMask |
00068                           kEnterWindowMask | kLeaveWindowMask);
00069 }
00070 
00071 FWGeometryBrowser::~FWGeometryBrowser()
00072 {}
00073 
00074 void
00075 FWGeometryBrowser::resetSetters()
00076 {
00077    if (!m_settersFrame->GetList()->IsEmpty())
00078    {
00079       m_setters.clear();
00080       TGFrameElement *el = (TGFrameElement*) m_settersFrame->GetList()->First();
00081       m_settersFrame->RemoveFrame(el->fFrame);
00082    }
00083    
00084    TGHorizontalFrame* frame = new TGHorizontalFrame(m_settersFrame);
00085    m_settersFrame->AddFrame(frame);
00086    makeSetter(frame, &m_mode);
00087    makeSetter(frame, &m_filter);
00088    makeSetter(frame, &m_autoExpand);
00089    if (geodebug) makeSetter(frame, &m_maxDaughters);
00090    m_settersFrame->MapSubwindows();
00091    Layout();
00092 }
00093 
00094 void
00095 FWGeometryBrowser::makeSetter(TGCompositeFrame* frame, FWParameterBase* param) 
00096 {
00097    boost::shared_ptr<FWParameterSetterBase> ptr( FWParameterSetterBase::makeSetterFor(param) );
00098    ptr->attach(param, this);
00099  
00100    TGFrame* pframe = ptr->build(frame, false);
00101    frame->AddFrame(pframe, new TGLayoutHints(kLHintsExpandX));
00102 
00103    m_setters.push_back(ptr);
00104 }
00105 //==============================================================================
00106 
00107 
00108 void
00109 FWGeometryBrowser::addTo(FWConfiguration& iTo) const
00110 {
00111    FWConfigurableParameterizable::addTo(iTo);
00112 }
00113   
00114 void
00115 FWGeometryBrowser::setFrom(const FWConfiguration& iFrom)
00116 { 
00117    for(const_iterator it =begin(), itEnd = end();
00118        it != itEnd;
00119        ++it) {
00120       
00121       if (!geodebug && (&m_maxDaughters == (*it)))
00122           continue;
00123           
00124       (*it)->setFrom(iFrom);
00125 
00126    }  
00127    resetSetters();
00128 }
00129 
00130 //==============================================================================
00131 void 
00132 FWGeometryBrowser::cellClicked(Int_t iRow, Int_t iColumn, Int_t iButton, Int_t iKeyMod, Int_t, Int_t)
00133 {
00134    if (iButton != kButton1)
00135    {
00136       // m_tableManager->setSelection(iRow, iColumn, iKeyMod);
00137       return;   
00138    }
00139  
00140    if (iButton == kButton1 && iColumn == 0)
00141    {
00142       m_tableManager->firstColumnClicked(iRow);
00143    }
00144 }
00145 
00146 bool FWGeometryBrowser::HandleKey(Event_t *event)
00147 {
00148       if (!fBindList) return kFALSE;
00149 
00150       TIter next(fBindList);
00151       TGMapKey *m;
00152       TGFrame  *w = 0;
00153 
00154       while ((m = (TGMapKey *) next())) {
00155          if (m->fKeyCode == event->fCode) {
00156             w = (TGFrame *) m->fWindow;
00157             if (w->HandleKey(event)) return kTRUE;
00158          }
00159       }
00160       return kFALSE;
00161 }
00162 
00163 void
00164 FWGeometryBrowser::windowIsClosing()
00165 {
00166   UnmapWindow();
00167 }
00168 
00169 void
00170 FWGeometryBrowser::newIndexSelected(int iSelectedRow, int iSelectedColumn)
00171 {
00172   if (iSelectedRow == -1)
00173     return;
00174 
00175   m_tableManager->dataChanged();
00176 }
00177 
00178 void 
00179 FWGeometryBrowser::readFile()
00180 {
00181    try {
00182 
00183       if ( ! m_geometryFile )
00184          throw std::runtime_error("No root file.");
00185   
00186       m_geometryFile->ls();
00187       
00188       if ( !m_geometryFile->Get("cmsGeo;1"))
00189          throw std::runtime_error("Can't find TGeoManager object in selected file.");
00190 
00191       TGeoManager* m_geoManager = (TGeoManager*) m_geometryFile->Get("cmsGeo;1");
00192       m_tableManager->loadGeometry(m_geoManager);
00193       MapRaised();
00194 
00195    }
00196    catch (std::runtime_error &e)
00197    {
00198       fwLog(fwlog::kError) << "Failed to load simulation geomtery.\n";
00199       updateStatusBar("Failed to load simulation geomtery from file");
00200    }
00201 }
00202 
00203 void
00204 FWGeometryBrowser::browse()
00205 {
00206    std::cout<<"FWGeometryBrowser::openFile()"<<std::endl;
00207 
00208    if (!geodebug)
00209    {  
00210       const char* kRootType[] = {"ROOT files","*.root", 0, 0};
00211       TGFileInfo fi;
00212       fi.fFileTypes = kRootType;
00213  
00214       m_guiManager->updateStatus("opening geometry file...");
00215 
00216       new TGFileDialog(gClient->GetDefaultRoot(), 
00217                        (TGWindow*) m_guiManager->getMainFrame(), kFDOpen, &fi);
00218 
00219       m_guiManager->updateStatus("loading geometry file...");
00220       m_geometryFile = new TFile(fi.fFilename, "READ");
00221    }
00222    else
00223    {
00224       // AMT developing environment
00225       m_geometryFile = new TFile("cmsSimGeom-14.root", "READ");
00226    }
00227    m_guiManager->clearStatus();
00228 
00229    readFile();
00230 }
00231 
00232 
00233 void FWGeometryBrowser::updateStatusBar(const char* status) {
00234    m_statBar->SetText(status, 0);
00235 }