Salome HOME
Integration of Pop-up management.
[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
27 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
28 {
29   if (myActions.contains(theId))
30     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
31   theAction->setData(theId);
32   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
33   myActions[theId] = theAction;
34 }
35
36 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
37 {
38   if (myActions.contains(theId))
39     return myActions[theId];
40   return 0;
41 }
42
43 QStringList XGUI_ContextMenuMgr::actionIds() const
44 {
45   return myActions.keys();
46 }
47
48 void XGUI_ContextMenuMgr::onAction(bool isChecked)
49 {
50   QAction* aAction = static_cast<QAction*>(sender());
51   emit actionTriggered(aAction->data().toString(), isChecked);
52 }
53
54 void XGUI_ContextMenuMgr::updateCommandsStatus()
55 {
56 }
57
58 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
59 {
60   QMenu* aMenu = 0;
61   if (sender() == myWorkshop->objectBrowser())
62     aMenu = objectBrowserMenu();
63
64   if (aMenu) {
65     aMenu->exec(theEvent->globalPos());
66     delete aMenu;
67   }
68 }
69
70 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
71 {
72   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
73   QFeatureList aFeatures = aSelMgr->selectedFeatures();
74   if (aFeatures.size() == 1) {
75     FeaturePtr aFeature = aFeatures.first();
76     if (aFeature->getKind() != "Part") {
77       QMenu* aMenu = new QMenu();
78       aMenu->addAction(action("EDIT_CMD"));
79       return aMenu;
80     }
81   }
82   return 0;
83 }
84
85 void XGUI_ContextMenuMgr::connectObjectBrowser() const
86 {
87   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
88     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
89 }