]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ContextMenuMgr.cpp
Salome HOME
Compsolid - Color/Wireframe/Shading operation apply to sub-objects.
[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 (hasResult && myWorkshop->canBeShaded(aObject)) {
178           action("WIREFRAME_CMD")->setEnabled(true);
179           action("SHADING_CMD")->setEnabled(true);
180         }
181         if (!hasFeature) {
182           if (aObject->isDisplayed()) {
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       // parameter is commented because the actions are not in the list of result parameter actions
202       if (hasResult /*&& (!hasParameter)*/) {
203         action("SHOW_CMD")->setEnabled(true);
204         action("HIDE_CMD")->setEnabled(true);
205         action("SHOW_ONLY_CMD")->setEnabled(true);
206         action("SHADING_CMD")->setEnabled(true);
207         action("WIREFRAME_CMD")->setEnabled(true);
208       }
209     }
210     bool allActive = true;
211     foreach( ObjectPtr aObject, aObjects )
212       if( aMgr->activeDocument() != aObject->document() )
213       {
214         allActive = false;
215         break;
216       }
217     if (!hasSubFeature && allActive ) {
218       if (hasFeature || hasParameter)
219         action("DELETE_CMD")->setEnabled(true);
220     }
221   }
222   if (myWorkshop->canChangeColor())
223     action("COLOR_CMD")->setEnabled(true);
224
225   ModuleBase_IModule* aModule = myWorkshop->module();
226   if (aModule)
227     aModule->updateObjectBrowserMenu(myActions);
228 }
229
230 void XGUI_ContextMenuMgr::updateViewerMenu()
231 {
232   foreach(QAction* aAction, myActions)
233     aAction->setEnabled(false);
234
235   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
236   QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
237   if (aPrsList.size() > 0) {
238     bool isVisible = false;
239     bool isShading = false;
240     bool canBeShaded = false;
241     ObjectPtr aObject;
242     foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
243       aObject = aPrs.object();
244       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
245       if (aRes && aRes->isDisplayed()) {
246         isVisible = true;
247         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
248         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
249         break;
250       }
251     }
252     if (isVisible) {
253       if (canBeShaded) {
254         action("WIREFRAME_CMD")->setEnabled(true);
255         action("SHADING_CMD")->setEnabled(true);
256       }
257       action("SHOW_ONLY_CMD")->setEnabled(true);
258       action("HIDE_CMD")->setEnabled(true);
259     } else
260       action("SHOW_CMD")->setEnabled(true);
261   }
262   if (myWorkshop->displayer()->objectsCount() > 0)
263     action("HIDEALL_CMD")->setEnabled(true);
264   if (myWorkshop->canChangeColor())
265     action("COLOR_CMD")->setEnabled(true);
266
267   action("DELETE_CMD")->setEnabled(true);
268
269   ModuleBase_IModule* aModule = myWorkshop->module();
270   if (aModule)
271     aModule->updateViewerMenu(myActions);
272 }
273
274 void XGUI_ContextMenuMgr::connectObjectBrowser()
275 {
276   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
277           SLOT(onContextMenuRequest(QContextMenuEvent*)));
278 }
279
280 void XGUI_ContextMenuMgr::connectViewer()
281 {
282   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
283           SLOT(onContextMenuRequest(QContextMenuEvent*)));
284 }
285
286
287 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
288 {
289   QAction* aSeparator = new QAction(this);
290   aSeparator->setSeparator(true);
291
292   QActionsList aList;
293   
294   // Result construction menu
295   aList.append(action("SHOW_CMD"));
296   aList.append(action("HIDE_CMD"));
297   aList.append(action("SHOW_ONLY_CMD"));
298   aList.append(action("COLOR_CMD"));
299   aList.append(mySeparator);
300   aList.append(action("RENAME_CMD"));
301   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
302   //-------------------------------------
303   // Result body menu
304   aList.clear();
305   aList.append(action("WIREFRAME_CMD"));
306   aList.append(action("SHADING_CMD"));
307   aList.append(action("COLOR_CMD"));
308   aList.append(mySeparator);
309   aList.append(action("SHOW_CMD"));
310   aList.append(action("HIDE_CMD"));
311   aList.append(action("SHOW_ONLY_CMD"));
312   aList.append(mySeparator);
313   aList.append(action("RENAME_CMD"));
314   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
315   // Group menu
316   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
317   // Result part menu
318   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
319   //-------------------------------------
320   // Feature menu
321   aList.clear();
322   aList.append(action("DELETE_CMD"));
323   aList.append(action("MOVE_CMD"));
324   aList.append(mySeparator);
325   aList.append(action("RENAME_CMD"));
326   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
327
328   aList.clear();
329   aList.append(action("DELETE_CMD"));
330   aList.append(mySeparator);
331   aList.append(action("RENAME_CMD"));
332   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
333   //-------------------------------------
334 }
335
336 void XGUI_ContextMenuMgr::buildViewerMenu()
337 {
338   QActionsList aList;
339   // Result construction menu
340   aList.append(action("HIDE_CMD"));
341   aList.append(action("SHOW_ONLY_CMD"));
342   aList.append(action("HIDEALL_CMD"));
343   aList.append(action("COLOR_CMD"));
344   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
345   // Result part menu
346   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
347   //-------------------------------------
348   // Result body menu
349   aList.clear();
350   aList.append(action("WIREFRAME_CMD"));
351   aList.append(action("SHADING_CMD"));
352   aList.append(action("COLOR_CMD"));
353   aList.append(mySeparator);
354   aList.append(action("HIDE_CMD"));
355   aList.append(action("SHOW_ONLY_CMD"));
356   aList.append(action("HIDEALL_CMD"));
357   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
358   // Group menu
359   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
360   //-------------------------------------
361
362 }
363
364
365 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
366 {
367   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
368   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
369   int aSelected = aObjects.size();
370   QActionsList aActions;
371   if (aSelected == 1) {
372     ObjectPtr aObject = aObjects.first();
373     std::string aName = aObject->groupName();
374     if (myObjBrowserMenus.contains(aName))
375       aActions = myObjBrowserMenus[aName];
376   } else if (aSelected > 1) {
377       aActions.append(action("SHADING_CMD"));
378       aActions.append(action("WIREFRAME_CMD"));
379       aActions.append(mySeparator);
380       aActions.append(action("SHOW_CMD"));
381       aActions.append(action("HIDE_CMD"));
382       aActions.append(action("SHOW_ONLY_CMD"));
383       aActions.append(mySeparator);
384       aActions.append(action("DELETE_CMD"));
385       //aActions.append(action("MOVE_CMD"));
386   }
387   theMenu->addActions(aActions);
388
389   ModuleBase_IModule* aModule = myWorkshop->module();
390   if (aModule) {
391     theMenu->addSeparator();
392     aModule->addObjectBrowserMenu(theMenu);
393   }
394   theMenu->addSeparator();
395   theMenu->addActions(myWorkshop->objectBrowser()->actions());
396 }
397
398 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
399 {
400   ModuleBase_IModule* aModule = myWorkshop->module();
401   if (aModule) {
402     if (aModule->addViewerMenu(theMenu, myActions))
403       theMenu->addSeparator();
404   }
405   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
406   QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
407   int aSelected = aPrsList.size();
408   QActionsList aActions;
409   if (aSelected == 1) {
410     ObjectPtr aObject = aPrsList.first().object();
411     std::string aName = aObject->groupName();
412     if (myViewerMenu.contains(aName))
413       aActions = myViewerMenu[aName];
414   } else if (aSelected > 1) {
415     aActions.append(action("HIDE_CMD"));
416   }
417   theMenu->addActions(aActions);
418
419   if (!myWorkshop->isSalomeMode()) {
420     theMenu->addSeparator();
421     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
422     if (aMDI->actions().size() > 0) {
423       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
424       aSubMenu->addActions(aMDI->actions());
425     }
426   }
427 }
428
429 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
430 {
431   QStringList aGroups;
432
433   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
434                                                   aLast = myObjBrowserMenus.end();
435   for (; anIt != aLast; anIt++) {
436     QString aGroupName(anIt.key().c_str());
437     if (aGroups.contains(aGroupName))
438       continue;
439     QActionsList anActions = anIt.value();
440     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
441     bool aFound = false;
442     for (; anAIt != anALast && !aFound; anAIt++)
443       aFound = (*anAIt)->data().toString() == theName;
444     if (aFound)
445       aGroups.append(aGroupName);
446   }
447   return aGroups;
448 }
449
450 void XGUI_ContextMenuMgr::onRename()
451 {
452   myWorkshop->objectBrowser()->onEditItem();
453 }