]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ContextMenuMgr.cpp
Salome HOME
Define Rename as an operation
[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 #include <AppElements_MainWindow.h>
16
17 //#include "PartSetPlugin_Part.h"
18
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>
28
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_Tools.h>
31 #include <ModuleBase_OperationAction.h>
32
33 #include <QAction>
34 #include <QContextMenuEvent>
35 #include <QMenu>
36 #include <QMdiArea>
37
38
39 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
40     : QObject(theParent),
41       myWorkshop(theParent),
42       mySeparator(0)
43 {
44 }
45
46 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
47 {
48 }
49
50 void XGUI_ContextMenuMgr::createActions()
51 {
52   QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
53   QMainWindow* aDesktop = myWorkshop->mainWindow();
54   if (!aDesktop)
55     aDesktop = myWorkshop->salomeConnector()->desktop();
56   aDesktop->addAction(aAction);
57
58   addAction("DELETE_CMD", aAction);
59   aAction->setShortcut(Qt::Key_Delete);
60   aAction->setShortcutContext(Qt::ApplicationShortcut);
61
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()));
65
66   aAction = new QAction(QIcon(":pictures/move.png"), tr("Move to the end"), this);
67   addAction("MOVE_CMD", aAction);
68
69   aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
70   addAction("COLOR_CMD", aAction);
71
72   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
73   addAction("SHOW_CMD", aAction);
74
75   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
76   addAction("SHOW_ONLY_CMD", aAction);
77
78   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
79   addAction("HIDE_CMD", aAction);
80
81   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
82   addAction("HIDEALL_CMD", aAction);
83
84   aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
85   addAction("SHADING_CMD", aAction);
86
87   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
88   addAction("WIREFRAME_CMD", aAction);
89
90   mySeparator = new QAction(this);
91   mySeparator->setSeparator(true);
92
93
94   buildObjBrowserMenu();
95   buildViewerMenu();
96 }
97
98 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
99 {
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;
105 }
106
107 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
108 {
109   if (myActions.contains(theId))
110     return myActions[theId];
111   return 0;
112 }
113
114 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
115 {
116   foreach(QAction* eachAction, myActions) {
117     if (eachAction->text() == theName) {
118       return eachAction;
119     }
120   }
121   return NULL;
122 }
123
124 QStringList XGUI_ContextMenuMgr::actionIds() const
125 {
126   return myActions.keys();
127 }
128
129 void XGUI_ContextMenuMgr::onAction(bool isChecked)
130 {
131   QAction* aAction = static_cast<QAction*>(sender());
132   emit actionTriggered(aAction->data().toString(), isChecked);
133 }
134
135 void XGUI_ContextMenuMgr::updateCommandsStatus()
136 {
137 }
138
139 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
140 {
141   QMenu* aMenu = new QMenu();
142   if (sender() == myWorkshop->objectBrowser()) {
143     updateObjectBrowserMenu();
144     addObjBrowserMenu(aMenu);
145   } else if (sender() == myWorkshop->viewer()) {
146     updateViewerMenu();
147     addViewerMenu(aMenu);
148   }
149
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
153     // a created entity
154     emit beforeContextMenu();
155     aMenu->exec(theEvent->globalPos());
156     emit afterContextMenu();
157     delete aMenu;
158   }
159 }
160
161 void XGUI_ContextMenuMgr::updateObjectBrowserMenu() 
162 {
163   foreach(QAction* aAction, myActions)
164     aAction->setEnabled(false);
165
166   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
167   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
168   int aSelected = aObjects.size();
169   if (aSelected > 0) {
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);
177
178     //Process Feature
179     if (aSelected == 1) {
180       ObjectPtr aObject = aObjects.first();
181       if (aObject) {
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);
187           } else {
188             action("WIREFRAME_CMD")->setEnabled(true);
189             action("SHADING_CMD")->setEnabled(true);
190           }
191         }
192         if (!hasFeature) {
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);
198           }
199           else {
200             if (aObject->isDisplayed()) {
201               action("HIDE_CMD")->setEnabled(true);
202             } else if (hasResult && (!hasParameter)) {
203               action("SHOW_CMD")->setEnabled(true);
204             }
205           }
206           if (!(hasParameter || hasFeature))
207             action("SHOW_ONLY_CMD")->setEnabled(true);
208         }
209         else if (hasFeature && myWorkshop->canMoveFeature())
210           action("MOVE_CMD")->setEnabled(true);
211
212         if( aMgr->activeDocument() == aObject->document() )
213         {
214           action("RENAME_CMD")->setEnabled(true);
215           action("DELETE_CMD")->setEnabled(true);
216         }
217       }
218     } else {
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);
226       }
227     }
228     bool allActive = true;
229     foreach( ObjectPtr aObject, aObjects )
230       if( aMgr->activeDocument() != aObject->document() )  {
231         allActive = false;
232         break;
233       }
234     if (!hasSubFeature && allActive ) {
235       if (hasFeature || hasParameter)
236         action("DELETE_CMD")->setEnabled(true);
237     }
238   }
239
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()) {
246         aDeactivate = true;
247         break;
248       }
249     }
250   }
251   if (aDeactivate) {
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);
256   }
257
258   if (myWorkshop->canChangeColor())
259     action("COLOR_CMD")->setEnabled(true);
260
261   ModuleBase_IModule* aModule = myWorkshop->module();
262   if (aModule)
263     aModule->updateObjectBrowserMenu(myActions);
264 }
265
266 void XGUI_ContextMenuMgr::updateViewerMenu()
267 {
268   foreach(QAction* aAction, myActions)
269     aAction->setEnabled(false);
270
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;
277     ObjectPtr aObject;
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()) {
282         isVisible = true;
283         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
284         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
285         break;
286       }
287     }
288     if (isVisible) {
289       if (canBeShaded) {
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);
295         } else {
296           action("WIREFRAME_CMD")->setEnabled(true);
297           action("SHADING_CMD")->setEnabled(true);
298         }
299       }
300       action("SHOW_ONLY_CMD")->setEnabled(true);
301       action("HIDE_CMD")->setEnabled(true);
302     } else
303       action("SHOW_CMD")->setEnabled(true);
304   }
305   if (myWorkshop->displayer()->objectsCount() > 0)
306     action("HIDEALL_CMD")->setEnabled(true);
307   if (myWorkshop->canChangeColor())
308     action("COLOR_CMD")->setEnabled(true);
309
310   action("DELETE_CMD")->setEnabled(true);
311
312   ModuleBase_IModule* aModule = myWorkshop->module();
313   if (aModule)
314     aModule->updateViewerMenu(myActions);
315 }
316
317 void XGUI_ContextMenuMgr::connectObjectBrowser()
318 {
319   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
320           SLOT(onContextMenuRequest(QContextMenuEvent*)));
321 }
322
323 void XGUI_ContextMenuMgr::connectViewer()
324 {
325   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
326           SLOT(onContextMenuRequest(QContextMenuEvent*)));
327 }
328
329
330 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
331 {
332   QAction* aSeparator = new QAction(this);
333   aSeparator->setSeparator(true);
334
335   QActionsList aList;
336   
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   //-------------------------------------
346   // Result body menu
347   aList.clear();
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;
358   // Group menu
359   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
360   // Result part menu
361   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
362   //-------------------------------------
363   // Feature menu
364   aList.clear();
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;
370
371   aList.clear();
372   aList.append(action("DELETE_CMD"));
373   aList.append(mySeparator);
374   aList.append(action("RENAME_CMD"));
375   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
376   //-------------------------------------
377 }
378
379 void XGUI_ContextMenuMgr::buildViewerMenu()
380 {
381   QActionsList aList;
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;
388   // Result part menu
389   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
390   //-------------------------------------
391   // Result body menu
392   aList.clear();
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;
401   // Group menu
402   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
403   //-------------------------------------
404
405 }
406
407
408 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
409 {
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"));
430   }
431   theMenu->addActions(aActions);
432
433   ModuleBase_IModule* aModule = myWorkshop->module();
434   if (aModule) {
435     theMenu->addSeparator();
436     aModule->addObjectBrowserMenu(theMenu);
437   }
438   theMenu->addSeparator();
439   theMenu->addActions(myWorkshop->objectBrowser()->actions());
440 }
441
442 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
443 {
444   ModuleBase_IModule* aModule = myWorkshop->module();
445   if (aModule) {
446     if (aModule->addViewerMenu(theMenu, myActions))
447       theMenu->addSeparator();
448   }
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"));
462   }
463   theMenu->addActions(aActions);
464
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());
471     }
472   }
473 }
474
475 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
476 {
477   QStringList aGroups;
478
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))
484       continue;
485     QActionsList anActions = anIt.value();
486     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
487     bool aFound = false;
488     for (; anAIt != anALast && !aFound; anAIt++)
489       aFound = (*anAIt)->data().toString() == theName;
490     if (aFound)
491       aGroups.append(aGroupName);
492   }
493   return aGroups;
494 }
495
496 void XGUI_ContextMenuMgr::onRename()
497 {
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();
503 }