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