Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:17

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWModelContextMenuHandler
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Tue Sep 22 13:26:04 CDT 2009
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 #include <iostream>
0016 #include "TGMenu.h"
0017 #include "KeySymbols.h"
0018 
0019 #include "FWCore/Reflection/interface/FunctionWithDict.h"
0020 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0021 #include "FWCore/Reflection/interface/TypeWithDict.h"
0022 #include "TClass.h"
0023 #include "TError.h"
0024 
0025 // user include files
0026 #include "Fireworks/Core/src/FWModelContextMenuHandler.h"
0027 #include "Fireworks/Core/interface/FWSelectionManager.h"
0028 #include "Fireworks/Core/interface/FWDetailViewManager.h"
0029 #include "Fireworks/Core/interface/FWColorManager.h"
0030 #include "Fireworks/Core/src/FWColorSelect.h"
0031 #include "Fireworks/Core/src/FWPopupMenu.cc"
0032 #include "Fireworks/Core/interface/FWEventItem.h"
0033 #include "Fireworks/Core/interface/FWGUIManager.h"
0034 #include "Fireworks/Core/interface/FWViewContextMenuHandlerBase.h"
0035 
0036 //
0037 // constants, enums and typedefs
0038 //
0039 enum MenuOptions {
0040   kSetVisibleMO,
0041   kSetColorMO,
0042   //   kPrint,
0043   kOpenDetailViewMO,
0044   kAfterOpenDetailViewMO = 10,
0045   kOpen3DRegion,
0046   kOpenObjectControllerMO = 100,
0047   kOpenCollectionControllerMO,
0048   kViewOptionsMO = 1000,
0049   kLastOfMO
0050 };
0051 
0052 //
0053 // static data member definitions
0054 //
0055 static const char* const kOpenDetailView = "Open Detailed View ...";
0056 
0057 //
0058 // constructors and destructor
0059 //
0060 FWModelContextMenuHandler::FWModelContextMenuHandler(FWSelectionManager* iSM,
0061                                                      FWDetailViewManager* iDVM,
0062                                                      FWColorManager* iCM,
0063                                                      FWGUIManager* iGM)
0064     : m_modelPopup(nullptr),
0065       m_colorPopup(nullptr),
0066       m_selectionManager(iSM),
0067       m_detailViewManager(iDVM),
0068       m_colorManager(iCM),
0069       m_guiManager(iGM),
0070       m_seperator(nullptr),
0071       m_viewSeperator(nullptr),
0072       m_afterViewSeperator(nullptr),
0073       m_x(0),
0074       m_y(0),
0075       m_nDetailViewEntries(0),
0076       m_nViewEntries(0),
0077       m_viewHander(nullptr) {}
0078 
0079 // FWModelContextMenuHandler::FWModelContextMenuHandler(const FWModelContextMenuHandler& rhs)
0080 // {
0081 //    // do actual copying here;
0082 // }
0083 
0084 FWModelContextMenuHandler::~FWModelContextMenuHandler() { delete m_modelPopup; }
0085 
0086 //
0087 // assignment operators
0088 //
0089 // const FWModelContextMenuHandler& FWModelContextMenuHandler::operator=(const FWModelContextMenuHandler& rhs)
0090 // {
0091 //   //An exception safe implementation is
0092 //   FWModelContextMenuHandler temp(rhs);
0093 //   swap(rhs);
0094 //
0095 //   return *this;
0096 // }
0097 
0098 //
0099 // member functions
0100 //
0101 #include "TROOT.h"
0102 namespace {
0103   class change_visibility {
0104   public:
0105     change_visibility(bool iIsVisible) : m_isVisible(iIsVisible) {}
0106     void operator()(const FWModelId& iID) const {
0107       FWDisplayProperties p = iID.item()->modelInfo(iID.index()).displayProperties();
0108       p.setIsVisible(m_isVisible);
0109       iID.item()->setDisplayProperties(iID.index(), p);
0110     }
0111     bool m_isVisible;
0112   };
0113 }  // namespace
0114 void FWModelContextMenuHandler::chosenItem(Int_t iChoice) {
0115   assert(!m_selectionManager->selected().empty());
0116   switch (iChoice) {
0117     case kSetVisibleMO: {
0118       FWModelId id = *(m_selectionManager->selected().begin());
0119       for_each(m_selectionManager->selected().begin(),
0120                m_selectionManager->selected().end(),
0121                change_visibility(!id.item()->modelInfo(id.index()).displayProperties().isVisible()));
0122       break;
0123     }
0124     case kSetColorMO: {
0125       FWModelId id = *(m_selectionManager->selected().begin());
0126       createColorPopup();
0127       m_colorPopup->SetName("Selected");
0128       std::vector<Color_t> colors;
0129       m_colorManager->fillLimitedColors(colors);
0130       m_colorPopup->ResetColors(colors, m_colorManager->backgroundColorIndex() == FWColorManager::kBlackIndex);
0131       m_colorPopup->SetSelection(id.item()->modelInfo(id.index()).displayProperties().color());
0132       m_colorPopup->PlacePopup(m_x, m_y, m_colorPopup->GetDefaultWidth(), m_colorPopup->GetDefaultHeight());
0133       break;
0134     } /*
0135       case kPrint:
0136       {
0137          FWModelId id = *(m_selectionManager->selected().begin());
0138          edm::TypeWithDict rtype(edm::TypeWithDict::byName(id.item()->modelType()->GetName()));
0139          edm::ObjectWithDict o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
0140 
0141          // void* xx = &std::cout;
0142          //const std::vector<void*> j(1, xx);
0143          //edm::TypeMemberQuery inh =  edm::TypeMemberQuery::InheritedAlso;
0144          //edm::FunctionWithDict m = rtype.functionMemberByName("print",edm::TypeWithDict(edm::TypeWithDict::byName("void (std::ostream&)"), edm::TypeModifiers::Const), edm::TypeModifiers::NoMod , inh))
0145          //m.Invoke(o, 0, j);
0146 
0147          const char* cmd  = Form("FWGUIManager::OStream() << *(%s*)%p ;",  id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
0148          //const char* cmd  = Form("*((std::ostream*)%p) << (%s*)%p ;", (void*)(&std::cout), id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
0149          std::cout << cmd << std::endl;
0150          gROOT->ProcessLine(cmd);
0151 
0152 
0153          break;
0154          }*/
0155     case kOpenObjectControllerMO: {
0156       m_guiManager->showModelPopup();
0157       break;
0158     }
0159     case kOpenCollectionControllerMO: {
0160       m_guiManager->showEDIFrame();
0161       break;
0162     }
0163     case kOpen3DRegion: {
0164       m_guiManager->open3DRegion();
0165       break;
0166     }
0167     case kOpenDetailViewMO:
0168     case kViewOptionsMO:
0169     default: {
0170       if (iChoice >= kViewOptionsMO) {
0171         assert(nullptr != m_viewHander);
0172         m_viewHander->select(iChoice - kViewOptionsMO, *(m_selectionManager->selected().begin()), m_x, m_y);
0173       } else {
0174         assert(iChoice < kOpenObjectControllerMO);
0175         assert(m_selectionManager->selected().size() == 1);
0176         std::vector<std::string> viewChoices =
0177             m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
0178         assert(!viewChoices.empty());
0179         m_detailViewManager->openDetailViewFor(*(m_selectionManager->selected().begin()),
0180                                                viewChoices[iChoice - kOpenDetailViewMO]);
0181       }
0182       break;
0183     } break;
0184   }
0185 }
0186 
0187 void FWModelContextMenuHandler::colorChangeRequested(Color_t color) {
0188   for (std::set<FWModelId>::const_iterator it = m_selectionManager->selected().begin(),
0189                                            itEnd = m_selectionManager->selected().end();
0190        it != itEnd;
0191        ++it) {
0192     FWDisplayProperties changeProperties = it->item()->modelInfo(it->index()).displayProperties();
0193     changeProperties.setColor(color);
0194     it->item()->setDisplayProperties(it->index(), changeProperties);
0195   }
0196 }
0197 
0198 void FWModelContextMenuHandler::addViewEntry(const char* iEntryName, int iEntryIndex, bool enabled) {
0199   if (!m_viewSeperator) {
0200     m_modelPopup->AddSeparator(m_afterViewSeperator);
0201     m_viewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Before(m_afterViewSeperator));
0202     assert(nullptr != m_viewSeperator);
0203   }
0204 
0205   if (static_cast<int>(m_nViewEntries) > iEntryIndex) {
0206     m_modelPopup->GetEntry(iEntryIndex + kViewOptionsMO)->GetLabel()->SetString(iEntryName);
0207     if (enabled)
0208       m_modelPopup->EnableEntry(iEntryIndex + kViewOptionsMO);
0209     else
0210       m_modelPopup->DisableEntry(iEntryIndex + kViewOptionsMO);
0211 
0212   } else {
0213     assert(static_cast<int>(m_nViewEntries) == iEntryIndex);
0214     m_modelPopup->AddEntry(iEntryName, kViewOptionsMO + iEntryIndex, nullptr, nullptr, m_viewSeperator);
0215 
0216     if (enabled)
0217       m_modelPopup->EnableEntry(kViewOptionsMO + iEntryIndex);
0218     else
0219       m_modelPopup->DisableEntry(kViewOptionsMO + iEntryIndex);
0220 
0221     ++m_nViewEntries;
0222   }
0223 }
0224 //
0225 // const member functions
0226 //
0227 void FWModelContextMenuHandler::showSelectedModelContext(Int_t iX,
0228                                                          Int_t iY,
0229                                                          FWViewContextMenuHandlerBase* iHandler) const {
0230   m_viewHander = iHandler;
0231   assert(!m_selectionManager->selected().empty());
0232   createModelContext();
0233 
0234   //setup the menu based on this object
0235   FWModelId id = *(m_selectionManager->selected().begin());
0236   if (id.item()->modelInfo(id.index()).displayProperties().isVisible()) {
0237     m_modelPopup->CheckEntry(kSetVisibleMO);
0238   } else {
0239     m_modelPopup->UnCheckEntry(kSetVisibleMO);
0240   }
0241 
0242   if (m_selectionManager->selected().size() == 1) {
0243     {
0244       //edm::TypeWithDict rtype(edm::TypeWithDict::byName(id.item()->modelType()->GetName()));
0245       //edm::ObjectWithDict o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
0246       //edm::TypeMemberQuery inh =  edm::TypeMemberQuery::InheritedAlso;
0247       //if ( rtype.functionMemberByName("print",edm::TypeWithDict(edm::TypeWithDict::byName("void (std::ostream&)"), Long_t(kIsConstant)), 0, inh))
0248       //{
0249       //m_modelPopup->EnableEntry(kPrint);
0250       // std::cout <<  "Enable " <<std::endl;
0251       //}
0252       //else
0253       //{
0254       m_modelPopup->DisableEntry(kPrint);
0255       // printf("Disable print \n");
0256       //}
0257     }
0258     //add the detail view entries
0259     std::vector<std::string> viewChoices =
0260         m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
0261     if (!viewChoices.empty()) {
0262       if (m_nDetailViewEntries < viewChoices.size()) {
0263         for (unsigned int index = m_nDetailViewEntries; index != viewChoices.size(); ++index) {
0264           m_modelPopup->AddEntry(kOpenDetailView, kOpenDetailViewMO + index, nullptr, nullptr, m_seperator);
0265         }
0266         m_nDetailViewEntries = viewChoices.size();
0267       }
0268       const std::string kStart("Open ");
0269       const std::string kEnd(" Detail View ...");
0270       for (unsigned int index = 0; index != viewChoices.size(); ++index) {
0271         m_modelPopup->EnableEntry(index + kOpenDetailViewMO);  // need to call this to make it visible
0272         if (viewChoices[index][0] != '!') {
0273           m_modelPopup->GetEntry(index + kOpenDetailViewMO)
0274               ->GetLabel()
0275               ->SetString((kStart + viewChoices[index] + kEnd).c_str());
0276         } else {
0277           m_modelPopup->GetEntry(index + kOpenDetailViewMO)
0278               ->GetLabel()
0279               ->SetString((kStart + &viewChoices[index][1] + kEnd).c_str());
0280           m_modelPopup->DisableEntry(index + kOpenDetailViewMO);
0281         }
0282       }
0283       for (unsigned int i = viewChoices.size(); i < m_nDetailViewEntries; ++i) {
0284         m_modelPopup->HideEntry(kOpenDetailViewMO + i);
0285       }
0286 
0287     } else {
0288       for (unsigned int i = 0; i < m_nDetailViewEntries; ++i) {
0289         m_modelPopup->HideEntry(kOpenDetailViewMO + i);
0290       }
0291     }
0292   } else {
0293     for (unsigned int i = 0; i < m_nDetailViewEntries; ++i) {
0294       m_modelPopup->HideEntry(kOpenDetailViewMO + i);
0295     }
0296   }
0297   //add necessary entries from the view
0298   m_modelPopup->DeleteEntry(m_viewSeperator);
0299   m_viewSeperator = nullptr;
0300 
0301   for (unsigned int i = 0; i < m_nViewEntries; ++i) {
0302     m_modelPopup->HideEntry(kViewOptionsMO + i);
0303   }
0304   if (m_viewHander) {
0305     m_viewHander->addTo(const_cast<FWModelContextMenuHandler&>(*this), *(m_selectionManager->selected().begin()));
0306   }
0307 
0308   m_x = iX;
0309   m_y = iY;
0310   m_modelPopup->PlaceMenu(iX, iY, false, true);
0311 }
0312 
0313 void FWModelContextMenuHandler::createModelContext() const {
0314   if (nullptr == m_modelPopup) {
0315     m_modelPopup = new FWPopupMenu();
0316 
0317     m_modelPopup->AddEntry("Set Visible", kSetVisibleMO);
0318     m_modelPopup->AddEntry("Set Color ...", kSetColorMO);
0319     //      m_modelPopup->AddEntry("Print ...",kPrint);
0320     m_modelPopup->AddEntry(kOpenDetailView, kOpenDetailViewMO);
0321     m_nDetailViewEntries = 1;
0322     m_seperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
0323     assert(nullptr != m_seperator);
0324     m_modelPopup->AddEntry("Open 3D Region ...", kOpen3DRegion);
0325     m_modelPopup->AddSeparator();
0326     m_modelPopup->AddEntry("Open Object Controller ...", kOpenObjectControllerMO);
0327     m_afterViewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
0328     m_modelPopup->AddEntry("Open Collection Controller ...", kOpenCollectionControllerMO);
0329 
0330     m_modelPopup->Connect("Activated(Int_t)",
0331                           "FWModelContextMenuHandler",
0332                           const_cast<FWModelContextMenuHandler*>(this),
0333                           "chosenItem(Int_t)");
0334   }
0335 }
0336 
0337 void FWModelContextMenuHandler::createColorPopup() const {
0338   if (nullptr == m_colorPopup) {
0339     std::vector<Color_t> colors;
0340     m_colorManager->fillLimitedColors(colors);
0341 
0342     m_colorPopup = new FWColorPopup(gClient->GetDefaultRoot(), colors.front());
0343     m_colorPopup->InitContent("", colors);
0344     m_colorPopup->Connect("ColorSelected(Color_t)",
0345                           "FWModelContextMenuHandler",
0346                           const_cast<FWModelContextMenuHandler*>(this),
0347                           "colorChangeRequested(Color_t)");
0348   }
0349 }
0350 
0351 //
0352 // static member functions
0353 //
0354 
0355 ClassImp(FWModelContextMenuHandler);