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"
12 #include <AppElements_MainWindow.h>
14 //#include "PartSetPlugin_Part.h"
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_AttributeDocRef.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_ResultGroup.h>
21 #include <ModelAPI_ResultParameter.h>
22 #include <ModelAPI_ResultConstruction.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_Tools.h>
26 #include <ModuleBase_IModule.h>
27 #include <ModuleBase_Tools.h>
30 #include <QContextMenuEvent>
35 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
37 myWorkshop(theParent),
42 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
46 void XGUI_ContextMenuMgr::createActions()
48 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
49 QMainWindow* aDesktop = myWorkshop->mainWindow();
51 aDesktop = myWorkshop->salomeConnector()->desktop();
52 aDesktop->addAction(aAction);
54 addAction("DELETE_CMD", aAction);
55 aAction->setShortcut(Qt::Key_Delete);
56 aAction->setShortcutContext(Qt::ApplicationShortcut);
58 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
59 addAction("RENAME_CMD", aAction);
60 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
62 aAction = new QAction(QIcon(":pictures/move.png"), tr("Move..."), this);
63 addAction("MOVE_CMD", aAction);
65 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
66 addAction("COLOR_CMD", aAction);
68 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
69 addAction("SHOW_CMD", aAction);
71 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
72 addAction("SHOW_ONLY_CMD", aAction);
74 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
75 addAction("HIDE_CMD", aAction);
77 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
78 addAction("HIDEALL_CMD", aAction);
80 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
81 addAction("SHADING_CMD", aAction);
83 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
84 addAction("WIREFRAME_CMD", aAction);
86 mySeparator = new QAction(this);
87 mySeparator->setSeparator(true);
90 buildObjBrowserMenu();
94 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
96 if (myActions.contains(theId))
97 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
98 theAction->setData(theId);
99 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
100 myActions[theId] = theAction;
103 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
105 if (myActions.contains(theId))
106 return myActions[theId];
110 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
112 foreach(QAction* eachAction, myActions) {
113 if (eachAction->text() == theName) {
120 QStringList XGUI_ContextMenuMgr::actionIds() const
122 return myActions.keys();
125 void XGUI_ContextMenuMgr::onAction(bool isChecked)
127 QAction* aAction = static_cast<QAction*>(sender());
128 emit actionTriggered(aAction->data().toString(), isChecked);
131 void XGUI_ContextMenuMgr::updateCommandsStatus()
135 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
137 QMenu* aMenu = new QMenu();
138 if (sender() == myWorkshop->objectBrowser()) {
139 updateObjectBrowserMenu();
140 addObjBrowserMenu(aMenu);
141 } else if (sender() == myWorkshop->viewer()) {
143 addViewerMenu(aMenu);
146 if (aMenu && (aMenu->actions().size() > 0)) {
147 // it is possible that some objects should do something before and after the popup menu exec
148 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
150 emit beforeContextMenu();
151 aMenu->exec(theEvent->globalPos());
152 emit afterContextMenu();
157 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
159 foreach(QAction* aAction, myActions)
160 aAction->setEnabled(false);
162 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
163 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
164 int aSelected = aObjects.size();
166 SessionPtr aMgr = ModelAPI_Session::get();
167 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
168 bool hasResult = false;
169 bool hasFeature = false;
170 bool hasParameter = false;
171 bool hasSubFeature = false;
172 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
175 if (aSelected == 1) {
176 ObjectPtr aObject = aObjects.first();
178 if (hasResult && myWorkshop->canBeShaded(aObject)) {
179 action("WIREFRAME_CMD")->setEnabled(true);
180 action("SHADING_CMD")->setEnabled(true);
183 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
184 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
185 if (aHasSubResults) {
186 action("HIDE_CMD")->setEnabled(true);
187 action("SHOW_CMD")->setEnabled(true);
190 if (aObject->isDisplayed()) {
191 action("HIDE_CMD")->setEnabled(true);
192 } else if (hasResult && (!hasParameter)) {
193 action("SHOW_CMD")->setEnabled(true);
196 if (!(hasParameter || hasFeature))
197 action("SHOW_ONLY_CMD")->setEnabled(true);
199 else if (hasFeature && myWorkshop->canMoveFeature())
200 action("MOVE_CMD")->setEnabled(true);
202 if( aMgr->activeDocument() == aObject->document() )
204 action("RENAME_CMD")->setEnabled(true);
205 action("DELETE_CMD")->setEnabled(true);
209 // parameter is commented because the actions are not in the list of result parameter actions
210 if (hasResult /*&& (!hasParameter)*/) {
211 action("SHOW_CMD")->setEnabled(true);
212 action("HIDE_CMD")->setEnabled(true);
213 action("SHOW_ONLY_CMD")->setEnabled(true);
214 action("SHADING_CMD")->setEnabled(true);
215 action("WIREFRAME_CMD")->setEnabled(true);
218 bool allActive = true;
219 foreach( ObjectPtr aObject, aObjects )
220 if( aMgr->activeDocument() != aObject->document() )
225 if (!hasSubFeature && allActive ) {
226 if (hasFeature || hasParameter)
227 action("DELETE_CMD")->setEnabled(true);
231 bool isPartSetDocActive =
232 ModelAPI_Session::get()->activeDocument()==ModelAPI_Session::get()->moduleDocument();
233 if( !isPartSetDocActive )
235 action("SHOW_CMD")->setEnabled(false);
236 action("HIDE_CMD")->setEnabled(false);
237 action("SHOW_ONLY_CMD")->setEnabled(false);
240 if (myWorkshop->canChangeColor())
241 action("COLOR_CMD")->setEnabled(true);
243 ModuleBase_IModule* aModule = myWorkshop->module();
245 aModule->updateObjectBrowserMenu(myActions);
248 void XGUI_ContextMenuMgr::updateViewerMenu()
250 foreach(QAction* aAction, myActions)
251 aAction->setEnabled(false);
253 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
254 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
255 if (aPrsList.size() > 0) {
256 bool isVisible = false;
257 bool isShading = false;
258 bool canBeShaded = false;
260 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
261 aObject = aPrs.object();
262 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
263 if (aRes && aRes->isDisplayed()) {
265 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
266 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
272 action("WIREFRAME_CMD")->setEnabled(true);
273 action("SHADING_CMD")->setEnabled(true);
275 action("SHOW_ONLY_CMD")->setEnabled(true);
276 action("HIDE_CMD")->setEnabled(true);
278 action("SHOW_CMD")->setEnabled(true);
280 if (myWorkshop->displayer()->objectsCount() > 0)
281 action("HIDEALL_CMD")->setEnabled(true);
282 if (myWorkshop->canChangeColor())
283 action("COLOR_CMD")->setEnabled(true);
285 action("DELETE_CMD")->setEnabled(true);
287 ModuleBase_IModule* aModule = myWorkshop->module();
289 aModule->updateViewerMenu(myActions);
292 void XGUI_ContextMenuMgr::connectObjectBrowser()
294 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
295 SLOT(onContextMenuRequest(QContextMenuEvent*)));
298 void XGUI_ContextMenuMgr::connectViewer()
300 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
301 SLOT(onContextMenuRequest(QContextMenuEvent*)));
305 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
307 QAction* aSeparator = new QAction(this);
308 aSeparator->setSeparator(true);
312 // Result construction menu
313 aList.append(action("SHOW_CMD"));
314 aList.append(action("HIDE_CMD"));
315 aList.append(action("SHOW_ONLY_CMD"));
316 aList.append(action("COLOR_CMD"));
317 aList.append(mySeparator);
318 aList.append(action("RENAME_CMD"));
319 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
320 //-------------------------------------
323 aList.append(action("WIREFRAME_CMD"));
324 aList.append(action("SHADING_CMD"));
325 aList.append(action("COLOR_CMD"));
326 aList.append(mySeparator);
327 aList.append(action("SHOW_CMD"));
328 aList.append(action("HIDE_CMD"));
329 aList.append(action("SHOW_ONLY_CMD"));
330 aList.append(mySeparator);
331 aList.append(action("RENAME_CMD"));
332 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
334 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
336 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
337 //-------------------------------------
340 aList.append(action("DELETE_CMD"));
341 aList.append(action("MOVE_CMD"));
342 aList.append(mySeparator);
343 aList.append(action("RENAME_CMD"));
344 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
347 aList.append(action("DELETE_CMD"));
348 aList.append(mySeparator);
349 aList.append(action("RENAME_CMD"));
350 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
351 //-------------------------------------
354 void XGUI_ContextMenuMgr::buildViewerMenu()
357 // Result construction menu
358 aList.append(action("HIDE_CMD"));
359 aList.append(action("SHOW_ONLY_CMD"));
360 aList.append(action("HIDEALL_CMD"));
361 aList.append(action("COLOR_CMD"));
362 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
364 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
365 //-------------------------------------
368 aList.append(action("WIREFRAME_CMD"));
369 aList.append(action("SHADING_CMD"));
370 aList.append(action("COLOR_CMD"));
371 aList.append(mySeparator);
372 aList.append(action("HIDE_CMD"));
373 aList.append(action("SHOW_ONLY_CMD"));
374 aList.append(action("HIDEALL_CMD"));
375 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
377 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
378 //-------------------------------------
383 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
385 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
386 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
387 int aSelected = aObjects.size();
388 QActionsList aActions;
389 if (aSelected == 1) {
390 ObjectPtr aObject = aObjects.first();
391 std::string aName = aObject->groupName();
392 if (myObjBrowserMenus.contains(aName))
393 aActions = myObjBrowserMenus[aName];
394 } else if (aSelected > 1) {
395 aActions.append(action("SHADING_CMD"));
396 aActions.append(action("WIREFRAME_CMD"));
397 aActions.append(mySeparator);
398 aActions.append(action("SHOW_CMD"));
399 aActions.append(action("HIDE_CMD"));
400 aActions.append(action("SHOW_ONLY_CMD"));
401 aActions.append(mySeparator);
402 aActions.append(action("DELETE_CMD"));
403 //aActions.append(action("MOVE_CMD"));
405 theMenu->addActions(aActions);
407 ModuleBase_IModule* aModule = myWorkshop->module();
409 theMenu->addSeparator();
410 aModule->addObjectBrowserMenu(theMenu);
412 theMenu->addSeparator();
413 theMenu->addActions(myWorkshop->objectBrowser()->actions());
416 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
418 ModuleBase_IModule* aModule = myWorkshop->module();
420 if (aModule->addViewerMenu(theMenu, myActions))
421 theMenu->addSeparator();
423 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
424 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
425 int aSelected = aPrsList.size();
426 QActionsList aActions;
427 if (aSelected == 1) {
428 ObjectPtr aObject = aPrsList.first().object();
429 std::string aName = aObject->groupName();
430 if (myViewerMenu.contains(aName))
431 aActions = myViewerMenu[aName];
432 } else if (aSelected > 1) {
433 aActions.append(action("HIDE_CMD"));
435 theMenu->addActions(aActions);
437 if (!myWorkshop->isSalomeMode()) {
438 theMenu->addSeparator();
439 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
440 if (aMDI->actions().size() > 0) {
441 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
442 aSubMenu->addActions(aMDI->actions());
447 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
451 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
452 aLast = myObjBrowserMenus.end();
453 for (; anIt != aLast; anIt++) {
454 QString aGroupName(anIt.key().c_str());
455 if (aGroups.contains(aGroupName))
457 QActionsList anActions = anIt.value();
458 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
460 for (; anAIt != anALast && !aFound; anAIt++)
461 aFound = (*anAIt)->data().toString() == theName;
463 aGroups.append(aGroupName);
468 void XGUI_ContextMenuMgr::onRename()
470 myWorkshop->objectBrowser()->onEditItem();