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/eye_pencil_closed.png"), tr("Hide all"), this);
56 addAction("HIDEALL_CMD", aAction);
58 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
59 addAction("SHADING_CMD", aAction);
61 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
62 addAction("WIREFRAME_CMD", aAction);
65 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
67 if (myActions.contains(theId))
68 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
69 theAction->setData(theId);
70 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
71 myActions[theId] = theAction;
74 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
76 if (myActions.contains(theId))
77 return myActions[theId];
81 QStringList XGUI_ContextMenuMgr::actionIds() const
83 return myActions.keys();
86 void XGUI_ContextMenuMgr::onAction(bool isChecked)
88 QAction* aAction = static_cast<QAction*>(sender());
89 emit actionTriggered(aAction->data().toString(), isChecked);
92 void XGUI_ContextMenuMgr::updateCommandsStatus()
96 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
99 if (sender() == myWorkshop->objectBrowser())
100 aMenu = objectBrowserMenu();
101 else if (sender() == myWorkshop->viewer()) {
102 aMenu = viewerMenu();
105 if (aMenu && (aMenu->actions().size() > 0)) {
106 aMenu->exec(theEvent->globalPos());
111 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
113 QMenu* aMenu = new QMenu();
114 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
115 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
116 int aSelected = aObjects.size();
118 SessionPtr aMgr = ModelAPI_Session::get();
119 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
120 bool hasResult = false;
121 bool hasFeature = false;
122 foreach(ObjectPtr aObj, aObjects)
124 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
125 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(aObj);
130 if (hasFeature && hasResult)
134 if (aSelected == 1) {
135 ObjectPtr aObject = aObjects.first();
137 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
139 if (aMgr->activeDocument() == aPart->partDoc())
140 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
142 aMenu->addAction(action("ACTIVATE_PART_CMD"));
143 } else if (hasFeature) {
144 aMenu->addAction(action("EDIT_CMD"));
146 if (aDisplayer->isVisible(aObject)) {
147 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
148 aMenu->addAction(action("WIREFRAME_CMD"));
150 aMenu->addAction(action("SHADING_CMD"));
151 aMenu->addSeparator();
152 aMenu->addAction(action("HIDE_CMD"));
154 aMenu->addAction(action("SHOW_CMD"));
156 aMenu->addAction(action("SHOW_ONLY_CMD"));
158 } else { // If feature is 0 the it means that selected root object (document)
159 if (aMgr->activeDocument() != aMgr->moduleDocument())
160 aMenu->addAction(action("ACTIVATE_PART_CMD"));
164 aMenu->addAction(action("SHOW_CMD"));
165 aMenu->addAction(action("HIDE_CMD"));
166 aMenu->addAction(action("SHOW_ONLY_CMD"));
167 aMenu->addSeparator();
168 aMenu->addAction(action("SHADING_CMD"));
169 aMenu->addAction(action("WIREFRAME_CMD"));
173 aMenu->addAction(action("DELETE_CMD"));
175 aMenu->addSeparator();
176 aMenu->addActions(myWorkshop->objectBrowser()->actions());
177 if (aMenu->actions().size() > 0) {
184 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
186 QMenu* aMenu = new QMenu();
187 addViewerItems(aMenu);
188 if (aMenu->actions().size() > 0) {
195 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
197 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
198 QList<ObjectPtr> aObjects = aSelMgr->selection()->selectedObjects();
199 if (aObjects.size() > 0) {
200 //if (aObjects.size() == 1)
201 // theMenu->addAction(action("EDIT_CMD"));
202 bool isVisible = false;
203 bool isShading = false;
204 foreach(ObjectPtr aObject, aObjects)
206 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
207 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
209 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
215 theMenu->addAction(action("WIREFRAME_CMD"));
217 theMenu->addAction(action("SHADING_CMD"));
218 theMenu->addSeparator();
219 theMenu->addAction(action("SHOW_ONLY_CMD"));
220 theMenu->addAction(action("HIDE_CMD"));
222 theMenu->addAction(action("SHOW_CMD"));
223 //theMenu->addAction(action("DELETE_CMD"));
225 if (myWorkshop->displayer()->objectsCount() > 0)
226 theMenu->addAction(action("HIDEALL_CMD"));
227 if (!myWorkshop->isSalomeMode()) {
228 theMenu->addSeparator();
229 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
230 if (aMDI->actions().size() > 0) {
231 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
232 aSubMenu->addActions(aMDI->actions());
237 void XGUI_ContextMenuMgr::connectObjectBrowser() const
239 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
240 SLOT(onContextMenuRequest(QContextMenuEvent*)));
243 void XGUI_ContextMenuMgr::connectViewer() const
245 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
246 SLOT(onContextMenuRequest(QContextMenuEvent*)));