Salome HOME
Merge remote-tracking branch 'remotes/origin/SketchSolver_Linux'
[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
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDocRef.h>
9
10 #include <QAction>
11 #include <QContextMenuEvent>
12 #include <QMenu>
13
14 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
15 QObject(theParent), myWorkshop(theParent)
16 {
17
18 }
19
20 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
21 {
22 }
23
24 void XGUI_ContextMenuMgr::createActions()
25 {
26   QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
27   addAction("EDIT_CMD", aAction);
28
29   aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
30   addAction("ACTIVATE_PART_CMD", aAction);
31
32   aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
33   addAction("DEACTIVATE_PART_CMD", aAction);
34
35   aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
36   addAction("DELETE_CMD", aAction);
37 }
38
39 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
40 {
41   if (myActions.contains(theId))
42     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
43   theAction->setData(theId);
44   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
45   myActions[theId] = theAction;
46 }
47
48 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
49 {
50   if (myActions.contains(theId))
51     return myActions[theId];
52   return 0;
53 }
54
55 QStringList XGUI_ContextMenuMgr::actionIds() const
56 {
57   return myActions.keys();
58 }
59
60 void XGUI_ContextMenuMgr::onAction(bool isChecked)
61 {
62   QAction* aAction = static_cast<QAction*>(sender());
63   emit actionTriggered(aAction->data().toString(), isChecked);
64 }
65
66 void XGUI_ContextMenuMgr::updateCommandsStatus()
67 {
68 }
69
70 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
71 {
72   QMenu* aMenu = 0;
73   if (sender() == myWorkshop->objectBrowser())
74     aMenu = objectBrowserMenu();
75
76   if (aMenu) {
77     aMenu->exec(theEvent->globalPos());
78     delete aMenu;
79   }
80 }
81
82 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
83 {
84   QList<QAction*> aActions;
85   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
86   QFeatureList aFeatures = aSelMgr->selectedFeatures();
87   if (aFeatures.size() == 1) {
88     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
89     FeaturePtr aFeature = aFeatures.first();
90     //Process Feature
91     if (aFeature) {
92       if (aFeature->getKind() == "Part") {
93         boost::shared_ptr<ModelAPI_Document> aFeaDoc = aFeature->data()->docRef("PartDocument")->value();
94         if (aMgr->currentDocument() == aFeaDoc)
95           aActions.append(action("DEACTIVATE_PART_CMD"));
96         else 
97           aActions.append(action("ACTIVATE_PART_CMD"));
98       } else {
99         aActions.append(action("EDIT_CMD"));
100       }
101       aActions.append(action("DELETE_CMD"));
102
103     // Process Root object (document)
104     } else { // If feature is 0 the it means that selected root object (document)
105       if (aMgr->currentDocument() != aMgr->rootDocument()) {
106         aActions.append(action("ACTIVATE_PART_CMD"));
107       }
108     }
109   }
110   aActions.append(myWorkshop->objectBrowser()->actions());
111   if (aActions.size() > 0) {
112     QMenu* aMenu = new QMenu();
113     aMenu->addActions(aActions);
114     return aMenu;
115   }
116   return 0;
117 }
118
119 void XGUI_ContextMenuMgr::connectObjectBrowser() const
120 {
121   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
122     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
123 }