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