CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/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.22 2011/06/28 01:35:50 amraktad 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          using namespace Reflex;
00147          FWModelId id = *(m_selectionManager->selected().begin());
00148          Type rtype(ROOT::Reflex::Type::ByName(id.item()->modelType()->GetName()));
00149          Object o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
00150 
00151          // void* xx = &std::cout;
00152          //const std::vector<void*> j(1, xx);
00153          //Member m = rtype.FunctionMemberByName("print",Type(Type::ByName("void (std::ostream&)"), CONST), 0 ,INHERITEDMEMBERS_ALSO );
00154          //m.Invoke(o, 0, j);
00155 
00156          const char* cmd  = Form("FWGUIManager::OStream() << *(%s*)%p ;",  id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
00157          //const char* cmd  = Form("*((std::ostream*)%p) << (%s*)%p ;", (void*)(&std::cout), id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
00158          std::cout << cmd << std::endl;
00159          gROOT->ProcessLine(cmd);
00160 
00161 
00162          break;
00163       }
00164       case kOpenObjectControllerMO:
00165       {
00166          m_guiManager->showModelPopup();
00167          break;
00168       }
00169       case kOpenCollectionControllerMO:
00170       {
00171          m_guiManager->showEDIFrame();
00172          break;
00173       }
00174       case kOpenDetailViewMO:
00175       case kViewOptionsMO:
00176       default:
00177       {
00178          if(iChoice>=kViewOptionsMO) {
00179             assert(0!=m_viewHander);
00180             m_viewHander->select(iChoice-kViewOptionsMO, *(m_selectionManager->selected().begin()), m_x, m_y);
00181          }else {
00182             assert(iChoice<kOpenObjectControllerMO);
00183             assert(m_selectionManager->selected().size()==1);
00184             std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
00185             assert(0!=viewChoices.size());
00186             m_detailViewManager->openDetailViewFor(*(m_selectionManager->selected().begin()),viewChoices[iChoice-kOpenDetailViewMO]) ;
00187          }
00188          break;
00189       }
00190       break;
00191    }
00192 }
00193 
00194 void 
00195 FWModelContextMenuHandler::colorChangeRequested(Color_t color)
00196 {
00197    for(std::set<FWModelId>::const_iterator it =m_selectionManager->selected().begin(),
00198        itEnd = m_selectionManager->selected().end();
00199        it != itEnd;
00200        ++it) {
00201       FWDisplayProperties changeProperties = it->item()->modelInfo(it->index()).displayProperties();
00202       changeProperties.setColor(color);
00203       it->item()->setDisplayProperties(it->index(), changeProperties);
00204    }
00205 }
00206 
00207 void 
00208 FWModelContextMenuHandler::addViewEntry(const char* iEntryName, int iEntryIndex, bool enabled)
00209 {
00210    if(!m_viewSeperator) {        
00211       m_modelPopup->AddSeparator(m_afterViewSeperator);          
00212       m_viewSeperator=dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Before(m_afterViewSeperator));
00213       assert(0!=m_viewSeperator);        
00214    }
00215  
00216    if(static_cast<int>(m_nViewEntries) > iEntryIndex) {
00217       m_modelPopup->GetEntry(iEntryIndex+kViewOptionsMO)->GetLabel()->SetString(iEntryName);
00218       if(enabled)
00219          m_modelPopup->EnableEntry(iEntryIndex+kViewOptionsMO);
00220       else
00221          m_modelPopup->DisableEntry(iEntryIndex+kViewOptionsMO);
00222 
00223    } else {
00224       assert(static_cast<int>(m_nViewEntries) == iEntryIndex);
00225       m_modelPopup->AddEntry(iEntryName,kViewOptionsMO+iEntryIndex,0,0,m_viewSeperator);
00226 
00227       if (enabled)
00228          m_modelPopup->EnableEntry(kViewOptionsMO+iEntryIndex);
00229       else
00230          m_modelPopup->DisableEntry(kViewOptionsMO+iEntryIndex);
00231 
00232       ++m_nViewEntries;
00233    }
00234 
00235 }
00236 //
00237 // const member functions
00238 //
00239 void 
00240 FWModelContextMenuHandler::showSelectedModelContext(Int_t iX, Int_t iY, FWViewContextMenuHandlerBase* iHandler) const
00241 {
00242    m_viewHander=iHandler;
00243    assert(!m_selectionManager->selected().empty());
00244    createModelContext();
00245 
00246    //setup the menu based on this object
00247    FWModelId id = *(m_selectionManager->selected().begin());
00248    const FWDisplayProperties& props = id.item()->modelInfo(id.index()).displayProperties();
00249    if(props.isVisible()) {
00250       m_modelPopup->CheckEntry(kSetVisibleMO);
00251    }else {
00252       m_modelPopup->UnCheckEntry(kSetVisibleMO);
00253    }
00254 
00255 
00256    if( m_selectionManager->selected().size()==1 ) {
00257       {
00258          using namespace Reflex;
00259          ROOT::Reflex::Type rtype(ROOT::Reflex::Type::ByName(id.item()->modelType()->GetName()));
00260          ROOT::Reflex::Object o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
00261          EMEMBERQUERY inh =  INHERITEDMEMBERS_ALSO;
00262          if ( rtype.FunctionMemberByName("print",Type(Type::ByName("void (std::ostream&)"), CONST), 0 , inh))
00263          {
00264             m_modelPopup->EnableEntry(kPrint);
00265             // std::cout <<  "Enable " <<std::endl;
00266          }
00267          else
00268          {           
00269             m_modelPopup->DisableEntry(kPrint);
00270             // printf("Disable print \n");
00271          }         
00272       }
00273       //add the detail view entries
00274       std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
00275       if(viewChoices.size()>0) {
00276          if(m_nDetailViewEntries < viewChoices.size()) {
00277             for(unsigned int index = m_nDetailViewEntries;
00278                 index != viewChoices.size();
00279                 ++index) {
00280                m_modelPopup->AddEntry(kOpenDetailView,kOpenDetailViewMO+index,0,0,m_seperator);
00281             }
00282             m_nDetailViewEntries=viewChoices.size();
00283          }
00284          const std::string kStart("Open ");
00285          const std::string kEnd(" Detail View ...");
00286          for(unsigned int index=0; index != viewChoices.size(); ++index) {
00287             m_modelPopup->GetEntry(index+kOpenDetailViewMO)->GetLabel()->SetString((kStart+viewChoices[index]+kEnd).c_str());
00288             m_modelPopup->EnableEntry(index+kOpenDetailViewMO);
00289          }
00290          for(unsigned int i =viewChoices.size(); i <m_nDetailViewEntries; ++i) {
00291             m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00292          }
00293          
00294       } else {
00295          for(unsigned int i =0; i <m_nDetailViewEntries; ++i) {
00296             m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00297          }
00298       }
00299    } else {
00300       for(unsigned int i =0; i <m_nDetailViewEntries; ++i) {
00301          m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00302       }
00303    }
00304    //add necessary entries from the view
00305    m_modelPopup->DeleteEntry(m_viewSeperator);
00306    m_viewSeperator=0;
00307 
00308    for(unsigned int i=0; i<m_nViewEntries; ++i) {
00309       m_modelPopup->HideEntry(kViewOptionsMO+i);
00310    }
00311    if(m_viewHander) {
00312       m_viewHander->addTo(const_cast<FWModelContextMenuHandler&>(*this), *(m_selectionManager->selected().begin()));
00313    }
00314    
00315    m_x=iX;
00316    m_y=iY;
00317    m_modelPopup->PlaceMenu(iX,iY,false,true);
00318 }
00319 
00320 void 
00321 FWModelContextMenuHandler::createModelContext() const
00322 {
00323    if(0==m_modelPopup) {
00324       m_modelPopup = new FWPopupMenu();
00325       
00326       m_modelPopup->AddEntry("Set Visible",kSetVisibleMO);
00327       m_modelPopup->AddEntry("Set Color ...",kSetColorMO);
00328       m_modelPopup->AddEntry("Print ...",kPrint);
00329       m_modelPopup->AddEntry(kOpenDetailView,kOpenDetailViewMO);
00330       m_nDetailViewEntries=1;
00331       m_modelPopup->AddSeparator();
00332       m_seperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
00333       assert(0!=m_seperator);
00334       m_modelPopup->AddEntry("Open Object Controller ...",kOpenObjectControllerMO);
00335       m_afterViewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
00336       m_modelPopup->AddEntry("Open Collection Controller ...",kOpenCollectionControllerMO);
00337 
00338       m_modelPopup->Connect("Activated(Int_t)",
00339                             "FWModelContextMenuHandler",
00340                             const_cast<FWModelContextMenuHandler*>(this),
00341                             "chosenItem(Int_t)");
00342    }
00343 }
00344 
00345 void 
00346 FWModelContextMenuHandler::createColorPopup() const
00347 {
00348    if(0==m_colorPopup) {
00349       std::vector<Color_t> colors;
00350       m_colorManager->fillLimitedColors(colors);
00351       
00352       m_colorPopup = new FWColorPopup(gClient->GetDefaultRoot(), colors.front());
00353       m_colorPopup->InitContent("", colors);
00354       m_colorPopup->Connect("ColorSelected(Color_t)","FWModelContextMenuHandler", const_cast<FWModelContextMenuHandler*>(this), "colorChangeRequested(Color_t)");
00355    }
00356 }
00357 
00358 //
00359 // static member functions
00360 //
00361 
00362 ClassImp(FWModelContextMenuHandler)