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