CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Fireworks/Core/src/FWDetailViewManager.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWDetailViewManager
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Wed Mar  5 09:13:47 EST 2008
00011 // $Id: FWDetailViewManager.cc,v 1.62 2011/05/27 04:03:42 amraktad Exp $
00012 //
00013 
00014 #include <stdio.h>
00015 #include <boost/bind.hpp>
00016 #include <algorithm>
00017 
00018 #include "TClass.h"
00019 #include "TROOT.h"
00020 #include "TGWindow.h"
00021 #include "TGFrame.h"
00022 #include "TEveManager.h"
00023 #include "TEveWindowManager.h"
00024 #include "TEveWindow.h"
00025 #include "TQObject.h"
00026 
00027 #include "Fireworks/Core/interface/FWDetailViewManager.h"
00028 #include "Fireworks/Core/interface/FWColorManager.h"
00029 #include "Fireworks/Core/interface/FWDetailViewBase.h"
00030 #include "Fireworks/Core/interface/FWModelId.h"
00031 #include "Fireworks/Core/interface/FWEventItem.h"
00032 #include "Fireworks/Core/interface/FWDetailViewFactory.h"
00033 #include "Fireworks/Core/interface/FWSimpleRepresentationChecker.h"
00034 #include "Fireworks/Core/interface/FWRepresentationInfo.h"
00035 #include "Fireworks/Core/interface/fwLog.h"
00036 
00037 
00038 static
00039 std::string viewNameFrom(const std::string& iFull)
00040 {
00041    std::string::size_type first = iFull.find_first_of('@');
00042    std::string::size_type second = iFull.find_first_of('@',first+1);
00043    return iFull.substr(first+1,second-first-1);
00044    
00045 }
00046 //
00047 // constructors and destructor
00048 //
00049 FWDetailViewManager::FWDetailViewManager(FWColorManager* colMng):
00050    m_colorManager(colMng)
00051 {  
00052    // force white background for all embedded canvases
00053    gROOT->SetStyle("Plain");
00054 
00055    m_colorManager->colorsHaveChanged_.connect(boost::bind(&FWDetailViewManager::colorsChanged,this));
00056    gEve->GetWindowManager()->Connect( "WindowDeleted(TEveWindow*)", "FWDetailViewManager", this, "eveWindowDestroyed(TEveWindow*)");   
00057 }
00058 
00059 FWDetailViewManager::~FWDetailViewManager()
00060 { 
00061    newEventCallback();
00062    gEve->GetWindowManager()->Disconnect("WindowDeleted(TEveWindow*)", this, "eveWindowDestroyed(TEveWindow*)" );
00063 }
00064 
00065 void
00066 FWDetailViewManager::openDetailViewFor(const FWModelId &id, const std::string& iViewName)
00067 {
00068    TEveWindowSlot* slot = TEveWindow::CreateWindowMainFrame();
00069    TEveCompositeFrameInMainFrame* eveFrame = (TEveCompositeFrameInMainFrame*)slot->GetEveFrame();
00070 
00071    // find the right viewer for this item
00072    std::string typeName = ROOT::Reflex::Type::ByTypeInfo(*(id.item()->modelType()->GetTypeInfo())).Name(ROOT::Reflex::SCOPED);
00073    std::vector<std::string> viewerNames = findViewersFor(typeName);
00074    if(0==viewerNames.size()) {
00075       fwLog(fwlog::kError) << "FWDetailViewManager: don't know what detailed view to "
00076          "use for object " << id.item()->name() << std::endl;
00077       assert(0!=viewerNames.size());
00078    }
00079 
00080    //see if one of the names matches iViewName
00081    std::string match;
00082    for(std::vector<std::string>::iterator it = viewerNames.begin(), itEnd = viewerNames.end(); it != itEnd; ++it) {
00083       std::string t = viewNameFrom(*it);
00084       //std::cout <<"'"<<iViewName<< "' '"<<t<<"'"<<std::endl;
00085       if(t == iViewName) {
00086          match = *it;
00087          break;
00088       }
00089    }
00090    assert(match.size() != 0);
00091    FWDetailViewBase* detailView = FWDetailViewFactory::get()->create(match);
00092    assert(0!=detailView);
00093 
00094    TEveWindowSlot* ws  = (TEveWindowSlot*)(eveFrame->GetEveWindow());
00095    detailView->init(ws);
00096    detailView->build(id);
00097    detailView->setBackgroundColor(m_colorManager->background());
00098 
00099    TGMainFrame* mf = (TGMainFrame*)(eveFrame->GetParent());
00100    mf->SetWindowName(Form("%s Detail View [%d]", id.item()->name().c_str(), id.index())); 
00101   
00102    m_views.push_back(ViewFrame(eveFrame, detailView, eveFrame->GetEveWindow()));
00103 
00104    mf->MapRaised();
00105 }
00106 
00107 std::vector<std::string>
00108 FWDetailViewManager::detailViewsFor(const FWModelId& iId) const
00109 {
00110    std::string typeName = ROOT::Reflex::Type::ByTypeInfo(*(iId.item()->modelType()->GetTypeInfo())).Name(ROOT::Reflex::SCOPED);
00111    std::vector<std::string> fullNames = findViewersFor(typeName);
00112    std::vector<std::string> justViewNames;
00113    justViewNames.reserve(fullNames.size());
00114    std::transform(fullNames.begin(),fullNames.end(),std::back_inserter(justViewNames),&viewNameFrom);
00115    return justViewNames;
00116 }
00117 
00118 std::vector<std::string>
00119 FWDetailViewManager::findViewersFor(const std::string& iType) const
00120 {
00121    std::vector<std::string> returnValue;
00122 
00123    std::map<std::string,std::vector<std::string> >::const_iterator itFind = m_typeToViewers.find(iType);
00124    if(itFind != m_typeToViewers.end()) {
00125       return itFind->second;
00126    }
00127    //create a list of the available ViewManager's
00128    std::set<std::string> detailViews;
00129 
00130    std::vector<edmplugin::PluginInfo> available = FWDetailViewFactory::get()->available();
00131    std::transform(available.begin(),
00132                   available.end(),
00133                   std::inserter(detailViews,detailViews.begin()),
00134                   boost::bind(&edmplugin::PluginInfo::name_,_1));
00135    unsigned int closestMatch= 0xFFFFFFFF;
00136    for(std::set<std::string>::iterator it = detailViews.begin(), itEnd=detailViews.end();
00137        it!=itEnd;
00138        ++it) {
00139       std::string::size_type first = it->find_first_of('@');
00140       std::string type = it->substr(0,first);
00141 
00142       if(type == iType) {
00143          returnValue.push_back(viewNameFrom(*it));
00144       }
00145       //see if we match via inheritance
00146       FWSimpleRepresentationChecker checker(type,"",0,false);
00147       FWRepresentationInfo info = checker.infoFor(iType);
00148       if(closestMatch > info.proximity()) {
00149          //closestMatch = info.proximity();
00150          returnValue.push_back(*it);
00151       }
00152    }
00153    m_typeToViewers[iType]=returnValue;
00154    return returnValue;
00155 }
00156 
00157 void
00158 FWDetailViewManager::colorsChanged()
00159 {
00160    for (vViews_i i = m_views.begin(); i !=  m_views.end(); ++i)
00161       (*i).m_detailView->setBackgroundColor(m_colorManager->background());
00162 }
00163 
00164 
00165 void
00166 FWDetailViewManager::newEventCallback()
00167 {
00168    while (!m_views.empty())
00169    {  
00170       m_views.front().m_eveWindow->DestroyWindowAndSlot();
00171    }
00172 }
00173 
00174 void
00175 FWDetailViewManager::eveWindowDestroyed(TEveWindow* ew)
00176 {
00177    for (vViews_i i = m_views.begin(); i != m_views.end(); ++i)
00178    {
00179       if (ew == i->m_eveWindow)
00180       {
00181          // printf("========================== delete %s \n", ew->GetElementName());
00182          delete i->m_detailView;
00183          m_views.erase(i);
00184          break;
00185       }
00186    }
00187 }