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