2 #include "XGUI_ContextMenuMgr.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_ObjectsBrowser.h"
5 #include "XGUI_SelectionMgr.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDocRef.h>
11 #include <QContextMenuEvent>
14 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
15 QObject(theParent), myWorkshop(theParent)
20 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
24 void XGUI_ContextMenuMgr::createActions()
26 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
27 addAction("EDIT_CMD", aAction);
29 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
30 addAction("ACTIVATE_PART_CMD", aAction);
32 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
33 addAction("DEACTIVATE_PART_CMD", aAction);
36 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
38 if (myActions.contains(theId))
39 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
40 theAction->setData(theId);
41 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
42 myActions[theId] = theAction;
45 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
47 if (myActions.contains(theId))
48 return myActions[theId];
52 QStringList XGUI_ContextMenuMgr::actionIds() const
54 return myActions.keys();
57 void XGUI_ContextMenuMgr::onAction(bool isChecked)
59 QAction* aAction = static_cast<QAction*>(sender());
60 emit actionTriggered(aAction->data().toString(), isChecked);
63 void XGUI_ContextMenuMgr::updateCommandsStatus()
67 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
70 if (sender() == myWorkshop->objectBrowser())
71 aMenu = objectBrowserMenu();
74 aMenu->exec(theEvent->globalPos());
79 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
81 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
82 QFeatureList aFeatures = aSelMgr->selectedFeatures();
83 if (aFeatures.size() == 1) {
84 FeaturePtr aFeature = aFeatures.first();
85 QMenu* aMenu = new QMenu();
86 if (aFeature->getKind() == "Part") {
87 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
88 boost::shared_ptr<ModelAPI_Document> aFeaDoc = aFeature->data()->docRef("PartDocument")->value();
89 if (aMgr->currentDocument() == aFeaDoc)
90 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
92 aMenu->addAction(action("ACTIVATE_PART_CMD"));
94 aMenu->addAction(action("EDIT_CMD"));
101 void XGUI_ContextMenuMgr::connectObjectBrowser() const
103 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
104 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));