CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWModelContextMenuHandler
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Tue Sep 22 13:26:04 CDT 2009
00011 // $Id: FWModelContextMenuHandler.cc,v 1.23 2012/06/26 22:09:35 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include <cassert>
00016 #include "TGMenu.h"
00017 #include "KeySymbols.h"
00018 
00019 #include "Reflex/Object.h"
00020 #include "Reflex/Type.h"
00021 #include "TClass.h"
00022 
00023 // user include files
00024 #include "Fireworks/Core/src/FWModelContextMenuHandler.h"
00025 #include "Fireworks/Core/interface/FWSelectionManager.h"
00026 #include "Fireworks/Core/interface/FWDetailViewManager.h"
00027 #include "Fireworks/Core/interface/FWColorManager.h"
00028 #include "Fireworks/Core/src/FWColorSelect.h"
00029 #include "Fireworks/Core/src/FWPopupMenu.cc"
00030 #include "Fireworks/Core/interface/FWEventItem.h"
00031 #include "Fireworks/Core/interface/FWGUIManager.h"
00032 #include "Fireworks/Core/interface/FWViewContextMenuHandlerBase.h"
00033 
00034 //
00035 // constants, enums and typedefs
00036 //
00037 enum MenuOptions {
00038    kSetVisibleMO,
00039    kSetColorMO,
00040    kPrint,
00041    kOpenDetailViewMO,
00042    kAfterOpenDetailViewMO,
00043    kOpenObjectControllerMO=100,
00044    kOpenCollectionControllerMO,
00045    kViewOptionsMO=1000,
00046    kLastOfMO
00047 };
00048 
00049 
00050 //
00051 // static data member definitions
00052 //
00053 static const char* const kOpenDetailView = "Open Detailed View ...";
00054 
00055 //
00056 // constructors and destructor
00057 //
00058 FWModelContextMenuHandler::FWModelContextMenuHandler(FWSelectionManager* iSM,
00059                                                      FWDetailViewManager* iDVM,
00060                                                      FWColorManager* iCM,
00061                                                      FWGUIManager* iGM):
00062 m_modelPopup(0),
00063 m_colorPopup(0),
00064 m_selectionManager(iSM),
00065 m_detailViewManager(iDVM),
00066 m_colorManager(iCM),
00067 m_guiManager(iGM),
00068 m_seperator(0),
00069 m_viewSeperator(0),
00070 m_afterViewSeperator(0),
00071 m_x(0),
00072 m_y(0),
00073 m_nDetailViewEntries(0),
00074 m_nViewEntries(0),
00075 m_viewHander(0)
00076 {
00077 }
00078 
00079 // FWModelContextMenuHandler::FWModelContextMenuHandler(const FWModelContextMenuHandler& rhs)
00080 // {
00081 //    // do actual copying here;
00082 // }
00083 
00084 FWModelContextMenuHandler::~FWModelContextMenuHandler()
00085 {
00086    delete m_modelPopup;
00087 }
00088 
00089 //
00090 // assignment operators
00091 //
00092 // const FWModelContextMenuHandler& FWModelContextMenuHandler::operator=(const FWModelContextMenuHandler& rhs)
00093 // {
00094 //   //An exception safe implementation is
00095 //   FWModelContextMenuHandler temp(rhs);
00096 //   swap(rhs);
00097 //
00098 //   return *this;
00099 // }
00100 
00101 //
00102 // member functions
00103 //
00104 #include "TROOT.h"
00105 namespace  {
00106    class change_visibility {
00107    public:
00108       change_visibility(bool iIsVisible): m_isVisible(iIsVisible) {}
00109       void operator()(const FWModelId& iID) const {
00110          FWDisplayProperties p = iID.item()->modelInfo(iID.index()).displayProperties();
00111          p.setIsVisible(m_isVisible);
00112          iID.item()->setDisplayProperties(iID.index(), p);
00113       }
00114       bool m_isVisible; 
00115    };
00116 }
00117 void 
00118 FWModelContextMenuHandler::chosenItem(Int_t iChoice)
00119 {
00120    assert(!m_selectionManager->selected().empty());
00121    switch (iChoice) {
00122       case kSetVisibleMO:
00123       {
00124          FWModelId id = *(m_selectionManager->selected().begin());
00125          const FWDisplayProperties& props = id.item()->modelInfo(id.index()).displayProperties();
00126          for_each(m_selectionManager->selected().begin(),
00127                   m_selectionManager->selected().end(), 
00128                   change_visibility(!props.isVisible())
00129                   );
00130          break;
00131       }
00132       case kSetColorMO:
00133       {
00134          FWModelId id = *(m_selectionManager->selected().begin());
00135          createColorPopup();
00136          m_colorPopup->SetName("Selected");
00137          std::vector<Color_t> colors;
00138          m_colorManager->fillLimitedColors(colors);
00139          m_colorPopup->ResetColors(colors, m_colorManager->backgroundColorIndex()==FWColorManager::kBlackIndex);
00140          m_colorPopup->SetSelection(id.item()->modelInfo(id.index()).displayProperties().color());
00141          m_colorPopup->PlacePopup(m_x, m_y, m_colorPopup->GetDefaultWidth(), m_colorPopup->GetDefaultHeight());
00142          break;
00143       }
00144       case kPrint:
00145       {
00146          FWModelId id = *(m_selectionManager->selected().begin());
00147          Reflex::Type rtype(Reflex::Type::ByName(id.item()->modelType()->GetName()));
00148          Reflex::Object o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
00149 
00150          // void* xx = &std::cout;
00151          //const std::vector<void*> j(1, xx);
00152          //Reflex::Member m = rtype.FunctionMemberByName("print",Reflex::Type(Reflex::Type::ByName("void (std::ostream&)"), Reflex::CONST), 0 ,INHERITEDMEMBERS_ALSO );
00153          //m.Invoke(o, 0, j);
00154 
00155          const char* cmd  = Form("FWGUIManager::OStream() << *(%s*)%p ;",  id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
00156          //const char* cmd  = Form("*((std::ostream*)%p) << (%s*)%p ;", (void*)(&std::cout), id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
00157          std::cout << cmd << std::endl;
00158          gROOT->ProcessLine(cmd);
00159 
00160 
00161          break;
00162       }
00163       case kOpenObjectControllerMO:
00164       {
00165          m_guiManager->showModelPopup();
00166          break;
00167       }
00168       case kOpenCollectionControllerMO:
00169       {
00170          m_guiManager->showEDIFrame();
00171          break;
00172       }
00173       case kOpenDetailViewMO:
00174       case kViewOptionsMO:
00175       default:
00176       {
00177          if(iChoice>=kViewOptionsMO) {
00178             assert(0!=m_viewHander);
00179             m_viewHander->select(iChoice-kViewOptionsMO, *(m_selectionManager->selected().begin()), m_x, m_y);
00180          }else {
00181             assert(iChoice<kOpenObjectControllerMO);
00182             assert(m_selectionManager->selected().size()==1);
00183             std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
00184             assert(0!=viewChoices.size());
00185             m_detailViewManager->openDetailViewFor(*(m_selectionManager->selected().begin()),viewChoices[iChoice-kOpenDetailViewMO]) ;
00186          }
00187          break;
00188       }
00189       break;
00190    }
00191 }
00192 
00193 void 
00194 FWModelContextMenuHandler::colorChangeRequested(Color_t color)
00195 {
00196    for(std::set<FWModelId>::const_iterator it =m_selectionManager->selected().begin(),
00197        itEnd = m_selectionManager->selected().end();
00198        it != itEnd;
00199        ++it) {
00200       FWDisplayProperties changeProperties = it->item()->modelInfo(it->index()).displayProperties();
00201       changeProperties.setColor(color);
00202       it->item()->setDisplayProperties(it->index(), changeProperties);
00203    }
00204 }
00205 
00206 void 
00207 FWModelContextMenuHandler::addViewEntry(const char* iEntryName, int iEntryIndex, bool enabled)
00208 {
00209    if(!m_viewSeperator) {        
00210       m_modelPopup->AddSeparator(m_afterViewSeperator);          
00211       m_viewSeperator=dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Before(m_afterViewSeperator));
00212       assert(0!=m_viewSeperator);        
00213    }
00214  
00215    if(static_cast<int>(m_nViewEntries) > iEntryIndex) {
00216       m_modelPopup->GetEntry(iEntryIndex+kViewOptionsMO)->GetLabel()->SetString(iEntryName);
00217       if(enabled)
00218          m_modelPopup->EnableEntry(iEntryIndex+kViewOptionsMO);
00219       else
00220          m_modelPopup->DisableEntry(iEntryIndex+kViewOptionsMO);
00221 
00222    } else {
00223       assert(static_cast<int>(m_nViewEntries) == iEntryIndex);
00224       m_modelPopup->AddEntry(iEntryName,kViewOptionsMO+iEntryIndex,0,0,m_viewSeperator);
00225 
00226       if (enabled)
00227          m_modelPopup->EnableEntry(kViewOptionsMO+iEntryIndex);
00228       else
00229          m_modelPopup->DisableEntry(kViewOptionsMO+iEntryIndex);
00230 
00231       ++m_nViewEntries;
00232    }
00233 
00234 }
00235 //
00236 // const member functions
00237 //
00238 void 
00239 FWModelContextMenuHandler::showSelectedModelContext(Int_t iX, Int_t iY, FWViewContextMenuHandlerBase* iHandler) const
00240 {
00241    m_viewHander=iHandler;
00242    assert(!m_selectionManager->selected().empty());
00243    createModelContext();
00244 
00245    //setup the menu based on this object
00246    FWModelId id = *(m_selectionManager->selected().begin());
00247    const FWDisplayProperties& props = id.item()->modelInfo(id.index()).displayProperties();
00248    if(props.isVisible()) {
00249       m_modelPopup->CheckEntry(kSetVisibleMO);
00250    }else {
00251       m_modelPopup->UnCheckEntry(kSetVisibleMO);
00252    }
00253 
00254 
00255    if( m_selectionManager->selected().size()==1 ) {
00256       {
00257          Reflex::Type rtype(Reflex::Type::ByName(id.item()->modelType()->GetName()));
00258          Reflex::Object o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
00259          Reflex::EMEMBERQUERY inh =  Reflex::INHERITEDMEMBERS_ALSO;
00260          if ( rtype.FunctionMemberByName("print",Reflex::Type(Reflex::Type::ByName("void (std::ostream&)"), Reflex::CONST), 0 , inh))
00261          {
00262             m_modelPopup->EnableEntry(kPrint);
00263             // std::cout <<  "Enable " <<std::endl;
00264          }
00265          else
00266          {           
00267             m_modelPopup->DisableEntry(kPrint);
00268             // printf("Disable print \n");
00269          }         
00270       }
00271       //add the detail view entries
00272       std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
00273       if(viewChoices.size()>0) {
00274          if(m_nDetailViewEntries < viewChoices.size()) {
00275             for(unsigned int index = m_nDetailViewEntries;
00276                 index != viewChoices.size();
00277                 ++index) {
00278                m_modelPopup->AddEntry(kOpenDetailView,kOpenDetailViewMO+index,0,0,m_seperator);
00279             }
00280             m_nDetailViewEntries=viewChoices.size();
00281          }
00282          const std::string kStart("Open ");
00283          const std::string kEnd(" Detail View ...");
00284          for(unsigned int index=0; index != viewChoices.size(); ++index) {
00285             m_modelPopup->GetEntry(index+kOpenDetailViewMO)->GetLabel()->SetString((kStart+viewChoices[index]+kEnd).c_str());
00286             m_modelPopup->EnableEntry(index+kOpenDetailViewMO);
00287          }
00288          for(unsigned int i =viewChoices.size(); i <m_nDetailViewEntries; ++i) {
00289             m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00290          }
00291          
00292       } else {
00293          for(unsigned int i =0; i <m_nDetailViewEntries; ++i) {
00294             m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00295          }
00296       }
00297    } else {
00298       for(unsigned int i =0; i <m_nDetailViewEntries; ++i) {
00299          m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00300       }
00301    }
00302    //add necessary entries from the view
00303    m_modelPopup->DeleteEntry(m_viewSeperator);
00304    m_viewSeperator=0;
00305 
00306    for(unsigned int i=0; i<m_nViewEntries; ++i) {
00307       m_modelPopup->HideEntry(kViewOptionsMO+i);
00308    }
00309    if(m_viewHander) {
00310       m_viewHander->addTo(const_cast<FWModelContextMenuHandler&>(*this), *(m_selectionManager->selected().begin()));
00311    }
00312    
00313    m_x=iX;
00314    m_y=iY;
00315    m_modelPopup->PlaceMenu(iX,iY,false,true);
00316 }
00317 
00318 void 
00319 FWModelContextMenuHandler::createModelContext() const
00320 {
00321    if(0==m_modelPopup) {
00322       m_modelPopup = new FWPopupMenu();
00323       
00324       m_modelPopup->AddEntry("Set Visible",kSetVisibleMO);
00325       m_modelPopup->AddEntry("Set Color ...",kSetColorMO);
00326       m_modelPopup->AddEntry("Print ...",kPrint);
00327       m_modelPopup->AddEntry(kOpenDetailView,kOpenDetailViewMO);
00328       m_nDetailViewEntries=1;
00329       m_modelPopup->AddSeparator();
00330       m_seperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
00331       assert(0!=m_seperator);
00332       m_modelPopup->AddEntry("Open Object Controller ...",kOpenObjectControllerMO);
00333       m_afterViewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
00334       m_modelPopup->AddEntry("Open Collection Controller ...",kOpenCollectionControllerMO);
00335 
00336       m_modelPopup->Connect("Activated(Int_t)",
00337                             "FWModelContextMenuHandler",
00338                             const_cast<FWModelContextMenuHandler*>(this),
00339                             "chosenItem(Int_t)");
00340    }
00341 }
00342 
00343 void 
00344 FWModelContextMenuHandler::createColorPopup() const
00345 {
00346    if(0==m_colorPopup) {
00347       std::vector<Color_t> colors;
00348       m_colorManager->fillLimitedColors(colors);
00349       
00350       m_colorPopup = new FWColorPopup(gClient->GetDefaultRoot(), colors.front());
00351       m_colorPopup->InitContent("", colors);
00352       m_colorPopup->Connect("ColorSelected(Color_t)","FWModelContextMenuHandler", const_cast<FWModelContextMenuHandler*>(this), "colorChangeRequested(Color_t)");
00353    }
00354 }
00355 
00356 //
00357 // static member functions
00358 //
00359 
00360 ClassImp(FWModelContextMenuHandler)