1 #include "XGUI_ContextMenuMgr.h"
2 #include "XGUI_Workshop.h"
3 #include "XGUI_ObjectsBrowser.h"
4 #include "XGUI_SelectionMgr.h"
5 #include "XGUI_Displayer.h"
6 #include "XGUI_MainWindow.h"
7 #include "XGUI_ViewerProxy.h"
8 #include "XGUI_Selection.h"
10 #include "PartSetPlugin_Part.h"
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_ResultPart.h>
18 #include <QContextMenuEvent>
22 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
28 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
32 void XGUI_ContextMenuMgr::createActions()
34 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
35 addAction("EDIT_CMD", aAction);
37 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
38 addAction("ACTIVATE_PART_CMD", aAction);
40 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
41 addAction("DEACTIVATE_PART_CMD", aAction);
43 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
44 addAction("DELETE_CMD", aAction);
46 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
47 addAction("SHOW_CMD", aAction);
49 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
50 addAction("SHOW_ONLY_CMD", aAction);
52 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
53 addAction("HIDE_CMD", aAction);
56 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
58 if (myActions.contains(theId))
59 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
60 theAction->setData(theId);
61 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
62 myActions[theId] = theAction;
65 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
67 if (myActions.contains(theId))
68 return myActions[theId];
72 QStringList XGUI_ContextMenuMgr::actionIds() const
74 return myActions.keys();
77 void XGUI_ContextMenuMgr::onAction(bool isChecked)
79 QAction* aAction = static_cast<QAction*>(sender());
80 emit actionTriggered(aAction->data().toString(), isChecked);
83 void XGUI_ContextMenuMgr::updateCommandsStatus()
87 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
90 if (sender() == myWorkshop->objectBrowser())
91 aMenu = objectBrowserMenu();
92 else if (sender() == myWorkshop->viewer()) {
96 if (aMenu && (aMenu->actions().size() > 0)) {
97 aMenu->exec(theEvent->globalPos());
102 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
104 QMenu* aMenu = new QMenu();
105 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
106 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
107 int aSelected = aObjects.size();
109 SessionPtr aMgr = ModelAPI_Session::get();
110 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
111 bool hasResult = false;
112 bool hasFeature = false;
113 foreach(ObjectPtr aObj, aObjects)
115 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
116 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
121 if (hasFeature && hasResult)
125 if (aSelected == 1) {
126 ObjectPtr aObject = aObjects.first();
128 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
130 if (aMgr->activeDocument() == aPart->partDoc())
131 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
133 aMenu->addAction(action("ACTIVATE_PART_CMD"));
134 } else if (hasFeature) {
135 aMenu->addAction(action("EDIT_CMD"));
137 if (aDisplayer->isVisible(aObject))
138 aMenu->addAction(action("HIDE_CMD"));
140 aMenu->addAction(action("SHOW_CMD"));
142 aMenu->addAction(action("SHOW_ONLY_CMD"));
144 } else { // If feature is 0 the it means that selected root object (document)
145 if (aMgr->activeDocument() != aMgr->moduleDocument())
146 aMenu->addAction(action("ACTIVATE_PART_CMD"));
150 aMenu->addAction(action("SHOW_CMD"));
151 aMenu->addAction(action("HIDE_CMD"));
152 aMenu->addAction(action("SHOW_ONLY_CMD"));
156 aMenu->addAction(action("DELETE_CMD"));
158 aMenu->addActions(myWorkshop->objectBrowser()->actions());
159 if (aMenu->actions().size() > 0) {
166 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
168 QMenu* aMenu = new QMenu();
169 addViewerItems(aMenu);
170 if (aMenu->actions().size() > 0) {
177 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
179 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
180 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
181 if (aObjects.size() > 0) {
182 //if (aObjects.size() == 1)
183 // theMenu->addAction(action("EDIT_CMD"));
184 bool isVisible = false;
185 foreach(ObjectPtr aObject, aObjects)
187 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
188 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
194 theMenu->addAction(action("HIDE_CMD"));
196 theMenu->addAction(action("SHOW_CMD"));
197 //theMenu->addAction(action("DELETE_CMD"));
199 if (!myWorkshop->isSalomeMode()) {
200 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
201 if (aMDI->actions().size() > 0) {
202 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
203 aSubMenu->addActions(aMDI->actions());
208 void XGUI_ContextMenuMgr::connectObjectBrowser() const
210 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
211 SLOT(onContextMenuRequest(QContextMenuEvent*)));
214 void XGUI_ContextMenuMgr::connectViewer() const
216 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
217 SLOT(onContextMenuRequest(QContextMenuEvent*)));