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>
36 #include <QContextMenuEvent>
39 #include <QMainWindow>
42 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
44 myWorkshop(theParent),
49 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
53 void XGUI_ContextMenuMgr::createActions()
56 QMainWindow* aDesktop = myWorkshop->salomeConnector()->desktop();
58 QMainWindow* aDesktop = myWorkshop->mainWindow();
61 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
62 aDesktop->addAction(aAction);
64 addAction("DELETE_CMD", aAction);
65 aAction->setShortcutContext(Qt::ApplicationShortcut);
67 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
68 addAction("RENAME_CMD", aAction);
69 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
71 aAction = new QAction(QIcon(":pictures/move.png"), XGUI_Workshop::MOVE_TO_END_COMMAND, this);
72 addAction("MOVE_CMD", aAction);
74 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
75 addAction("COLOR_CMD", aAction);
77 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
78 addAction("SHOW_CMD", aAction);
80 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
81 addAction("SHOW_ONLY_CMD", aAction);
83 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
84 addAction("HIDE_CMD", aAction);
86 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
87 addAction("HIDEALL_CMD", aAction);
89 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
90 addAction("SHADING_CMD", aAction);
92 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
93 addAction("WIREFRAME_CMD", aAction);
95 mySeparator = new QAction(this);
96 mySeparator->setSeparator(true);
99 buildObjBrowserMenu();
103 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
105 if (myActions.contains(theId))
106 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
107 theAction->setData(theId);
108 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
109 myActions[theId] = theAction;
112 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
114 if (myActions.contains(theId))
115 return myActions[theId];
119 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
121 foreach(QAction* eachAction, myActions) {
122 if (eachAction->text() == theName) {
129 QStringList XGUI_ContextMenuMgr::actionIds() const
131 return myActions.keys();
134 void XGUI_ContextMenuMgr::onAction(bool isChecked)
136 QAction* aAction = static_cast<QAction*>(sender());
137 emit actionTriggered(aAction->data().toString(), isChecked);
140 void XGUI_ContextMenuMgr::updateCommandsStatus()
144 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
146 QMenu* aMenu = new QMenu();
147 if (sender() == myWorkshop->objectBrowser()) {
148 updateObjectBrowserMenu();
149 addObjBrowserMenu(aMenu);
150 } else if (sender() == myWorkshop->viewer()) {
152 addViewerMenu(aMenu);
155 if (aMenu && (aMenu->actions().size() > 0)) {
156 // it is possible that some objects should do something before and after the popup menu exec
157 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
159 emit beforeContextMenu();
160 aMenu->exec(theEvent->globalPos());
161 emit afterContextMenu();
166 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
168 foreach(QAction* aAction, myActions)
169 aAction->setEnabled(false);
171 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
172 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
173 int aSelected = aObjects.size();
175 SessionPtr aMgr = ModelAPI_Session::get();
176 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
177 bool hasResult = false;
178 bool hasFeature = false;
179 bool hasParameter = false;
180 bool hasSubFeature = false;
181 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
184 if (aSelected == 1) {
185 ObjectPtr aObject = aObjects.first();
187 if (hasResult && myWorkshop->canBeShaded(aObject)) {
188 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
189 if (aMode != XGUI_Displayer::NoMode) {
190 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
191 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
193 action("WIREFRAME_CMD")->setEnabled(true);
194 action("SHADING_CMD")->setEnabled(true);
198 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
199 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
200 if (aHasSubResults) {
201 action("HIDE_CMD")->setEnabled(true);
202 action("SHOW_CMD")->setEnabled(true);
205 if (aObject->isDisplayed()) {
206 action("HIDE_CMD")->setEnabled(true);
207 } else if (hasResult && (!hasParameter)) {
208 action("SHOW_CMD")->setEnabled(true);
211 if (!(hasParameter || hasFeature))
212 action("SHOW_ONLY_CMD")->setEnabled(true);
214 else if (hasFeature && myWorkshop->canMoveFeature())
215 action("MOVE_CMD")->setEnabled(true);
217 if( aMgr->activeDocument() == aObject->document() )
219 action("RENAME_CMD")->setEnabled(true);
220 action("DELETE_CMD")->setEnabled(!hasSubFeature);
224 // parameter is commented because the actions are not in the list of result parameter actions
225 if (hasResult /*&& (!hasParameter)*/) {
226 action("SHOW_CMD")->setEnabled(true);
227 action("HIDE_CMD")->setEnabled(true);
228 action("SHOW_ONLY_CMD")->setEnabled(true);
229 action("SHADING_CMD")->setEnabled(true);
230 action("WIREFRAME_CMD")->setEnabled(true);
233 bool allActive = true;
234 foreach( ObjectPtr aObject, aObjects )
235 if( aMgr->activeDocument() != aObject->document() ) {
239 if (!hasSubFeature && allActive ) {
240 if (hasFeature || hasParameter)
241 action("DELETE_CMD")->setEnabled(true);
245 // Show/Hide command has to be disabled for objects from non active document
246 bool aDeactivate = false;
247 foreach (ObjectPtr aObj, aObjects) {
248 if (!aObj->document()->isActive()) {
249 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
250 aObj->groupName() == ModelAPI_ResultPart::group()) {
257 // If at leas a one objec can not be edited then Show/Hide has to be disabled
258 action("SHOW_CMD")->setEnabled(false);
259 action("HIDE_CMD")->setEnabled(false);
260 action("SHOW_ONLY_CMD")->setEnabled(false);
263 if (myWorkshop->canChangeColor())
264 action("COLOR_CMD")->setEnabled(true);
266 ModuleBase_IModule* aModule = myWorkshop->module();
268 aModule->updateObjectBrowserMenu(myActions);
271 void XGUI_ContextMenuMgr::updateViewerMenu()
273 foreach(QAction* aAction, myActions)
274 aAction->setEnabled(false);
276 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
277 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
278 if (aPrsList.size() > 0) {
279 bool isVisible = false;
280 bool isShading = false;
281 bool canBeShaded = false;
283 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
284 aObject = aPrs.object();
285 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
286 if (aRes && aRes->isDisplayed()) {
288 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
289 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
295 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
296 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
297 if (aMode != XGUI_Displayer::NoMode) {
298 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
299 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
301 action("WIREFRAME_CMD")->setEnabled(true);
302 action("SHADING_CMD")->setEnabled(true);
305 action("SHOW_ONLY_CMD")->setEnabled(true);
306 action("HIDE_CMD")->setEnabled(true);
308 action("SHOW_CMD")->setEnabled(true);
310 if (myWorkshop->displayer()->objectsCount() > 0)
311 action("HIDEALL_CMD")->setEnabled(true);
312 if (myWorkshop->canChangeColor())
313 action("COLOR_CMD")->setEnabled(true);
315 action("DELETE_CMD")->setEnabled(true);
317 ModuleBase_IModule* aModule = myWorkshop->module();
319 aModule->updateViewerMenu(myActions);
322 void XGUI_ContextMenuMgr::connectObjectBrowser()
324 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
325 SLOT(onContextMenuRequest(QContextMenuEvent*)));
328 void XGUI_ContextMenuMgr::connectViewer()
330 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
331 SLOT(onContextMenuRequest(QContextMenuEvent*)));
335 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
337 QAction* aSeparator = new QAction(this);
338 aSeparator->setSeparator(true);
342 // Result construction menu
343 aList.append(action("SHOW_CMD"));
344 aList.append(action("HIDE_CMD"));
345 aList.append(action("SHOW_ONLY_CMD"));
346 aList.append(action("COLOR_CMD"));
347 aList.append(mySeparator);
348 aList.append(action("RENAME_CMD"));
349 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
350 //-------------------------------------
353 aList.append(action("WIREFRAME_CMD"));
354 aList.append(action("SHADING_CMD"));
355 aList.append(action("COLOR_CMD"));
356 aList.append(mySeparator);
357 aList.append(action("SHOW_CMD"));
358 aList.append(action("HIDE_CMD"));
359 aList.append(action("SHOW_ONLY_CMD"));
360 aList.append(mySeparator);
361 aList.append(action("RENAME_CMD"));
362 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
364 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
366 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
367 //-------------------------------------
370 aList.append(action("DELETE_CMD"));
371 aList.append(action("MOVE_CMD"));
372 aList.append(mySeparator);
373 aList.append(action("RENAME_CMD"));
374 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
377 aList.append(action("DELETE_CMD"));
378 aList.append(mySeparator);
379 aList.append(action("RENAME_CMD"));
380 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
381 //-------------------------------------
384 void XGUI_ContextMenuMgr::buildViewerMenu()
387 // Result construction menu
388 aList.append(action("HIDE_CMD"));
389 aList.append(action("SHOW_ONLY_CMD"));
390 aList.append(action("COLOR_CMD"));
391 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
393 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
394 //-------------------------------------
397 aList.append(action("WIREFRAME_CMD"));
398 aList.append(action("SHADING_CMD"));
399 aList.append(action("COLOR_CMD"));
400 aList.append(mySeparator);
401 aList.append(action("HIDE_CMD"));
402 aList.append(action("SHOW_ONLY_CMD"));
403 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
405 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
406 //-------------------------------------
411 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
413 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
414 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
415 int aSelected = aObjects.size();
416 QActionsList aActions;
417 if (aSelected == 1) {
418 ObjectPtr aObject = aObjects.first();
419 std::string aName = aObject->groupName();
420 if (myObjBrowserMenus.contains(aName))
421 aActions = myObjBrowserMenus[aName];
422 } else if (aSelected > 1) {
423 aActions.append(action("SHADING_CMD"));
424 aActions.append(action("WIREFRAME_CMD"));
425 aActions.append(mySeparator);
426 aActions.append(action("SHOW_CMD"));
427 aActions.append(action("HIDE_CMD"));
428 aActions.append(action("SHOW_ONLY_CMD"));
429 aActions.append(mySeparator);
430 aActions.append(action("DELETE_CMD"));
431 //aActions.append(action("MOVE_CMD"));
432 aActions.append(action("COLOR_CMD"));
434 theMenu->addActions(aActions);
436 ModuleBase_IModule* aModule = myWorkshop->module();
438 theMenu->addSeparator();
439 aModule->addObjectBrowserMenu(theMenu);
441 theMenu->addSeparator();
442 theMenu->addActions(myWorkshop->objectBrowser()->actions());
445 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
447 ModuleBase_IModule* aModule = myWorkshop->module();
449 if (aModule->addViewerMenu(theMenu, myActions))
450 theMenu->addSeparator();
452 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
453 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
454 int aSelected = aPrsList.size();
455 QActionsList aActions;
456 if (aSelected == 1) {
457 ObjectPtr aObject = aPrsList.first().object();
458 if (aObject.get() != NULL) {
459 std::string aName = aObject->groupName();
460 if (myViewerMenu.contains(aName))
461 aActions = myViewerMenu[aName];
463 aActions.append(action("COLOR_CMD"));
464 } else if (aSelected > 1) {
465 aActions.append(action("HIDE_CMD"));
466 aActions.append(action("COLOR_CMD"));
468 // hide all is shown always even if selection in the viewer is empty
469 aActions.append(action("HIDEALL_CMD"));
470 theMenu->addActions(aActions);
473 theMenu->addSeparator();
474 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
475 if (aMDI->actions().size() > 0) {
476 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
477 aSubMenu->addActions(aMDI->actions());
482 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
486 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
487 aLast = myObjBrowserMenus.end();
488 for (; anIt != aLast; anIt++) {
489 QString aGroupName(anIt.key().c_str());
490 if (aGroups.contains(aGroupName))
492 QActionsList anActions = anIt.value();
493 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
495 for (; anAIt != anALast && !aFound; anAIt++)
496 aFound = (*anAIt)->data().toString() == theName;
498 aGroups.append(aGroupName);
503 void XGUI_ContextMenuMgr::onRename()
505 QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
506 if (!myWorkshop->abortAllOperations())
508 // restore selection in case if dialog box was shown
509 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
510 myWorkshop->objectBrowser()->onEditItem();