Salome HOME
Merge tag 'V_1.3.1' into HEAD
[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
12 #include <AppElements_MainWindow.h>
13
14 //#include "PartSetPlugin_Part.h"
15
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_AttributeDocRef.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_ResultGroup.h>
21 #include <ModelAPI_ResultParameter.h>
22 #include <ModelAPI_ResultConstruction.h>
23 #include <ModelAPI_ResultBody.h>
24
25 #include <ModuleBase_IModule.h>
26 #include <ModuleBase_Tools.h>
27
28 #include <QAction>
29 #include <QContextMenuEvent>
30 #include <QMenu>
31 #include <QMdiArea>
32
33
34 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
35     : QObject(theParent),
36       myWorkshop(theParent),
37       mySeparator(0)
38 {
39 }
40
41 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
42 {
43 }
44
45 void XGUI_ContextMenuMgr::createActions()
46 {
47   QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
48   QMainWindow* aDesktop = myWorkshop->mainWindow();
49   if (!aDesktop)
50     aDesktop = myWorkshop->salomeConnector()->desktop();
51   aDesktop->addAction(aAction);
52
53   addAction("DELETE_CMD", aAction);
54   aAction->setShortcut(Qt::Key_Delete);
55   aAction->setShortcutContext(Qt::ApplicationShortcut);
56
57   aAction = new QAction(QIcon(":pictures/move.png"), tr("Move..."), this);
58   addAction("MOVE_CMD", aAction);
59
60   aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
61   addAction("COLOR_CMD", aAction);
62
63   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
64   addAction("SHOW_CMD", aAction);
65
66   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
67   addAction("SHOW_ONLY_CMD", aAction);
68
69   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
70   addAction("HIDE_CMD", aAction);
71
72   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
73   addAction("HIDEALL_CMD", aAction);
74
75   aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
76   addAction("SHADING_CMD", aAction);
77
78   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
79   addAction("WIREFRAME_CMD", aAction);
80
81   mySeparator = new QAction(this);
82   mySeparator->setSeparator(true);
83
84
85   buildObjBrowserMenu();
86   buildViewerMenu();
87 }
88
89 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
90 {
91   if (myActions.contains(theId))
92     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
93   theAction->setData(theId);
94   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
95   myActions[theId] = theAction;
96 }
97
98 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
99 {
100   if (myActions.contains(theId))
101     return myActions[theId];
102   return 0;
103 }
104
105 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
106 {
107   foreach(QAction* eachAction, myActions) {
108     if (eachAction->text() == theName) {
109       return eachAction;
110     }
111   }
112   return NULL;
113 }
114
115 QStringList XGUI_ContextMenuMgr::actionIds() const
116 {
117   return myActions.keys();
118 }
119
120 void XGUI_ContextMenuMgr::onAction(bool isChecked)
121 {
122   QAction* aAction = static_cast<QAction*>(sender());
123   emit actionTriggered(aAction->data().toString(), isChecked);
124 }
125
126 void XGUI_ContextMenuMgr::updateCommandsStatus()
127 {
128 }
129
130 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
131 {
132   QMenu* aMenu = new QMenu();
133   if (sender() == myWorkshop->objectBrowser()) {
134     updateObjectBrowserMenu();
135     addObjBrowserMenu(aMenu);
136   } else if (sender() == myWorkshop->viewer()) {
137     updateViewerMenu();
138     addViewerMenu(aMenu);
139   }
140
141   if (aMenu && (aMenu->actions().size() > 0)) {
142     // it is possible that some objects should do something before and after the popup menu exec
143     // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
144     // a created entity
145     emit beforeContextMenu();
146     aMenu->exec(theEvent->globalPos());
147     emit afterContextMenu();
148     delete aMenu;
149   }
150 }
151
152 void XGUI_ContextMenuMgr::updateObjectBrowserMenu() 
153 {
154   foreach(QAction* aAction, myActions)
155     aAction->setEnabled(false);
156
157   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
158   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
159   int aSelected = aObjects.size();
160   if (aSelected > 0) {
161     SessionPtr aMgr = ModelAPI_Session::get();
162     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
163     bool hasResult = false;
164     bool hasFeature = false;
165     bool hasParameter = false;
166     bool hasSubFeature = false;
167     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
168
169     //Process Feature
170     if (aSelected == 1) {
171       ObjectPtr aObject = aObjects.first();
172       if (aObject) {
173         if (!hasFeature) {
174           if (aObject->isDisplayed()) {
175             if (aDisplayer->canBeShaded(aObject)) {
176               action("WIREFRAME_CMD")->setEnabled(true);
177               action("SHADING_CMD")->setEnabled(true);
178             }
179             action("HIDE_CMD")->setEnabled(true);
180           } else if (hasResult && (!hasParameter)) {
181             action("SHOW_CMD")->setEnabled(true);
182           }
183
184           if (!(hasParameter || hasFeature))
185             action("SHOW_ONLY_CMD")->setEnabled(true);
186         }
187         else if (hasFeature && myWorkshop->canMoveFeature())
188           action("MOVE_CMD")->setEnabled(true);
189       }
190     } else {
191       if (hasResult && (!hasParameter)) {
192         action("SHOW_CMD")->setEnabled(true);
193         action("HIDE_CMD")->setEnabled(true);
194         action("SHOW_ONLY_CMD")->setEnabled(true);
195         action("SHADING_CMD")->setEnabled(true);
196         action("WIREFRAME_CMD")->setEnabled(true);
197       }
198     }
199     if (!hasSubFeature) {
200       if (hasFeature || hasParameter)
201         action("DELETE_CMD")->setEnabled(true);
202     }
203   }
204   if (myWorkshop->canChangeColor())
205     action("COLOR_CMD")->setEnabled(true);
206
207   ModuleBase_IModule* aModule = myWorkshop->module();
208   if (aModule)
209     aModule->updateObjectBrowserMenu(myActions);
210 }
211
212 void XGUI_ContextMenuMgr::updateViewerMenu()
213 {
214   foreach(QAction* aAction, myActions)
215     aAction->setEnabled(false);
216
217   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
218   QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
219   if (aPrsList.size() > 0) {
220     bool isVisible = false;
221     bool isShading = false;
222     bool canBeShaded = false;
223     ObjectPtr aObject;
224     foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
225       aObject = aPrs.object();
226       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
227       if (aRes && aRes->isDisplayed()) {
228         isVisible = true;
229         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
230         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
231         break;
232       }
233     }
234     if (isVisible) {
235       if (canBeShaded) {
236         action("WIREFRAME_CMD")->setEnabled(true);
237         action("SHADING_CMD")->setEnabled(true);
238       }
239       action("SHOW_ONLY_CMD")->setEnabled(true);
240       action("HIDE_CMD")->setEnabled(true);
241     } else
242       action("SHOW_CMD")->setEnabled(true);
243   }
244   if (myWorkshop->displayer()->objectsCount() > 0)
245     action("HIDEALL_CMD")->setEnabled(true);
246   if (myWorkshop->canChangeColor())
247     action("COLOR_CMD")->setEnabled(true);
248
249   action("DELETE_CMD")->setEnabled(true);
250
251   ModuleBase_IModule* aModule = myWorkshop->module();
252   if (aModule)
253     aModule->updateViewerMenu(myActions);
254 }
255
256 void XGUI_ContextMenuMgr::connectObjectBrowser()
257 {
258   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
259           SLOT(onContextMenuRequest(QContextMenuEvent*)));
260 }
261
262 void XGUI_ContextMenuMgr::connectViewer()
263 {
264   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
265           SLOT(onContextMenuRequest(QContextMenuEvent*)));
266 }
267
268
269 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
270 {
271   QAction* aSeparator = new QAction(this);
272   aSeparator->setSeparator(true);
273
274   QActionsList aList;
275   
276   // Result construction menu
277   aList.append(action("SHOW_CMD"));
278   aList.append(action("HIDE_CMD"));
279   aList.append(action("SHOW_ONLY_CMD"));
280   aList.append(action("COLOR_CMD"));
281   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
282   //-------------------------------------
283   // Result body menu
284   aList.clear();
285   aList.append(action("WIREFRAME_CMD"));
286   aList.append(action("SHADING_CMD"));
287   aList.append(action("COLOR_CMD"));
288   aList.append(mySeparator);
289   aList.append(action("SHOW_CMD"));
290   aList.append(action("HIDE_CMD"));
291   aList.append(action("SHOW_ONLY_CMD"));
292   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
293   // Group menu
294   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
295   // Result part menu
296   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
297   //-------------------------------------
298   // Feature menu
299   aList.clear();
300   aList.append(action("DELETE_CMD"));
301   aList.append(action("MOVE_CMD"));
302   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
303
304   aList.clear();
305   aList.append(action("DELETE_CMD"));
306   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
307   //-------------------------------------
308 }
309
310 void XGUI_ContextMenuMgr::buildViewerMenu()
311 {
312   QActionsList aList;
313   // Result construction menu
314   aList.append(action("HIDE_CMD"));
315   aList.append(action("SHOW_ONLY_CMD"));
316   aList.append(action("HIDEALL_CMD"));
317   aList.append(action("COLOR_CMD"));
318   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
319   // Result part menu
320   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
321   //-------------------------------------
322   // Result body menu
323   aList.clear();
324   aList.append(action("WIREFRAME_CMD"));
325   aList.append(action("SHADING_CMD"));
326   aList.append(action("COLOR_CMD"));
327   aList.append(mySeparator);
328   aList.append(action("HIDE_CMD"));
329   aList.append(action("SHOW_ONLY_CMD"));
330   aList.append(action("HIDEALL_CMD"));
331   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
332   // Group menu
333   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
334   //-------------------------------------
335
336 }
337
338
339 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
340 {
341   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
342   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
343   int aSelected = aObjects.size();
344   QActionsList aActions;
345   if (aSelected == 1) {
346     ObjectPtr aObject = aObjects.first();
347     std::string aName = aObject->groupName();
348     if (myObjBrowserMenus.contains(aName))
349       aActions = myObjBrowserMenus[aName];
350   } else if (aSelected > 1) {
351       aActions.append(action("SHADING_CMD"));
352       aActions.append(action("WIREFRAME_CMD"));
353       aActions.append(mySeparator);
354       aActions.append(action("SHOW_CMD"));
355       aActions.append(action("HIDE_CMD"));
356       aActions.append(action("SHOW_ONLY_CMD"));
357       aActions.append(mySeparator);
358       aActions.append(action("DELETE_CMD"));
359       //aActions.append(action("MOVE_CMD"));
360   }
361   theMenu->addActions(aActions);
362
363   ModuleBase_IModule* aModule = myWorkshop->module();
364   if (aModule) {
365     theMenu->addSeparator();
366     aModule->addObjectBrowserMenu(theMenu);
367   }
368   theMenu->addSeparator();
369   theMenu->addActions(myWorkshop->objectBrowser()->actions());
370 }
371
372 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
373 {
374   ModuleBase_IModule* aModule = myWorkshop->module();
375   if (aModule) {
376     if (aModule->addViewerMenu(theMenu, myActions))
377       theMenu->addSeparator();
378   }
379   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
380   QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
381   int aSelected = aPrsList.size();
382   QActionsList aActions;
383   if (aSelected == 1) {
384     ObjectPtr aObject = aPrsList.first().object();
385     std::string aName = aObject->groupName();
386     if (myViewerMenu.contains(aName))
387       aActions = myViewerMenu[aName];
388   } else if (aSelected > 1) {
389     aActions.append(action("HIDE_CMD"));
390   }
391   theMenu->addActions(aActions);
392
393   if (!myWorkshop->isSalomeMode()) {
394     theMenu->addSeparator();
395     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
396     if (aMDI->actions().size() > 0) {
397       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
398       aSubMenu->addActions(aMDI->actions());
399     }
400   }
401 }
402
403 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
404 {
405   QStringList aGroups;
406
407   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
408                                                   aLast = myObjBrowserMenus.end();
409   for (; anIt != aLast; anIt++) {
410     QString aGroupName(anIt.key().c_str());
411     if (aGroups.contains(aGroupName))
412       continue;
413     QActionsList anActions = anIt.value();
414     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
415     bool aFound = false;
416     for (; anAIt != anALast && !aFound; anAIt++)
417       aFound = (*anAIt)->data().toString() == theName;
418     if (aFound)
419       aGroups.append(aGroupName);
420   }
421   return aGroups;
422 }