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