Salome HOME
Merge branch 'master' of newgeom:newgeom
[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/shading.png"), tr("Shading"), this);
56   addAction("SHADING_CMD", aAction);
57
58   aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
59   addAction("WIREFRAME_CMD", aAction);
60 }
61
62 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
63 {
64   if (myActions.contains(theId))
65     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
66   theAction->setData(theId);
67   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
68   myActions[theId] = theAction;
69 }
70
71 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
72 {
73   if (myActions.contains(theId))
74     return myActions[theId];
75   return 0;
76 }
77
78 QStringList XGUI_ContextMenuMgr::actionIds() const
79 {
80   return myActions.keys();
81 }
82
83 void XGUI_ContextMenuMgr::onAction(bool isChecked)
84 {
85   QAction* aAction = static_cast<QAction*>(sender());
86   emit actionTriggered(aAction->data().toString(), isChecked);
87 }
88
89 void XGUI_ContextMenuMgr::updateCommandsStatus()
90 {
91 }
92
93 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
94 {
95   QMenu* aMenu = 0;
96   if (sender() == myWorkshop->objectBrowser())
97     aMenu = objectBrowserMenu();
98   else if (sender() == myWorkshop->viewer()) {
99     aMenu = viewerMenu();
100   }
101
102   if (aMenu && (aMenu->actions().size() > 0)) {
103     aMenu->exec(theEvent->globalPos());
104     delete aMenu;
105   }
106 }
107
108 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
109 {
110   QMenu* aMenu = new QMenu();
111   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
112   QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
113   int aSelected = aObjects.size();
114   if (aSelected > 0) {
115     SessionPtr aMgr = ModelAPI_Session::get();
116     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
117     bool hasResult = false;
118     bool hasFeature = false;
119     foreach(ObjectPtr aObj, aObjects)
120     {
121       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
122       ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
123       if (aResult)
124         hasResult = true;
125       if (aFeature)
126         hasFeature = true;
127       if (hasFeature && hasResult)
128         break;
129     }
130     //Process Feature
131     if (aSelected == 1) {
132       ObjectPtr aObject = aObjects.first();
133       if (aObject) {
134         ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
135         if (aPart) {
136           if (aMgr->activeDocument() == aPart->partDoc())
137             aMenu->addAction(action("DEACTIVATE_PART_CMD"));
138           else
139             aMenu->addAction(action("ACTIVATE_PART_CMD"));
140         } else if (hasFeature) {
141           aMenu->addAction(action("EDIT_CMD"));
142         } else {
143           if (aDisplayer->isVisible(aObject)) {
144             aMenu->addAction(action("HIDE_CMD"));
145             if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
146               aMenu->addAction(action("WIREFRAME_CMD"));
147             else
148               aMenu->addAction(action("SHADING_CMD"));
149           } else {
150             aMenu->addAction(action("SHOW_CMD"));
151           }
152           aMenu->addAction(action("SHOW_ONLY_CMD"));
153         }
154       } else {  // If feature is 0 the it means that selected root object (document)
155         if (aMgr->activeDocument() != aMgr->moduleDocument())
156           aMenu->addAction(action("ACTIVATE_PART_CMD"));
157       }
158     } else {
159       if (hasResult) {
160         aMenu->addAction(action("SHOW_CMD"));
161         aMenu->addAction(action("HIDE_CMD"));
162         aMenu->addAction(action("SHOW_ONLY_CMD"));
163         aMenu->addSeparator();
164         aMenu->addAction(action("SHADING_CMD"));
165         aMenu->addAction(action("WIREFRAME_CMD"));
166       }
167     }
168     if (hasFeature)
169       aMenu->addAction(action("DELETE_CMD"));
170   }
171   aMenu->addActions(myWorkshop->objectBrowser()->actions());
172   if (aMenu->actions().size() > 0) {
173     return aMenu;
174   }
175   delete aMenu;
176   return 0;
177 }
178
179 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
180 {
181   QMenu* aMenu = new QMenu();
182   addViewerItems(aMenu);
183   if (aMenu->actions().size() > 0) {
184     return aMenu;
185   }
186   delete aMenu;
187   return 0;
188 }
189
190 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
191 {
192   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
193   QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
194   if (aObjects.size() > 0) {
195     //if (aObjects.size() == 1)
196     //  theMenu->addAction(action("EDIT_CMD"));
197     bool isVisible = false;
198     bool isShading = false;
199     foreach(ObjectPtr aObject, aObjects)
200     {
201       ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
202       if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
203         isVisible = true;
204         isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);      
205         break;
206       }
207     }
208     if (isVisible) {
209       theMenu->addAction(action("HIDE_CMD"));
210       if (isShading)
211         theMenu->addAction(action("WIREFRAME_CMD"));
212       else
213         theMenu->addAction(action("SHADING_CMD"));
214     } else
215       theMenu->addAction(action("SHOW_CMD"));
216     //theMenu->addAction(action("DELETE_CMD"));
217   }
218   if (!myWorkshop->isSalomeMode()) {
219     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
220     if (aMDI->actions().size() > 0) {
221       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
222       aSubMenu->addActions(aMDI->actions());
223     }
224   }
225 }
226
227 void XGUI_ContextMenuMgr::connectObjectBrowser() const
228 {
229   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
230           SLOT(onContextMenuRequest(QContextMenuEvent*)));
231 }
232
233 void XGUI_ContextMenuMgr::connectViewer() const
234 {
235   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
236           SLOT(onContextMenuRequest(QContextMenuEvent*)));
237 }
238