CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Fireworks/Core/src/FWGeometryTableView.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWGeometryTableView
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  
00010 //         Created:  Wed Jan  4 00:05:34 CET 2012
00011 // $Id: FWGeometryTableView.cc,v 1.40 2012/05/10 21:31:18 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <boost/bind.hpp>
00016 
00017 // user include files
00018 #include "Fireworks/Core/src/FWGeometryTableView.h"
00019 #include "Fireworks/Core/src/FWGeoTopNodeScene.h"
00020 #include "Fireworks/Core/interface/FWGeometryTableViewManager.h"
00021 #include "Fireworks/Core/interface/FWViewType.h"
00022 #include "Fireworks/Core/interface/FWGeometryTableManagerBase.h"
00023 #include "Fireworks/Core/interface/CmsShowViewPopup.h"
00024 #include "Fireworks/Core/src/FWGeometryTableManager.h"
00025 #include "Fireworks/Core/interface/fwLog.h"
00026 #include "Fireworks/Core/interface/FWEnumParameter.h"
00027 #include "Fireworks/Core/interface/FWEnumParameterSetter.h"
00028 
00029 #include "Fireworks/Core/src/FWGUIValidatingTextEntry.h"
00030 #include "Fireworks/Core/interface/FWGUIManager.h"
00031 #include "Fireworks/Core/src/FWValidatorBase.h"
00032 #include "Fireworks/Core/src/FWEveDetectorGeo.h"
00033 
00034 #include "KeySymbols.h"
00035 #include "TGButton.h"
00036 #include "TGComboBox.h"
00037 #include "TGLabel.h"
00038 #include "TGListBox.h"
00039 #include "TGLViewer.h"
00040 #include "TGeoMatrix.h"
00041 #include "TGeoBBox.h"
00042 
00043 #include "TEveViewer.h"
00044 #include "TEveScene.h"
00045 #include "TEveSceneInfo.h"
00046 #include "TEveManager.h"
00047 #include "TGeoManager.h"
00048 #include "TGLCamera.h"
00049 
00050 
00051 //==============================================================================
00052 //==============================================================================
00053 //==============================================================================
00054 //==============================================================================
00055 
00056 
00057 class FWGeoMaterialValidator : public FWValidatorBase 
00058 {
00059 public:
00060 
00061    FWGeometryTableView* m_browser;
00062   mutable std::vector<const char*> m_list;
00063    FWGeoMaterialValidator( FWGeometryTableView* v) { m_browser = v;}
00064    virtual ~FWGeoMaterialValidator() {}
00065 
00066   virtual void addDaughtersRec(TGeoVolume* v) const
00067   {
00068     switch (m_browser->getFilterType())
00069     {
00070       case FWGeometryTableView::kFilterMaterialName:
00071         m_list.push_back(v->GetMaterial()->GetName());
00072         break;
00073       case FWGeometryTableView::kFilterMaterialTitle:
00074         m_list.push_back(v->GetMaterial()->GetTitle());
00075         break;
00076       case FWGeometryTableView::kFilterShapeName:
00077         m_list.push_back(v->GetShape()->GetName());
00078         break;
00079       case FWGeometryTableView::kFilterShapeClassName:
00080         m_list.push_back(v->GetShape()->ClassName());
00081         break;
00082       default:
00083         std::cerr << "FWGeoMaterialValidator unhandeled case. \n";
00084     }
00085 
00086     for (int i = 0; i < v->GetNdaughters(); ++i)
00087       addDaughtersRec(v->GetNode(i)->GetVolume());      
00088 
00089   }
00090 
00091    virtual void fillOptions(const char* iBegin, const char* iEnd, std::vector<std::pair<boost::shared_ptr<std::string>, std::string> >& oOptions) const 
00092    {
00093       oOptions.clear();
00094       m_list.clear();
00095 
00096     FWGeometryTableManagerBase::Entries_i tnit = m_browser->getTableManager()->refEntries().begin();
00097     std::advance(tnit, m_browser->getTopNodeIdx());
00098     FWGeometryTableViewManager::getGeoMangeur();
00099     addDaughtersRec(tnit->m_node->GetVolume());
00100 
00101     std::sort(m_list.begin(), m_list.end());
00102     std::vector<const char*>::iterator ui = std::unique(m_list.begin(), m_list.end());
00103     m_list.resize(ui - m_list.begin());
00104 
00105     std::string part(iBegin,iEnd);
00106     unsigned int part_size = part.size();
00107       std::string h = "";
00108   // int cnt = 0;  
00109     oOptions.push_back(std::make_pair(boost::shared_ptr<std::string>(new std::string(*m_list.begin())), h));
00110     std::vector<const char*>::iterator startIt = m_list.begin();  startIt++;
00111     for (std::vector<const char*>::iterator i = startIt; i!=m_list.end(); ++i)
00112       {
00113     //      std::cout << *i << " " << cnt++ << std::endl;
00114       if ((strlen(*i) >= part_size) && strncmp(*i, part.c_str(), part_size ) == 0)
00115          {
00116         oOptions.push_back(std::make_pair(boost::shared_ptr<std::string>(new std::string((*i))),&((*i)[part_size]) ));
00117          }
00118       }
00119    }
00120 
00121    bool isStringValid(std::string& exp) 
00122    {
00123     std::cerr << "validation not used \n";
00124       return false;
00125    }
00126 };
00127 
00128 
00129 //==============================================================================
00130 //==============================================================================
00131 //==============================================================================
00132 //==============================================================================
00133 FWGeometryTableView::FWGeometryTableView(TEveWindowSlot* iParent, FWColorManager* colMng)
00134    : FWGeometryTableViewBase(iParent, FWViewType::kGeometryTable, colMng),
00135      m_tableManager(0),
00136      m_filterEntry(0),
00137      m_filterValidator(0),
00138      m_mode(this, "Mode", 0l, 0l, 1l),
00139      m_disableTopNode(this,"HideTopNode", true),
00140      m_visLevel(this,"VisLevel", 3l, 1l, 100l),
00141      m_filter(this,"Materials", std::string()),
00142      m_filterType(this,"FilterType:", 0l, 0l, 3l),
00143      m_visLevelFilter(this,"IgnoreVisLevelOnFilter", true),
00144      m_selectRegion(this, "SelectNearCameraCenter", false),
00145      m_regionRadius(this, "SphereRadius", 10.0, 1.0, 300.0),
00146      m_proximityAlgo(this, "Proximity algorithm", 1l, 0l, 1l)
00147 {
00148    FWGeoTopNodeGLScene *gls = new FWGeoTopNodeGLScene(0);
00149 #if ROOT_VERSION_CODE < ROOT_VERSION(5,32,0)
00150    m_eveScene  = new  FWGeoTopNodeEveScene(gls, "TopGeoNodeScene", "");
00151 #else
00152    m_eveScene  = new  TEveScene(gls, "TopGeoNodeScene", "");
00153 #endif
00154    gEve->GetScenes()->AddElement(m_eveScene);
00155 
00156    m_eveTopNode = new  FWEveDetectorGeo(this);
00157    m_eveTopNode->IncDenyDestroy();
00158    m_eveTopNode->SetPickable(true);
00159    m_eveScene->AddElement(m_eveTopNode);
00160 
00161    gls->m_eveTopNode = m_eveTopNode;
00162    m_eveTopNode->m_scene   = gls;
00163 
00164    // top row
00165    TGHorizontalFrame *hp = new TGHorizontalFrame(m_frame);
00166    {
00167       TGTextButton *rb = new TGTextButton (hp, "CdTop");
00168       hp->AddFrame(rb, new TGLayoutHints(kLHintsNormal, 2, 2, 0, 0) );
00169       rb->Connect("Clicked()","FWGeometryTableViewBase",this,"cdTop()");
00170    } 
00171    {
00172       TGTextButton *rb = new TGTextButton (hp, "CdUp");
00173       hp->AddFrame(rb, new TGLayoutHints(kLHintsNormal, 2, 2, 0, 0));
00174       rb->Connect("Clicked()","FWGeometryTableViewBase",this,"cdUp()");
00175    }
00176    {
00177       m_viewBox = new FWViewCombo(hp, this);
00178       hp->AddFrame( m_viewBox,new TGLayoutHints(kLHintsExpandY, 2, 2, 0, 0));
00179    }
00180    {
00181 
00182       m_filterType.addEntry(kFilterMaterialName,   "MaterialName");
00183       m_filterType.addEntry(kFilterMaterialTitle,  "MaterialTitle");
00184       m_filterType.addEntry(kFilterShapeName,      "ShapeName");
00185       m_filterType.addEntry(kFilterShapeClassName, "ShapeClassName");
00186 
00187       boost::shared_ptr<FWParameterSetterBase> ptr( FWParameterSetterBase::makeSetterFor((FWParameterBase*)&m_filterType) );
00188       ptr->attach((FWParameterBase*)&m_filterType, this);
00189 
00190       TGFrame* pframe = ptr->build(hp, false);
00191       hp->AddFrame(pframe, new TGLayoutHints(kLHintsLeft));
00192       m_filterTypeSetter.swap(ptr);
00193    }
00194    {
00195       hp->AddFrame(new TGLabel(hp, "FilterExp:"), new TGLayoutHints(kLHintsBottom, 0, 0, 0, 2));
00196       m_filterEntry = new FWGUIValidatingTextEntry(hp);
00197       m_filterEntry->SetHeight(20);
00198       m_filterValidator = new FWGeoMaterialValidator(this);
00199       m_filterEntry->setValidator(m_filterValidator);
00200       hp->AddFrame(m_filterEntry, new TGLayoutHints(kLHintsExpandX,  0, 2, 1, 0));
00201       m_filterEntry->setMaxListBoxHeight(150);
00202       m_filterEntry->getListBox()->Connect("Selected(int)", "FWGeometryTableView",  this, "filterListCallback()");
00203       m_filterEntry->Connect("ReturnPressed()", "FWGeometryTableView",  this, "filterTextEntryCallback()");
00204 
00205       gVirtualX->GrabKey( m_filterEntry->GetId(),gVirtualX->KeysymToKeycode((int)kKey_A),  kKeyControlMask, true);
00206    }
00207    m_frame->AddFrame(hp,new TGLayoutHints(kLHintsLeft|kLHintsExpandX, 2, 2, 2, 0));
00208 
00209    m_tableManager = new FWGeometryTableManager(this);
00210    {
00211       TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
00212       m_tableManager->loadGeometry( gGeoManager->GetTopNode(), gGeoManager->GetListOfVolumes());
00213    }
00214    cdTop();
00215 
00216    m_mode.addEntry(kNode,   "Node");
00217    m_mode.addEntry(kVolume, "Volume");
00218 
00219    m_mode.changed_.connect(boost::bind(&FWGeometryTableView::refreshTable3D,this));
00220    m_autoExpand.changed_.connect(boost::bind(&FWGeometryTableView::autoExpandCallback, this));
00221    m_visLevel.changed_.connect(boost::bind(&FWGeometryTableView::refreshTable3D,this));
00222    
00223    
00224    m_visLevelFilter.changed_.connect(boost::bind(&FWGeometryTableView::refreshTable3D,this));
00225 
00226    m_disableTopNode.changed_.connect(boost::bind(&FWGeometryTableView::updateVisibilityTopNode,this));
00227    postConst();
00228 
00229    m_proximityAlgo.addEntry(kBBoxCenter,  "BBox center");
00230    m_proximityAlgo.addEntry(kBBoxSurface, "BBox surface");
00231 
00232    m_selectRegion.changed_.connect(boost::bind(&FWGeometryTableView::checkRegionOfInterest,this));
00233    m_regionRadius.changed_.connect(boost::bind(&FWGeometryTableView::checkRegionOfInterest,this));
00234    m_proximityAlgo.changed_.connect(boost::bind(&FWGeometryTableView::checkRegionOfInterest,this));
00235    
00236 }
00237 
00238 FWGeometryTableView::~FWGeometryTableView()
00239 {}
00240 
00241 //______________________________________________________________________________
00242 void FWGeometryTableView::setPath(int parentIdx, std::string&)
00243 {
00244    m_eveTopNode->clearSelection();
00245 
00246    m_topNodeIdx.set(parentIdx);
00247    getTableManager()->refEntries().at(getTopNodeIdx()).setBitVal(FWGeometryTableManagerBase::kVisNodeSelf,!m_disableTopNode.value() );
00248    getTableManager()->setLevelOffset(getTableManager()->refEntries().at(getTopNodeIdx()).m_level);
00249 
00250    checkExpandLevel();
00251    refreshTable3D(); 
00252 }
00253 
00254 //______________________________________________________________________________
00255 FWGeometryTableManagerBase* FWGeometryTableView::getTableManager()
00256 {
00257    return m_tableManager;
00258 }
00259 
00260 //______________________________________________________________________________
00261 void FWGeometryTableView::autoExpandCallback()
00262 { 
00263    if (!m_enableRedraw) return;
00264    checkExpandLevel();
00265    getTableManager()->redrawTable(true);
00266 }
00267 
00268 //______________________________________________________________________________
00269 void FWGeometryTableView::filterTextEntryCallback()
00270 {
00271    // std::cout << "text entry click ed \n" ;
00272    std::string exp = m_filterEntry->GetText();
00273    updateFilter(exp);
00274 }
00275 
00276 //______________________________________________________________________________
00277 void FWGeometryTableView::filterListCallback()
00278 { 
00279    // std::cout << "list click ed [" << m_filterEntry->GetText() << "] \n" ;
00280 
00281    std::string exp = m_filterEntry->GetText();
00282    updateFilter(exp);
00283 }
00284 
00285 //______________________________________________________________________________
00286 void FWGeometryTableView::updateFilter(std::string& exp)
00287 {
00288    // std::cout << "=FWGeometryTableViewBase::updateFilter()" << m_filterEntry->GetText() <<std::endl;
00289   
00290    
00291    if (exp.empty())
00292    {
00293       // std::cout << "FITLER OFF \n";
00294       for (FWGeometryTableManagerBase::Entries_i i = m_tableManager->refEntries().begin(); i !=  m_tableManager->refEntries().end(); ++i)
00295       {
00296          m_tableManager->setVisibility(*i, true);
00297          m_tableManager->setVisibilityChld(*i, true);
00298       }
00299 
00300       // NOTE: entry should be cleared automatically
00301       m_filterEntry->Clear();
00302    }
00303   
00304    m_filter.set(exp);
00305    m_tableManager->updateFilter(m_filterType.value());
00306    refreshTable3D();
00307 
00308 }
00309 
00310 //==============================================================================
00311 
00312 void FWGeometryTableView::populateController(ViewerParameterGUI& gui) const
00313 {
00314   gui.requestTab("Style").
00315     addParam(&m_mode).
00316     addParam(&m_autoExpand).
00317     separator().
00318     addParam(&m_disableTopNode).
00319     addParam(&m_minParentTransparency).
00320     addParam(&m_minLeafTransparency).
00321     addParam(&m_visLevel).
00322     addParam(&m_visLevelFilter).
00323     separator().
00324     addParam(&m_selectRegion).
00325     addParam(&m_regionRadius).
00326     addParam(&m_proximityAlgo).separator();
00327 
00328    TGTextButton* butt = new TGTextButton(gui.getTabContainer(), "ReloadColors");
00329    gui.getTabContainer()->AddFrame(butt);
00330    butt->Connect("Clicked()", "FWGeometryTableViewBase", (FWGeometryTableViewBase*)this, "reloadColors()");
00331 }
00332 
00333 
00334 //------------------------------------------------------------------------------
00335 
00336 /*
00337 void FWGeometryTableView::setPath(int parentIdx, std::string& path)
00338 {
00339    //   printf("Set Path to [%s], current node \n", path.c_str());
00340    m_topNodeIdx.set(parentIdx);
00341    getTableManager()->refEntries().at(getTopNodeIdx()).setBitVal(FWGeometryTableManagerBase::kVisNodeSelf,!m_disableTopNode.value() );
00342    getTableManager()->setLevelOffset(getTableManager()->refEntries().at(getTopNodeIdx()).m_level);
00343 
00344    m_eveTopNode->clearSelection(); 
00345 
00346    checkExpandLevel();
00347    refreshTable3D(); 
00348    FWGUIManager::getGUIManager()->updateStatus(path.c_str());
00349    }*/
00350 
00351 //--------------------------------------------------------------
00352 bool viewIsChecked(TEveViewer* v, TEveElement* el)
00353 {
00354    if (strstr( v->GetElementName(), "3D") )
00355    {
00356       for (TEveElement::List_i eit = v->BeginChildren(); eit != v->EndChildren(); ++eit )
00357       {
00358          TEveScene* s = ((TEveSceneInfo*)*eit)->GetScene();
00359          if (el && s->HasChildren() && s->FirstChild() == el) 
00360             return true;
00361       }
00362       
00363    }
00364    return false;
00365 }
00366 
00367 void FWGeometryTableView::checkRegionOfInterest()
00368 {
00369    if (m_selectRegion.value())
00370    {
00371       double* center = 0;
00372       for (TEveElement::List_i it = gEve->GetViewers()->BeginChildren(); it != gEve->GetViewers()->EndChildren(); ++it)
00373       { 
00374          TEveViewer* v = ((TEveViewer*)(*it));
00375          if (viewIsChecked(v, m_eveTopNode))
00376          {
00377             if (center) {
00378                fwLog(fwlog::kWarning) << "Center picked from first view \n";
00379             } else {
00380                center = v->GetGLViewer()->CurrentCamera().GetCenterVec();
00381                fwLog(fwlog::kInfo) << Form("Center picked (%.1f, %.1f, %.1f) from first selected 3D view \n", 
00382                                            center[0], center[1], center[2]);
00383             }
00384          }
00385       } 
00386 
00387       if (! center)
00388       {
00389          fwLog(fwlog::kError) << "No 3D view selected \n";
00390          return;
00391       }
00392       
00393       m_tableManager->checkRegionOfInterest(center, m_regionRadius.value(), m_proximityAlgo.value());
00394    }
00395    else 
00396    {
00397       m_tableManager->resetRegionOfInterest();
00398    }
00399 
00400    refreshTable3D();
00401 }
00402 
00403 //------------------------------------------------------------------------------
00404 
00405 void FWGeometryTableView::setFrom(const FWConfiguration& iFrom)
00406 { 
00407    m_enableRedraw = false;
00408    for (const_iterator it =begin(), itEnd = end(); it != itEnd; ++it)
00409    {
00410       // printf("set from %s \n",(*it)->name().c_str() );
00411       if ((*it)->name() == m_topNodeIdx.name()  )
00412          setTopNodePathFromConfig(iFrom);
00413       else 
00414          (*it)->setFrom(iFrom);
00415    }  
00416    
00417    
00418    cdNode(m_topNodeIdx.value());
00419 
00420    TGComboBox* cbox = ((FWEnumParameterSetter*) m_filterTypeSetter.get())->getWidget();
00421    cbox->Select(m_filterType.value(), false);
00422    m_viewersConfig = iFrom.valueForKey("Viewers");
00423 
00424 
00425    m_enableRedraw = true;
00426    m_filterEntry->SetText(m_filter.value().c_str(), false);
00427    m_tableManager->updateFilter(m_filterType.value());
00428    checkExpandLevel();
00429    refreshTable3D();
00430    /*
00431      getTableManager()->redrawTable();
00432      m_eveTopNode->ElementChanged();
00433      gEve->FullRedraw3D(false, true);
00434    */
00435 }
00436 
00437 //------------------------------------------------------------------------------
00438 
00439 void FWGeometryTableView::updateVisibilityTopNode()
00440 {
00441    getTableManager()->refEntries().at(getTopNodeIdx()).setBitVal(FWGeometryTableManagerBase::kVisNodeSelf,!m_disableTopNode.value() );
00442    refreshTable3D();
00443 }