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