1 #include "XGUI_ContextMenuMgr.h"
2 #include "XGUI_Workshop.h"
3 #include "XGUI_ObjectsBrowser.h"
4 #include "XGUI_SelectionMgr.h"
5 #include "XGUI_Displayer.h"
6 #include "XGUI_MainWindow.h"
7 #include "XGUI_ViewerProxy.h"
8 #include "XGUI_Selection.h"
10 #include "PartSetPlugin_Part.h"
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_ResultPart.h>
18 #include <QContextMenuEvent>
22 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* 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.png"), tr("Show only"), this);
50 addAction("SHOW_ONLY_CMD", aAction);
52 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
53 addAction("HIDE_CMD", aAction);
55 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
56 addAction("SHADING_CMD", aAction);
58 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
59 addAction("WIREFRAME_CMD", aAction);
62 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
64 if (myActions.contains(theId))
65 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
66 theAction->setData(theId);
67 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
68 myActions[theId] = theAction;
71 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
73 if (myActions.contains(theId))
74 return myActions[theId];
78 QStringList XGUI_ContextMenuMgr::actionIds() const
80 return myActions.keys();
83 void XGUI_ContextMenuMgr::onAction(bool isChecked)
85 QAction* aAction = static_cast<QAction*>(sender());
86 emit actionTriggered(aAction->data().toString(), isChecked);
89 void XGUI_ContextMenuMgr::updateCommandsStatus()
93 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
96 if (sender() == myWorkshop->objectBrowser())
97 aMenu = objectBrowserMenu();
98 else if (sender() == myWorkshop->viewer()) {
102 if (aMenu && (aMenu->actions().size() > 0)) {
103 aMenu->exec(theEvent->globalPos());
108 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
110 QMenu* aMenu = new QMenu();
111 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
112 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
113 int aSelected = aObjects.size();
115 SessionPtr aMgr = ModelAPI_Session::get();
116 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
117 bool hasResult = false;
118 bool hasFeature = false;
119 foreach(ObjectPtr aObj, aObjects)
121 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
122 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
127 if (hasFeature && hasResult)
131 if (aSelected == 1) {
132 ObjectPtr aObject = aObjects.first();
134 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
136 if (aMgr->activeDocument() == aPart->partDoc())
137 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
139 aMenu->addAction(action("ACTIVATE_PART_CMD"));
140 } else if (hasFeature) {
141 aMenu->addAction(action("EDIT_CMD"));
143 if (aDisplayer->isVisible(aObject)) {
144 aMenu->addAction(action("HIDE_CMD"));
145 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
146 aMenu->addAction(action("WIREFRAME_CMD"));
148 aMenu->addAction(action("SHADING_CMD"));
150 aMenu->addAction(action("SHOW_CMD"));
152 aMenu->addAction(action("SHOW_ONLY_CMD"));
154 } else { // If feature is 0 the it means that selected root object (document)
155 if (aMgr->activeDocument() != aMgr->moduleDocument())
156 aMenu->addAction(action("ACTIVATE_PART_CMD"));
160 aMenu->addAction(action("SHOW_CMD"));
161 aMenu->addAction(action("HIDE_CMD"));
162 aMenu->addAction(action("SHOW_ONLY_CMD"));
163 aMenu->addSeparator();
164 aMenu->addAction(action("SHADING_CMD"));
165 aMenu->addAction(action("WIREFRAME_CMD"));
169 aMenu->addAction(action("DELETE_CMD"));
171 aMenu->addActions(myWorkshop->objectBrowser()->actions());
172 if (aMenu->actions().size() > 0) {
179 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
181 QMenu* aMenu = new QMenu();
182 addViewerItems(aMenu);
183 if (aMenu->actions().size() > 0) {
190 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
192 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
193 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
194 if (aObjects.size() > 0) {
195 //if (aObjects.size() == 1)
196 // theMenu->addAction(action("EDIT_CMD"));
197 bool isVisible = false;
198 bool isShading = false;
199 foreach(ObjectPtr aObject, aObjects)
201 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
202 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
204 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
209 theMenu->addAction(action("HIDE_CMD"));
211 theMenu->addAction(action("WIREFRAME_CMD"));
213 theMenu->addAction(action("SHADING_CMD"));
215 theMenu->addAction(action("SHOW_CMD"));
216 //theMenu->addAction(action("DELETE_CMD"));
218 if (!myWorkshop->isSalomeMode()) {
219 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
220 if (aMDI->actions().size() > 0) {
221 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
222 aSubMenu->addActions(aMDI->actions());
227 void XGUI_ContextMenuMgr::connectObjectBrowser() const
229 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
230 SLOT(onContextMenuRequest(QContextMenuEvent*)));
233 void XGUI_ContextMenuMgr::connectViewer() const
235 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
236 SLOT(onContextMenuRequest(QContextMenuEvent*)));