Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[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 #include "XGUI_Displayer.h"
7
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_AttributeDocRef.h>
10 #include <ModelAPI_Object.h>
11
12 #include <QAction>
13 #include <QContextMenuEvent>
14 #include <QMenu>
15
16 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
17 QObject(theParent), myWorkshop(theParent)
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   aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
40   addAction("SHOW_CMD", aAction);
41
42   aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
43   addAction("HIDE_CMD", aAction);
44 }
45
46 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
47 {
48   if (myActions.contains(theId))
49     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
50   theAction->setData(theId);
51   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
52   myActions[theId] = theAction;
53 }
54
55 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
56 {
57   if (myActions.contains(theId))
58     return myActions[theId];
59   return 0;
60 }
61
62 QStringList XGUI_ContextMenuMgr::actionIds() const
63 {
64   return myActions.keys();
65 }
66
67 void XGUI_ContextMenuMgr::onAction(bool isChecked)
68 {
69   QAction* aAction = static_cast<QAction*>(sender());
70   emit actionTriggered(aAction->data().toString(), isChecked);
71 }
72
73 void XGUI_ContextMenuMgr::updateCommandsStatus()
74 {
75 }
76
77 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
78 {
79   QMenu* aMenu = 0;
80   if (sender() == myWorkshop->objectBrowser())
81     aMenu = objectBrowserMenu();
82
83   if (aMenu) {
84     aMenu->exec(theEvent->globalPos());
85     delete aMenu;
86   }
87 }
88
89 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
90 {
91   QMenu* aMenu = new QMenu();
92   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
93   QFeatureList aFeatures = aSelMgr->selectedFeatures();
94   if (aFeatures.size() == 1) {
95     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
96     FeaturePtr aFeature = aFeatures.first();
97     //Process Feature
98     if (aFeature) {
99       if (aFeature->getKind() == "Part") {
100         ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
101         DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
102         if (aMgr->currentDocument() == aFeaDoc)
103           aMenu->addAction(action("DEACTIVATE_PART_CMD"));
104         else 
105           aMenu->addAction(action("ACTIVATE_PART_CMD"));
106       } else {
107         aMenu->addAction(action("EDIT_CMD"));
108
109         XGUI_Displayer* aDisplayer = myWorkshop->displayer();
110         if (aDisplayer->IsVisible(aFeature))
111           aMenu->addAction(action("HIDE_CMD"));
112         else
113           aMenu->addAction(action("SHOW_CMD"));
114       }
115       aMenu->addAction(action("DELETE_CMD"));
116       aMenu->addSeparator();
117
118     // Process Root object (document)
119     } else { // If feature is 0 the it means that selected root object (document)
120       if (aMgr->currentDocument() != aMgr->rootDocument()) {
121         aMenu->addAction(action("ACTIVATE_PART_CMD"));
122       }
123     }
124   }
125   aMenu->addActions(myWorkshop->objectBrowser()->actions());
126   if (aMenu->actions().size() > 0) {
127     return aMenu;
128   }
129   delete aMenu;
130   return 0;
131 }
132
133 void XGUI_ContextMenuMgr::connectObjectBrowser() const
134 {
135   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), 
136     this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
137 }