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