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);
35 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
36 addAction("DELETE_CMD", aAction);
39 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
41 if (myActions.contains(theId))
42 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
43 theAction->setData(theId);
44 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
45 myActions[theId] = theAction;
48 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
50 if (myActions.contains(theId))
51 return myActions[theId];
55 QStringList XGUI_ContextMenuMgr::actionIds() const
57 return myActions.keys();
60 void XGUI_ContextMenuMgr::onAction(bool isChecked)
62 QAction* aAction = static_cast<QAction*>(sender());
63 emit actionTriggered(aAction->data().toString(), isChecked);
66 void XGUI_ContextMenuMgr::updateCommandsStatus()
70 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
73 if (sender() == myWorkshop->objectBrowser())
74 aMenu = objectBrowserMenu();
77 aMenu->exec(theEvent->globalPos());
82 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
84 QList<QAction*> aActions;
85 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
86 QFeatureList aFeatures = aSelMgr->selectedFeatures();
87 if (aFeatures.size() == 1) {
88 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
89 FeaturePtr aFeature = aFeatures.first();
92 if (aFeature->getKind() == "Part") {
93 boost::shared_ptr<ModelAPI_Document> aFeaDoc = aFeature->data()->docRef("PartDocument")->value();
94 if (aMgr->currentDocument() == aFeaDoc)
95 aActions.append(action("DEACTIVATE_PART_CMD"));
97 aActions.append(action("ACTIVATE_PART_CMD"));
99 aActions.append(action("EDIT_CMD"));
101 aActions.append(action("DELETE_CMD"));
103 // Process Root object (document)
104 } else { // If feature is 0 the it means that selected root object (document)
105 if (aMgr->currentDocument() != aMgr->rootDocument()) {
106 aActions.append(action("ACTIVATE_PART_CMD"));
110 aActions.append(myWorkshop->objectBrowser()->actions());
111 if (aActions.size() > 0) {
112 QMenu* aMenu = new QMenu();
113 aMenu->addActions(aActions);
119 void XGUI_ContextMenuMgr::connectObjectBrowser() const
121 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
122 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));