1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_ContextMenuMgr.h"
4 #include "XGUI_Workshop.h"
5 #include "XGUI_ObjectsBrowser.h"
6 #include "XGUI_SelectionMgr.h"
7 #include "XGUI_Displayer.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Selection.h"
10 #include "XGUI_SalomeConnector.h"
12 #include <AppElements_MainWindow.h>
14 //#include "PartSetPlugin_Part.h"
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_AttributeDocRef.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_ResultGroup.h>
21 #include <ModelAPI_ResultParameter.h>
23 #include <ModuleBase_IModule.h>
24 #include <ModuleBase_Tools.h>
27 #include <QContextMenuEvent>
31 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
37 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
41 void XGUI_ContextMenuMgr::createActions()
43 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
44 QMainWindow* aDesktop = myWorkshop->mainWindow();
46 aDesktop = myWorkshop->salomeConnector()->desktop();
47 aDesktop->addAction(aAction);
49 addAction("DELETE_CMD", aAction);
50 aAction->setShortcut(Qt::Key_Delete);
51 aAction->setShortcutContext(Qt::ApplicationShortcut);
53 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color"), this);
54 addAction("COLOR_CMD", aAction);
56 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
57 addAction("SHOW_CMD", aAction);
59 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
60 addAction("SHOW_ONLY_CMD", aAction);
62 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
63 addAction("HIDE_CMD", aAction);
65 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
66 addAction("HIDEALL_CMD", aAction);
68 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
69 addAction("SHADING_CMD", aAction);
71 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
72 addAction("WIREFRAME_CMD", aAction);
75 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
77 if (myActions.contains(theId))
78 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
79 theAction->setData(theId);
80 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
81 myActions[theId] = theAction;
84 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
86 if (myActions.contains(theId))
87 return myActions[theId];
91 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
93 foreach(QAction* eachAction, myActions) {
94 if (eachAction->text() == theName) {
101 QStringList XGUI_ContextMenuMgr::actionIds() const
103 return myActions.keys();
106 void XGUI_ContextMenuMgr::onAction(bool isChecked)
108 QAction* aAction = static_cast<QAction*>(sender());
109 emit actionTriggered(aAction->data().toString(), isChecked);
112 void XGUI_ContextMenuMgr::updateCommandsStatus()
116 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
119 if (sender() == myWorkshop->objectBrowser())
120 aMenu = objectBrowserMenu();
121 else if (sender() == myWorkshop->viewer()) {
122 aMenu = viewerMenu();
125 if (aMenu && (aMenu->actions().size() > 0)) {
126 // it is possible that some objects should do something before and after the popup menu exec
127 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
129 emit beforeContextMenu();
130 aMenu->exec(theEvent->globalPos());
131 emit afterContextMenu();
136 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
138 QMenu* aMenu = new QMenu();
139 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
140 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
141 int aSelected = aObjects.size();
143 SessionPtr aMgr = ModelAPI_Session::get();
144 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
145 bool hasResult = false;
146 bool hasFeature = false;
147 bool hasParameter = false;
148 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
151 if (aSelected == 1) {
152 ObjectPtr aObject = aObjects.first();
155 if (aObject->isDisplayed()) {
156 if (aDisplayer->canBeShaded(aObject)) {
157 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
158 aMenu->addAction(action("WIREFRAME_CMD"));
160 aMenu->addAction(action("SHADING_CMD"));
162 aMenu->addSeparator();
163 aMenu->addAction(action("HIDE_CMD"));
164 } else if (hasResult && (!hasParameter)) {
165 aMenu->addAction(action("SHOW_CMD"));
168 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
170 aMenu->addAction(action("SHOW_CMD"));
173 if (!(hasParameter || hasFeature))
174 aMenu->addAction(action("SHOW_ONLY_CMD"));
178 if (hasResult && (!hasParameter)) {
179 aMenu->addAction(action("SHOW_CMD"));
180 aMenu->addAction(action("HIDE_CMD"));
181 aMenu->addAction(action("SHOW_ONLY_CMD"));
182 aMenu->addSeparator();
183 aMenu->addAction(action("SHADING_CMD"));
184 aMenu->addAction(action("WIREFRAME_CMD"));
187 if (hasFeature || hasParameter)
188 aMenu->addAction(action("DELETE_CMD"));
190 if (myWorkshop->canChangeColor())
191 aMenu->addAction(action("COLOR_CMD"));
193 aMenu->addSeparator();
194 ModuleBase_IModule* aModule = myWorkshop->module();
196 aModule->addObjectBrowserMenu(aMenu);
197 aMenu->addSeparator();
199 aMenu->addActions(myWorkshop->objectBrowser()->actions());
202 if (aMenu->actions().size() > 0) {
209 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
211 QMenu* aMenu = new QMenu();
212 addViewerMenu(aMenu);
213 if (aMenu->actions().size() > 0) {
220 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
222 bool aIsDone = false;
223 ModuleBase_IModule* aModule = myWorkshop->module();
225 aIsDone = aModule->addViewerMenu(theMenu, myActions);
228 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
229 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
230 if (aObjects.size() > 0) {
231 bool isVisible = false;
232 bool isShading = false;
233 bool canBeShaded = false;
234 foreach(ObjectPtr aObject, aObjects)
236 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
237 if (aRes && aRes->isDisplayed()) {
239 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
240 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
247 theMenu->addAction(action("WIREFRAME_CMD"));
249 theMenu->addAction(action("SHADING_CMD"));
251 theMenu->addSeparator();
252 theMenu->addAction(action("SHOW_ONLY_CMD"));
253 theMenu->addAction(action("HIDE_CMD"));
255 theMenu->addAction(action("SHOW_CMD"));
257 if (myWorkshop->displayer()->objectsCount() > 0)
258 theMenu->addAction(action("HIDEALL_CMD"));
259 if (myWorkshop->canChangeColor())
260 theMenu->addAction(action("COLOR_CMD"));
262 if (!myWorkshop->isSalomeMode()) {
263 theMenu->addSeparator();
264 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
265 if (aMDI->actions().size() > 0) {
266 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
267 aSubMenu->addActions(aMDI->actions());
273 void XGUI_ContextMenuMgr::connectObjectBrowser() const
275 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
276 SLOT(onContextMenuRequest(QContextMenuEvent*)));
279 void XGUI_ContextMenuMgr::connectViewer() const
281 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
282 SLOT(onContextMenuRequest(QContextMenuEvent*)));