MenuOptions

R__dummyintdefault355_c

change_visibility

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
// -*- C++ -*-
//
// Package:     Core
// Class  :     FWModelContextMenuHandler
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Chris Jones
//         Created:  Tue Sep 22 13:26:04 CDT 2009
//

// system include files
#include <cassert>
#include <iostream>
#include "TGMenu.h"
#include "KeySymbols.h"

#include "FWCore/Reflection/interface/FunctionWithDict.h"
#include "FWCore/Reflection/interface/ObjectWithDict.h"
#include "FWCore/Reflection/interface/TypeWithDict.h"
#include "TClass.h"
#include "TError.h"

// user include files
#include "Fireworks/Core/src/FWModelContextMenuHandler.h"
#include "Fireworks/Core/interface/FWSelectionManager.h"
#include "Fireworks/Core/interface/FWDetailViewManager.h"
#include "Fireworks/Core/interface/FWColorManager.h"
#include "Fireworks/Core/src/FWColorSelect.h"
#include "Fireworks/Core/src/FWPopupMenu.cc"
#include "Fireworks/Core/interface/FWEventItem.h"
#include "Fireworks/Core/interface/FWGUIManager.h"
#include "Fireworks/Core/interface/FWViewContextMenuHandlerBase.h"

//
// constants, enums and typedefs
//
enum MenuOptions {
  kSetVisibleMO,
  kSetColorMO,
  //   kPrint,
  kOpenDetailViewMO,
  kAfterOpenDetailViewMO = 10,
  kOpen3DRegion,
  kOpenObjectControllerMO = 100,
  kOpenCollectionControllerMO,
  kViewOptionsMO = 1000,
  kLastOfMO
};

//
// static data member definitions
//
static const char* const kOpenDetailView = "Open Detailed View ...";

//
// constructors and destructor
//
FWModelContextMenuHandler::FWModelContextMenuHandler(FWSelectionManager* iSM,
                                                     FWDetailViewManager* iDVM,
                                                     FWColorManager* iCM,
                                                     FWGUIManager* iGM)
    : m_modelPopup(nullptr),
      m_colorPopup(nullptr),
      m_selectionManager(iSM),
      m_detailViewManager(iDVM),
      m_colorManager(iCM),
      m_guiManager(iGM),
      m_seperator(nullptr),
      m_viewSeperator(nullptr),
      m_afterViewSeperator(nullptr),
      m_x(0),
      m_y(0),
      m_nDetailViewEntries(0),
      m_nViewEntries(0),
      m_viewHander(nullptr) {}

// FWModelContextMenuHandler::FWModelContextMenuHandler(const FWModelContextMenuHandler& rhs)
// {
//    // do actual copying here;
// }

FWModelContextMenuHandler::~FWModelContextMenuHandler() { delete m_modelPopup; }

//
// assignment operators
//
// const FWModelContextMenuHandler& FWModelContextMenuHandler::operator=(const FWModelContextMenuHandler& rhs)
// {
//   //An exception safe implementation is
//   FWModelContextMenuHandler temp(rhs);
//   swap(rhs);
//
//   return *this;
// }

//
// member functions
//
#include "TROOT.h"
namespace {
  class change_visibility {
  public:
    change_visibility(bool iIsVisible) : m_isVisible(iIsVisible) {}
    void operator()(const FWModelId& iID) const {
      FWDisplayProperties p = iID.item()->modelInfo(iID.index()).displayProperties();
      p.setIsVisible(m_isVisible);
      iID.item()->setDisplayProperties(iID.index(), p);
    }
    bool m_isVisible;
  };
}  // namespace
void FWModelContextMenuHandler::chosenItem(Int_t iChoice) {
  assert(!m_selectionManager->selected().empty());
  switch (iChoice) {
    case kSetVisibleMO: {
      FWModelId id = *(m_selectionManager->selected().begin());
      for_each(m_selectionManager->selected().begin(),
               m_selectionManager->selected().end(),
               change_visibility(!id.item()->modelInfo(id.index()).displayProperties().isVisible()));
      break;
    }
    case kSetColorMO: {
      FWModelId id = *(m_selectionManager->selected().begin());
      createColorPopup();
      m_colorPopup->SetName("Selected");
      std::vector<Color_t> colors;
      m_colorManager->fillLimitedColors(colors);
      m_colorPopup->ResetColors(colors, m_colorManager->backgroundColorIndex() == FWColorManager::kBlackIndex);
      m_colorPopup->SetSelection(id.item()->modelInfo(id.index()).displayProperties().color());
      m_colorPopup->PlacePopup(m_x, m_y, m_colorPopup->GetDefaultWidth(), m_colorPopup->GetDefaultHeight());
      break;
    } /*
      case kPrint:
      {
         FWModelId id = *(m_selectionManager->selected().begin());
         edm::TypeWithDict rtype(edm::TypeWithDict::byName(id.item()->modelType()->GetName()));
         edm::ObjectWithDict o(rtype, const_cast<void *>(id.item()->modelData(id.index())));

         // void* xx = &std::cout;
         //const std::vector<void*> j(1, xx);
         //edm::TypeMemberQuery inh =  edm::TypeMemberQuery::InheritedAlso;
         //edm::FunctionWithDict m = rtype.functionMemberByName("print",edm::TypeWithDict(edm::TypeWithDict::byName("void (std::ostream&)"), edm::TypeModifiers::Const), edm::TypeModifiers::NoMod , inh))
         //m.Invoke(o, 0, j);

         const char* cmd  = Form("FWGUIManager::OStream() << *(%s*)%p ;",  id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
         //const char* cmd  = Form("*((std::ostream*)%p) << (%s*)%p ;", (void*)(&std::cout), id.item()->modelType()->GetName(), (void*)id.item()->modelData(id.index()));
         std::cout << cmd << std::endl;
         gROOT->ProcessLine(cmd);


         break;
         }*/
    case kOpenObjectControllerMO: {
      m_guiManager->showModelPopup();
      break;
    }
    case kOpenCollectionControllerMO: {
      m_guiManager->showEDIFrame();
      break;
    }
    case kOpen3DRegion: {
      m_guiManager->open3DRegion();
      break;
    }
    case kOpenDetailViewMO:
    case kViewOptionsMO:
    default: {
      if (iChoice >= kViewOptionsMO) {
        assert(nullptr != m_viewHander);
        m_viewHander->select(iChoice - kViewOptionsMO, *(m_selectionManager->selected().begin()), m_x, m_y);
      } else {
        assert(iChoice < kOpenObjectControllerMO);
        assert(m_selectionManager->selected().size() == 1);
        std::vector<std::string> viewChoices =
            m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
        assert(!viewChoices.empty());
        m_detailViewManager->openDetailViewFor(*(m_selectionManager->selected().begin()),
                                               viewChoices[iChoice - kOpenDetailViewMO]);
      }
      break;
    } break;
  }
}

void FWModelContextMenuHandler::colorChangeRequested(Color_t color) {
  for (std::set<FWModelId>::const_iterator it = m_selectionManager->selected().begin(),
                                           itEnd = m_selectionManager->selected().end();
       it != itEnd;
       ++it) {
    FWDisplayProperties changeProperties = it->item()->modelInfo(it->index()).displayProperties();
    changeProperties.setColor(color);
    it->item()->setDisplayProperties(it->index(), changeProperties);
  }
}

void FWModelContextMenuHandler::addViewEntry(const char* iEntryName, int iEntryIndex, bool enabled) {
  if (!m_viewSeperator) {
    m_modelPopup->AddSeparator(m_afterViewSeperator);
    m_viewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Before(m_afterViewSeperator));
    assert(nullptr != m_viewSeperator);
  }

  if (static_cast<int>(m_nViewEntries) > iEntryIndex) {
    m_modelPopup->GetEntry(iEntryIndex + kViewOptionsMO)->GetLabel()->SetString(iEntryName);
    if (enabled)
      m_modelPopup->EnableEntry(iEntryIndex + kViewOptionsMO);
    else
      m_modelPopup->DisableEntry(iEntryIndex + kViewOptionsMO);

  } else {
    assert(static_cast<int>(m_nViewEntries) == iEntryIndex);
    m_modelPopup->AddEntry(iEntryName, kViewOptionsMO + iEntryIndex, nullptr, nullptr, m_viewSeperator);

    if (enabled)
      m_modelPopup->EnableEntry(kViewOptionsMO + iEntryIndex);
    else
      m_modelPopup->DisableEntry(kViewOptionsMO + iEntryIndex);

    ++m_nViewEntries;
  }
}
//
// const member functions
//
void FWModelContextMenuHandler::showSelectedModelContext(Int_t iX,
                                                         Int_t iY,
                                                         FWViewContextMenuHandlerBase* iHandler) const {
  m_viewHander = iHandler;
  assert(!m_selectionManager->selected().empty());
  createModelContext();

  //setup the menu based on this object
  FWModelId id = *(m_selectionManager->selected().begin());
  if (id.item()->modelInfo(id.index()).displayProperties().isVisible()) {
    m_modelPopup->CheckEntry(kSetVisibleMO);
  } else {
    m_modelPopup->UnCheckEntry(kSetVisibleMO);
  }

  if (m_selectionManager->selected().size() == 1) {
    {
      //edm::TypeWithDict rtype(edm::TypeWithDict::byName(id.item()->modelType()->GetName()));
      //edm::ObjectWithDict o(rtype, const_cast<void *>(id.item()->modelData(id.index())));
      //edm::TypeMemberQuery inh =  edm::TypeMemberQuery::InheritedAlso;
      //if ( rtype.functionMemberByName("print",edm::TypeWithDict(edm::TypeWithDict::byName("void (std::ostream&)"), Long_t(kIsConstant)), 0, inh))
      //{
      //m_modelPopup->EnableEntry(kPrint);
      // std::cout <<  "Enable " <<std::endl;
      //}
      //else
      //{
      m_modelPopup->DisableEntry(kPrint);
      // printf("Disable print \n");
      //}
    }
    //add the detail view entries
    std::vector<std::string> viewChoices =
        m_detailViewManager->detailViewsFor(*(m_selectionManager->selected().begin()));
    if (!viewChoices.empty()) {
      if (m_nDetailViewEntries < viewChoices.size()) {
        for (unsigned int index = m_nDetailViewEntries; index != viewChoices.size(); ++index) {
          m_modelPopup->AddEntry(kOpenDetailView, kOpenDetailViewMO + index, nullptr, nullptr, m_seperator);
        }
        m_nDetailViewEntries = viewChoices.size();
      }
      const std::string kStart("Open ");
      const std::string kEnd(" Detail View ...");
      for (unsigned int index = 0; index != viewChoices.size(); ++index) {
        m_modelPopup->EnableEntry(index + kOpenDetailViewMO);  // need to call this to make it visible
        if (viewChoices[index][0] != '!') {
          m_modelPopup->GetEntry(index + kOpenDetailViewMO)
              ->GetLabel()
              ->SetString((kStart + viewChoices[index] + kEnd).c_str());
        } else {
          m_modelPopup->GetEntry(index + kOpenDetailViewMO)
              ->GetLabel()
              ->SetString((kStart + &viewChoices[index][1] + kEnd).c_str());
          m_modelPopup->DisableEntry(index + kOpenDetailViewMO);
        }
      }
      for (unsigned int i = viewChoices.size(); i < m_nDetailViewEntries; ++i) {
        m_modelPopup->HideEntry(kOpenDetailViewMO + i);
      }

    } else {
      for (unsigned int i = 0; i < m_nDetailViewEntries; ++i) {
        m_modelPopup->HideEntry(kOpenDetailViewMO + i);
      }
    }
  } else {
    for (unsigned int i = 0; i < m_nDetailViewEntries; ++i) {
      m_modelPopup->HideEntry(kOpenDetailViewMO + i);
    }
  }
  //add necessary entries from the view
  m_modelPopup->DeleteEntry(m_viewSeperator);
  m_viewSeperator = nullptr;

  for (unsigned int i = 0; i < m_nViewEntries; ++i) {
    m_modelPopup->HideEntry(kViewOptionsMO + i);
  }
  if (m_viewHander) {
    m_viewHander->addTo(const_cast<FWModelContextMenuHandler&>(*this), *(m_selectionManager->selected().begin()));
  }

  m_x = iX;
  m_y = iY;
  m_modelPopup->PlaceMenu(iX, iY, false, true);
}

void FWModelContextMenuHandler::createModelContext() const {
  if (nullptr == m_modelPopup) {
    m_modelPopup = new FWPopupMenu();

    m_modelPopup->AddEntry("Set Visible", kSetVisibleMO);
    m_modelPopup->AddEntry("Set Color ...", kSetColorMO);
    //      m_modelPopup->AddEntry("Print ...",kPrint);
    m_modelPopup->AddEntry(kOpenDetailView, kOpenDetailViewMO);
    m_nDetailViewEntries = 1;
    m_seperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
    assert(nullptr != m_seperator);
    m_modelPopup->AddEntry("Open 3D Region ...", kOpen3DRegion);
    m_modelPopup->AddSeparator();
    m_modelPopup->AddEntry("Open Object Controller ...", kOpenObjectControllerMO);
    m_afterViewSeperator = dynamic_cast<TGMenuEntry*>(m_modelPopup->GetListOfEntries()->Last());
    m_modelPopup->AddEntry("Open Collection Controller ...", kOpenCollectionControllerMO);

    m_modelPopup->Connect("Activated(Int_t)",
                          "FWModelContextMenuHandler",
                          const_cast<FWModelContextMenuHandler*>(this),
                          "chosenItem(Int_t)");
  }
}

void FWModelContextMenuHandler::createColorPopup() const {
  if (nullptr == m_colorPopup) {
    std::vector<Color_t> colors;
    m_colorManager->fillLimitedColors(colors);

    m_colorPopup = new FWColorPopup(gClient->GetDefaultRoot(), colors.front());
    m_colorPopup->InitContent("", colors);
    m_colorPopup->Connect("ColorSelected(Color_t)",
                          "FWModelContextMenuHandler",
                          const_cast<FWModelContextMenuHandler*>(this),
                          "colorChangeRequested(Color_t)");
  }
}

//
// static member functions
//

ClassImp(FWModelContextMenuHandler);