Salome HOME
753027907a5a02784ae85c35dc299e37a55d3fa4
[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   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
82   QFeatureList aFeatures = aSelMgr->selectedFeatures();
83   if (aFeatures.size() == 1) {
84     FeaturePtr aFeature = aFeatures.first();
85     QMenu* aMenu = new QMenu();
86     if (aFeature->getKind() == "Part") {
87       boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
88       boost::shared_ptr<ModelAPI_Document> aFeaDoc = aFeature->data()->docRef("PartDocument")->value();
89       if (aMgr->currentDocument() == aFeaDoc)
90         aMenu->addAction(action("DEACTIVATE_PART_CMD"));
91       else 
92         aMenu->addAction(action("ACTIVATE_PART_CMD"));
93     } else {
94       aMenu->addAction(action("EDIT_CMD"));
95     }
96     return aMenu;
97   }
98   return 0;
99 }
100
101 void XGUI_ContextMenuMgr::connectObjectBrowser() const
102 {
103   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
104     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
105 }