2 #include "XGUI_ContextMenuMgr.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_ObjectsBrowser.h"
5 #include "XGUI_SelectionMgr.h"
6 #include "XGUI_Displayer.h"
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_AttributeDocRef.h>
10 #include <ModelAPI_Object.h>
13 #include <QContextMenuEvent>
16 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
17 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);
39 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
40 addAction("SHOW_CMD", aAction);
42 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
43 addAction("HIDE_CMD", aAction);
46 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
48 if (myActions.contains(theId))
49 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
50 theAction->setData(theId);
51 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
52 myActions[theId] = theAction;
55 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
57 if (myActions.contains(theId))
58 return myActions[theId];
62 QStringList XGUI_ContextMenuMgr::actionIds() const
64 return myActions.keys();
67 void XGUI_ContextMenuMgr::onAction(bool isChecked)
69 QAction* aAction = static_cast<QAction*>(sender());
70 emit actionTriggered(aAction->data().toString(), isChecked);
73 void XGUI_ContextMenuMgr::updateCommandsStatus()
77 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
80 if (sender() == myWorkshop->objectBrowser())
81 aMenu = objectBrowserMenu();
84 aMenu->exec(theEvent->globalPos());
89 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
91 QMenu* aMenu = new QMenu();
92 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
93 QFeatureList aFeatures = aSelMgr->selectedFeatures();
94 if (aFeatures.size() == 1) {
95 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
96 FeaturePtr aFeature = aFeatures.first();
99 if (aFeature->getKind() == PARTSET_PART_KIND) {
100 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
101 DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
102 if (aMgr->currentDocument() == aFeaDoc)
103 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
105 aMenu->addAction(action("ACTIVATE_PART_CMD"));
107 aMenu->addAction(action("EDIT_CMD"));
109 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
110 if (aDisplayer->isVisible(aFeature))
111 aMenu->addAction(action("HIDE_CMD"));
113 aMenu->addAction(action("SHOW_CMD"));
115 aMenu->addAction(action("DELETE_CMD"));
116 aMenu->addSeparator();
118 // Process Root object (document)
119 } else { // If feature is 0 the it means that selected root object (document)
120 if (aMgr->currentDocument() != aMgr->rootDocument()) {
121 aMenu->addAction(action("ACTIVATE_PART_CMD"));
125 aMenu->addActions(myWorkshop->objectBrowser()->actions());
126 if (aMenu->actions().size() > 0) {
133 void XGUI_ContextMenuMgr::connectObjectBrowser() const
135 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
136 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));