1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_ContextMenuMgr.h"
4 #include "XGUI_Workshop.h"
5 #include "XGUI_ObjectsBrowser.h"
6 #include "XGUI_SelectionMgr.h"
7 #include "XGUI_Displayer.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Selection.h"
10 #include "XGUI_SalomeConnector.h"
11 #include "XGUI_Tools.h"
13 #include <AppElements_MainWindow.h>
15 //#include "PartSetPlugin_Part.h"
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_AttributeDocRef.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_ResultPart.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_ResultGroup.h>
23 #include <ModelAPI_ResultParameter.h>
25 #include <ModuleBase_IModule.h>
28 #include <QContextMenuEvent>
32 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
38 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
42 void XGUI_ContextMenuMgr::createActions()
44 QAction* aAction = new QAction(QIcon(":pictures/edit.png"), tr("Edit..."), this);
45 addAction("EDIT_CMD", aAction);
47 aAction = new QAction(QIcon(":pictures/activate.png"), tr("Activate"), this);
48 addAction("ACTIVATE_PART_CMD", aAction);
50 aAction = new QAction(QIcon(":pictures/assembly.png"), tr("Deactivate"), this);
51 addAction("DEACTIVATE_PART_CMD", aAction);
53 aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
54 QMainWindow* aDesktop = myWorkshop->mainWindow();
56 aDesktop = myWorkshop->salomeConnector()->desktop();
57 aDesktop->addAction(aAction);
59 addAction("DELETE_CMD", aAction);
60 aAction->setShortcut(Qt::Key_Delete);
61 aAction->setShortcutContext(Qt::ApplicationShortcut);
63 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color"), this);
64 addAction("COLOR_CMD", aAction);
66 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
67 addAction("SHOW_CMD", aAction);
69 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
70 addAction("SHOW_ONLY_CMD", aAction);
72 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
73 addAction("HIDE_CMD", aAction);
75 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
76 addAction("HIDEALL_CMD", aAction);
78 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
79 addAction("SHADING_CMD", aAction);
81 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
82 addAction("WIREFRAME_CMD", aAction);
85 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
87 if (myActions.contains(theId))
88 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
89 theAction->setData(theId);
90 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
91 myActions[theId] = theAction;
94 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
96 if (myActions.contains(theId))
97 return myActions[theId];
101 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
103 foreach(QAction* eachAction, myActions) {
104 if (eachAction->text() == theName) {
111 QStringList XGUI_ContextMenuMgr::actionIds() const
113 return myActions.keys();
116 void XGUI_ContextMenuMgr::onAction(bool isChecked)
118 QAction* aAction = static_cast<QAction*>(sender());
119 emit actionTriggered(aAction->data().toString(), isChecked);
122 void XGUI_ContextMenuMgr::updateCommandsStatus()
126 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
129 if (sender() == myWorkshop->objectBrowser())
130 aMenu = objectBrowserMenu();
131 else if (sender() == myWorkshop->viewer()) {
132 aMenu = viewerMenu();
135 if (aMenu && (aMenu->actions().size() > 0)) {
136 // it is possible that some objects should do something before and after the popup menu exec
137 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
139 emit beforeContextMenu();
140 aMenu->exec(theEvent->globalPos());
141 emit afterContextMenu();
146 QMenu* XGUI_ContextMenuMgr::objectBrowserMenu() const
148 QMenu* aMenu = new QMenu();
149 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
150 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
151 int aSelected = aObjects.size();
153 SessionPtr aMgr = ModelAPI_Session::get();
154 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
155 bool hasResult = false;
156 bool hasFeature = false;
157 bool hasParameter = false;
158 XGUI_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
161 if (aSelected == 1) {
162 ObjectPtr aObject = aObjects.first();
164 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
166 if (aMgr->activeDocument() == aPart->partDoc())
167 aMenu->addAction(action("DEACTIVATE_PART_CMD"));
169 aMenu->addAction(action("ACTIVATE_PART_CMD"));
170 } else if (hasFeature && aObject->document() == aMgr->activeDocument()) {
171 aMenu->addAction(action("EDIT_CMD"));
173 if (aDisplayer->isVisible(aObject)) {
174 if (aDisplayer->canBeShaded(aObject)) {
175 if (aDisplayer->displayMode(aObject) == XGUI_Displayer::Shading)
176 aMenu->addAction(action("WIREFRAME_CMD"));
178 aMenu->addAction(action("SHADING_CMD"));
180 aMenu->addSeparator();
181 aMenu->addAction(action("HIDE_CMD"));
182 } else if (!hasParameter) {
183 aMenu->addAction(action("SHOW_CMD"));
186 aMenu->addAction(action("EDIT_CMD"));
188 aMenu->addAction(action("SHOW_ONLY_CMD"));
190 } else { // If feature is 0 the it means that selected root object (document)
191 if (aMgr->activeDocument() != aMgr->moduleDocument())
192 aMenu->addAction(action("ACTIVATE_PART_CMD"));
195 if (hasResult && (!hasParameter)) {
196 aMenu->addAction(action("SHOW_CMD"));
197 aMenu->addAction(action("HIDE_CMD"));
198 aMenu->addAction(action("SHOW_ONLY_CMD"));
199 aMenu->addSeparator();
200 aMenu->addAction(action("SHADING_CMD"));
201 aMenu->addAction(action("WIREFRAME_CMD"));
204 if (hasFeature || hasParameter)
205 aMenu->addAction(action("DELETE_CMD"));
207 if (myWorkshop->canChangeColor())
208 aMenu->addAction(action("COLOR_CMD"));
210 aMenu->addSeparator();
211 aMenu->addActions(myWorkshop->objectBrowser()->actions());
213 ModuleBase_IModule* aModule = myWorkshop->module();
215 aModule->addObjectBrowserItems(aMenu);
217 if (aMenu->actions().size() > 0) {
224 QMenu* XGUI_ContextMenuMgr::viewerMenu() const
226 QMenu* aMenu = new QMenu();
227 addViewerItems(aMenu);
228 if (aMenu->actions().size() > 0) {
235 void XGUI_ContextMenuMgr::addViewerItems(QMenu* theMenu) const
237 bool aIsDone = false;
238 ModuleBase_IModule* aModule = myWorkshop->module();
240 aIsDone = aModule->addViewerItems(theMenu, myActions);
243 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
244 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
245 if (aObjects.size() > 0) {
246 //if (aObjects.size() == 1)
247 // theMenu->addAction(action("EDIT_CMD"));
248 bool isVisible = false;
249 bool isShading = false;
250 bool canBeShaded = false;
251 foreach(ObjectPtr aObject, aObjects)
253 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
254 if (aRes && myWorkshop->displayer()->isVisible(aRes)) {
256 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
257 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
264 theMenu->addAction(action("WIREFRAME_CMD"));
266 theMenu->addAction(action("SHADING_CMD"));
268 theMenu->addSeparator();
269 theMenu->addAction(action("SHOW_ONLY_CMD"));
270 theMenu->addAction(action("HIDE_CMD"));
272 theMenu->addAction(action("SHOW_CMD"));
273 //theMenu->addAction(action("DELETE_CMD"));
275 if (myWorkshop->displayer()->objectsCount() > 0)
276 theMenu->addAction(action("HIDEALL_CMD"));
277 if (myWorkshop->canChangeColor())
278 theMenu->addAction(action("COLOR_CMD"));
280 if (!myWorkshop->isSalomeMode()) {
281 theMenu->addSeparator();
282 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
283 if (aMDI->actions().size() > 0) {
284 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
285 aSubMenu->addActions(aMDI->actions());
291 void XGUI_ContextMenuMgr::connectObjectBrowser() const
293 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
294 SLOT(onContextMenuRequest(QContextMenuEvent*)));
297 void XGUI_ContextMenuMgr::connectViewer() const
299 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
300 SLOT(onContextMenuRequest(QContextMenuEvent*)));