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"
15 #include <AppElements_MainWindow.h>
17 //#include "PartSetPlugin_Part.h"
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_AttributeDocRef.h>
21 #include <ModelAPI_Object.h>
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_ResultGroup.h>
24 #include <ModelAPI_ResultParameter.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_Tools.h>
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_Tools.h>
31 #include <ModuleBase_OperationAction.h>
34 #include <QContextMenuEvent>
39 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
41 myWorkshop(theParent),
46 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
50 void XGUI_ContextMenuMgr::createActions()
52 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
53 QMainWindow* aDesktop = myWorkshop->mainWindow();
55 aDesktop = myWorkshop->salomeConnector()->desktop();
56 aDesktop->addAction(aAction);
58 addAction("DELETE_CMD", aAction);
59 aAction->setShortcut(Qt::Key_Delete);
60 aAction->setShortcutContext(Qt::ApplicationShortcut);
62 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
63 addAction("RENAME_CMD", aAction);
64 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
66 aAction = new QAction(QIcon(":pictures/move.png"), tr("Move to the end"), this);
67 addAction("MOVE_CMD", aAction);
69 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
70 addAction("COLOR_CMD", aAction);
72 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
73 addAction("SHOW_CMD", aAction);
75 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
76 addAction("SHOW_ONLY_CMD", aAction);
78 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
79 addAction("HIDE_CMD", aAction);
81 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
82 addAction("HIDEALL_CMD", aAction);
84 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
85 addAction("SHADING_CMD", aAction);
87 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
88 addAction("WIREFRAME_CMD", aAction);
90 mySeparator = new QAction(this);
91 mySeparator->setSeparator(true);
94 buildObjBrowserMenu();
98 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
100 if (myActions.contains(theId))
101 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
102 theAction->setData(theId);
103 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
104 myActions[theId] = theAction;
107 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
109 if (myActions.contains(theId))
110 return myActions[theId];
114 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
116 foreach(QAction* eachAction, myActions) {
117 if (eachAction->text() == theName) {
124 QStringList XGUI_ContextMenuMgr::actionIds() const
126 return myActions.keys();
129 void XGUI_ContextMenuMgr::onAction(bool isChecked)
131 QAction* aAction = static_cast<QAction*>(sender());
132 emit actionTriggered(aAction->data().toString(), isChecked);
135 void XGUI_ContextMenuMgr::updateCommandsStatus()
139 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
141 QMenu* aMenu = new QMenu();
142 if (sender() == myWorkshop->objectBrowser()) {
143 updateObjectBrowserMenu();
144 addObjBrowserMenu(aMenu);
145 } else if (sender() == myWorkshop->viewer()) {
147 addViewerMenu(aMenu);
150 if (aMenu && (aMenu->actions().size() > 0)) {
151 // it is possible that some objects should do something before and after the popup menu exec
152 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
154 emit beforeContextMenu();
155 aMenu->exec(theEvent->globalPos());
156 emit afterContextMenu();
161 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
163 foreach(QAction* aAction, myActions)
164 aAction->setEnabled(false);
166 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
167 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
168 int aSelected = aObjects.size();
170 SessionPtr aMgr = ModelAPI_Session::get();
171 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
172 bool hasResult = false;
173 bool hasFeature = false;
174 bool hasParameter = false;
175 bool hasSubFeature = false;
176 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
179 if (aSelected == 1) {
180 ObjectPtr aObject = aObjects.first();
182 if (hasResult && myWorkshop->canBeShaded(aObject)) {
183 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
184 if (aMode != XGUI_Displayer::NoMode) {
185 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
186 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
188 action("WIREFRAME_CMD")->setEnabled(true);
189 action("SHADING_CMD")->setEnabled(true);
193 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
194 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
195 if (aHasSubResults) {
196 action("HIDE_CMD")->setEnabled(true);
197 action("SHOW_CMD")->setEnabled(true);
200 if (aObject->isDisplayed()) {
201 action("HIDE_CMD")->setEnabled(true);
202 } else if (hasResult && (!hasParameter)) {
203 action("SHOW_CMD")->setEnabled(true);
206 if (!(hasParameter || hasFeature))
207 action("SHOW_ONLY_CMD")->setEnabled(true);
209 else if (hasFeature && myWorkshop->canMoveFeature())
210 action("MOVE_CMD")->setEnabled(true);
212 if( aMgr->activeDocument() == aObject->document() )
214 action("RENAME_CMD")->setEnabled(true);
215 action("DELETE_CMD")->setEnabled(true);
219 // parameter is commented because the actions are not in the list of result parameter actions
220 if (hasResult /*&& (!hasParameter)*/) {
221 action("SHOW_CMD")->setEnabled(true);
222 action("HIDE_CMD")->setEnabled(true);
223 action("SHOW_ONLY_CMD")->setEnabled(true);
224 action("SHADING_CMD")->setEnabled(true);
225 action("WIREFRAME_CMD")->setEnabled(true);
228 bool allActive = true;
229 foreach( ObjectPtr aObject, aObjects )
230 if( aMgr->activeDocument() != aObject->document() ) {
234 if (!hasSubFeature && allActive ) {
235 if (hasFeature || hasParameter)
236 action("DELETE_CMD")->setEnabled(true);
240 // Show/Hide command has to be disabled for objects from non active document
241 bool aDeactivate = false;
242 foreach (ObjectPtr aObj, aObjects) {
243 if (!aObj->document()->isActive()) {
244 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
245 aObj->groupName() == ModelAPI_ResultPart::group()) {
252 // If at leas a one objec can not be edited then Show/Hide has to be disabled
253 action("SHOW_CMD")->setEnabled(false);
254 action("HIDE_CMD")->setEnabled(false);
255 action("SHOW_ONLY_CMD")->setEnabled(false);
258 if (myWorkshop->canChangeColor())
259 action("COLOR_CMD")->setEnabled(true);
261 ModuleBase_IModule* aModule = myWorkshop->module();
263 aModule->updateObjectBrowserMenu(myActions);
266 void XGUI_ContextMenuMgr::updateViewerMenu()
268 foreach(QAction* aAction, myActions)
269 aAction->setEnabled(false);
271 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
272 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
273 if (aPrsList.size() > 0) {
274 bool isVisible = false;
275 bool isShading = false;
276 bool canBeShaded = false;
278 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
279 aObject = aPrs.object();
280 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
281 if (aRes && aRes->isDisplayed()) {
283 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
284 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
290 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
291 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
292 if (aMode != XGUI_Displayer::NoMode) {
293 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
294 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
296 action("WIREFRAME_CMD")->setEnabled(true);
297 action("SHADING_CMD")->setEnabled(true);
300 action("SHOW_ONLY_CMD")->setEnabled(true);
301 action("HIDE_CMD")->setEnabled(true);
303 action("SHOW_CMD")->setEnabled(true);
305 if (myWorkshop->displayer()->objectsCount() > 0)
306 action("HIDEALL_CMD")->setEnabled(true);
307 if (myWorkshop->canChangeColor())
308 action("COLOR_CMD")->setEnabled(true);
310 action("DELETE_CMD")->setEnabled(true);
312 ModuleBase_IModule* aModule = myWorkshop->module();
314 aModule->updateViewerMenu(myActions);
317 void XGUI_ContextMenuMgr::connectObjectBrowser()
319 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
320 SLOT(onContextMenuRequest(QContextMenuEvent*)));
323 void XGUI_ContextMenuMgr::connectViewer()
325 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
326 SLOT(onContextMenuRequest(QContextMenuEvent*)));
330 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
332 QAction* aSeparator = new QAction(this);
333 aSeparator->setSeparator(true);
337 // Result construction menu
338 aList.append(action("SHOW_CMD"));
339 aList.append(action("HIDE_CMD"));
340 aList.append(action("SHOW_ONLY_CMD"));
341 aList.append(action("COLOR_CMD"));
342 aList.append(mySeparator);
343 aList.append(action("RENAME_CMD"));
344 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
345 //-------------------------------------
348 aList.append(action("WIREFRAME_CMD"));
349 aList.append(action("SHADING_CMD"));
350 aList.append(action("COLOR_CMD"));
351 aList.append(mySeparator);
352 aList.append(action("SHOW_CMD"));
353 aList.append(action("HIDE_CMD"));
354 aList.append(action("SHOW_ONLY_CMD"));
355 aList.append(mySeparator);
356 aList.append(action("RENAME_CMD"));
357 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
359 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
361 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
362 //-------------------------------------
365 aList.append(action("DELETE_CMD"));
366 aList.append(action("MOVE_CMD"));
367 aList.append(mySeparator);
368 aList.append(action("RENAME_CMD"));
369 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
372 aList.append(action("DELETE_CMD"));
373 aList.append(mySeparator);
374 aList.append(action("RENAME_CMD"));
375 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
376 //-------------------------------------
379 void XGUI_ContextMenuMgr::buildViewerMenu()
382 // Result construction menu
383 aList.append(action("HIDE_CMD"));
384 aList.append(action("SHOW_ONLY_CMD"));
385 aList.append(action("HIDEALL_CMD"));
386 aList.append(action("COLOR_CMD"));
387 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
389 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
390 //-------------------------------------
393 aList.append(action("WIREFRAME_CMD"));
394 aList.append(action("SHADING_CMD"));
395 aList.append(action("COLOR_CMD"));
396 aList.append(mySeparator);
397 aList.append(action("HIDE_CMD"));
398 aList.append(action("SHOW_ONLY_CMD"));
399 aList.append(action("HIDEALL_CMD"));
400 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
402 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
403 //-------------------------------------
408 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
410 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
411 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
412 int aSelected = aObjects.size();
413 QActionsList aActions;
414 if (aSelected == 1) {
415 ObjectPtr aObject = aObjects.first();
416 std::string aName = aObject->groupName();
417 if (myObjBrowserMenus.contains(aName))
418 aActions = myObjBrowserMenus[aName];
419 } else if (aSelected > 1) {
420 aActions.append(action("SHADING_CMD"));
421 aActions.append(action("WIREFRAME_CMD"));
422 aActions.append(mySeparator);
423 aActions.append(action("SHOW_CMD"));
424 aActions.append(action("HIDE_CMD"));
425 aActions.append(action("SHOW_ONLY_CMD"));
426 aActions.append(mySeparator);
427 aActions.append(action("DELETE_CMD"));
428 //aActions.append(action("MOVE_CMD"));
429 aActions.append(action("COLOR_CMD"));
431 theMenu->addActions(aActions);
433 ModuleBase_IModule* aModule = myWorkshop->module();
435 theMenu->addSeparator();
436 aModule->addObjectBrowserMenu(theMenu);
438 theMenu->addSeparator();
439 theMenu->addActions(myWorkshop->objectBrowser()->actions());
442 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
444 ModuleBase_IModule* aModule = myWorkshop->module();
446 if (aModule->addViewerMenu(theMenu, myActions))
447 theMenu->addSeparator();
449 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
450 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
451 int aSelected = aPrsList.size();
452 QActionsList aActions;
453 if (aSelected == 1) {
454 ObjectPtr aObject = aPrsList.first().object();
455 std::string aName = aObject->groupName();
456 if (myViewerMenu.contains(aName))
457 aActions = myViewerMenu[aName];
458 aActions.append(action("COLOR_CMD"));
459 } else if (aSelected > 1) {
460 aActions.append(action("HIDE_CMD"));
461 aActions.append(action("COLOR_CMD"));
463 theMenu->addActions(aActions);
465 if (!myWorkshop->isSalomeMode()) {
466 theMenu->addSeparator();
467 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
468 if (aMDI->actions().size() > 0) {
469 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
470 aSubMenu->addActions(aMDI->actions());
475 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
479 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
480 aLast = myObjBrowserMenus.end();
481 for (; anIt != aLast; anIt++) {
482 QString aGroupName(anIt.key().c_str());
483 if (aGroups.contains(aGroupName))
485 QActionsList anActions = anIt.value();
486 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
488 for (; anAIt != anALast && !aFound; anAIt++)
489 aFound = (*anAIt)->data().toString() == theName;
491 aGroups.append(aGroupName);
496 void XGUI_ContextMenuMgr::onRename()
498 ModuleBase_OperationAction* anAction = new ModuleBase_OperationAction("Rename", this);
499 XGUI_OperationMgr* anOpMgr = myWorkshop->operationMgr();
500 anOpMgr->startOperation(anAction);
501 myWorkshop->objectBrowser()->onEditItem();
502 anOpMgr->commitOperation();