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