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