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