Salome HOME
Set font of the Root Label the same as in Tree View
[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->setShortcut(Qt::Key_Delete);
66   aAction->setShortcutContext(Qt::ApplicationShortcut);
67
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()));
71
72   aAction = new QAction(QIcon(":pictures/move.png"), tr("Move to the end"), this);
73   addAction("MOVE_CMD", aAction);
74
75   aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
76   addAction("COLOR_CMD", aAction);
77
78   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
79   addAction("SHOW_CMD", aAction);
80
81   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
82   addAction("SHOW_ONLY_CMD", aAction);
83
84   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
85   addAction("HIDE_CMD", aAction);
86
87   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
88   addAction("HIDEALL_CMD", aAction);
89
90   aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
91   addAction("SHADING_CMD", aAction);
92
93   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
94   addAction("WIREFRAME_CMD", aAction);
95
96   mySeparator = new QAction(this);
97   mySeparator->setSeparator(true);
98
99
100   buildObjBrowserMenu();
101   buildViewerMenu();
102 }
103
104 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
105 {
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;
111 }
112
113 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
114 {
115   if (myActions.contains(theId))
116     return myActions[theId];
117   return 0;
118 }
119
120 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
121 {
122   foreach(QAction* eachAction, myActions) {
123     if (eachAction->text() == theName) {
124       return eachAction;
125     }
126   }
127   return NULL;
128 }
129
130 QStringList XGUI_ContextMenuMgr::actionIds() const
131 {
132   return myActions.keys();
133 }
134
135 void XGUI_ContextMenuMgr::onAction(bool isChecked)
136 {
137   QAction* aAction = static_cast<QAction*>(sender());
138   emit actionTriggered(aAction->data().toString(), isChecked);
139 }
140
141 void XGUI_ContextMenuMgr::updateCommandsStatus()
142 {
143 }
144
145 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
146 {
147   QMenu* aMenu = new QMenu();
148   if (sender() == myWorkshop->objectBrowser()) {
149     updateObjectBrowserMenu();
150     addObjBrowserMenu(aMenu);
151   } else if (sender() == myWorkshop->viewer()) {
152     updateViewerMenu();
153     addViewerMenu(aMenu);
154   }
155
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
159     // a created entity
160     emit beforeContextMenu();
161     aMenu->exec(theEvent->globalPos());
162     emit afterContextMenu();
163     delete aMenu;
164   }
165 }
166
167 void XGUI_ContextMenuMgr::updateObjectBrowserMenu() 
168 {
169   foreach(QAction* aAction, myActions)
170     aAction->setEnabled(false);
171
172   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
173   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
174   int aSelected = aObjects.size();
175   if (aSelected > 0) {
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);
183
184     //Process Feature
185     if (aSelected == 1) {
186       ObjectPtr aObject = aObjects.first();
187       if (aObject) {
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);
193           } else {
194             action("WIREFRAME_CMD")->setEnabled(true);
195             action("SHADING_CMD")->setEnabled(true);
196           }
197         }
198         if (!hasFeature) {
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);
204           }
205           else {
206             if (aObject->isDisplayed()) {
207               action("HIDE_CMD")->setEnabled(true);
208             } else if (hasResult && (!hasParameter)) {
209               action("SHOW_CMD")->setEnabled(true);
210             }
211           }
212           if (!(hasParameter || hasFeature))
213             action("SHOW_ONLY_CMD")->setEnabled(true);
214         }
215         else if (hasFeature && myWorkshop->canMoveFeature())
216           action("MOVE_CMD")->setEnabled(true);
217
218         if( aMgr->activeDocument() == aObject->document() )
219         {
220           action("RENAME_CMD")->setEnabled(true);
221           action("DELETE_CMD")->setEnabled(!hasSubFeature);
222         }
223       }
224     } else {
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);
232       }
233     }
234     bool allActive = true;
235     foreach( ObjectPtr aObject, aObjects )
236       if( aMgr->activeDocument() != aObject->document() )  {
237         allActive = false;
238         break;
239       }
240     if (!hasSubFeature && allActive ) {
241       if (hasFeature || hasParameter)
242         action("DELETE_CMD")->setEnabled(true);
243     }
244   }
245
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()) {
252         aDeactivate = true;
253         break;
254       }
255     }
256   }
257   if (aDeactivate) {
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);
262   }
263
264   if (myWorkshop->canChangeColor())
265     action("COLOR_CMD")->setEnabled(true);
266
267   ModuleBase_IModule* aModule = myWorkshop->module();
268   if (aModule)
269     aModule->updateObjectBrowserMenu(myActions);
270 }
271
272 void XGUI_ContextMenuMgr::updateViewerMenu()
273 {
274   foreach(QAction* aAction, myActions)
275     aAction->setEnabled(false);
276
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;
283     ObjectPtr aObject;
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()) {
288         isVisible = true;
289         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
290         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
291         break;
292       }
293     }
294     if (isVisible) {
295       if (canBeShaded) {
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);
301         } else {
302           action("WIREFRAME_CMD")->setEnabled(true);
303           action("SHADING_CMD")->setEnabled(true);
304         }
305       }
306       action("SHOW_ONLY_CMD")->setEnabled(true);
307       action("HIDE_CMD")->setEnabled(true);
308     } else
309       action("SHOW_CMD")->setEnabled(true);
310   }
311   if (myWorkshop->displayer()->objectsCount() > 0)
312     action("HIDEALL_CMD")->setEnabled(true);
313   if (myWorkshop->canChangeColor())
314     action("COLOR_CMD")->setEnabled(true);
315
316   action("DELETE_CMD")->setEnabled(true);
317
318   ModuleBase_IModule* aModule = myWorkshop->module();
319   if (aModule)
320     aModule->updateViewerMenu(myActions);
321 }
322
323 void XGUI_ContextMenuMgr::connectObjectBrowser()
324 {
325   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
326           SLOT(onContextMenuRequest(QContextMenuEvent*)));
327 }
328
329 void XGUI_ContextMenuMgr::connectViewer()
330 {
331   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
332           SLOT(onContextMenuRequest(QContextMenuEvent*)));
333 }
334
335
336 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
337 {
338   QAction* aSeparator = new QAction(this);
339   aSeparator->setSeparator(true);
340
341   QActionsList aList;
342   
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   //-------------------------------------
352   // Result body menu
353   aList.clear();
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;
364   // Group menu
365   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
366   // Result part menu
367   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
368   //-------------------------------------
369   // Feature menu
370   aList.clear();
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;
376
377   aList.clear();
378   aList.append(action("DELETE_CMD"));
379   aList.append(mySeparator);
380   aList.append(action("RENAME_CMD"));
381   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
382   //-------------------------------------
383 }
384
385 void XGUI_ContextMenuMgr::buildViewerMenu()
386 {
387   QActionsList aList;
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;
394   // Result part menu
395   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
396   //-------------------------------------
397   // Result body menu
398   aList.clear();
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;
407   // Group menu
408   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
409   //-------------------------------------
410
411 }
412
413
414 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
415 {
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"));
436   }
437   theMenu->addActions(aActions);
438
439   ModuleBase_IModule* aModule = myWorkshop->module();
440   if (aModule) {
441     theMenu->addSeparator();
442     aModule->addObjectBrowserMenu(theMenu);
443   }
444   theMenu->addSeparator();
445   theMenu->addActions(myWorkshop->objectBrowser()->actions());
446 }
447
448 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
449 {
450   ModuleBase_IModule* aModule = myWorkshop->module();
451   if (aModule) {
452     if (aModule->addViewerMenu(theMenu, myActions))
453       theMenu->addSeparator();
454   }
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"));
468   }
469   theMenu->addActions(aActions);
470
471 #ifndef HAVE_SALOME
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());
477   }
478 #endif
479 }
480
481 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
482 {
483   QStringList aGroups;
484
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))
490       continue;
491     QActionsList anActions = anIt.value();
492     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
493     bool aFound = false;
494     for (; anAIt != anALast && !aFound; anAIt++)
495       aFound = (*anAIt)->data().toString() == theName;
496     if (aFound)
497       aGroups.append(aGroupName);
498   }
499   return aGroups;
500 }
501
502 void XGUI_ContextMenuMgr::onRename()
503 {
504   QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
505   if (!myWorkshop->abortAllOperations())
506     return; 
507   // restore selection in case if dialog box was shown
508   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
509   myWorkshop->objectBrowser()->onEditItem();
510 }