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