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->setShortcut(Qt::Key_Delete);
66 aAction->setShortcutContext(Qt::ApplicationShortcut);
68 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
69 addAction("RENAME_CMD", aAction);
70 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
72 aAction = new QAction(QIcon(":pictures/move.png"), tr("Move to the end"), this);
73 addAction("MOVE_CMD", aAction);
75 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
76 addAction("COLOR_CMD", aAction);
78 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
79 addAction("SHOW_CMD", aAction);
81 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
82 addAction("SHOW_ONLY_CMD", aAction);
84 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
85 addAction("HIDE_CMD", aAction);
87 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
88 addAction("HIDEALL_CMD", aAction);
90 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
91 addAction("SHADING_CMD", aAction);
93 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
94 addAction("WIREFRAME_CMD", aAction);
96 mySeparator = new QAction(this);
97 mySeparator->setSeparator(true);
100 buildObjBrowserMenu();
104 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
106 if (myActions.contains(theId))
107 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
108 theAction->setData(theId);
109 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
110 myActions[theId] = theAction;
113 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
115 if (myActions.contains(theId))
116 return myActions[theId];
120 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
122 foreach(QAction* eachAction, myActions) {
123 if (eachAction->text() == theName) {
130 QStringList XGUI_ContextMenuMgr::actionIds() const
132 return myActions.keys();
135 void XGUI_ContextMenuMgr::onAction(bool isChecked)
137 QAction* aAction = static_cast<QAction*>(sender());
138 emit actionTriggered(aAction->data().toString(), isChecked);
141 void XGUI_ContextMenuMgr::updateCommandsStatus()
145 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
147 QMenu* aMenu = new QMenu();
148 if (sender() == myWorkshop->objectBrowser()) {
149 updateObjectBrowserMenu();
150 addObjBrowserMenu(aMenu);
151 } else if (sender() == myWorkshop->viewer()) {
153 addViewerMenu(aMenu);
156 if (aMenu && (aMenu->actions().size() > 0)) {
157 // it is possible that some objects should do something before and after the popup menu exec
158 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
160 emit beforeContextMenu();
161 aMenu->exec(theEvent->globalPos());
162 emit afterContextMenu();
167 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
169 foreach(QAction* aAction, myActions)
170 aAction->setEnabled(false);
172 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
173 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
174 int aSelected = aObjects.size();
176 SessionPtr aMgr = ModelAPI_Session::get();
177 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
178 bool hasResult = false;
179 bool hasFeature = false;
180 bool hasParameter = false;
181 bool hasSubFeature = false;
182 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
185 if (aSelected == 1) {
186 ObjectPtr aObject = aObjects.first();
188 if (hasResult && myWorkshop->canBeShaded(aObject)) {
189 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
190 if (aMode != XGUI_Displayer::NoMode) {
191 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
192 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
194 action("WIREFRAME_CMD")->setEnabled(true);
195 action("SHADING_CMD")->setEnabled(true);
199 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
200 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
201 if (aHasSubResults) {
202 action("HIDE_CMD")->setEnabled(true);
203 action("SHOW_CMD")->setEnabled(true);
206 if (aObject->isDisplayed()) {
207 action("HIDE_CMD")->setEnabled(true);
208 } else if (hasResult && (!hasParameter)) {
209 action("SHOW_CMD")->setEnabled(true);
212 if (!(hasParameter || hasFeature))
213 action("SHOW_ONLY_CMD")->setEnabled(true);
215 else if (hasFeature && myWorkshop->canMoveFeature())
216 action("MOVE_CMD")->setEnabled(true);
218 if( aMgr->activeDocument() == aObject->document() )
220 action("RENAME_CMD")->setEnabled(true);
221 action("DELETE_CMD")->setEnabled(!hasSubFeature);
225 // parameter is commented because the actions are not in the list of result parameter actions
226 if (hasResult /*&& (!hasParameter)*/) {
227 action("SHOW_CMD")->setEnabled(true);
228 action("HIDE_CMD")->setEnabled(true);
229 action("SHOW_ONLY_CMD")->setEnabled(true);
230 action("SHADING_CMD")->setEnabled(true);
231 action("WIREFRAME_CMD")->setEnabled(true);
234 bool allActive = true;
235 foreach( ObjectPtr aObject, aObjects )
236 if( aMgr->activeDocument() != aObject->document() ) {
240 if (!hasSubFeature && allActive ) {
241 if (hasFeature || hasParameter)
242 action("DELETE_CMD")->setEnabled(true);
246 // Show/Hide command has to be disabled for objects from non active document
247 bool aDeactivate = false;
248 foreach (ObjectPtr aObj, aObjects) {
249 if (!aObj->document()->isActive()) {
250 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
251 aObj->groupName() == ModelAPI_ResultPart::group()) {
258 // If at leas a one objec can not be edited then Show/Hide has to be disabled
259 action("SHOW_CMD")->setEnabled(false);
260 action("HIDE_CMD")->setEnabled(false);
261 action("SHOW_ONLY_CMD")->setEnabled(false);
264 if (myWorkshop->canChangeColor())
265 action("COLOR_CMD")->setEnabled(true);
267 ModuleBase_IModule* aModule = myWorkshop->module();
269 aModule->updateObjectBrowserMenu(myActions);
272 void XGUI_ContextMenuMgr::updateViewerMenu()
274 foreach(QAction* aAction, myActions)
275 aAction->setEnabled(false);
277 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
278 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
279 if (aPrsList.size() > 0) {
280 bool isVisible = false;
281 bool isShading = false;
282 bool canBeShaded = false;
284 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
285 aObject = aPrs.object();
286 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
287 if (aRes && aRes->isDisplayed()) {
289 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
290 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
296 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
297 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
298 if (aMode != XGUI_Displayer::NoMode) {
299 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
300 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
302 action("WIREFRAME_CMD")->setEnabled(true);
303 action("SHADING_CMD")->setEnabled(true);
306 action("SHOW_ONLY_CMD")->setEnabled(true);
307 action("HIDE_CMD")->setEnabled(true);
309 action("SHOW_CMD")->setEnabled(true);
311 if (myWorkshop->displayer()->objectsCount() > 0)
312 action("HIDEALL_CMD")->setEnabled(true);
313 if (myWorkshop->canChangeColor())
314 action("COLOR_CMD")->setEnabled(true);
316 action("DELETE_CMD")->setEnabled(true);
318 ModuleBase_IModule* aModule = myWorkshop->module();
320 aModule->updateViewerMenu(myActions);
323 void XGUI_ContextMenuMgr::connectObjectBrowser()
325 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
326 SLOT(onContextMenuRequest(QContextMenuEvent*)));
329 void XGUI_ContextMenuMgr::connectViewer()
331 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
332 SLOT(onContextMenuRequest(QContextMenuEvent*)));
336 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
338 QAction* aSeparator = new QAction(this);
339 aSeparator->setSeparator(true);
343 // Result construction menu
344 aList.append(action("SHOW_CMD"));
345 aList.append(action("HIDE_CMD"));
346 aList.append(action("SHOW_ONLY_CMD"));
347 aList.append(action("COLOR_CMD"));
348 aList.append(mySeparator);
349 aList.append(action("RENAME_CMD"));
350 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
351 //-------------------------------------
354 aList.append(action("WIREFRAME_CMD"));
355 aList.append(action("SHADING_CMD"));
356 aList.append(action("COLOR_CMD"));
357 aList.append(mySeparator);
358 aList.append(action("SHOW_CMD"));
359 aList.append(action("HIDE_CMD"));
360 aList.append(action("SHOW_ONLY_CMD"));
361 aList.append(mySeparator);
362 aList.append(action("RENAME_CMD"));
363 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
365 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
367 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
368 //-------------------------------------
371 aList.append(action("DELETE_CMD"));
372 aList.append(action("MOVE_CMD"));
373 aList.append(mySeparator);
374 aList.append(action("RENAME_CMD"));
375 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
378 aList.append(action("DELETE_CMD"));
379 aList.append(mySeparator);
380 aList.append(action("RENAME_CMD"));
381 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
382 //-------------------------------------
385 void XGUI_ContextMenuMgr::buildViewerMenu()
388 // Result construction menu
389 aList.append(action("HIDE_CMD"));
390 aList.append(action("SHOW_ONLY_CMD"));
391 aList.append(action("HIDEALL_CMD"));
392 aList.append(action("COLOR_CMD"));
393 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
395 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
396 //-------------------------------------
399 aList.append(action("WIREFRAME_CMD"));
400 aList.append(action("SHADING_CMD"));
401 aList.append(action("COLOR_CMD"));
402 aList.append(mySeparator);
403 aList.append(action("HIDE_CMD"));
404 aList.append(action("SHOW_ONLY_CMD"));
405 aList.append(action("HIDEALL_CMD"));
406 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
408 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
409 //-------------------------------------
414 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
416 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
417 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
418 int aSelected = aObjects.size();
419 QActionsList aActions;
420 if (aSelected == 1) {
421 ObjectPtr aObject = aObjects.first();
422 std::string aName = aObject->groupName();
423 if (myObjBrowserMenus.contains(aName))
424 aActions = myObjBrowserMenus[aName];
425 } else if (aSelected > 1) {
426 aActions.append(action("SHADING_CMD"));
427 aActions.append(action("WIREFRAME_CMD"));
428 aActions.append(mySeparator);
429 aActions.append(action("SHOW_CMD"));
430 aActions.append(action("HIDE_CMD"));
431 aActions.append(action("SHOW_ONLY_CMD"));
432 aActions.append(mySeparator);
433 aActions.append(action("DELETE_CMD"));
434 //aActions.append(action("MOVE_CMD"));
435 aActions.append(action("COLOR_CMD"));
437 theMenu->addActions(aActions);
439 ModuleBase_IModule* aModule = myWorkshop->module();
441 theMenu->addSeparator();
442 aModule->addObjectBrowserMenu(theMenu);
444 theMenu->addSeparator();
445 theMenu->addActions(myWorkshop->objectBrowser()->actions());
448 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
450 ModuleBase_IModule* aModule = myWorkshop->module();
452 if (aModule->addViewerMenu(theMenu, myActions))
453 theMenu->addSeparator();
455 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
456 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
457 int aSelected = aPrsList.size();
458 QActionsList aActions;
459 if (aSelected == 1) {
460 ObjectPtr aObject = aPrsList.first().object();
461 std::string aName = aObject->groupName();
462 if (myViewerMenu.contains(aName))
463 aActions = myViewerMenu[aName];
464 aActions.append(action("COLOR_CMD"));
465 } else if (aSelected > 1) {
466 aActions.append(action("HIDE_CMD"));
467 aActions.append(action("COLOR_CMD"));
469 theMenu->addActions(aActions);
472 theMenu->addSeparator();
473 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
474 if (aMDI->actions().size() > 0) {
475 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
476 aSubMenu->addActions(aMDI->actions());
481 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
485 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
486 aLast = myObjBrowserMenus.end();
487 for (; anIt != aLast; anIt++) {
488 QString aGroupName(anIt.key().c_str());
489 if (aGroups.contains(aGroupName))
491 QActionsList anActions = anIt.value();
492 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
494 for (; anAIt != anALast && !aFound; anAIt++)
495 aFound = (*anAIt)->data().toString() == theName;
497 aGroups.append(aGroupName);
502 void XGUI_ContextMenuMgr::onRename()
504 QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
505 if (!myWorkshop->abortAllOperations())
507 // restore selection in case if dialog box was shown
508 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
509 myWorkshop->objectBrowser()->onEditItem();