Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <ModelAPI_ResultPart.h>
17
18 #include <QAction>
19 #include <QContextMenuEvent>
20 #include <QMenu>
21 #include <QMdiArea>
22
23 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
24 QObject(theParent), 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   if (aObjects.size() == 1) {
105     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
106     ObjectPtr aObject = aObjects.first();
107     //Process Feature
108     if (aObject) {
109       ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
110       if (aPart) {
111         if (aMgr->currentDocument() == aPart->partDoc())
112           aMenu->addAction(action("DEACTIVATE_PART_CMD"));
113         else 
114           aMenu->addAction(action("ACTIVATE_PART_CMD"));
115       } else {
116         ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
117         if (aResult) {
118           XGUI_Displayer* aDisplayer = myWorkshop->displayer();
119           if (aDisplayer->isVisible(aResult))
120             aMenu->addAction(action("HIDE_CMD"));
121           else
122             aMenu->addAction(action("SHOW_CMD"));
123         } else {
124           FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
125           if (aFeature) {
126             aMenu->addAction(action("EDIT_CMD"));
127             aMenu->addAction(action("DELETE_CMD"));
128           }
129         }
130       }
131       aMenu->addSeparator();
132
133     // Process Root object (document)
134     } else { // If feature is 0 the it means that selected root object (document)
135       if (aMgr->currentDocument() != aMgr->rootDocument()) {
136         aMenu->addAction(action("ACTIVATE_PART_CMD"));
137       }
138     }
139   }
140   aMenu->addActions(myWorkshop->objectBrowser()->actions());
141   if (aMenu->actions().size() > 0) {
142     return aMenu;
143   }
144   delete aMenu;
145   return 0;
146 }
147
148 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
149 {
150   QMenu* aMenu = new QMenu();
151   addViewerItems(aMenu);
152   if (aMenu->actions().size() > 0) {
153     return aMenu;
154   }
155   delete aMenu;
156   return 0;
157 }
158
159 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
160 {
161   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
162   QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
163   if (aObjects.size() > 0) {
164     //if (aObjects.size() == 1)
165     //  theMenu->addAction(action("EDIT_CMD"));
166     bool isVisible = false;
167     foreach(ObjectPtr aObject, aObjects) {
168       ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
169       if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
170         isVisible = true;
171         break;
172       }
173     }
174     if (isVisible)
175       theMenu->addAction(action("HIDE_CMD"));
176     else 
177       theMenu->addAction(action("SHOW_CMD"));
178     //theMenu->addAction(action("DELETE_CMD"));
179   }
180   if (!myWorkshop->isSalomeMode()) {
181     QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
182     if (aMDI->actions().size() > 0) {
183       QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
184       aSubMenu->addActions(aMDI->actions());
185     }
186   }
187 }
188
189 void XGUI_ContextMenuMgr::connectObjectBrowser() const
190 {
191   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
192     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
193 }
194
195 void XGUI_ContextMenuMgr::connectViewer() const
196 {
197   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
198     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
199 }
200