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