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 "PartSetPlugin_Part.h"
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_AttributeDocRef.h>
12 #include <ModelAPI_Object.h>
15 #include <QContextMenuEvent>
18 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
19 QObject(theParent), myWorkshop(theParent)
23 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
27 void XGUI_ContextMenuMgr::createActions()
29 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
30 addAction("EDIT_CMD", aAction);
32 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
33 addAction("ACTIVATE_PART_CMD", aAction);
35 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
36 addAction("DEACTIVATE_PART_CMD", aAction);
38 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
39 addAction("DELETE_CMD", aAction);
41 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
42 addAction("SHOW_CMD", aAction);
44 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
45 addAction("HIDE_CMD", aAction);
48 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
50 if (myActions.contains(theId))
51 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
52 theAction->setData(theId);
53 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
54 myActions[theId] = theAction;
57 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
59 if (myActions.contains(theId))
60 return myActions[theId];
64 QStringList XGUI_ContextMenuMgr::actionIds() const
66 return myActions.keys();
69 void XGUI_ContextMenuMgr::onAction(bool isChecked)
71 QAction* aAction = static_cast<QAction*>(sender());
72 emit actionTriggered(aAction->data().toString(), isChecked);
75 void XGUI_ContextMenuMgr::updateCommandsStatus()
79 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
82 if (sender() == myWorkshop->objectBrowser())
83 aMenu = objectBrowserMenu();
86 aMenu->exec(theEvent->globalPos());
91 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
93 QMenu* aMenu = new QMenu();
94 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
95 QFeatureList aFeatures = aSelMgr->selectedFeatures();
96 if (aFeatures.size() == 1) {
97 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
98 FeaturePtr aFeature = aFeatures.first();
101 if (aFeature->getKind() == PARTSET_PART_KIND) {
102 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
103 DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
104 if (aMgr->currentDocument() == aFeaDoc)
105 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
107 aMenu->addAction(action("ACTIVATE_PART_CMD"));
109 aMenu->addAction(action("EDIT_CMD"));
111 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
112 if (aDisplayer->isVisible(aFeature))
113 aMenu->addAction(action("HIDE_CMD"));
115 aMenu->addAction(action("SHOW_CMD"));
117 aMenu->addAction(action("DELETE_CMD"));
118 aMenu->addSeparator();
120 // Process Root object (document)
121 } else { // If feature is 0 the it means that selected root object (document)
122 if (aMgr->currentDocument() != aMgr->rootDocument()) {
123 aMenu->addAction(action("ACTIVATE_PART_CMD"));
127 aMenu->addActions(myWorkshop->objectBrowser()->actions());
128 if (aMenu->actions().size() > 0) {
135 void XGUI_ContextMenuMgr::connectObjectBrowser() const
137 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
138 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));