Salome HOME
ee93708255b494dff5e60eb125f0ceabf40ad29a
[modules/shaper.git] / src / XGUI / XGUI_ContextMenuMgr.cpp
1
2 #include "XGUI_ContextMenuMgr.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_ObjectsBrowser.h"
5 #include "XGUI_SelectionMgr.h"
6 #include "XGUI_Displayer.h"
7 #include "XGUI_MainWindow.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Selection.h"
10
11 #include "PartSetPlugin_Part.h"
12
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <ModelAPI_Object.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), myWorkshop(theParent)
24 {
25 }
26
27 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
28 {
29 }
30
31 void XGUI_ContextMenuMgr::createActions()
32 {
33   QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
34   addAction("EDIT_CMD", aAction);
35
36   aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
37   addAction("ACTIVATE_PART_CMD", aAction);
38
39   aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
40   addAction("DEACTIVATE_PART_CMD", aAction);
41
42   aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
43   addAction("DELETE_CMD", aAction);
44
45   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
46   addAction("SHOW_CMD", aAction);
47
48   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
49   addAction("HIDE_CMD", aAction);
50 }
51
52 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
53 {
54   if (myActions.contains(theId))
55     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
56   theAction->setData(theId);
57   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
58   myActions[theId] = theAction;
59 }
60
61 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
62 {
63   if (myActions.contains(theId))
64     return myActions[theId];
65   return 0;
66 }
67
68 QStringList XGUI_ContextMenuMgr::actionIds() const
69 {
70   return myActions.keys();
71 }
72
73 void XGUI_ContextMenuMgr::onAction(bool isChecked)
74 {
75   QAction* aAction = static_cast<QAction*>(sender());
76   emit actionTriggered(aAction->data().toString(), isChecked);
77 }
78
79 void XGUI_ContextMenuMgr::updateCommandsStatus()
80 {
81 }
82
83 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
84 {
85   QMenu* aMenu = 0;
86   if (sender() == myWorkshop->objectBrowser())
87     aMenu = objectBrowserMenu();
88   else if (sender() == myWorkshop->viewer()) {
89     aMenu = viewerMenu();
90   }
91   
92   if (aMenu && (aMenu->actions().size() > 0)) {
93     aMenu->exec(theEvent->globalPos());
94     delete aMenu;
95   }
96 }
97
98 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
99 {
100   QMenu* aMenu = new QMenu();
101   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
102   QList<ObjectPtr> aFeatures = aSelMgr->selection()->selectedObjects();
103   if (aFeatures.size() == 1) {
104     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
105     FeaturePtr aFeature = aFeatures.first();
106     //Process Feature
107     if (aFeature) {
108       if (aFeature->getKind() == PARTSET_PART_KIND) {
109         ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
110         DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
111         if (aMgr->currentDocument() == aFeaDoc)
112           aMenu->addAction(action("DEACTIVATE_PART_CMD"));
113         else 
114           aMenu->addAction(action("ACTIVATE_PART_CMD"));
115       } else {
116         aMenu->addAction(action("EDIT_CMD"));
117
118         XGUI_Displayer* aDisplayer = myWorkshop->displayer();
119         if (aDisplayer->isVisible(aFeature))
120           aMenu->addAction(action("HIDE_CMD"));
121         else
122           aMenu->addAction(action("SHOW_CMD"));
123       }
124       aMenu->addAction(action("DELETE_CMD"));
125       aMenu->addSeparator();
126
127     // Process Root object (document)
128     } else { // If feature is 0 the it means that selected root object (document)
129       if (aMgr->currentDocument() != aMgr->rootDocument()) {
130         aMenu->addAction(action("ACTIVATE_PART_CMD"));
131       }
132     }
133   }
134   aMenu->addActions(myWorkshop->objectBrowser()->actions());
135   if (aMenu->actions().size() > 0) {
136     return aMenu;
137   }
138   delete aMenu;
139   return 0;
140 }
141
142 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
143 {
144   QMenu* aMenu = new QMenu();
145   addViewerItems(aMenu);
146   if (aMenu->actions().size() > 0) {
147     return aMenu;
148   }
149   delete aMenu;
150   return 0;
151 }
152
153 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
154 {
155   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
156   QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
157   if (aObjects.size() > 0) {
158     if (aObjects.size() == 1)
159       theMenu->addAction(action("EDIT_CMD"));
160     bool isVisible = false;
161     foreach(ObjectPtr aObject, aObjects) {
162       ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
163       if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
164         isVisible = true;
165         break;
166       }
167     }
168     if (isVisible)
169       theMenu->addAction(action("HIDE_CMD"));
170     else 
171       theMenu->addAction(action("SHOW_CMD"));
172     theMenu->addAction(action("DELETE_CMD"));
173   }
174   if (!myWorkshop->isSalomeMode()) {
175     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
176     if (aMDI->actions().size() > 0) {
177       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
178       aSubMenu->addActions(aMDI->actions());
179     }
180   }
181 }
182
183 void XGUI_ContextMenuMgr::connectObjectBrowser() const
184 {
185   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
186     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
187 }
188
189 void XGUI_ContextMenuMgr::connectViewer() const
190 {
191   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
192     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
193 }
194