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