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"
9 #include "XGUI_Selection.h"
11 #include "PartSetPlugin_Part.h"
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_ResultPart.h>
19 #include <QContextMenuEvent>
23 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent) :
24 QObject(theParent), myWorkshop(theParent)
28 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
32 void XGUI_ContextMenuMgr::createActions()
34 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
35 addAction("EDIT_CMD", aAction);
37 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
38 addAction("ACTIVATE_PART_CMD", aAction);
40 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
41 addAction("DEACTIVATE_PART_CMD", aAction);
43 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
44 addAction("DELETE_CMD", aAction);
46 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
47 addAction("SHOW_CMD", aAction);
49 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
50 addAction("HIDE_CMD", aAction);
53 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
55 if (myActions.contains(theId))
56 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
57 theAction->setData(theId);
58 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
59 myActions[theId] = theAction;
62 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
64 if (myActions.contains(theId))
65 return myActions[theId];
69 QStringList XGUI_ContextMenuMgr::actionIds() const
71 return myActions.keys();
74 void XGUI_ContextMenuMgr::onAction(bool isChecked)
76 QAction* aAction = static_cast<QAction*>(sender());
77 emit actionTriggered(aAction->data().toString(), isChecked);
80 void XGUI_ContextMenuMgr::updateCommandsStatus()
84 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
87 if (sender() == myWorkshop->objectBrowser())
88 aMenu = objectBrowserMenu();
89 else if (sender() == myWorkshop->viewer()) {
93 if (aMenu && (aMenu->actions().size() > 0)) {
94 aMenu->exec(theEvent->globalPos());
99 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
101 QMenu* aMenu = new QMenu();
102 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
103 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
104 int aSelected = aObjects.size();
106 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
107 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
109 if (aSelected == 1) {
110 ObjectPtr aObject = aObjects.first();
112 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
113 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
115 if (aMgr->currentDocument() == aPart->partDoc())
116 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
118 aMenu->addAction(action("ACTIVATE_PART_CMD"));
119 } else if (aFeature) {
120 aMenu->addAction(action("EDIT_CMD"));
122 if (aDisplayer->isVisible(aObject))
123 aMenu->addAction(action("HIDE_CMD"));
125 aMenu->addAction(action("SHOW_CMD"));
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"));
131 } else if (aSelected >= 1) {
132 bool hasResult = false;
133 bool hasFeature = false;
134 foreach(ObjectPtr aObj, aObjects) {
135 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
136 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
137 if (aResult) hasResult = true;
138 if (aFeature) hasFeature = true;
139 if (hasFeature && hasResult)
143 aMenu->addAction(action("SHOW_CMD"));
144 aMenu->addAction(action("HIDE_CMD"));
147 aMenu->addAction(action("DELETE_CMD"));
150 aMenu->addActions(myWorkshop->objectBrowser()->actions());
151 if (aMenu->actions().size() > 0) {
158 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
160 QMenu* aMenu = new QMenu();
161 addViewerItems(aMenu);
162 if (aMenu->actions().size() > 0) {
169 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
171 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
172 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
173 if (aObjects.size() > 0) {
174 //if (aObjects.size() == 1)
175 // theMenu->addAction(action("EDIT_CMD"));
176 bool isVisible = false;
177 foreach(ObjectPtr aObject, aObjects) {
178 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
179 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
185 theMenu->addAction(action("HIDE_CMD"));
187 theMenu->addAction(action("SHOW_CMD"));
188 //theMenu->addAction(action("DELETE_CMD"));
190 if (!myWorkshop->isSalomeMode()) {
191 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
192 if (aMDI->actions().size() > 0) {
193 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
194 aSubMenu->addActions(aMDI->actions());
199 void XGUI_ContextMenuMgr::connectObjectBrowser() const
201 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
202 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));
205 void XGUI_ContextMenuMgr::connectViewer() const
207 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)),
208 this, SLOT(onContextMenuRequest(QContextMenuEvent*)));