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>
25 #include <ModuleBase_IModule.h>
26 #include <ModuleBase_Tools.h>
29 #include <QContextMenuEvent>
34 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
36 myWorkshop(theParent),
41 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
45 void XGUI_ContextMenuMgr::createActions()
47 QAction* aAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
48 QMainWindow* aDesktop = myWorkshop->mainWindow();
50 aDesktop = myWorkshop->salomeConnector()->desktop();
51 aDesktop->addAction(aAction);
53 addAction("DELETE_CMD", aAction);
54 aAction->setShortcut(Qt::Key_Delete);
55 aAction->setShortcutContext(Qt::ApplicationShortcut);
57 aAction = new QAction(QIcon(":pictures/rename_edit.png"), tr("Rename"), this);
58 addAction("RENAME_CMD", aAction);
59 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onRename()));
61 aAction = new QAction(QIcon(":pictures/move.png"), tr("Move..."), this);
62 addAction("MOVE_CMD", aAction);
64 aAction = new QAction(QIcon(":pictures/color.png"), tr("Color..."), this);
65 addAction("COLOR_CMD", aAction);
67 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), this);
68 addAction("SHOW_CMD", aAction);
70 aAction = new QAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"), this);
71 addAction("SHOW_ONLY_CMD", aAction);
73 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"), this);
74 addAction("HIDE_CMD", aAction);
76 aAction = new QAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"), this);
77 addAction("HIDEALL_CMD", aAction);
79 aAction = new QAction(QIcon(":pictures/shading.png"), tr("Shading"), this);
80 addAction("SHADING_CMD", aAction);
82 aAction = new QAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"), this);
83 addAction("WIREFRAME_CMD", aAction);
85 mySeparator = new QAction(this);
86 mySeparator->setSeparator(true);
89 buildObjBrowserMenu();
93 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
95 if (myActions.contains(theId))
96 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
97 theAction->setData(theId);
98 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
99 myActions[theId] = theAction;
102 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
104 if (myActions.contains(theId))
105 return myActions[theId];
109 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
111 foreach(QAction* eachAction, myActions) {
112 if (eachAction->text() == theName) {
119 QStringList XGUI_ContextMenuMgr::actionIds() const
121 return myActions.keys();
124 void XGUI_ContextMenuMgr::onAction(bool isChecked)
126 QAction* aAction = static_cast<QAction*>(sender());
127 emit actionTriggered(aAction->data().toString(), isChecked);
130 void XGUI_ContextMenuMgr::updateCommandsStatus()
134 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
136 QMenu* aMenu = new QMenu();
137 if (sender() == myWorkshop->objectBrowser()) {
138 updateObjectBrowserMenu();
139 addObjBrowserMenu(aMenu);
140 } else if (sender() == myWorkshop->viewer()) {
142 addViewerMenu(aMenu);
145 if (aMenu && (aMenu->actions().size() > 0)) {
146 // it is possible that some objects should do something before and after the popup menu exec
147 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
149 emit beforeContextMenu();
150 aMenu->exec(theEvent->globalPos());
151 emit afterContextMenu();
156 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
158 foreach(QAction* aAction, myActions)
159 aAction->setEnabled(false);
161 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
162 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
163 int aSelected = aObjects.size();
165 SessionPtr aMgr = ModelAPI_Session::get();
166 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
167 bool hasResult = false;
168 bool hasFeature = false;
169 bool hasParameter = false;
170 bool hasSubFeature = false;
171 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
174 if (aSelected == 1) {
175 ObjectPtr aObject = aObjects.first();
177 if (hasResult && myWorkshop->canBeShaded(aObject)) {
178 action("WIREFRAME_CMD")->setEnabled(true);
179 action("SHADING_CMD")->setEnabled(true);
182 if (aObject->isDisplayed()) {
183 action("HIDE_CMD")->setEnabled(true);
184 } else if (hasResult && (!hasParameter)) {
185 action("SHOW_CMD")->setEnabled(true);
188 if (!(hasParameter || hasFeature))
189 action("SHOW_ONLY_CMD")->setEnabled(true);
191 else if (hasFeature && myWorkshop->canMoveFeature())
192 action("MOVE_CMD")->setEnabled(true);
194 if( aMgr->activeDocument() == aObject->document() )
196 action("RENAME_CMD")->setEnabled(true);
197 action("DELETE_CMD")->setEnabled(true);
201 // parameter is commented because the actions are not in the list of result parameter actions
202 if (hasResult /*&& (!hasParameter)*/) {
203 action("SHOW_CMD")->setEnabled(true);
204 action("HIDE_CMD")->setEnabled(true);
205 action("SHOW_ONLY_CMD")->setEnabled(true);
206 action("SHADING_CMD")->setEnabled(true);
207 action("WIREFRAME_CMD")->setEnabled(true);
210 bool allActive = true;
211 foreach( ObjectPtr aObject, aObjects )
212 if( aMgr->activeDocument() != aObject->document() )
217 if (!hasSubFeature && allActive ) {
218 if (hasFeature || hasParameter)
219 action("DELETE_CMD")->setEnabled(true);
222 if (myWorkshop->canChangeColor())
223 action("COLOR_CMD")->setEnabled(true);
225 ModuleBase_IModule* aModule = myWorkshop->module();
227 aModule->updateObjectBrowserMenu(myActions);
230 void XGUI_ContextMenuMgr::updateViewerMenu()
232 foreach(QAction* aAction, myActions)
233 aAction->setEnabled(false);
235 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
236 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
237 if (aPrsList.size() > 0) {
238 bool isVisible = false;
239 bool isShading = false;
240 bool canBeShaded = false;
242 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
243 aObject = aPrs.object();
244 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
245 if (aRes && aRes->isDisplayed()) {
247 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
248 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
254 action("WIREFRAME_CMD")->setEnabled(true);
255 action("SHADING_CMD")->setEnabled(true);
257 action("SHOW_ONLY_CMD")->setEnabled(true);
258 action("HIDE_CMD")->setEnabled(true);
260 action("SHOW_CMD")->setEnabled(true);
262 if (myWorkshop->displayer()->objectsCount() > 0)
263 action("HIDEALL_CMD")->setEnabled(true);
264 if (myWorkshop->canChangeColor())
265 action("COLOR_CMD")->setEnabled(true);
267 action("DELETE_CMD")->setEnabled(true);
269 ModuleBase_IModule* aModule = myWorkshop->module();
271 aModule->updateViewerMenu(myActions);
274 void XGUI_ContextMenuMgr::connectObjectBrowser()
276 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
277 SLOT(onContextMenuRequest(QContextMenuEvent*)));
280 void XGUI_ContextMenuMgr::connectViewer()
282 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
283 SLOT(onContextMenuRequest(QContextMenuEvent*)));
287 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
289 QAction* aSeparator = new QAction(this);
290 aSeparator->setSeparator(true);
294 // Result construction menu
295 aList.append(action("SHOW_CMD"));
296 aList.append(action("HIDE_CMD"));
297 aList.append(action("SHOW_ONLY_CMD"));
298 aList.append(action("COLOR_CMD"));
299 aList.append(mySeparator);
300 aList.append(action("RENAME_CMD"));
301 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
302 //-------------------------------------
305 aList.append(action("WIREFRAME_CMD"));
306 aList.append(action("SHADING_CMD"));
307 aList.append(action("COLOR_CMD"));
308 aList.append(mySeparator);
309 aList.append(action("SHOW_CMD"));
310 aList.append(action("HIDE_CMD"));
311 aList.append(action("SHOW_ONLY_CMD"));
312 aList.append(mySeparator);
313 aList.append(action("RENAME_CMD"));
314 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
316 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
318 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
319 //-------------------------------------
322 aList.append(action("DELETE_CMD"));
323 aList.append(action("MOVE_CMD"));
324 aList.append(mySeparator);
325 aList.append(action("RENAME_CMD"));
326 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
329 aList.append(action("DELETE_CMD"));
330 aList.append(mySeparator);
331 aList.append(action("RENAME_CMD"));
332 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
333 //-------------------------------------
336 void XGUI_ContextMenuMgr::buildViewerMenu()
339 // Result construction menu
340 aList.append(action("HIDE_CMD"));
341 aList.append(action("SHOW_ONLY_CMD"));
342 aList.append(action("HIDEALL_CMD"));
343 aList.append(action("COLOR_CMD"));
344 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
346 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
347 //-------------------------------------
350 aList.append(action("WIREFRAME_CMD"));
351 aList.append(action("SHADING_CMD"));
352 aList.append(action("COLOR_CMD"));
353 aList.append(mySeparator);
354 aList.append(action("HIDE_CMD"));
355 aList.append(action("SHOW_ONLY_CMD"));
356 aList.append(action("HIDEALL_CMD"));
357 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
359 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
360 //-------------------------------------
365 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
367 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
368 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
369 int aSelected = aObjects.size();
370 QActionsList aActions;
371 if (aSelected == 1) {
372 ObjectPtr aObject = aObjects.first();
373 std::string aName = aObject->groupName();
374 if (myObjBrowserMenus.contains(aName))
375 aActions = myObjBrowserMenus[aName];
376 } else if (aSelected > 1) {
377 aActions.append(action("SHADING_CMD"));
378 aActions.append(action("WIREFRAME_CMD"));
379 aActions.append(mySeparator);
380 aActions.append(action("SHOW_CMD"));
381 aActions.append(action("HIDE_CMD"));
382 aActions.append(action("SHOW_ONLY_CMD"));
383 aActions.append(mySeparator);
384 aActions.append(action("DELETE_CMD"));
385 //aActions.append(action("MOVE_CMD"));
387 theMenu->addActions(aActions);
389 ModuleBase_IModule* aModule = myWorkshop->module();
391 theMenu->addSeparator();
392 aModule->addObjectBrowserMenu(theMenu);
394 theMenu->addSeparator();
395 theMenu->addActions(myWorkshop->objectBrowser()->actions());
398 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
400 ModuleBase_IModule* aModule = myWorkshop->module();
402 if (aModule->addViewerMenu(theMenu, myActions))
403 theMenu->addSeparator();
405 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
406 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
407 int aSelected = aPrsList.size();
408 QActionsList aActions;
409 if (aSelected == 1) {
410 ObjectPtr aObject = aPrsList.first().object();
411 std::string aName = aObject->groupName();
412 if (myViewerMenu.contains(aName))
413 aActions = myViewerMenu[aName];
414 } else if (aSelected > 1) {
415 aActions.append(action("HIDE_CMD"));
417 theMenu->addActions(aActions);
419 if (!myWorkshop->isSalomeMode()) {
420 theMenu->addSeparator();
421 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
422 if (aMDI->actions().size() > 0) {
423 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
424 aSubMenu->addActions(aMDI->actions());
429 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
433 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
434 aLast = myObjBrowserMenus.end();
435 for (; anIt != aLast; anIt++) {
436 QString aGroupName(anIt.key().c_str());
437 if (aGroups.contains(aGroupName))
439 QActionsList anActions = anIt.value();
440 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
442 for (; anAIt != anALast && !aFound; anAIt++)
443 aFound = (*anAIt)->data().toString() == theName;
445 aGroups.append(aGroupName);
450 void XGUI_ContextMenuMgr::onRename()
452 myWorkshop->objectBrowser()->onEditItem();