]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ContextMenuMgr.cpp
Salome HOME
Improve multi-selector control to provide "Delete" key processing.
[modules/shaper.git] / src / XGUI / XGUI_ContextMenuMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
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"
14
15 #ifndef HAVE_SALOME
16 #include <AppElements_MainWindow.h>
17 #endif
18
19 //#include "PartSetPlugin_Part.h"
20
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>
30
31 #include <ModuleBase_IModule.h>
32 #include <ModuleBase_Tools.h>
33 #include <ModuleBase_OperationAction.h>
34
35 #include <QAction>
36 #include <QContextMenuEvent>
37 #include <QMenu>
38 #include <QMdiArea>
39 #include <QMainWindow>
40
41
42 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
43     : QObject(theParent),
44       myWorkshop(theParent),
45       mySeparator(0)
46 {
47 }
48
49 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
50 {
51 }
52
53 void XGUI_ContextMenuMgr::createActions()
54 {
55 #ifdef HAVE_SALOME
56   QMainWindow* aDesktop = myWorkshop->salomeConnector()->desktop();
57 #else
58   QMainWindow* aDesktop = myWorkshop->mainWindow();
59 #endif
60
61   QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
62   aDesktop->addAction(aAction);
63
64   addAction("DELETE_CMD", aAction);
65   aAction->setShortcutContext(Qt::ApplicationShortcut);
66
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()));
70
71   aAction = new QAction(QIcon(":pictures/move.png"), XGUI_Workshop::MOVE_TO_END_COMMAND, this);
72   addAction("MOVE_CMD", aAction);
73
74   aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
75   addAction("COLOR_CMD", aAction);
76
77   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
78   addAction("SHOW_CMD", aAction);
79
80   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
81   addAction("SHOW_ONLY_CMD", aAction);
82
83   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
84   addAction("HIDE_CMD", aAction);
85
86   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
87   addAction("HIDEALL_CMD", aAction);
88
89   aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
90   addAction("SHADING_CMD", aAction);
91
92   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
93   addAction("WIREFRAME_CMD", aAction);
94
95   mySeparator = new QAction(this);
96   mySeparator->setSeparator(true);
97
98
99   buildObjBrowserMenu();
100   buildViewerMenu();
101 }
102
103 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
104 {
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;
110 }
111
112 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
113 {
114   if (myActions.contains(theId))
115     return myActions[theId];
116   return 0;
117 }
118
119 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
120 {
121   foreach(QAction* eachAction, myActions) {
122     if (eachAction->text() == theName) {
123       return eachAction;
124     }
125   }
126   return NULL;
127 }
128
129 QStringList XGUI_ContextMenuMgr::actionIds() const
130 {
131   return myActions.keys();
132 }
133
134 void XGUI_ContextMenuMgr::onAction(bool isChecked)
135 {
136   QAction* aAction = static_cast<QAction*>(sender());
137   emit actionTriggered(aAction->data().toString(), isChecked);
138 }
139
140 void XGUI_ContextMenuMgr::updateCommandsStatus()
141 {
142 }
143
144 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
145 {
146   QMenu* aMenu = new QMenu();
147   if (sender() == myWorkshop->objectBrowser()) {
148     updateObjectBrowserMenu();
149     addObjBrowserMenu(aMenu);
150   } else if (sender() == myWorkshop->viewer()) {
151     updateViewerMenu();
152     addViewerMenu(aMenu);
153   }
154
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
158     // a created entity
159     emit beforeContextMenu();
160     aMenu->exec(theEvent->globalPos());
161     emit afterContextMenu();
162     delete aMenu;
163   }
164 }
165
166 void XGUI_ContextMenuMgr::updateObjectBrowserMenu() 
167 {
168   foreach(QAction* aAction, myActions)
169     aAction->setEnabled(false);
170
171   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
172   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
173   int aSelected = aObjects.size();
174   if (aSelected > 0) {
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);
182
183     //Process Feature
184     if (aSelected == 1) {
185       ObjectPtr aObject = aObjects.first();
186       if (aObject) {
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);
192           } else {
193             action("WIREFRAME_CMD")->setEnabled(true);
194             action("SHADING_CMD")->setEnabled(true);
195           }
196         }
197         if (!hasFeature) {
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);
203           }
204           else {
205             if (aObject->isDisplayed()) {
206               action("HIDE_CMD")->setEnabled(true);
207             } else if (hasResult && (!hasParameter)) {
208               action("SHOW_CMD")->setEnabled(true);
209             }
210           }
211           if (!(hasParameter || hasFeature))
212             action("SHOW_ONLY_CMD")->setEnabled(true);
213         }
214         else if (hasFeature && myWorkshop->canMoveFeature())
215           action("MOVE_CMD")->setEnabled(true);
216
217         if( aMgr->activeDocument() == aObject->document() )
218         {
219           action("RENAME_CMD")->setEnabled(true);
220           action("DELETE_CMD")->setEnabled(!hasSubFeature);
221         }
222       }
223     } else {
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);
231       }
232     }
233     bool allActive = true;
234     foreach( ObjectPtr aObject, aObjects )
235       if( aMgr->activeDocument() != aObject->document() )  {
236         allActive = false;
237         break;
238       }
239     if (!hasSubFeature && allActive ) {
240       if (hasFeature || hasParameter)
241         action("DELETE_CMD")->setEnabled(true);
242     }
243   }
244
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()) {
251         aDeactivate = true;
252         break;
253       }
254     }
255   }
256   if (aDeactivate) {
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);
261   }
262
263   if (myWorkshop->canChangeColor())
264     action("COLOR_CMD")->setEnabled(true);
265
266   ModuleBase_IModule* aModule = myWorkshop->module();
267   if (aModule)
268     aModule->updateObjectBrowserMenu(myActions);
269 }
270
271 void XGUI_ContextMenuMgr::updateViewerMenu()
272 {
273   foreach(QAction* aAction, myActions)
274     aAction->setEnabled(false);
275
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;
282     ObjectPtr aObject;
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()) {
287         isVisible = true;
288         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
289         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
290         break;
291       }
292     }
293     if (isVisible) {
294       if (canBeShaded) {
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);
300         } else {
301           action("WIREFRAME_CMD")->setEnabled(true);
302           action("SHADING_CMD")->setEnabled(true);
303         }
304       }
305       action("SHOW_ONLY_CMD")->setEnabled(true);
306       action("HIDE_CMD")->setEnabled(true);
307     } else
308       action("SHOW_CMD")->setEnabled(true);
309   }
310   if (myWorkshop->displayer()->objectsCount() > 0)
311     action("HIDEALL_CMD")->setEnabled(true);
312   if (myWorkshop->canChangeColor())
313     action("COLOR_CMD")->setEnabled(true);
314
315   action("DELETE_CMD")->setEnabled(true);
316
317   ModuleBase_IModule* aModule = myWorkshop->module();
318   if (aModule)
319     aModule->updateViewerMenu(myActions);
320 }
321
322 void XGUI_ContextMenuMgr::connectObjectBrowser()
323 {
324   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
325           SLOT(onContextMenuRequest(QContextMenuEvent*)));
326 }
327
328 void XGUI_ContextMenuMgr::connectViewer()
329 {
330   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
331           SLOT(onContextMenuRequest(QContextMenuEvent*)));
332 }
333
334
335 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
336 {
337   QAction* aSeparator = new QAction(this);
338   aSeparator->setSeparator(true);
339
340   QActionsList aList;
341   
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   //-------------------------------------
351   // Result body menu
352   aList.clear();
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;
363   // Group menu
364   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
365   // Result part menu
366   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
367   //-------------------------------------
368   // Feature menu
369   aList.clear();
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;
375
376   aList.clear();
377   aList.append(action("DELETE_CMD"));
378   aList.append(mySeparator);
379   aList.append(action("RENAME_CMD"));
380   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
381   //-------------------------------------
382 }
383
384 void XGUI_ContextMenuMgr::buildViewerMenu()
385 {
386   QActionsList aList;
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;
392   // Result part menu
393   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
394   //-------------------------------------
395   // Result body menu
396   aList.clear();
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;
404   // Group menu
405   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
406   //-------------------------------------
407
408 }
409
410
411 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
412 {
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"));
433   }
434   theMenu->addActions(aActions);
435
436   ModuleBase_IModule* aModule = myWorkshop->module();
437   if (aModule) {
438     theMenu->addSeparator();
439     aModule->addObjectBrowserMenu(theMenu);
440   }
441   theMenu->addSeparator();
442   theMenu->addActions(myWorkshop->objectBrowser()->actions());
443 }
444
445 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
446 {
447   ModuleBase_IModule* aModule = myWorkshop->module();
448   if (aModule) {
449     if (aModule->addViewerMenu(theMenu, myActions))
450       theMenu->addSeparator();
451   }
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];
462     }
463     aActions.append(action("COLOR_CMD"));
464   } else if (aSelected > 1) {
465     aActions.append(action("HIDE_CMD"));
466     aActions.append(action("COLOR_CMD"));
467   }
468   // hide all is shown always even if selection in the viewer is empty
469   aActions.append(action("HIDEALL_CMD"));
470   theMenu->addActions(aActions);
471
472 #ifndef HAVE_SALOME
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());
478   }
479 #endif
480 }
481
482 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
483 {
484   QStringList aGroups;
485
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))
491       continue;
492     QActionsList anActions = anIt.value();
493     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
494     bool aFound = false;
495     for (; anAIt != anALast && !aFound; anAIt++)
496       aFound = (*anAIt)->data().toString() == theName;
497     if (aFound)
498       aGroups.append(aGroupName);
499   }
500   return aGroups;
501 }
502
503 void XGUI_ContextMenuMgr::onRename()
504 {
505   QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
506   if (!myWorkshop->abortAllOperations())
507     return; 
508   // restore selection in case if dialog box was shown
509   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
510   myWorkshop->objectBrowser()->onEditItem();
511 }