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