Salome HOME
GUI for extrusion/revolution features.
[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 #include "XGUI_Tools.h"
12
13 #include <AppElements_MainWindow.h>
14
15 //#include "PartSetPlugin_Part.h"
16
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_AttributeDocRef.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_ResultPart.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_ResultGroup.h>
23 #include <ModelAPI_ResultParameter.h>
24
25 #include <ModuleBase_IModule.h>
26
27 #include <QAction>
28 #include <QContextMenuEvent>
29 #include <QMenu>
30 #include <QMdiArea>
31
32 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
33     : QObject(theParent),
34       myWorkshop(theParent)
35 {
36 }
37
38 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
39 {
40 }
41
42 void XGUI_ContextMenuMgr::createActions()
43 {
44   QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
45   addAction("EDIT_CMD", aAction);
46
47   aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
48   addAction("ACTIVATE_PART_CMD", aAction);
49
50   aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
51   addAction("DEACTIVATE_PART_CMD", aAction);
52
53   aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
54   QMainWindow* aDesktop = myWorkshop->mainWindow();
55   if (!aDesktop)
56     aDesktop = myWorkshop->salomeConnector()->desktop();
57   aDesktop->addAction(aAction);
58
59   addAction("DELETE_CMD", aAction);
60   aAction->setShortcut(Qt::Key_Delete);
61   aAction->setShortcutContext(Qt::ApplicationShortcut);
62
63   aAction = new QAction(QIcon(":pictures/color.png"), tr("Color"), this);
64   addAction("COLOR_CMD", aAction);
65
66   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
67   addAction("SHOW_CMD", aAction);
68
69   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
70   addAction("SHOW_ONLY_CMD", aAction);
71
72   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
73   addAction("HIDE_CMD", aAction);
74
75   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
76   addAction("HIDEALL_CMD", aAction);
77
78   aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
79   addAction("SHADING_CMD", aAction);
80
81   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
82   addAction("WIREFRAME_CMD", aAction);
83 }
84
85 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
86 {
87   if (myActions.contains(theId))
88     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
89   theAction->setData(theId);
90   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
91   myActions[theId] = theAction;
92 }
93
94 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
95 {
96   if (myActions.contains(theId))
97     return myActions[theId];
98   return 0;
99 }
100
101 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
102 {
103   foreach(QAction* eachAction, myActions) {
104     if (eachAction->text() == theName) {
105       return eachAction;
106     }
107   }
108   return NULL;
109 }
110
111 QStringList XGUI_ContextMenuMgr::actionIds() const
112 {
113   return myActions.keys();
114 }
115
116 void XGUI_ContextMenuMgr::onAction(bool isChecked)
117 {
118   QAction* aAction = static_cast<QAction*>(sender());
119   emit actionTriggered(aAction->data().toString(), isChecked);
120 }
121
122 void XGUI_ContextMenuMgr::updateCommandsStatus()
123 {
124 }
125
126 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
127 {
128   QMenu* aMenu = 0;
129   if (sender() == myWorkshop->objectBrowser())
130     aMenu = objectBrowserMenu();
131   else if (sender() == myWorkshop->viewer()) {
132     aMenu = viewerMenu();
133   }
134
135   if (aMenu && (aMenu->actions().size() > 0)) {
136     // it is possible that some objects should do something before and after the popup menu exec
137     // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
138     // a created entity
139     emit beforeContextMenu();
140     aMenu->exec(theEvent->globalPos());
141     emit afterContextMenu();
142     delete aMenu;
143   }
144 }
145
146 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
147 {
148   QMenu* aMenu = new QMenu();
149   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
150   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
151   int aSelected = aObjects.size();
152   if (aSelected > 0) {
153     SessionPtr aMgr = ModelAPI_Session::get();
154     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
155     bool hasResult = false;
156     bool hasFeature = false;
157     bool hasParameter = false;
158     XGUI_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
159
160     //Process Feature
161     if (aSelected == 1) {
162       ObjectPtr aObject = aObjects.first();
163       if (aObject) {
164         ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
165         if (aPart) {
166           if (aMgr->activeDocument() == aPart->partDoc())
167             aMenu->addAction(action("DEACTIVATE_PART_CMD"));
168           else
169             aMenu->addAction(action("ACTIVATE_PART_CMD"));
170         } else if (hasFeature && aObject->document() == aMgr->activeDocument()) {
171           aMenu->addAction(action("EDIT_CMD"));
172         } else {
173           if (aDisplayer->isVisible(aObject)) {
174             if (aDisplayer->canBeShaded(aObject)) {
175               if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
176                 aMenu->addAction(action("WIREFRAME_CMD"));
177               else
178                 aMenu->addAction(action("SHADING_CMD"));
179             }
180             aMenu->addSeparator();
181             aMenu->addAction(action("HIDE_CMD"));
182           } else if (!hasParameter) {
183             aMenu->addAction(action("SHOW_CMD"));
184           }
185           if (hasParameter)
186             aMenu->addAction(action("EDIT_CMD"));
187           else
188             aMenu->addAction(action("SHOW_ONLY_CMD"));
189         }
190       } else {  // If feature is 0 the it means that selected root object (document)
191         if (aMgr->activeDocument() != aMgr->moduleDocument())
192           aMenu->addAction(action("ACTIVATE_PART_CMD"));
193       }
194     } else {
195       if (hasResult && (!hasParameter)) {
196         aMenu->addAction(action("SHOW_CMD"));
197         aMenu->addAction(action("HIDE_CMD"));
198         aMenu->addAction(action("SHOW_ONLY_CMD"));
199         aMenu->addSeparator();
200         aMenu->addAction(action("SHADING_CMD"));
201         aMenu->addAction(action("WIREFRAME_CMD"));
202       }
203     }
204     if (hasFeature || hasParameter)
205       aMenu->addAction(action("DELETE_CMD"));
206   }
207   if (myWorkshop->canChangeColor())
208     aMenu->addAction(action("COLOR_CMD"));
209
210   aMenu->addSeparator();
211   aMenu->addActions(myWorkshop->objectBrowser()->actions());
212
213   ModuleBase_IModule* aModule = myWorkshop->module();
214   if (aModule)
215     aModule->addObjectBrowserItems(aMenu);
216
217   if (aMenu->actions().size() > 0) {
218     return aMenu;
219   }
220   delete aMenu;
221   return 0;
222 }
223
224 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
225 {
226   QMenu* aMenu = new QMenu();
227   addViewerItems(aMenu);
228   if (aMenu->actions().size() > 0) {
229     return aMenu;
230   }
231   delete aMenu;
232   return 0;
233 }
234
235 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
236 {
237   bool aIsDone = false;
238   ModuleBase_IModule* aModule = myWorkshop->module();
239   if (aModule) 
240     aIsDone = aModule->addViewerItems(theMenu, myActions);
241
242   if (!aIsDone) {
243     XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
244     QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
245     if (aObjects.size() > 0) {
246       //if (aObjects.size() == 1)
247       //  theMenu->addAction(action("EDIT_CMD"));
248       bool isVisible = false;
249       bool isShading = false;
250       bool canBeShaded = false;
251       foreach(ObjectPtr aObject, aObjects)
252       {
253         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
254         if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
255           isVisible = true;
256           canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
257           isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
258           break;
259         }
260       }
261       if (isVisible) {
262         if (canBeShaded) {
263           if (isShading)
264             theMenu->addAction(action("WIREFRAME_CMD"));
265           else
266             theMenu->addAction(action("SHADING_CMD"));
267         }
268         theMenu->addSeparator();
269         theMenu->addAction(action("SHOW_ONLY_CMD"));
270         theMenu->addAction(action("HIDE_CMD"));
271       } else
272         theMenu->addAction(action("SHOW_CMD"));
273       //theMenu->addAction(action("DELETE_CMD"));
274     }
275     if (myWorkshop->displayer()->objectsCount() > 0)
276       theMenu->addAction(action("HIDEALL_CMD"));
277     if (myWorkshop->canChangeColor())
278       theMenu->addAction(action("COLOR_CMD"));
279   }
280   if (!myWorkshop->isSalomeMode()) {
281     theMenu->addSeparator();
282     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
283     if (aMDI->actions().size() > 0) {
284       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
285       aSubMenu->addActions(aMDI->actions());
286     }
287   }
288
289 }
290
291 void XGUI_ContextMenuMgr::connectObjectBrowser() const
292 {
293   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
294           SLOT(onContextMenuRequest(QContextMenuEvent*)));
295 }
296
297 void XGUI_ContextMenuMgr::connectViewer() const
298 {
299   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
300           SLOT(onContextMenuRequest(QContextMenuEvent*)));
301 }
302