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>
22 #include <ModelAPI_ResultConstruction.h>
23 #include <ModelAPI_ResultBody.h>
25 #include <ModuleBase_IModule.h>
26 #include <ModuleBase_Tools.h>
29 #include <QContextMenuEvent>
34 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
36 myWorkshop(theParent),
41 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
45 void XGUI_ContextMenuMgr::createActions()
47 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
48 QMainWindow* aDesktop = myWorkshop->mainWindow();
50 aDesktop = myWorkshop->salomeConnector()->desktop();
51 aDesktop->addAction(aAction);
53 addAction("DELETE_CMD", aAction);
54 aAction->setShortcut(Qt::Key_Delete);
55 aAction->setShortcutContext(Qt::ApplicationShortcut);
57 aAction = new QAction(QIcon(":pictures/move.png"), tr("Move..."), this);
58 addAction("MOVE_CMD", aAction);
60 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
61 addAction("COLOR_CMD", aAction);
63 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
64 addAction("SHOW_CMD", aAction);
66 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
67 addAction("SHOW_ONLY_CMD", aAction);
69 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
70 addAction("HIDE_CMD", aAction);
72 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
73 addAction("HIDEALL_CMD", aAction);
75 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
76 addAction("SHADING_CMD", aAction);
78 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
79 addAction("WIREFRAME_CMD", aAction);
81 mySeparator = new QAction(this);
82 mySeparator->setSeparator(true);
85 buildObjBrowserMenu();
89 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
91 if (myActions.contains(theId))
92 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
93 theAction->setData(theId);
94 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
95 myActions[theId] = theAction;
98 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
100 if (myActions.contains(theId))
101 return myActions[theId];
105 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
107 foreach(QAction* eachAction, myActions) {
108 if (eachAction->text() == theName) {
115 QStringList XGUI_ContextMenuMgr::actionIds() const
117 return myActions.keys();
120 void XGUI_ContextMenuMgr::onAction(bool isChecked)
122 QAction* aAction = static_cast<QAction*>(sender());
123 emit actionTriggered(aAction->data().toString(), isChecked);
126 void XGUI_ContextMenuMgr::updateCommandsStatus()
130 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
133 if (sender() == myWorkshop->objectBrowser()) {
134 updateObjectBrowserMenu();
135 aMenu = objBrowserMenu();
136 } else if (sender() == myWorkshop->viewer()) {
138 aMenu = viewerMenu();
141 if (aMenu && (aMenu->actions().size() > 0)) {
142 // it is possible that some objects should do something before and after the popup menu exec
143 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
145 emit beforeContextMenu();
146 aMenu->exec(theEvent->globalPos());
147 emit afterContextMenu();
152 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
154 foreach(QAction* aAction, myActions)
155 aAction->setEnabled(false);
157 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
158 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
159 int aSelected = aObjects.size();
161 SessionPtr aMgr = ModelAPI_Session::get();
162 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
163 bool hasResult = false;
164 bool hasFeature = false;
165 bool hasParameter = false;
166 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
169 if (aSelected == 1) {
170 ObjectPtr aObject = aObjects.first();
173 if (aObject->isDisplayed()) {
174 if (aDisplayer->canBeShaded(aObject)) {
175 action("WIREFRAME_CMD")->setEnabled(true);
176 action("SHADING_CMD")->setEnabled(true);
178 action("HIDE_CMD")->setEnabled(true);
179 } else if (hasResult && (!hasParameter)) {
180 action("SHOW_CMD")->setEnabled(true);
183 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
185 action("SHOW_CMD")->setEnabled(true);
188 if (!(hasParameter || hasFeature))
189 action("SHOW_ONLY_CMD")->setEnabled(true);
191 else if (hasFeature && myWorkshop->canMoveFeature())
192 action("MOVE_CMD")->setEnabled(true);
195 if (hasResult && (!hasParameter)) {
196 action("SHOW_CMD")->setEnabled(true);
197 action("HIDE_CMD")->setEnabled(true);
198 action("SHOW_ONLY_CMD")->setEnabled(true);
199 action("SHADING_CMD")->setEnabled(true);
200 action("WIREFRAME_CMD")->setEnabled(true);
203 if (hasFeature || hasParameter)
204 action("DELETE_CMD")->setEnabled(true);
206 if (myWorkshop->canChangeColor())
207 action("COLOR_CMD")->setEnabled(true);
210 void XGUI_ContextMenuMgr::updateViewerMenu()
212 foreach(QAction* aAction, myActions)
213 aAction->setEnabled(false);
215 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
216 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
217 if (aObjects.size() > 0) {
218 bool isVisible = false;
219 bool isShading = false;
220 bool canBeShaded = false;
221 foreach(ObjectPtr aObject, aObjects)
223 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
224 if (aRes && aRes->isDisplayed()) {
226 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
227 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
233 action("WIREFRAME_CMD")->setEnabled(true);
234 action("SHADING_CMD")->setEnabled(true);
236 action("SHOW_ONLY_CMD")->setEnabled(true);
237 action("HIDE_CMD")->setEnabled(true);
239 action("SHOW_CMD")->setEnabled(true);
241 if (myWorkshop->displayer()->objectsCount() > 0)
242 action("HIDEALL_CMD")->setEnabled(true);
243 if (myWorkshop->canChangeColor())
244 action("COLOR_CMD")->setEnabled(true);
246 action("DELETE_CMD")->setEnabled(true);
249 void XGUI_ContextMenuMgr::connectObjectBrowser()
251 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
252 SLOT(onContextMenuRequest(QContextMenuEvent*)));
255 void XGUI_ContextMenuMgr::connectViewer()
257 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
258 SLOT(onContextMenuRequest(QContextMenuEvent*)));
262 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
264 QAction* aSeparator = new QAction(this);
265 aSeparator->setSeparator(true);
269 // Result construction menu
270 aList.append(action("SHOW_CMD"));
271 aList.append(action("HIDE_CMD"));
272 aList.append(action("SHOW_ONLY_CMD"));
273 aList.append(action("COLOR_CMD"));
274 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
276 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
277 //-------------------------------------
280 aList.append(action("WIREFRAME_CMD"));
281 aList.append(action("SHADING_CMD"));
282 aList.append(action("COLOR_CMD"));
283 aList.append(mySeparator);
284 aList.append(action("SHOW_CMD"));
285 aList.append(action("HIDE_CMD"));
286 aList.append(action("SHOW_ONLY_CMD"));
287 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
289 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
290 //-------------------------------------
293 aList.append(action("DELETE_CMD"));
294 aList.append(action("MOVE_CMD"));
295 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
298 aList.append(action("DELETE_CMD"));
299 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
300 //-------------------------------------
303 void XGUI_ContextMenuMgr::buildViewerMenu()
306 // Result construction menu
307 aList.append(action("HIDE_CMD"));
308 aList.append(action("SHOW_ONLY_CMD"));
309 aList.append(action("HIDEALL_CMD"));
310 aList.append(action("COLOR_CMD"));
311 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
313 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
314 //-------------------------------------
317 aList.append(action("WIREFRAME_CMD"));
318 aList.append(action("SHADING_CMD"));
319 aList.append(action("COLOR_CMD"));
320 aList.append(mySeparator);
321 aList.append(action("HIDE_CMD"));
322 aList.append(action("SHOW_ONLY_CMD"));
323 aList.append(action("HIDEALL_CMD"));
324 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
326 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
327 //-------------------------------------
332 QMenu* XGUI_ContextMenuMgr::objBrowserMenu() const
334 QMenu* aMenu = new QMenu();
335 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
336 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
337 int aSelected = aObjects.size();
338 QActionsList aActions;
339 if (aSelected == 1) {
340 ObjectPtr aObject = aObjects.first();
341 std::string aName = aObject->groupName();
342 if (myObjBrowserMenus.contains(aName))
343 aActions = myObjBrowserMenus[aName];
344 } else if (aSelected > 1) {
345 aActions.append(action("SHADING_CMD"));
346 aActions.append(action("WIREFRAME_CMD"));
347 aActions.append(mySeparator);
348 aActions.append(action("SHOW_CMD"));
349 aActions.append(action("HIDE_CMD"));
350 aActions.append(action("SHOW_ONLY_CMD"));
351 aActions.append(mySeparator);
352 aActions.append(action("DELETE_CMD"));
353 //aActions.append(action("MOVE_CMD"));
355 aMenu->addActions(aActions);
357 ModuleBase_IModule* aModule = myWorkshop->module();
359 aMenu->addSeparator();
360 aModule->addObjectBrowserMenu(aMenu);
362 aMenu->addSeparator();
363 aMenu->addActions(myWorkshop->objectBrowser()->actions());
368 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
370 QMenu* aMenu = new QMenu();
371 ModuleBase_IModule* aModule = myWorkshop->module();
373 aModule->addViewerMenu(aMenu, myActions);
375 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
376 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
377 int aSelected = aObjects.size();
378 QActionsList aActions;
379 if (aSelected == 1) {
380 ObjectPtr aObject = aObjects.first();
381 std::string aName = aObject->groupName();
382 if (myViewerMenu.contains(aName))
383 aActions = myViewerMenu[aName];
384 } else if (aSelected > 1) {
385 aActions.append(action("HIDE_CMD"));
387 aMenu->addActions(aActions);
389 if (!myWorkshop->isSalomeMode()) {
390 aMenu->addSeparator();
391 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
392 if (aMDI->actions().size() > 0) {
393 QMenu* aSubMenu = aMenu->addMenu(tr("Windows"));
394 aSubMenu->addActions(aMDI->actions());
400 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
404 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
405 aLast = myObjBrowserMenus.end();
406 for (; anIt != aLast; anIt++) {
407 QString aGroupName(anIt.key().c_str());
408 if (aGroups.contains(aGroupName))
410 QActionsList anActions = anIt.value();
411 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
413 for (; anAIt != anALast && !aFound; anAIt++)
414 aFound = (*anAIt)->data().toString() == theName;
416 aGroups.append(aGroupName);