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
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
36 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
37 {
38   if (myActions.contains(theId))
39     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
40   theAction->setData(theId);
41   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
42   myActions[theId] = theAction;
43 }
44
45 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
46 {
47   if (myActions.contains(theId))
48     return myActions[theId];
49   return 0;
50 }
51
52 QStringList XGUI_ContextMenuMgr::actionIds() const
53 {
54   return myActions.keys();
55 }
56
57 void XGUI_ContextMenuMgr::onAction(bool isChecked)
58 {
59   QAction* aAction = static_cast<QAction*>(sender());
60   emit actionTriggered(aAction->data().toString(), isChecked);
61 }
62
63 void XGUI_ContextMenuMgr::updateCommandsStatus()
64 {
65 }
66
67 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
68 {
69   QMenu* aMenu = 0;
70   if (sender() == myWorkshop->objectBrowser())
71     aMenu = objectBrowserMenu();
72
73   if (aMenu) {
74     aMenu->exec(theEvent->globalPos());
75     delete aMenu;
76   }
77 }
78
79 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
80 {
81   QList<QAction*> aActions;
82   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
83   QFeatureList aFeatures = aSelMgr->selectedFeatures();
84   if (aFeatures.size() == 1) {
85     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
86     FeaturePtr aFeature = aFeatures.first();
87     //Process Feature
88     if (aFeature) {
89       if (aFeature->getKind() == "Part") {
90         boost::shared_ptr<ModelAPI_Document> aFeaDoc = aFeature->data()->docRef("PartDocument")->value();
91         if (aMgr->currentDocument() == aFeaDoc)
92           aActions.append(action("DEACTIVATE_PART_CMD"));
93         else 
94           aActions.append(action("ACTIVATE_PART_CMD"));
95       } else {
96         aActions.append(action("EDIT_CMD"));
97       }
98
99     // Process Root object (document)
100     } else { // If feature is 0 the it means that selected root object (document)
101       if (aMgr->currentDocument() != aMgr->rootDocument()) {
102         aActions.append(action("ACTIVATE_PART_CMD"));
103       }
104     }
105   }
106   if (aActions.size() > 0) {
107     QMenu* aMenu = new QMenu();
108     aMenu->addActions(aActions);
109     return aMenu;
110   }
111   return 0;
112 }
113
114 void XGUI_ContextMenuMgr::connectObjectBrowser() const
115 {
116   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
117     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
118 }