]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ContextMenuMgr.cpp
Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[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   QList<ObjectPtr> 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     bool hasGroup = false;
125     foreach(ObjectPtr aObj, aObjects)
126     {
127       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
128       ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
129       ResultGroupPtr aGroupRes = boost::dynamic_pointer_cast<ModelAPI_ResultGroup>(aObj);
130       if (aResult)
131         hasResult = true;
132       if (aFeature)
133         hasFeature = true;
134       if (aGroupRes)
135         hasGroup = true;
136       if (hasFeature && hasResult && hasGroup)
137         break;
138     }
139     //Process Feature
140     if (!hasGroup) {
141       if (aSelected == 1) {
142         ObjectPtr aObject = aObjects.first();
143         if (aObject) {
144           ResultPartPtr aPart = boost::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) {
151             aMenu->addAction(action("EDIT_CMD"));
152           } else {
153             if (aDisplayer->isVisible(aObject)) {
154               if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
155                 aMenu->addAction(action("WIREFRAME_CMD"));
156               else
157                 aMenu->addAction(action("SHADING_CMD"));
158               aMenu->addSeparator();
159               aMenu->addAction(action("HIDE_CMD"));
160             } else {
161               aMenu->addAction(action("SHOW_CMD"));
162             }
163             aMenu->addAction(action("SHOW_ONLY_CMD"));
164           }
165         } else {  // If feature is 0 the it means that selected root object (document)
166           if (aMgr->activeDocument() != aMgr->moduleDocument())
167             aMenu->addAction(action("ACTIVATE_PART_CMD"));
168         }
169       } else {
170         if (hasResult) {
171           aMenu->addAction(action("SHOW_CMD"));
172           aMenu->addAction(action("HIDE_CMD"));
173           aMenu->addAction(action("SHOW_ONLY_CMD"));
174           aMenu->addSeparator();
175           aMenu->addAction(action("SHADING_CMD"));
176           aMenu->addAction(action("WIREFRAME_CMD"));
177         }
178       }
179       if (hasFeature)
180         aMenu->addAction(action("DELETE_CMD"));
181     }
182   }
183   aMenu->addSeparator();
184   aMenu->addActions(myWorkshop->objectBrowser()->actions());
185   if (aMenu->actions().size() > 0) {
186     return aMenu;
187   }
188   delete aMenu;
189   return 0;
190 }
191
192 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
193 {
194   QMenu* aMenu = new QMenu();
195   addViewerItems(aMenu);
196   if (aMenu->actions().size() > 0) {
197     return aMenu;
198   }
199   delete aMenu;
200   return 0;
201 }
202
203 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
204 {
205   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
206   QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
207   if (aObjects.size() > 0) {
208     //if (aObjects.size() == 1)
209     //  theMenu->addAction(action("EDIT_CMD"));
210     bool isVisible = false;
211     bool isShading = false;
212     foreach(ObjectPtr aObject, aObjects)
213     {
214       ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
215       if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
216         isVisible = true;
217         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
218         break;
219       }
220     }
221     if (isVisible) {
222       if (isShading)
223         theMenu->addAction(action("WIREFRAME_CMD"));
224       else
225         theMenu->addAction(action("SHADING_CMD"));
226       theMenu->addSeparator();
227       theMenu->addAction(action("SHOW_ONLY_CMD"));
228       theMenu->addAction(action("HIDE_CMD"));
229     } else
230       theMenu->addAction(action("SHOW_CMD"));
231     //theMenu->addAction(action("DELETE_CMD"));
232   }
233   if (myWorkshop->displayer()->objectsCount() > 0)
234     theMenu->addAction(action("HIDEALL_CMD"));
235   if (!myWorkshop->isSalomeMode()) {
236     theMenu->addSeparator();
237     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
238     if (aMDI->actions().size() > 0) {
239       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
240       aSubMenu->addActions(aMDI->actions());
241     }
242   }
243 }
244
245 void XGUI_ContextMenuMgr::connectObjectBrowser() const
246 {
247   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
248           SLOT(onContextMenuRequest(QContextMenuEvent*)));
249 }
250
251 void XGUI_ContextMenuMgr::connectViewer() const
252 {
253   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
254           SLOT(onContextMenuRequest(QContextMenuEvent*)));
255 }
256