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();
178 if (aObject->isDisplayed()) {
179 if (aDisplayer->canBeShaded(aObject)) {
180 action("WIREFRAME_CMD")->setEnabled(true);
181 action("SHADING_CMD")->setEnabled(true);
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 if (hasResult && (!hasParameter)) {
202 action("SHOW_CMD")->setEnabled(true);
203 action("HIDE_CMD")->setEnabled(true);
204 action("SHOW_ONLY_CMD")->setEnabled(true);
205 action("SHADING_CMD")->setEnabled(true);
206 action("WIREFRAME_CMD")->setEnabled(true);
209 bool allActive = true;
210 foreach( ObjectPtr aObject, aObjects )
211 if( aMgr->activeDocument() != aObject->document() )
216 if (!hasSubFeature && allActive ) {
217 if (hasFeature || hasParameter)
218 action("DELETE_CMD")->setEnabled(true);
221 if (myWorkshop->canChangeColor())
222 action("COLOR_CMD")->setEnabled(true);
224 ModuleBase_IModule* aModule = myWorkshop->module();
226 aModule->updateObjectBrowserMenu(myActions);
229 void XGUI_ContextMenuMgr::updateViewerMenu()
231 foreach(QAction* aAction, myActions)
232 aAction->setEnabled(false);
234 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
235 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
236 if (aPrsList.size() > 0) {
237 bool isVisible = false;
238 bool isShading = false;
239 bool canBeShaded = false;
241 foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
242 aObject = aPrs.object();
243 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
244 if (aRes && aRes->isDisplayed()) {
246 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
247 isShading = (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
253 action("WIREFRAME_CMD")->setEnabled(true);
254 action("SHADING_CMD")->setEnabled(true);
256 action("SHOW_ONLY_CMD")->setEnabled(true);
257 action("HIDE_CMD")->setEnabled(true);
259 action("SHOW_CMD")->setEnabled(true);
261 if (myWorkshop->displayer()->objectsCount() > 0)
262 action("HIDEALL_CMD")->setEnabled(true);
263 if (myWorkshop->canChangeColor())
264 action("COLOR_CMD")->setEnabled(true);
266 action("DELETE_CMD")->setEnabled(true);
268 ModuleBase_IModule* aModule = myWorkshop->module();
270 aModule->updateViewerMenu(myActions);
273 void XGUI_ContextMenuMgr::connectObjectBrowser()
275 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
276 SLOT(onContextMenuRequest(QContextMenuEvent*)));
279 void XGUI_ContextMenuMgr::connectViewer()
281 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
282 SLOT(onContextMenuRequest(QContextMenuEvent*)));
286 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
288 QAction* aSeparator = new QAction(this);
289 aSeparator->setSeparator(true);
293 // Result construction menu
294 aList.append(action("SHOW_CMD"));
295 aList.append(action("HIDE_CMD"));
296 aList.append(action("SHOW_ONLY_CMD"));
297 aList.append(action("COLOR_CMD"));
298 aList.append(mySeparator);
299 aList.append(action("RENAME_CMD"));
300 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
301 //-------------------------------------
304 aList.append(action("WIREFRAME_CMD"));
305 aList.append(action("SHADING_CMD"));
306 aList.append(action("COLOR_CMD"));
307 aList.append(mySeparator);
308 aList.append(action("SHOW_CMD"));
309 aList.append(action("HIDE_CMD"));
310 aList.append(action("SHOW_ONLY_CMD"));
311 aList.append(mySeparator);
312 aList.append(action("RENAME_CMD"));
313 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
315 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
317 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
318 //-------------------------------------
321 aList.append(action("DELETE_CMD"));
322 aList.append(action("MOVE_CMD"));
323 aList.append(mySeparator);
324 aList.append(action("RENAME_CMD"));
325 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
328 aList.append(action("DELETE_CMD"));
329 aList.append(mySeparator);
330 aList.append(action("RENAME_CMD"));
331 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
332 //-------------------------------------
335 void XGUI_ContextMenuMgr::buildViewerMenu()
338 // Result construction menu
339 aList.append(action("HIDE_CMD"));
340 aList.append(action("SHOW_ONLY_CMD"));
341 aList.append(action("HIDEALL_CMD"));
342 aList.append(action("COLOR_CMD"));
343 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
345 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
346 //-------------------------------------
349 aList.append(action("WIREFRAME_CMD"));
350 aList.append(action("SHADING_CMD"));
351 aList.append(action("COLOR_CMD"));
352 aList.append(mySeparator);
353 aList.append(action("HIDE_CMD"));
354 aList.append(action("SHOW_ONLY_CMD"));
355 aList.append(action("HIDEALL_CMD"));
356 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
358 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
359 //-------------------------------------
364 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
366 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
367 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
368 int aSelected = aObjects.size();
369 QActionsList aActions;
370 if (aSelected == 1) {
371 ObjectPtr aObject = aObjects.first();
372 std::string aName = aObject->groupName();
373 if (myObjBrowserMenus.contains(aName))
374 aActions = myObjBrowserMenus[aName];
375 } else if (aSelected > 1) {
376 aActions.append(action("SHADING_CMD"));
377 aActions.append(action("WIREFRAME_CMD"));
378 aActions.append(mySeparator);
379 aActions.append(action("SHOW_CMD"));
380 aActions.append(action("HIDE_CMD"));
381 aActions.append(action("SHOW_ONLY_CMD"));
382 aActions.append(mySeparator);
383 aActions.append(action("DELETE_CMD"));
384 //aActions.append(action("MOVE_CMD"));
386 theMenu->addActions(aActions);
388 ModuleBase_IModule* aModule = myWorkshop->module();
390 theMenu->addSeparator();
391 aModule->addObjectBrowserMenu(theMenu);
393 theMenu->addSeparator();
394 theMenu->addActions(myWorkshop->objectBrowser()->actions());
397 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
399 ModuleBase_IModule* aModule = myWorkshop->module();
401 if (aModule->addViewerMenu(theMenu, myActions))
402 theMenu->addSeparator();
404 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
405 QList<ModuleBase_ViewerPrs> aPrsList = aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
406 int aSelected = aPrsList.size();
407 QActionsList aActions;
408 if (aSelected == 1) {
409 ObjectPtr aObject = aPrsList.first().object();
410 std::string aName = aObject->groupName();
411 if (myViewerMenu.contains(aName))
412 aActions = myViewerMenu[aName];
413 } else if (aSelected > 1) {
414 aActions.append(action("HIDE_CMD"));
416 theMenu->addActions(aActions);
418 if (!myWorkshop->isSalomeMode()) {
419 theMenu->addSeparator();
420 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
421 if (aMDI->actions().size() > 0) {
422 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
423 aSubMenu->addActions(aMDI->actions());
428 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
432 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
433 aLast = myObjBrowserMenus.end();
434 for (; anIt != aLast; anIt++) {
435 QString aGroupName(anIt.key().c_str());
436 if (aGroups.contains(aGroupName))
438 QActionsList anActions = anIt.value();
439 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
441 for (; anAIt != anALast && !aFound; anAIt++)
442 aFound = (*anAIt)->data().toString() == theName;
444 aGroups.append(aGroupName);
449 void XGUI_ContextMenuMgr::onRename()
451 myWorkshop->objectBrowser()->onEditItem();