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