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"
11 #include "XGUI_DataModel.h"
12 #include "XGUI_OperationMgr.h"
13 #include "XGUI_Tools.h"
16 #include <AppElements_MainWindow.h>
19 //#include "PartSetPlugin_Part.h"
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_AttributeDocRef.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_ResultGroup.h>
26 #include <ModelAPI_ResultParameter.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_Tools.h>
31 #include <ModuleBase_IModule.h>
32 #include <ModuleBase_Tools.h>
33 #include <ModuleBase_OperationAction.h>
34 #include <ModuleBase_ViewerPrs.h>
37 #include <QActionGroup>
38 #include <QContextMenuEvent>
41 #include <QMainWindow>
44 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
46 myWorkshop(theParent),
51 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
55 void XGUI_ContextMenuMgr::createActions()
58 QMainWindow* aDesktop = myWorkshop->salomeConnector()->desktop();
60 QMainWindow* aDesktop = myWorkshop->mainWindow();
63 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
64 aDesktop->addAction(aAction);
66 addAction("DELETE_CMD", aAction);
67 aAction->setShortcutContext(Qt::ApplicationShortcut);
69 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
70 addAction("RENAME_CMD", aAction);
71 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
73 aAction = new QAction(QIcon(":pictures/move.png"), XGUI_Workshop::MOVE_TO_END_COMMAND, this);
74 addAction("MOVE_CMD", aAction);
76 aAction = new QAction(QIcon(":pictures/clean_history.png"), tr("Clean history"), this);
77 addAction("CLEAN_HISTORY_CMD", aAction);
79 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
80 addAction("COLOR_CMD", aAction);
82 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
83 addAction("SHOW_CMD", aAction);
85 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
86 addAction("SHOW_ONLY_CMD", aAction);
88 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
89 addAction("HIDE_CMD", aAction);
91 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
92 addAction("HIDEALL_CMD", aAction);
94 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
95 addAction("SHADING_CMD", aAction);
97 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
98 addAction("WIREFRAME_CMD", aAction);
100 mySeparator = new QAction(this);
101 mySeparator->setSeparator(true);
103 mySelectActions = new QActionGroup(this);
104 mySelectActions->setExclusive(true);
106 aAction = new QAction(QIcon(":pictures/vertex.png"), tr("Vertices"), this);
107 aAction->setCheckable(true);
108 addAction("SELECT_VERTEX_CMD", aAction);
109 mySelectActions->addAction(aAction);
111 aAction = new QAction(QIcon(":pictures/edge.png"), tr("Edges"), this);
112 aAction->setCheckable(true);
113 addAction("SELECT_EDGE_CMD", aAction);
114 mySelectActions->addAction(aAction);
116 aAction = new QAction(QIcon(":pictures/face.png"), tr("Faces"), this);
117 aAction->setCheckable(true);
118 addAction("SELECT_FACE_CMD", aAction);
119 mySelectActions->addAction(aAction);
121 aAction = new QAction(QIcon(":pictures/result.png"), tr("Result"), this);
122 aAction->setCheckable(true);
123 addAction("SELECT_RESULT_CMD", aAction);
124 mySelectActions->addAction(aAction);
126 aAction->setChecked(true);
128 aAction = new QAction(QIcon(":pictures/find_result.png"), tr("Select results"), this);
129 addAction("SHOW_RESULTS_CMD", aAction);
131 buildObjBrowserMenu();
135 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
137 if (myActions.contains(theId))
138 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
139 theAction->setData(theId);
140 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
141 myActions[theId] = theAction;
144 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
146 if (myActions.contains(theId))
147 return myActions[theId];
151 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
153 foreach(QAction* eachAction, myActions) {
154 if (eachAction->text() == theName) {
161 QStringList XGUI_ContextMenuMgr::actionIds() const
163 return myActions.keys();
166 void XGUI_ContextMenuMgr::onAction(bool isChecked)
168 QAction* aAction = static_cast<QAction*>(sender());
169 emit actionTriggered(aAction->data().toString(), isChecked);
172 void XGUI_ContextMenuMgr::updateCommandsStatus()
176 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
178 QMenu* aMenu = new QMenu();
179 if (sender() == myWorkshop->objectBrowser()) {
180 updateObjectBrowserMenu();
181 addObjBrowserMenu(aMenu);
182 } else if (sender() == myWorkshop->viewer()) {
184 addViewerMenu(aMenu);
187 if (aMenu && (aMenu->actions().size() > 0)) {
188 // it is possible that some objects should do something before and after the popup menu exec
189 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
191 emit beforeContextMenu();
192 aMenu->exec(theEvent->globalPos());
193 emit afterContextMenu();
198 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
200 foreach(QAction* aAction, myActions)
201 aAction->setEnabled(false);
203 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
204 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
205 int aSelected = aObjects.size();
207 SessionPtr aMgr = ModelAPI_Session::get();
208 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
209 bool hasResult = false;
210 bool hasFeature = false;
211 bool hasParameter = false;
212 bool hasSubFeature = false;
213 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
216 if (aSelected == 1) {
217 ObjectPtr aObject = aObjects.first();
219 if (hasResult && myWorkshop->canBeShaded(aObject)) {
220 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
221 if (aMode != XGUI_Displayer::NoMode) {
222 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
223 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
225 action("WIREFRAME_CMD")->setEnabled(true);
226 action("SHADING_CMD")->setEnabled(true);
230 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
231 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
232 if (aHasSubResults) {
233 action("HIDE_CMD")->setEnabled(true);
234 action("SHOW_CMD")->setEnabled(true);
237 if (aObject->isDisplayed()) {
238 action("HIDE_CMD")->setEnabled(true);
239 } else if (hasResult && (!hasParameter)) {
240 action("SHOW_CMD")->setEnabled(true);
243 if (!(hasParameter || hasFeature))
244 action("SHOW_ONLY_CMD")->setEnabled(true);
246 else if (hasFeature && myWorkshop->canMoveFeature())
247 action("MOVE_CMD")->setEnabled(true);
249 else if (hasFeature || hasParameter)
250 action("CLEAN_HISTORY_CMD")->setEnabled(!hasSubFeature);
252 if( aMgr->activeDocument() == aObject->document() )
254 action("RENAME_CMD")->setEnabled(true);
255 action("DELETE_CMD")->setEnabled(!hasSubFeature);
259 // parameter is commented because the actions are not in the list of result parameter actions
260 if (hasResult /*&& (!hasParameter)*/) {
261 action("SHOW_CMD")->setEnabled(true);
262 action("HIDE_CMD")->setEnabled(true);
263 action("SHOW_ONLY_CMD")->setEnabled(true);
264 action("SHADING_CMD")->setEnabled(true);
265 action("WIREFRAME_CMD")->setEnabled(true);
268 bool allActive = true;
269 foreach( ObjectPtr aObject, aObjects )
270 if( aMgr->activeDocument() != aObject->document() ) {
274 if (!hasSubFeature && allActive ) {
275 if (hasFeature || hasParameter)
276 action("DELETE_CMD")->setEnabled(true);
278 if (!hasSubFeature && allActive && (hasFeature|| hasParameter))
279 action("CLEAN_HISTORY_CMD")->setEnabled(true);
281 action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
284 // Show/Hide command has to be disabled for objects from non active document
285 bool aDeactivate = false;
286 foreach (ObjectPtr aObj, aObjects) {
287 if (!aObj->document()->isActive()) {
288 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
289 aObj->groupName() == ModelAPI_ResultPart::group()) {
296 // If at leas a one objec can not be edited then Show/Hide has to be disabled
297 action("SHOW_CMD")->setEnabled(false);
298 action("HIDE_CMD")->setEnabled(false);
299 action("SHOW_ONLY_CMD")->setEnabled(false);
302 if (myWorkshop->canChangeColor())
303 action("COLOR_CMD")->setEnabled(true);
305 ModuleBase_IModule* aModule = myWorkshop->module();
307 aModule->updateObjectBrowserMenu(myActions);
310 void XGUI_ContextMenuMgr::updateViewerMenu()
312 foreach(QAction* aAction, myActions)
313 aAction->setEnabled(false);
315 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
316 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
317 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
318 if (aPrsList.size() > 0) {
319 bool isVisible = false;
320 bool isShading = false;
321 bool canBeShaded = false;
323 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
324 aObject = aPrs->object();
325 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
326 if (aRes && aRes->isDisplayed()) {
328 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
329 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
335 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
336 if (aMode != XGUI_Displayer::NoMode) {
337 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
338 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
340 action("WIREFRAME_CMD")->setEnabled(true);
341 action("SHADING_CMD")->setEnabled(true);
344 action("SHOW_ONLY_CMD")->setEnabled(true);
345 action("HIDE_CMD")->setEnabled(true);
347 action("SHOW_CMD")->setEnabled(true);
349 if (myWorkshop->displayer()->objectsCount() > 0)
350 action("HIDEALL_CMD")->setEnabled(true);
351 if (myWorkshop->canChangeColor())
352 action("COLOR_CMD")->setEnabled(true);
354 action("DELETE_CMD")->setEnabled(true);
356 // Update selection menu
357 QIntList aModes = aDisplayer->activeSelectionModes();
358 if (aModes.count() <= 1) {
359 action("SELECT_VERTEX_CMD")->setEnabled(true);
360 action("SELECT_EDGE_CMD")->setEnabled(true);
361 action("SELECT_FACE_CMD")->setEnabled(true);
362 action("SELECT_RESULT_CMD")->setEnabled(true);
363 if (aModes.count() == 1) {
364 switch (aModes.first()) {
366 action("SELECT_VERTEX_CMD")->setChecked(true);
369 action("SELECT_EDGE_CMD")->setChecked(true);
372 action("SELECT_FACE_CMD")->setChecked(true);
375 action("SELECT_RESULT_CMD")->setChecked(true);
378 action("SELECT_RESULT_CMD")->setChecked(true);
380 ModuleBase_IModule* aModule = myWorkshop->module();
382 aModule->updateViewerMenu(myActions);
385 void XGUI_ContextMenuMgr::connectObjectBrowser()
387 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
388 SLOT(onContextMenuRequest(QContextMenuEvent*)));
391 void XGUI_ContextMenuMgr::connectViewer()
393 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
394 SLOT(onContextMenuRequest(QContextMenuEvent*)));
398 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
400 QAction* aSeparator = new QAction(this);
401 aSeparator->setSeparator(true);
405 // Result construction menu
406 aList.append(action("SHOW_CMD"));
407 aList.append(action("HIDE_CMD"));
408 aList.append(action("SHOW_ONLY_CMD"));
409 aList.append(action("COLOR_CMD"));
410 aList.append(mySeparator);
411 aList.append(action("RENAME_CMD"));
412 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
413 //-------------------------------------
416 aList.append(action("WIREFRAME_CMD"));
417 aList.append(action("SHADING_CMD"));
418 aList.append(action("COLOR_CMD"));
419 aList.append(mySeparator);
420 aList.append(action("SHOW_CMD"));
421 aList.append(action("HIDE_CMD"));
422 aList.append(action("SHOW_ONLY_CMD"));
423 aList.append(mySeparator);
424 aList.append(action("RENAME_CMD"));
425 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
427 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
429 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
430 //-------------------------------------
433 aList.append(action("DELETE_CMD"));
434 aList.append(action("MOVE_CMD"));
435 aList.append(action("CLEAN_HISTORY_CMD"));
436 aList.append(mySeparator);
437 aList.append(action("RENAME_CMD"));
438 aList.append(mySeparator);
439 aList.append(action("SHOW_RESULTS_CMD"));
440 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
443 aList.append(action("DELETE_CMD"));
444 aList.append(action("CLEAN_HISTORY_CMD"));
445 aList.append(mySeparator);
446 aList.append(action("RENAME_CMD"));
447 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
448 //-------------------------------------
451 void XGUI_ContextMenuMgr::buildViewerMenu()
454 // Result construction menu
455 aList.append(action("HIDE_CMD"));
456 aList.append(action("SHOW_ONLY_CMD"));
457 aList.append(action("COLOR_CMD"));
458 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
460 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
461 //-------------------------------------
464 aList.append(action("WIREFRAME_CMD"));
465 aList.append(action("SHADING_CMD"));
466 aList.append(action("COLOR_CMD"));
467 aList.append(mySeparator);
468 aList.append(action("HIDE_CMD"));
469 aList.append(action("SHOW_ONLY_CMD"));
470 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
472 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
473 //-------------------------------------
478 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
480 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
481 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
482 int aSelected = aObjects.size();
483 QActionsList aActions;
484 if (aSelected == 1) {
485 ObjectPtr aObject = aObjects.first();
486 std::string aName = aObject->groupName();
487 if (myObjBrowserMenus.contains(aName))
488 aActions = myObjBrowserMenus[aName];
489 } else if (aSelected > 1) {
490 aActions.append(action("SHADING_CMD"));
491 aActions.append(action("WIREFRAME_CMD"));
492 aActions.append(mySeparator);
493 aActions.append(action("SHOW_CMD"));
494 aActions.append(action("HIDE_CMD"));
495 aActions.append(action("SHOW_ONLY_CMD"));
496 aActions.append(mySeparator);
497 aActions.append(action("DELETE_CMD"));
498 //aActions.append(action("MOVE_CMD"));
499 aActions.append(action("CLEAN_HISTORY_CMD"));
500 aActions.append(action("COLOR_CMD"));
502 theMenu->addActions(aActions);
504 ModuleBase_IModule* aModule = myWorkshop->module();
506 theMenu->addSeparator();
507 aModule->addObjectBrowserMenu(theMenu);
509 theMenu->addSeparator();
510 theMenu->addActions(myWorkshop->objectBrowser()->actions());
513 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
515 ModuleBase_IModule* aModule = myWorkshop->module();
517 if (aModule->addViewerMenu(theMenu, myActions))
518 theMenu->addSeparator();
520 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
521 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
522 int aSelected = aPrsList.size();
523 QActionsList aActions;
525 // Create selection menu
526 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
528 myWorkshop->module()->activeSelectionModes(aModes);
529 if ((!aOpMgr->hasOperation()) && aModes.isEmpty()) {
530 QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
531 aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
532 aSelMenu->addAction(action("SELECT_EDGE_CMD"));
533 aSelMenu->addAction(action("SELECT_FACE_CMD"));
534 aSelMenu->addAction(action("SELECT_RESULT_CMD"));
535 theMenu->addMenu(aSelMenu);
536 theMenu->addSeparator();
538 if (aSelected == 1) {
539 ObjectPtr aObject = aPrsList.first()->object();
540 if (aObject.get() != NULL) {
541 std::string aName = aObject->groupName();
542 if (myViewerMenu.contains(aName))
543 aActions = myViewerMenu[aName];
545 aActions.append(action("COLOR_CMD"));
546 } else if (aSelected > 1) {
547 aActions.append(action("HIDE_CMD"));
548 aActions.append(action("COLOR_CMD"));
550 // hide all is shown always even if selection in the viewer is empty
551 aActions.append(action("HIDEALL_CMD"));
552 theMenu->addActions(aActions);
555 theMenu->addSeparator();
556 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
557 if (aMDI->actions().size() > 0) {
558 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
559 aSubMenu->addActions(aMDI->actions());
564 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
568 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
569 aLast = myObjBrowserMenus.end();
570 for (; anIt != aLast; anIt++) {
571 QString aGroupName(anIt.key().c_str());
572 if (aGroups.contains(aGroupName))
574 QActionsList anActions = anIt.value();
575 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
577 for (; anAIt != anALast && !aFound; anAIt++)
578 aFound = (*anAIt)->data().toString() == theName;
580 aGroups.append(aGroupName);
585 void XGUI_ContextMenuMgr::onRename()
587 QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
588 if (!myWorkshop->abortAllOperations())
590 // restore selection in case if dialog box was shown
591 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
592 myWorkshop->objectBrowser()->onEditItem();