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 #include "XGUI_MainWindow.h"
8 #include "XGUI_ViewerProxy.h"
10 #include "PartSetPlugin_Part.h"
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_Object.h>
17 #include <QContextMenuEvent>
21 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
22 QObject(theParent), myWorkshop(theParent)
26 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
30 void XGUI_ContextMenuMgr::createActions()
32 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
33 addAction("EDIT_CMD", aAction);
35 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
36 addAction("ACTIVATE_PART_CMD", aAction);
38 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
39 addAction("DEACTIVATE_PART_CMD", aAction);
41 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
42 addAction("DELETE_CMD", aAction);
44 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
45 addAction("SHOW_CMD", aAction);
47 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
48 addAction("HIDE_CMD", aAction);
51 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
53 if (myActions.contains(theId))
54 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
55 theAction->setData(theId);
56 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
57 myActions[theId] = theAction;
60 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
62 if (myActions.contains(theId))
63 return myActions[theId];
67 QStringList XGUI_ContextMenuMgr::actionIds() const
69 return myActions.keys();
72 void XGUI_ContextMenuMgr::onAction(bool isChecked)
74 QAction* aAction = static_cast<QAction*>(sender());
75 emit actionTriggered(aAction->data().toString(), isChecked);
78 void XGUI_ContextMenuMgr::updateCommandsStatus()
82 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
85 if (sender() == myWorkshop->objectBrowser())
86 aMenu = objectBrowserMenu();
87 else if (sender() == myWorkshop->viewer()) {
91 if (aMenu && (aMenu->actions().size() > 0)) {
92 aMenu->exec(theEvent->globalPos());
97 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
99 QMenu* aMenu = new QMenu();
100 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
101 QFeatureList aFeatures = aSelMgr->selectedFeatures();
102 if (aFeatures.size() == 1) {
103 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
104 FeaturePtr aFeature = aFeatures.first();
107 if (aFeature->getKind() == PARTSET_PART_KIND) {
108 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
109 DocumentPtr aFeaDoc = aObject->featureRef()->data()->docRef("PartDocument")->value();
110 if (aMgr->currentDocument() == aFeaDoc)
111 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
113 aMenu->addAction(action("ACTIVATE_PART_CMD"));
115 aMenu->addAction(action("EDIT_CMD"));
117 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
118 if (aDisplayer->isVisible(aFeature))
119 aMenu->addAction(action("HIDE_CMD"));
121 aMenu->addAction(action("SHOW_CMD"));
123 aMenu->addAction(action("DELETE_CMD"));
124 aMenu->addSeparator();
126 // Process Root object (document)
127 } else { // If feature is 0 the it means that selected root object (document)
128 if (aMgr->currentDocument() != aMgr->rootDocument()) {
129 aMenu->addAction(action("ACTIVATE_PART_CMD"));
133 aMenu->addActions(myWorkshop->objectBrowser()->actions());
134 if (aMenu->actions().size() > 0) {
141 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
143 QMenu* aMenu = new QMenu();
144 addViewerItems(aMenu);
145 if (aMenu->actions().size() > 0) {
152 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
154 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
155 QFeatureList aFeatures = aSelMgr->selectedFeatures();
156 if (aFeatures.size() > 0) {
157 if (aFeatures.size() == 1)
158 theMenu->addAction(action("EDIT_CMD"));
159 bool isVisible = false;
160 foreach(FeaturePtr aFeature, aFeatures) {
161 if (myWorkshop->displayer()->isVisible(aFeature)) {
167 theMenu->addAction(action("HIDE_CMD"));
169 theMenu->addAction(action("SHOW_CMD"));
170 theMenu->addAction(action("DELETE_CMD"));
172 if (!myWorkshop->isSalomeMode()) {
173 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
174 if (aMDI->actions().size() > 0) {
175 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
176 aSubMenu->addActions(aMDI->actions());
181 void XGUI_ContextMenuMgr::connectObjectBrowser() const
183 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
184 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
187 void XGUI_ContextMenuMgr::connectViewer() const
189 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
190 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));