]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ContextMenuMgr.cpp
Salome HOME
2a8fe108b3e4a5b23ff8e4515ebe37a5128a2f21
[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 <QAction>
8 #include <QContextMenuEvent>
9 #include <QMenu>
10
11 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
12 QObject(theParent), myWorkshop(theParent)
13 {
14
15 }
16
17 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
18 {
19 }
20
21 void XGUI_ContextMenuMgr::createActions()
22 {
23   QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
24   addAction("EDIT_CMD", aAction);
25
26   aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
27   addAction("ACTIVATE_PART_CMD", aAction);
28
29   aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
30   addAction("DEACTIVATE_PART_CMD", aAction);
31 }
32
33 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
34 {
35   if (myActions.contains(theId))
36     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
37   theAction->setData(theId);
38   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
39   myActions[theId] = theAction;
40 }
41
42 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
43 {
44   if (myActions.contains(theId))
45     return myActions[theId];
46   return 0;
47 }
48
49 QStringList XGUI_ContextMenuMgr::actionIds() const
50 {
51   return myActions.keys();
52 }
53
54 void XGUI_ContextMenuMgr::onAction(bool isChecked)
55 {
56   QAction* aAction = static_cast<QAction*>(sender());
57   emit actionTriggered(aAction->data().toString(), isChecked);
58 }
59
60 void XGUI_ContextMenuMgr::updateCommandsStatus()
61 {
62 }
63
64 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
65 {
66   QMenu* aMenu = 0;
67   if (sender() == myWorkshop->objectBrowser())
68     aMenu = objectBrowserMenu();
69
70   if (aMenu) {
71     aMenu->exec(theEvent->globalPos());
72     delete aMenu;
73   }
74 }
75
76 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
77 {
78   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
79   QFeatureList aFeatures = aSelMgr->selectedFeatures();
80   if (aFeatures.size() == 1) {
81     FeaturePtr aFeature = aFeatures.first();
82     QMenu* aMenu = new QMenu();
83     if (aFeature->getKind() == "Part") {
84       //TODO: Check that feature is active
85       aMenu->addAction(action("ACTIVATE_PART_CMD"));
86     } else {
87       aMenu->addAction(action("EDIT_CMD"));
88     }
89     return aMenu;
90   }
91   return 0;
92 }
93
94 void XGUI_ContextMenuMgr::connectObjectBrowser() const
95 {
96   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
97     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
98 }