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_DataModel.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_Session.h>
21 #include <ModelAPI_ResultGroup.h>
22 #include <ModelAPI_ResultParameter.h>
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_Tools.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_Tools.h>
31 #include <QContextMenuEvent>
36 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
38 myWorkshop(theParent),
43 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
47 void XGUI_ContextMenuMgr::createActions()
49 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
50 QMainWindow* aDesktop = myWorkshop->mainWindow();
52 aDesktop = myWorkshop->salomeConnector()->desktop();
53 aDesktop->addAction(aAction);
55 addAction("DELETE_CMD", aAction);
56 aAction->setShortcut(Qt::Key_Delete);
57 aAction->setShortcutContext(Qt::ApplicationShortcut);
59 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
60 addAction("RENAME_CMD", aAction);
61 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
63 aAction = new QAction(QIcon(":pictures/move.png"), tr("Move to the end"), this);
64 addAction("MOVE_CMD", aAction);
66 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
67 addAction("COLOR_CMD", aAction);
69 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
70 addAction("SHOW_CMD", aAction);
72 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
73 addAction("SHOW_ONLY_CMD", aAction);
75 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
76 addAction("HIDE_CMD", aAction);
78 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
79 addAction("HIDEALL_CMD", aAction);
81 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
82 addAction("SHADING_CMD", aAction);
84 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
85 addAction("WIREFRAME_CMD", aAction);
87 mySeparator = new QAction(this);
88 mySeparator->setSeparator(true);
91 buildObjBrowserMenu();
95 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
97 if (myActions.contains(theId))
98 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
99 theAction->setData(theId);
100 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
101 myActions[theId] = theAction;
104 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
106 if (myActions.contains(theId))
107 return myActions[theId];
111 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
113 foreach(QAction* eachAction, myActions) {
114 if (eachAction->text() == theName) {
121 QStringList XGUI_ContextMenuMgr::actionIds() const
123 return myActions.keys();
126 void XGUI_ContextMenuMgr::onAction(bool isChecked)
128 QAction* aAction = static_cast<QAction*>(sender());
129 emit actionTriggered(aAction->data().toString(), isChecked);
132 void XGUI_ContextMenuMgr::updateCommandsStatus()
136 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
138 QMenu* aMenu = new QMenu();
139 if (sender() == myWorkshop->objectBrowser()) {
140 updateObjectBrowserMenu();
141 addObjBrowserMenu(aMenu);
142 } else if (sender() == myWorkshop->viewer()) {
144 addViewerMenu(aMenu);
147 if (aMenu && (aMenu->actions().size() > 0)) {
148 // it is possible that some objects should do something before and after the popup menu exec
149 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
151 emit beforeContextMenu();
152 aMenu->exec(theEvent->globalPos());
153 emit afterContextMenu();
158 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
160 foreach(QAction* aAction, myActions)
161 aAction->setEnabled(false);
163 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
164 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
165 int aSelected = aObjects.size();
167 SessionPtr aMgr = ModelAPI_Session::get();
168 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
169 bool hasResult = false;
170 bool hasFeature = false;
171 bool hasParameter = false;
172 bool hasSubFeature = false;
173 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
176 if (aSelected == 1) {
177 ObjectPtr aObject = aObjects.first();
179 if (hasResult && myWorkshop->canBeShaded(aObject)) {
180 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
181 if (aMode != XGUI_Displayer::NoMode) {
182 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
183 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
185 action("WIREFRAME_CMD")->setEnabled(true);
186 action("SHADING_CMD")->setEnabled(true);
190 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
191 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
192 if (aHasSubResults) {
193 action("HIDE_CMD")->setEnabled(true);
194 action("SHOW_CMD")->setEnabled(true);
197 if (aObject->isDisplayed()) {
198 action("HIDE_CMD")->setEnabled(true);
199 } else if (hasResult && (!hasParameter)) {
200 action("SHOW_CMD")->setEnabled(true);
203 if (!(hasParameter || hasFeature))
204 action("SHOW_ONLY_CMD")->setEnabled(true);
206 else if (hasFeature && myWorkshop->canMoveFeature())
207 action("MOVE_CMD")->setEnabled(true);
209 if( aMgr->activeDocument() == aObject->document() )
211 action("RENAME_CMD")->setEnabled(true);
212 action("DELETE_CMD")->setEnabled(true);
216 // parameter is commented because the actions are not in the list of result parameter actions
217 if (hasResult /*&& (!hasParameter)*/) {
218 action("SHOW_CMD")->setEnabled(true);
219 action("HIDE_CMD")->setEnabled(true);
220 action("SHOW_ONLY_CMD")->setEnabled(true);
221 action("SHADING_CMD")->setEnabled(true);
222 action("WIREFRAME_CMD")->setEnabled(true);
225 bool allActive = true;
226 foreach( ObjectPtr aObject, aObjects )
227 if( aMgr->activeDocument() != aObject->document() ) {
231 if (!hasSubFeature && allActive ) {
232 if (hasFeature || hasParameter)
233 action("DELETE_CMD")->setEnabled(true);
237 // Show/Hide command has to be disabled for objects from non active document
238 bool aDeactivate = false;
239 foreach (ObjectPtr aObj, aObjects) {
240 if (!aObj->document()->isActive()) {
241 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
242 aObj->groupName() == ModelAPI_ResultPart::group()) {
249 // If at leas a one objec can not be edited then Show/Hide has to be disabled
250 action("SHOW_CMD")->setEnabled(false);
251 action("HIDE_CMD")->setEnabled(false);
252 action("SHOW_ONLY_CMD")->setEnabled(false);
255 if (myWorkshop->canChangeColor())
256 action("COLOR_CMD")->setEnabled(true);
258 ModuleBase_IModule* aModule = myWorkshop->module();
260 aModule->updateObjectBrowserMenu(myActions);
263 void XGUI_ContextMenuMgr::updateViewerMenu()
265 foreach(QAction* aAction, myActions)
266 aAction->setEnabled(false);
268 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
269 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
270 if (aPrsList.size() > 0) {
271 bool isVisible = false;
272 bool isShading = false;
273 bool canBeShaded = false;
275 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
276 aObject = aPrs.object();
277 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
278 if (aRes && aRes->isDisplayed()) {
280 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
281 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
287 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
288 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
289 if (aMode != XGUI_Displayer::NoMode) {
290 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
291 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
293 action("WIREFRAME_CMD")->setEnabled(true);
294 action("SHADING_CMD")->setEnabled(true);
297 action("SHOW_ONLY_CMD")->setEnabled(true);
298 action("HIDE_CMD")->setEnabled(true);
300 action("SHOW_CMD")->setEnabled(true);
302 if (myWorkshop->displayer()->objectsCount() > 0)
303 action("HIDEALL_CMD")->setEnabled(true);
304 if (myWorkshop->canChangeColor())
305 action("COLOR_CMD")->setEnabled(true);
307 action("DELETE_CMD")->setEnabled(true);
309 ModuleBase_IModule* aModule = myWorkshop->module();
311 aModule->updateViewerMenu(myActions);
314 void XGUI_ContextMenuMgr::connectObjectBrowser()
316 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
317 SLOT(onContextMenuRequest(QContextMenuEvent*)));
320 void XGUI_ContextMenuMgr::connectViewer()
322 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
323 SLOT(onContextMenuRequest(QContextMenuEvent*)));
327 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
329 QAction* aSeparator = new QAction(this);
330 aSeparator->setSeparator(true);
334 // Result construction menu
335 aList.append(action("SHOW_CMD"));
336 aList.append(action("HIDE_CMD"));
337 aList.append(action("SHOW_ONLY_CMD"));
338 aList.append(action("COLOR_CMD"));
339 aList.append(mySeparator);
340 aList.append(action("RENAME_CMD"));
341 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
342 //-------------------------------------
345 aList.append(action("WIREFRAME_CMD"));
346 aList.append(action("SHADING_CMD"));
347 aList.append(action("COLOR_CMD"));
348 aList.append(mySeparator);
349 aList.append(action("SHOW_CMD"));
350 aList.append(action("HIDE_CMD"));
351 aList.append(action("SHOW_ONLY_CMD"));
352 aList.append(mySeparator);
353 aList.append(action("RENAME_CMD"));
354 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
356 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
358 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
359 //-------------------------------------
362 aList.append(action("DELETE_CMD"));
363 aList.append(action("MOVE_CMD"));
364 aList.append(mySeparator);
365 aList.append(action("RENAME_CMD"));
366 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
369 aList.append(action("DELETE_CMD"));
370 aList.append(mySeparator);
371 aList.append(action("RENAME_CMD"));
372 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
373 //-------------------------------------
376 void XGUI_ContextMenuMgr::buildViewerMenu()
379 // Result construction menu
380 aList.append(action("HIDE_CMD"));
381 aList.append(action("SHOW_ONLY_CMD"));
382 aList.append(action("HIDEALL_CMD"));
383 aList.append(action("COLOR_CMD"));
384 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
386 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
387 //-------------------------------------
390 aList.append(action("WIREFRAME_CMD"));
391 aList.append(action("SHADING_CMD"));
392 aList.append(action("COLOR_CMD"));
393 aList.append(mySeparator);
394 aList.append(action("HIDE_CMD"));
395 aList.append(action("SHOW_ONLY_CMD"));
396 aList.append(action("HIDEALL_CMD"));
397 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
399 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
400 //-------------------------------------
405 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
407 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
408 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
409 int aSelected = aObjects.size();
410 QActionsList aActions;
411 if (aSelected == 1) {
412 ObjectPtr aObject = aObjects.first();
413 std::string aName = aObject->groupName();
414 if (myObjBrowserMenus.contains(aName))
415 aActions = myObjBrowserMenus[aName];
416 } else if (aSelected > 1) {
417 aActions.append(action("SHADING_CMD"));
418 aActions.append(action("WIREFRAME_CMD"));
419 aActions.append(mySeparator);
420 aActions.append(action("SHOW_CMD"));
421 aActions.append(action("HIDE_CMD"));
422 aActions.append(action("SHOW_ONLY_CMD"));
423 aActions.append(mySeparator);
424 aActions.append(action("DELETE_CMD"));
425 //aActions.append(action("MOVE_CMD"));
426 aActions.append(action("COLOR_CMD"));
428 theMenu->addActions(aActions);
430 ModuleBase_IModule* aModule = myWorkshop->module();
432 theMenu->addSeparator();
433 aModule->addObjectBrowserMenu(theMenu);
435 theMenu->addSeparator();
436 theMenu->addActions(myWorkshop->objectBrowser()->actions());
439 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
441 ModuleBase_IModule* aModule = myWorkshop->module();
443 if (aModule->addViewerMenu(theMenu, myActions))
444 theMenu->addSeparator();
446 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
447 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
448 int aSelected = aPrsList.size();
449 QActionsList aActions;
450 if (aSelected == 1) {
451 ObjectPtr aObject = aPrsList.first().object();
452 std::string aName = aObject->groupName();
453 if (myViewerMenu.contains(aName))
454 aActions = myViewerMenu[aName];
455 aActions.append(action("COLOR_CMD"));
456 } else if (aSelected > 1) {
457 aActions.append(action("HIDE_CMD"));
458 aActions.append(action("COLOR_CMD"));
460 theMenu->addActions(aActions);
462 if (!myWorkshop->isSalomeMode()) {
463 theMenu->addSeparator();
464 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
465 if (aMDI->actions().size() > 0) {
466 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
467 aSubMenu->addActions(aMDI->actions());
472 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
476 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
477 aLast = myObjBrowserMenus.end();
478 for (; anIt != aLast; anIt++) {
479 QString aGroupName(anIt.key().c_str());
480 if (aGroups.contains(aGroupName))
482 QActionsList anActions = anIt.value();
483 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
485 for (; anAIt != anALast && !aFound; anAIt++)
486 aFound = (*anAIt)->data().toString() == theName;
488 aGroups.append(aGroupName);
493 void XGUI_ContextMenuMgr::onRename()
495 myWorkshop->objectBrowser()->onEditItem();