Salome HOME
bd1f2012fab765c247f8aebe29090346f04bdc2e
[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 = 0;
133   if (sender() == myWorkshop->objectBrowser()) {
134     updateObjectBrowserMenu();
135     aMenu = objBrowserMenu();
136   } else if (sender() == myWorkshop->viewer()) {
137     updateViewerMenu();
138     aMenu = viewerMenu();
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     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
167
168     //Process Feature
169     if (aSelected == 1) {
170       ObjectPtr aObject = aObjects.first();
171       if (aObject) {
172         if (!hasFeature) {
173           if (aObject->isDisplayed()) {
174             if (aDisplayer->canBeShaded(aObject)) {
175               action("WIREFRAME_CMD")->setEnabled(true);
176               action("SHADING_CMD")->setEnabled(true);
177             }
178             action("HIDE_CMD")->setEnabled(true);
179           } else if (hasResult && (!hasParameter)) {
180             action("SHOW_CMD")->setEnabled(true);
181           }
182
183           ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
184           if (aPartRes) {
185             action("SHOW_CMD")->setEnabled(true);
186           }
187
188           if (!(hasParameter || hasFeature))
189             action("SHOW_ONLY_CMD")->setEnabled(true);
190         }
191       } 
192     } else {
193       if (hasResult && (!hasParameter)) {
194         action("SHOW_CMD")->setEnabled(true);
195         action("HIDE_CMD")->setEnabled(true);
196         action("SHOW_ONLY_CMD")->setEnabled(true);
197         action("SHADING_CMD")->setEnabled(true);
198         action("WIREFRAME_CMD")->setEnabled(true);
199       }
200     }
201     if (hasFeature || hasParameter)
202       action("DELETE_CMD")->setEnabled(true);
203
204     if (hasFeature || hasParameter)
205       action("MOVE_CMD")->setEnabled(true);
206   }
207   if (myWorkshop->canChangeColor())
208     action("COLOR_CMD")->setEnabled(true);
209 }
210
211 void XGUI_ContextMenuMgr::updateViewerMenu()
212 {
213   foreach(QAction* aAction, myActions)
214     aAction->setEnabled(false);
215
216   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
217   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
218   if (aObjects.size() > 0) {
219     bool isVisible = false;
220     bool isShading = false;
221     bool canBeShaded = false;
222     foreach(ObjectPtr aObject, aObjects)
223     {
224       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
225       if (aRes && aRes->isDisplayed()) {
226         isVisible = true;
227         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
228         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
229         break;
230       }
231     }
232     if (isVisible) {
233       if (canBeShaded) {
234         action("WIREFRAME_CMD")->setEnabled(true);
235         action("SHADING_CMD")->setEnabled(true);
236       }
237       action("SHOW_ONLY_CMD")->setEnabled(true);
238       action("HIDE_CMD")->setEnabled(true);
239     } else
240       action("SHOW_CMD")->setEnabled(true);
241   }
242   if (myWorkshop->displayer()->objectsCount() > 0)
243     action("HIDEALL_CMD")->setEnabled(true);
244   if (myWorkshop->canChangeColor())
245     action("COLOR_CMD")->setEnabled(true);
246
247   action("DELETE_CMD")->setEnabled(true);
248 }
249
250 void XGUI_ContextMenuMgr::connectObjectBrowser()
251 {
252   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
253           SLOT(onContextMenuRequest(QContextMenuEvent*)));
254 }
255
256 void XGUI_ContextMenuMgr::connectViewer()
257 {
258   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
259           SLOT(onContextMenuRequest(QContextMenuEvent*)));
260 }
261
262
263 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
264 {
265   QAction* aSeparator = new QAction(this);
266   aSeparator->setSeparator(true);
267
268   QActionsList aList;
269   
270   // Result construction menu
271   aList.append(action("SHOW_CMD"));
272   aList.append(action("HIDE_CMD"));
273   aList.append(action("SHOW_ONLY_CMD"));
274   aList.append(action("COLOR_CMD"));
275   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
276   // Result part menu
277   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
278   //-------------------------------------
279   // Result body menu
280   aList.clear();
281   aList.append(action("WIREFRAME_CMD"));
282   aList.append(action("SHADING_CMD"));
283   aList.append(action("COLOR_CMD"));
284   aList.append(mySeparator);
285   aList.append(action("SHOW_CMD"));
286   aList.append(action("HIDE_CMD"));
287   aList.append(action("SHOW_ONLY_CMD"));
288   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
289   // Group menu
290   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
291   //-------------------------------------
292   // Feature menu
293   aList.clear();
294   aList.append(action("DELETE_CMD"));
295   aList.append(action("MOVE_CMD"));
296   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
297   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
298   //-------------------------------------
299 }
300
301 void XGUI_ContextMenuMgr::buildViewerMenu()
302 {
303   QActionsList aList;
304   // Result construction menu
305   aList.append(action("HIDE_CMD"));
306   aList.append(action("SHOW_ONLY_CMD"));
307   aList.append(action("HIDEALL_CMD"));
308   aList.append(action("COLOR_CMD"));
309   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
310   // Result part menu
311   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
312   //-------------------------------------
313   // Result body menu
314   aList.clear();
315   aList.append(action("WIREFRAME_CMD"));
316   aList.append(action("SHADING_CMD"));
317   aList.append(action("COLOR_CMD"));
318   aList.append(mySeparator);
319   aList.append(action("HIDE_CMD"));
320   aList.append(action("SHOW_ONLY_CMD"));
321   aList.append(action("HIDEALL_CMD"));
322   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
323   // Group menu
324   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
325   //-------------------------------------
326
327 }
328
329
330 QMenu* XGUI_ContextMenuMgr::objBrowserMenu() const
331 {
332   QMenu* aMenu = new QMenu();
333   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
334   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
335   int aSelected = aObjects.size();
336   QActionsList aActions;
337   if (aSelected == 1) {
338     ObjectPtr aObject = aObjects.first();
339     std::string aName = aObject->groupName();
340     if (myObjBrowserMenus.contains(aName))
341       aActions = myObjBrowserMenus[aName];
342   } else if (aSelected > 1) {
343       aActions.append(action("SHADING_CMD"));
344       aActions.append(action("WIREFRAME_CMD"));
345       aActions.append(mySeparator);
346       aActions.append(action("SHOW_CMD"));
347       aActions.append(action("HIDE_CMD"));
348       aActions.append(action("SHOW_ONLY_CMD"));
349       aActions.append(mySeparator);
350       aActions.append(action("DELETE_CMD"));
351       aActions.append(action("MOVE_CMD"));
352   }
353   aMenu->addActions(aActions);
354
355   ModuleBase_IModule* aModule = myWorkshop->module();
356   if (aModule) {
357     aMenu->addSeparator();
358     aModule->addObjectBrowserMenu(aMenu);
359   }
360   aMenu->addSeparator();
361   aMenu->addActions(myWorkshop->objectBrowser()->actions());
362
363   return aMenu;
364 }
365
366 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
367 {
368   QMenu* aMenu = new QMenu();
369   ModuleBase_IModule* aModule = myWorkshop->module();
370   if (aModule) 
371     aModule->addViewerMenu(aMenu, myActions);
372
373   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
374   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
375   int aSelected = aObjects.size();
376   QActionsList aActions;
377   if (aSelected == 1) {
378     ObjectPtr aObject = aObjects.first();
379     std::string aName = aObject->groupName();
380     if (myViewerMenu.contains(aName))
381       aActions = myViewerMenu[aName];
382   } else if (aSelected > 1) {
383     aActions.append(action("HIDE_CMD"));
384   }
385   aMenu->addActions(aActions);
386
387   if (!myWorkshop->isSalomeMode()) {
388     aMenu->addSeparator();
389     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
390     if (aMDI->actions().size() > 0) {
391       QMenu* aSubMenu = aMenu->addMenu(tr("Windows"));
392       aSubMenu->addActions(aMDI->actions());
393     }
394   }
395   return aMenu;
396 }
397
398 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
399 {
400   QStringList aGroups;
401
402   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
403                                                   aLast = myObjBrowserMenus.end();
404   for (; anIt != aLast; anIt++) {
405     QString aGroupName(anIt.key().c_str());
406     if (aGroups.contains(aGroupName))
407       continue;
408     QActionsList anActions = anIt.value();
409     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
410     bool aFound = false;
411     for (; anAIt != anALast && !aFound; anAIt++)
412       aFound = (*anAIt)->data().toString() == theName;
413     if (aFound)
414       aGroups.append(aGroupName);
415   }
416   return aGroups;
417 }