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>
9 #include <ModelAPI_Object.h>
12 #include <QContextMenuEvent>
15 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
16 QObject(theParent), myWorkshop(theParent)
21 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
25 void XGUI_ContextMenuMgr::createActions()
27 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
28 addAction("EDIT_CMD", aAction);
30 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
31 addAction("ACTIVATE_PART_CMD", aAction);
33 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
34 addAction("DEACTIVATE_PART_CMD", aAction);
36 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
37 addAction("DELETE_CMD", aAction);
40 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
42 if (myActions.contains(theId))
43 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
44 theAction->setData(theId);
45 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
46 myActions[theId] = theAction;
49 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
51 if (myActions.contains(theId))
52 return myActions[theId];
56 QStringList XGUI_ContextMenuMgr::actionIds() const
58 return myActions.keys();
61 void XGUI_ContextMenuMgr::onAction(bool isChecked)
63 QAction* aAction = static_cast<QAction*>(sender());
64 emit actionTriggered(aAction->data().toString(), isChecked);
67 void XGUI_ContextMenuMgr::updateCommandsStatus()
71 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
74 if (sender() == myWorkshop->objectBrowser())
75 aMenu = objectBrowserMenu();
78 aMenu->exec(theEvent->globalPos());
83 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
85 QList<QAction*> aActions;
86 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
87 QFeatureList aFeatures = aSelMgr->selectedFeatures();
88 if (aFeatures.size() == 1) {
89 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
90 FeaturePtr aFeature = aFeatures.first();
93 if (aFeature->getKind() == "Part") {
94 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
95 DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
96 if (aMgr->currentDocument() == aFeaDoc)
97 aActions.append(action("DEACTIVATE_PART_CMD"));
99 aActions.append(action("ACTIVATE_PART_CMD"));
101 aActions.append(action("EDIT_CMD"));
103 aActions.append(action("DELETE_CMD"));
105 // Process Root object (document)
106 } else { // If feature is 0 the it means that selected root object (document)
107 if (aMgr->currentDocument() != aMgr->rootDocument()) {
108 aActions.append(action("ACTIVATE_PART_CMD"));
112 aActions.append(myWorkshop->objectBrowser()->actions());
113 if (aActions.size() > 0) {
114 QMenu* aMenu = new QMenu();
115 aMenu->addActions(aActions);
121 void XGUI_ContextMenuMgr::connectObjectBrowser() const
123 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
124 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));