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