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