CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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.17 2010/09/01 18:49:00 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 #include <cassert>
00016 #include "TGMenu.h"
00017 #include "KeySymbols.h"
00018 
00019 // user include files
00020 #include "Fireworks/Core/src/FWModelContextMenuHandler.h"
00021 #include "Fireworks/Core/interface/FWSelectionManager.h"
00022 #include "Fireworks/Core/interface/FWDetailViewManager.h"
00023 #include "Fireworks/Core/interface/FWColorManager.h"
00024 #include "Fireworks/Core/src/FWColorSelect.h"
00025 #include "Fireworks/Core/interface/FWEventItem.h"
00026 #include "Fireworks/Core/interface/FWGUIManager.h"
00027 #include "Fireworks/Core/interface/FWViewContextMenuHandlerBase.h"
00028 
00029 //
00030 // constants, enums and typedefs
00031 //
00032 enum MenuOptions {
00033    kSetVisibleMO,
00034    kSetColorMO,
00035    kOpenDetailViewMO,
00036    kAfterOpenDetailViewMO,
00037    kOpenObjectControllerMO=100,
00038    kOpenCollectionControllerMO,
00039    kViewOptionsMO=1000,
00040    kLastOfMO
00041 };
00042 
00043 
00044 class FWPopupMenu : public TGPopupMenu
00045 {
00046 public:
00047    FWPopupMenu(const TGWindow* p=0, UInt_t w=10, UInt_t h=10, UInt_t options=0) :
00048       TGPopupMenu(p, w, h, options)
00049    {
00050       AddInput(kKeyPressMask);
00051    }
00052 
00053    // virtual void      PlaceMenu(Int_t x, Int_t y, Bool_t stick_mode, Bool_t grab_pointer)
00054    // {
00055    //    TGPopupMenu::PlaceMenu(x, y, stick_mode, grab_pointer);
00056    //    gVirtualX->GrabKey(fId, 0l, kAnyModifier, kTRUE);
00057    // }
00058 
00059    virtual void PoppedUp()
00060    {
00061       TGPopupMenu::PoppedUp();
00062       gVirtualX->SetInputFocus(fId);
00063       gVirtualX->GrabKey(fId, 0l, kAnyModifier, kTRUE);
00064       
00065    }
00066 
00067    virtual void PoppedDown()
00068    {
00069       gVirtualX->GrabKey(fId, 0l, kAnyModifier, kFALSE);
00070       TGPopupMenu::PoppedDown();
00071    }
00072 
00073    virtual Bool_t HandleKey(Event_t* event)
00074    {
00075       if (event->fType != kGKeyPress) return kTRUE;
00076 
00077       UInt_t keysym;
00078       char tmp[2];
00079       gVirtualX->LookupString(event, tmp, sizeof(tmp), keysym);
00080 
00081       TGMenuEntry *ce = fCurrent;
00082 
00083       switch (keysym)
00084       {
00085          case kKey_Up:
00086          {
00087             if (ce) ce = (TGMenuEntry*)GetListOfEntries()->Before(ce);
00088             while (ce && ((ce->GetType() == kMenuSeparator) ||
00089                           (ce->GetType() == kMenuLabel) ||
00090                           !(ce->GetStatus() & kMenuEnableMask)))
00091             {
00092                ce = (TGMenuEntry*)GetListOfEntries()->Before(ce);
00093             }
00094             if (!ce) ce = (TGMenuEntry*)GetListOfEntries()->Last();
00095             Activate(ce);
00096             break;
00097          }
00098          case kKey_Down:
00099          {
00100             if (ce) ce = (TGMenuEntry*)GetListOfEntries()->After(ce);
00101             while (ce && ((ce->GetType() == kMenuSeparator) ||
00102                           (ce->GetType() == kMenuLabel) ||
00103                           !(ce->GetStatus() & kMenuEnableMask)))
00104             {
00105                ce = (TGMenuEntry*)GetListOfEntries()->After(ce);
00106             }
00107             if (!ce) ce = (TGMenuEntry*)GetListOfEntries()->First();
00108             Activate(ce);
00109             break;
00110          }
00111          case kKey_Enter:
00112          case kKey_Return:
00113          {
00114             Event_t ev;
00115             ev.fType = kButtonRelease;
00116             ev.fWindow = fId;
00117             return HandleButton(&ev);
00118          }
00119          case kKey_Escape:
00120          {
00121             fCurrent = 0;
00122             void *dummy = 0;
00123             return EndMenu(dummy);
00124          }
00125          default:
00126          {
00127             break;
00128          }
00129       }
00130 
00131       return kTRUE;
00132    }
00133 };
00134 
00135 
00136 //
00137 // static data member definitions
00138 //
00139 static const char* const kOpenDetailView = "Open Detailed View ...";
00140 
00141 //
00142 // constructors and destructor
00143 //
00144 FWModelContextMenuHandler::FWModelContextMenuHandler(FWSelectionManager* iSM,
00145                                                      FWDetailViewManager* iDVM,
00146                                                      FWColorManager* iCM,
00147                                                      FWGUIManager* iGM):
00148 m_modelPopup(0),
00149 m_colorPopup(0),
00150 m_selectionManager(iSM),
00151 m_detailViewManager(iDVM),
00152 m_colorManager(iCM),
00153 m_guiManager(iGM),
00154 m_seperator(0),
00155 m_viewSeperator(0),
00156 m_afterViewSeperator(0),
00157 m_x(0),
00158 m_y(0),
00159 m_nDetailViewEntries(0),
00160 m_nViewEntries(0),
00161 m_viewHander(0)
00162 {
00163 }
00164 
00165 // FWModelContextMenuHandler::FWModelContextMenuHandler(const FWModelContextMenuHandler& rhs)
00166 // {
00167 //    // do actual copying here;
00168 // }
00169 
00170 FWModelContextMenuHandler::~FWModelContextMenuHandler()
00171 {
00172    delete m_modelPopup;
00173 }
00174 
00175 //
00176 // assignment operators
00177 //
00178 // const FWModelContextMenuHandler& FWModelContextMenuHandler::operator=(const FWModelContextMenuHandler& rhs)
00179 // {
00180 //   //An exception safe implementation is
00181 //   FWModelContextMenuHandler temp(rhs);
00182 //   swap(rhs);
00183 //
00184 //   return *this;
00185 // }
00186 
00187 //
00188 // member functions
00189 //
00190 namespace  {
00191    class change_visibility {
00192    public:
00193       change_visibility(bool iIsVisible): m_isVisible(iIsVisible) {}
00194       void operator()(const FWModelId& iID) const {
00195          FWDisplayProperties p = iID.item()->modelInfo(iID.index()).displayProperties();
00196          p.setIsVisible(m_isVisible);
00197          iID.item()->setDisplayProperties(iID.index(), p);
00198       }
00199       bool m_isVisible; 
00200    };
00201 }
00202 void 
00203 FWModelContextMenuHandler::chosenItem(Int_t iChoice)
00204 {
00205    assert(!m_selectionManager->selected().empty());
00206    switch (iChoice) {
00207       case kSetVisibleMO:
00208       {
00209          FWModelId id = *(m_selectionManager->selected().begin());
00210          const FWDisplayProperties& props = id.item()->modelInfo(id.index()).displayProperties();
00211          for_each(m_selectionManager->selected().begin(),
00212                   m_selectionManager->selected().end(), 
00213                   change_visibility(!props.isVisible())
00214                   );
00215          break;
00216       }
00217       case kSetColorMO:
00218       {
00219          FWModelId id = *(m_selectionManager->selected().begin());
00220          createColorPopup();
00221          m_colorPopup->SetName("Selected");
00222          std::vector<Color_t> colors;
00223          m_colorManager->fillLimitedColors(colors);
00224          m_colorPopup->ResetColors(colors, m_colorManager->backgroundColorIndex()==FWColorManager::kBlackIndex);
00225          m_colorPopup->SetSelection(id.item()->modelInfo(id.index()).displayProperties().color());
00226          m_colorPopup->PlacePopup(m_x, m_y, m_colorPopup->GetDefaultWidth(), m_colorPopup->GetDefaultHeight());
00227          break;
00228       }
00229       case kOpenObjectControllerMO:
00230       {
00231          m_guiManager->showModelPopup();
00232          break;
00233       }
00234       case kOpenCollectionControllerMO:
00235       {
00236          m_guiManager->showEDIFrame();
00237          break;
00238       }
00239       case kOpenDetailViewMO:
00240       case kViewOptionsMO:
00241       default:
00242       {
00243          if(iChoice>=kViewOptionsMO) {
00244             assert(0!=m_viewHander);
00245             m_viewHander->select(iChoice-kViewOptionsMO, *(m_selectionManager->selected().begin()), m_x, m_y);
00246          }else {
00247             assert(iChoice<kOpenObjectControllerMO);
00248             assert(m_selectionManager->selected().size()==1);
00249             std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
00250             assert(0!=viewChoices.size());
00251             m_detailViewManager->openDetailViewFor(*(m_selectionManager->selected().begin()),viewChoices[iChoice-kOpenDetailViewMO]) ;
00252          }
00253          break;
00254       }
00255       break;
00256    }
00257 }
00258 
00259 void 
00260 FWModelContextMenuHandler::colorChangeRequested(Color_t color)
00261 {
00262    for(std::set<FWModelId>::const_iterator it =m_selectionManager->selected().begin(),
00263        itEnd = m_selectionManager->selected().end();
00264        it != itEnd;
00265        ++it) {
00266       FWDisplayProperties changeProperties = it->item()->modelInfo(it->index()).displayProperties();
00267       changeProperties.setColor(color);
00268       it->item()->setDisplayProperties(it->index(), changeProperties);
00269    }
00270 }
00271 
00272 void 
00273 FWModelContextMenuHandler::addViewEntry(const char* iEntryName, int iEntryIndex)
00274 {
00275    if(!m_viewSeperator) {        
00276       m_modelPopup->AddSeparator(m_afterViewSeperator);          
00277       m_viewSeperator=dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Before(m_afterViewSeperator));
00278       assert(0!=m_viewSeperator);        
00279    }
00280    if(static_cast<int>(m_nViewEntries) > iEntryIndex) {
00281       m_modelPopup->GetEntry(iEntryIndex+kViewOptionsMO)->GetLabel()->SetString(iEntryName);
00282       m_modelPopup->EnableEntry(iEntryIndex+kViewOptionsMO);
00283    } else {
00284       assert(static_cast<int>(m_nViewEntries) == iEntryIndex);
00285       m_modelPopup->AddEntry(iEntryName,kViewOptionsMO+iEntryIndex,0,0,m_viewSeperator);
00286       ++m_nViewEntries;
00287    }
00288 }
00289 
00290 //
00291 // const member functions
00292 //
00293 void 
00294 FWModelContextMenuHandler::showSelectedModelContext(Int_t iX, Int_t iY, FWViewContextMenuHandlerBase* iHandler) const
00295 {
00296    m_viewHander=iHandler;
00297    assert(!m_selectionManager->selected().empty());
00298    createModelContext();
00299 
00300    //setup the menu based on this object
00301    FWModelId id = *(m_selectionManager->selected().begin());
00302    const FWDisplayProperties& props = id.item()->modelInfo(id.index()).displayProperties();
00303    if(props.isVisible()) {
00304       m_modelPopup->CheckEntry(kSetVisibleMO);
00305    }else {
00306       m_modelPopup->UnCheckEntry(kSetVisibleMO);
00307    }
00308 
00309    if(m_selectionManager->selected().size()==1) {
00310       //add the detail view entries
00311       std::vector<std::string> viewChoices = m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
00312       if(viewChoices.size()>0) {
00313          if(m_nDetailViewEntries < viewChoices.size()) {
00314             for(unsigned int index = m_nDetailViewEntries;
00315                 index != viewChoices.size();
00316                 ++index) {
00317                m_modelPopup->AddEntry(kOpenDetailView,kOpenDetailViewMO+index,0,0,m_seperator);
00318             }
00319             m_nDetailViewEntries=viewChoices.size();
00320          }
00321          const std::string kStart("Open ");
00322          const std::string kEnd(" Detail View ...");
00323          for(unsigned int index=0; index != viewChoices.size(); ++index) {
00324             m_modelPopup->GetEntry(index+kOpenDetailViewMO)->GetLabel()->SetString((kStart+viewChoices[index]+kEnd).c_str());
00325             m_modelPopup->EnableEntry(index+kOpenDetailViewMO);
00326          }
00327          for(unsigned int i =viewChoices.size(); i <m_nDetailViewEntries; ++i) {
00328             m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00329          }
00330          
00331       } else {
00332          for(unsigned int i =0; i <m_nDetailViewEntries; ++i) {
00333             m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00334          }
00335       }
00336    } else {
00337       for(unsigned int i =0; i <m_nDetailViewEntries; ++i) {
00338          m_modelPopup->HideEntry(kOpenDetailViewMO+i);
00339       }
00340    }
00341    //add necessary entries from the view
00342    m_modelPopup->DeleteEntry(m_viewSeperator);
00343    m_viewSeperator=0;
00344 
00345    for(unsigned int i=0; i<m_nViewEntries; ++i) {
00346       m_modelPopup->HideEntry(kViewOptionsMO+i);
00347    }
00348    if(m_viewHander) {
00349       m_viewHander->addTo(const_cast<FWModelContextMenuHandler&>(*this));
00350    }
00351    
00352    m_x=iX;
00353    m_y=iY;
00354    m_modelPopup->PlaceMenu(iX,iY,false,true);
00355 }
00356 
00357 void 
00358 FWModelContextMenuHandler::createModelContext() const
00359 {
00360    if(0==m_modelPopup) {
00361       m_modelPopup = new FWPopupMenu();
00362       
00363       m_modelPopup->AddEntry("Set Visible",kSetVisibleMO);
00364       m_modelPopup->AddEntry("Set Color ...",kSetColorMO);
00365       m_modelPopup->AddEntry(kOpenDetailView,kOpenDetailViewMO);
00366       m_nDetailViewEntries=1;
00367       m_modelPopup->AddSeparator();
00368       m_seperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
00369       assert(0!=m_seperator);
00370       m_modelPopup->AddEntry("Open Object Controller ...",kOpenObjectControllerMO);
00371       m_afterViewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
00372       m_modelPopup->AddEntry("Open Collection Controller ...",kOpenCollectionControllerMO);
00373 
00374       m_modelPopup->Connect("Activated(Int_t)",
00375                             "FWModelContextMenuHandler",
00376                             const_cast<FWModelContextMenuHandler*>(this),
00377                             "chosenItem(Int_t)");
00378    }
00379 }
00380 
00381 void 
00382 FWModelContextMenuHandler::createColorPopup() const
00383 {
00384    if(0==m_colorPopup) {
00385       std::vector<Color_t> colors;
00386       m_colorManager->fillLimitedColors(colors);
00387       
00388       m_colorPopup = new FWColorPopup(gClient->GetDefaultRoot(), colors.front());
00389       m_colorPopup->InitContent("", colors);
00390       m_colorPopup->Connect("ColorSelected(Color_t)","FWModelContextMenuHandler", const_cast<FWModelContextMenuHandler*>(this), "colorChangeRequested(Color_t)");
00391    }
00392 }
00393 
00394 //
00395 // static member functions
00396 //
00397 
00398 ClassImp(FWModelContextMenuHandler)