Salome HOME
a02f1d2dc397bc3a573ad4e54727067e52ba7883
[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_Viewer.h"
9
10 #include "PartSetPlugin_Part.h"
11
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_Object.h>
15
16 #include <QAction>
17 #include <QContextMenuEvent>
18 #include <QMenu>
19 #include <QMdiArea>
20
21 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
22 QObject(theParent), myWorkshop(theParent)
23 {
24 }
25
26 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
27 {
28 }
29
30 void XGUI_ContextMenuMgr::createActions()
31 {
32   QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
33   addAction("EDIT_CMD", aAction);
34
35   aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
36   addAction("ACTIVATE_PART_CMD", aAction);
37
38   aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
39   addAction("DEACTIVATE_PART_CMD", aAction);
40
41   aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
42   addAction("DELETE_CMD", aAction);
43
44   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
45   addAction("SHOW_CMD", aAction);
46
47   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
48   addAction("HIDE_CMD", aAction);
49 }
50
51 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
52 {
53   if (myActions.contains(theId))
54     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
55   theAction->setData(theId);
56   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
57   myActions[theId] = theAction;
58 }
59
60 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
61 {
62   if (myActions.contains(theId))
63     return myActions[theId];
64   return 0;
65 }
66
67 QStringList XGUI_ContextMenuMgr::actionIds() const
68 {
69   return myActions.keys();
70 }
71
72 void XGUI_ContextMenuMgr::onAction(bool isChecked)
73 {
74   QAction* aAction = static_cast<QAction*>(sender());
75   emit actionTriggered(aAction->data().toString(), isChecked);
76 }
77
78 void XGUI_ContextMenuMgr::updateCommandsStatus()
79 {
80 }
81
82 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
83 {
84   QMenu* aMenu = 0;
85   if (sender() == myWorkshop->objectBrowser())
86     aMenu = objectBrowserMenu();
87   else if (sender() == myWorkshop->mainWindow()->viewer()) {
88     aMenu = viewerMenu();
89   }
90   
91   if (aMenu && (aMenu->actions().size() > 0)) {
92     aMenu->exec(theEvent->globalPos());
93     delete aMenu;
94   }
95 }
96
97 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
98 {
99   QMenu* aMenu = new QMenu();
100   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
101   QFeatureList aFeatures = aSelMgr->selectedFeatures();
102   if (aFeatures.size() == 1) {
103     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
104     FeaturePtr aFeature = aFeatures.first();
105     //Process Feature
106     if (aFeature) {
107       if (aFeature->getKind() == PARTSET_PART_KIND) {
108         ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
109         DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
110         if (aMgr->currentDocument() == aFeaDoc)
111           aMenu->addAction(action("DEACTIVATE_PART_CMD"));
112         else 
113           aMenu->addAction(action("ACTIVATE_PART_CMD"));
114       } else {
115         aMenu->addAction(action("EDIT_CMD"));
116
117         XGUI_Displayer* aDisplayer = myWorkshop->displayer();
118         if (aDisplayer->isVisible(aFeature))
119           aMenu->addAction(action("HIDE_CMD"));
120         else
121           aMenu->addAction(action("SHOW_CMD"));
122       }
123       aMenu->addAction(action("DELETE_CMD"));
124       aMenu->addSeparator();
125
126     // Process Root object (document)
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     }
132   }
133   aMenu->addActions(myWorkshop->objectBrowser()->actions());
134   if (aMenu->actions().size() > 0) {
135     return aMenu;
136   }
137   delete aMenu;
138   return 0;
139 }
140
141 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
142 {
143   QMenu* aMenu = new QMenu();
144   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
145   QFeatureList aFeatures = aSelMgr->selectedFeatures();
146   if (aFeatures.size() > 0) {
147     if (aFeatures.size() > 0)
148       aMenu->addAction(action("EDIT_CMD"));
149     aMenu->addAction(action("HIDE_CMD"));
150     aMenu->addAction(action("DELETE_CMD"));
151   }
152   QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
153   if (aMDI->actions().size() > 0) {
154     QMenu* aSubMenu = aMenu->addMenu(tr("Windows"));
155     aSubMenu->addActions(aMDI->actions());
156   }
157   if (aMenu->actions().size() > 0) {
158     return aMenu;
159   }
160   delete aMenu;
161   return 0;
162 }
163
164 void XGUI_ContextMenuMgr::connectObjectBrowser() const
165 {
166   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
167     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
168 }
169
170 void XGUI_ContextMenuMgr::connectViewer() const
171 {
172   // TODO: Adapt to SALOME mode
173   connect(myWorkshop->mainWindow()->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
174     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
175 }