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