Salome HOME
Better management of errors.
[modules/shaper.git] / src / XGUI / XGUI_ContextMenuMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
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"
12 #include "XGUI_OperationMgr.h"
13 #include "XGUI_Tools.h"
14 #include "XGUI_ActionsMgr.h"
15
16 #ifndef HAVE_SALOME
17 #include <AppElements_MainWindow.h>
18 #endif
19
20 //#include "PartSetPlugin_Part.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_AttributeDocRef.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultGroup.h>
27 #include <ModelAPI_ResultParameter.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_ResultField.h>
32
33 #include <Config_DataModelReader.h>
34
35 #include <ModuleBase_IModule.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_OperationAction.h>
38 #include <ModuleBase_ViewerPrs.h>
39
40 #include <QAction>
41 #include <QActionGroup>
42 #include <QContextMenuEvent>
43 #include <QMenu>
44 #include <QMdiArea>
45 #include <QMainWindow>
46 #include <QModelIndex>
47
48 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
49     : QObject(theParent),
50       myWorkshop(theParent),
51       mySeparator(0)
52 {
53 }
54
55 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
56 {
57 }
58
59 void XGUI_ContextMenuMgr::createActions()
60 {
61 #ifdef HAVE_SALOME
62   QMainWindow* aDesktop = myWorkshop->salomeConnector()->desktop();
63 #else
64   QMainWindow* aDesktop = myWorkshop->mainWindow();
65 #endif
66
67   QAction* aAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
68                                                     aDesktop);
69   aDesktop->addAction(aAction);
70
71   addAction("DELETE_CMD", aAction);
72   aAction->setShortcutContext(Qt::ApplicationShortcut);
73
74   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/rename_edit.png"), tr("Rename"),
75                                            aDesktop, this, SLOT(onRename()));
76   addAction("RENAME_CMD", aAction);
77
78   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move.png"),
79                                            XGUI_Workshop::MOVE_TO_END_COMMAND, this);
80   addAction("MOVE_CMD", aAction);
81
82   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/clean_history.png"),
83                                            tr("Clean history"), aDesktop);
84   addAction("CLEAN_HISTORY_CMD", aAction);
85
86   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/color.png"), tr("Color..."), aDesktop);
87   addAction("COLOR_CMD", aAction);
88
89   aAction = ModuleBase_Tools::createAction(QIcon(""), tr("Deflection..."), aDesktop);
90   addAction("DEFLECTION_CMD", aAction);
91
92   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), aDesktop);
93   addAction("SHOW_CMD", aAction);
94
95   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"),
96                                            aDesktop);
97   addAction("SHOW_ONLY_CMD", aAction);
98
99   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"),
100                                            aDesktop);
101   addAction("HIDE_CMD", aAction);
102
103   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"),
104                                            aDesktop);
105   addAction("HIDEALL_CMD", aAction);
106
107   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/shading.png"), tr("Shading"), aDesktop);
108   addAction("SHADING_CMD", aAction);
109
110   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"),
111                                            aDesktop);
112   addAction("WIREFRAME_CMD", aAction);
113
114   mySeparator = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
115   mySeparator->setSeparator(true);
116
117   //mySelectActions = new QActionGroup(this);
118   //mySelectActions->setExclusive(true);
119
120   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/vertex.png"), tr("Vertices"), aDesktop,
121                                            this, SLOT(onShapeSelection(bool)));
122   aAction->setCheckable(true);
123   addAction("SELECT_VERTEX_CMD", aAction);
124   //mySelectActions->addAction(aAction);
125
126   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/edge.png"), tr("Edges"), aDesktop,
127                                            this, SLOT(onShapeSelection(bool)));
128   aAction->setCheckable(true);
129   addAction("SELECT_EDGE_CMD", aAction);
130   //mySelectActions->addAction(aAction);
131
132   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/face.png"), tr("Faces"), aDesktop,
133                                            this, SLOT(onShapeSelection(bool)));
134   aAction->setCheckable(true);
135   addAction("SELECT_FACE_CMD", aAction);
136   //mySelectActions->addAction(aAction);
137
138   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/result.png"), tr("Results"), aDesktop,
139                                            this, SLOT(onResultSelection(bool)));
140   aAction->setCheckable(true);
141   addAction("SELECT_RESULT_CMD", aAction);
142   //mySelectActions->addAction(aAction);
143
144   aAction->setChecked(true);
145
146   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
147                                            tr("Select results"), aDesktop);
148   addAction("SHOW_RESULTS_CMD", aAction);
149
150   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
151                                            tr("Select parent feature"), aDesktop);
152   addAction("SHOW_FEATURE_CMD", aAction);
153
154 #ifdef VINSPECTOR
155   aAction = ModuleBase_Tools::createAction(QIcon(), tr("Debug Visualization"), aDesktop);
156   addAction("VINSPECTOR_VIEW", aAction);
157 #endif
158 #ifdef DFBROWSER
159   aAction = ModuleBase_Tools::createAction(QIcon(), tr("DFBrowser"), aDesktop);
160   addAction("DFBROWSER_VIEW", aAction);
161 #endif
162
163   buildObjBrowserMenu();
164   buildViewerMenu();
165 }
166
167 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
168 {
169   if (myActions.contains(theId))
170     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
171   theAction->setData(theId);
172   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
173   myActions[theId] = theAction;
174 }
175
176 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
177 {
178   if (myActions.contains(theId))
179     return myActions[theId];
180   return 0;
181 }
182
183 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
184 {
185   foreach(QAction* eachAction, myActions) {
186     if (eachAction->text() == theName) {
187       return eachAction;
188     }
189   }
190   return NULL;
191 }
192
193 QStringList XGUI_ContextMenuMgr::actionIds() const
194 {
195   return myActions.keys();
196 }
197
198 void XGUI_ContextMenuMgr::onAction(bool isChecked)
199 {
200   QAction* aAction = static_cast<QAction*>(sender());
201   emit actionTriggered(aAction->data().toString(), isChecked);
202 }
203
204 void XGUI_ContextMenuMgr::updateCommandsStatus()
205 {
206 }
207
208 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
209 {
210   QMenu* aMenu = new QMenu();
211   if (sender() == myWorkshop->objectBrowser()) {
212     updateObjectBrowserMenu();
213     addObjBrowserMenu(aMenu);
214   } else if (sender() == myWorkshop->viewer()) {
215     updateViewerMenu();
216     addViewerMenu(aMenu);
217   }
218
219   if (aMenu && (aMenu->actions().size() > 0)) {
220     // it is possible that some objects should do something before and after the popup menu exec
221     // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
222     // a created entity
223     emit beforeContextMenu();
224     aMenu->exec(theEvent->globalPos());
225     emit afterContextMenu();
226     delete aMenu;
227   }
228 }
229
230 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
231 {
232   foreach(QAction* aAction, myActions)
233     aAction->setEnabled(false);
234
235   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
236   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
237   int aSelected = aObjects.size();
238   if (aSelected > 0) {
239     SessionPtr aMgr = ModelAPI_Session::get();
240     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
241     bool hasResult = false;
242     bool hasFeature = false;
243     bool hasParameter = false;
244     bool hasCompositeOwner = false;
245     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
246                                    hasCompositeOwner);
247     //Process Feature
248     if (aSelected == 1) {
249       ObjectPtr aObject = aObjects.first();
250       if (aObject) {
251         if (hasResult && myWorkshop->canBeShaded(aObject)) {
252           XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
253           if (aMode != XGUI_Displayer::NoMode) {
254             action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
255             action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
256           } else {
257             action("WIREFRAME_CMD")->setEnabled(true);
258             action("SHADING_CMD")->setEnabled(true);
259           }
260         }
261         if (!hasFeature) {
262           bool aHasSubResults = ModelAPI_Tools::hasSubResults(
263                                             std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
264           if (aHasSubResults) {
265             action("HIDE_CMD")->setEnabled(true);
266             action("SHOW_CMD")->setEnabled(true);
267           }
268           else {
269             if (aObject->isDisplayed()) {
270               action("HIDE_CMD")->setEnabled(true);
271             } else if (hasResult && (!hasParameter)) {
272               action("SHOW_CMD")->setEnabled(true);
273             }
274           }
275           if (!(hasParameter || hasFeature))
276             action("SHOW_ONLY_CMD")->setEnabled(true);
277         }
278         else if (hasFeature && myWorkshop->canMoveFeature())
279           action("MOVE_CMD")->setEnabled(true);
280
281         if( aMgr->activeDocument() == aObject->document() )
282         {
283           action("RENAME_CMD")->setEnabled(true);
284           action("DELETE_CMD")->setEnabled(!hasCompositeOwner);
285           action("CLEAN_HISTORY_CMD")->setEnabled(!hasCompositeOwner &&
286                                                   (hasFeature || hasParameter));
287         }
288       }
289     } else {
290       // parameter is commented because the actions are not in the list of result parameter actions
291       if (hasResult /*&& (!hasParameter)*/) {
292         action("SHOW_CMD")->setEnabled(true);
293         action("HIDE_CMD")->setEnabled(true);
294         action("SHOW_ONLY_CMD")->setEnabled(true);
295         action("SHADING_CMD")->setEnabled(true);
296         action("WIREFRAME_CMD")->setEnabled(true);
297       }
298     }
299     bool allActive = true;
300     foreach( ObjectPtr aObject, aObjects )
301       if( aMgr->activeDocument() != aObject->document() )  {
302         allActive = false;
303         break;
304       }
305     if (!hasCompositeOwner && allActive ) {
306       if (hasFeature || hasParameter)
307         action("DELETE_CMD")->setEnabled(true);
308     }
309     if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
310       action("CLEAN_HISTORY_CMD")->setEnabled(true);
311
312     action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
313     action("SHOW_FEATURE_CMD")->setEnabled(hasResult);
314   }
315
316   // Show/Hide command has to be disabled for objects from non active document
317   bool aDeactivate = false;
318   foreach (ObjectPtr aObj, aObjects) {
319     if (!aObj->document()->isActive()) {
320       if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
321            aObj->groupName() == ModelAPI_ResultPart::group()) {
322         aDeactivate = true;
323         break;
324       }
325     }
326   }
327   if (aDeactivate) {
328     // If at leas a one objec can not be edited then Show/Hide has to be disabled
329     action("SHOW_CMD")->setEnabled(false);
330     action("HIDE_CMD")->setEnabled(false);
331     action("SHOW_ONLY_CMD")->setEnabled(false);
332   }
333
334   if (myWorkshop->canChangeColor())
335     action("COLOR_CMD")->setEnabled(true);
336
337   if (myWorkshop->canChangeDeflection())
338     action("DEFLECTION_CMD")->setEnabled(true);
339
340   #ifdef _DEBUG
341     #ifdef VINSPECTOR
342       action("VINSPECTOR_VIEW")->setEnabled(true);
343     #endif
344     #ifdef DFBROWSER
345       action("DFBROWSER_VIEW")->setEnabled(true);
346     #endif
347   #endif
348
349
350   ModuleBase_IModule* aModule = myWorkshop->module();
351   if (aModule)
352     aModule->updateObjectBrowserMenu(myActions);
353 }
354
355 void XGUI_ContextMenuMgr::updateViewerMenu()
356 {
357   foreach(QAction* aAction, myActions)
358     aAction->setEnabled(false);
359
360   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
361   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
362   QList<ModuleBase_ViewerPrsPtr> aPrsList =
363     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
364   if (aPrsList.size() > 0) {
365     bool isVisible = false;
366     bool isShading = false;
367     bool canBeShaded = false;
368     ObjectPtr aObject;
369     foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
370       aObject = aPrs->object();
371       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
372       if (aRes && aRes->isDisplayed()) {
373         isVisible = true;
374         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
375         isShading =
376           (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
377         break;
378       }
379     }
380     if (isVisible) {
381       if (canBeShaded) {
382         XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
383         if (aMode != XGUI_Displayer::NoMode) {
384           action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
385           action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
386         } else {
387           action("WIREFRAME_CMD")->setEnabled(true);
388           action("SHADING_CMD")->setEnabled(true);
389         }
390       }
391       action("SHOW_ONLY_CMD")->setEnabled(true);
392       action("HIDE_CMD")->setEnabled(true);
393     } else
394       action("SHOW_CMD")->setEnabled(true);
395   }
396   if (myWorkshop->displayer()->objectsCount() > 0)
397     action("HIDEALL_CMD")->setEnabled(true);
398
399   // Update selection menu
400   QIntList aModes = aDisplayer->activeSelectionModes();
401   action("SELECT_VERTEX_CMD")->setEnabled(true);
402   action("SELECT_EDGE_CMD")->setEnabled(true);
403   action("SELECT_FACE_CMD")->setEnabled(true);
404   action("SELECT_RESULT_CMD")->setEnabled(true);
405
406   action("SELECT_RESULT_CMD")->setChecked(false);
407   action("SELECT_VERTEX_CMD")->setChecked(false);
408   action("SELECT_EDGE_CMD")->setChecked(false);
409   action("SELECT_FACE_CMD")->setChecked(false);
410   action("SELECT_RESULT_CMD")->setChecked(false);
411   if (aModes.count() == 0) {
412     action("SELECT_RESULT_CMD")->setChecked(true);
413   } else {
414     foreach(int aMode, aModes) {
415       switch (aMode) {
416       case TopAbs_VERTEX:
417         action("SELECT_VERTEX_CMD")->setChecked(true);
418         break;
419       case TopAbs_EDGE:
420         action("SELECT_EDGE_CMD")->setChecked(true);
421         break;
422       case TopAbs_FACE:
423         action("SELECT_FACE_CMD")->setChecked(true);
424         break;
425       default:
426         action("SELECT_RESULT_CMD")->setChecked(true);
427       }
428     }
429   }
430
431   ModuleBase_IModule* aModule = myWorkshop->module();
432   if (aModule)
433     aModule->updateViewerMenu(myActions);
434
435   if (myWorkshop->canChangeColor())
436     action("COLOR_CMD")->setEnabled(true);
437
438   if (myWorkshop->canChangeDeflection())
439     action("DEFLECTION_CMD")->setEnabled(true);
440
441   action("DELETE_CMD")->setEnabled(true);
442 }
443
444 void XGUI_ContextMenuMgr::connectObjectBrowser()
445 {
446   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
447           SLOT(onContextMenuRequest(QContextMenuEvent*)));
448 }
449
450 void XGUI_ContextMenuMgr::connectViewer()
451 {
452   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
453           SLOT(onContextMenuRequest(QContextMenuEvent*)));
454 }
455
456
457 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
458 {
459   QAction* aSeparator = ModuleBase_Tools::createAction(QIcon(), "", myWorkshop->desktop());
460   aSeparator->setSeparator(true);
461
462   QActionsList aList;
463
464   // Result construction menu
465   aList.append(action("SHOW_CMD"));
466   aList.append(action("HIDE_CMD"));
467   aList.append(action("SHOW_ONLY_CMD"));
468   aList.append(mySeparator);
469   aList.append(action("RENAME_CMD"));
470   aList.append(action("COLOR_CMD"));
471   aList.append(action("DEFLECTION_CMD"));
472   aList.append(action("SHOW_FEATURE_CMD"));
473   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
474
475   //-------------------------------------
476   // Result body menu
477   aList.clear();
478   aList.append(action("WIREFRAME_CMD"));
479   aList.append(action("SHADING_CMD"));
480   aList.append(mySeparator); // this separator is not shown as this action is added after show only
481   // qt list container contains only one instance of the same action
482   aList.append(action("SHOW_CMD"));
483   aList.append(action("HIDE_CMD"));
484   aList.append(action("SHOW_ONLY_CMD"));
485   aList.append(mySeparator);
486   aList.append(action("RENAME_CMD"));
487   aList.append(action("COLOR_CMD"));
488   aList.append(action("DEFLECTION_CMD"));
489   aList.append(action("SHOW_FEATURE_CMD"));
490   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
491   // Group menu
492   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
493   myObjBrowserMenus[ModelAPI_ResultField::group()] = aList;
494   // Result part menu
495   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
496   //-------------------------------------
497   // Feature menu
498   aList.clear();
499   aList.append(action("RENAME_CMD"));
500   aList.append(action("SHOW_RESULTS_CMD"));
501   aList.append(action("MOVE_CMD"));
502   aList.append(mySeparator);
503   aList.append(action("CLEAN_HISTORY_CMD"));
504   aList.append(action("DELETE_CMD"));
505   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
506
507   aList.clear();
508   aList.append(action("RENAME_CMD"));
509   aList.append(mySeparator);
510   aList.append(action("CLEAN_HISTORY_CMD"));
511   aList.append(action("DELETE_CMD"));
512   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
513   //-------------------------------------
514 }
515
516 void XGUI_ContextMenuMgr::buildViewerMenu()
517 {
518   QActionsList aList;
519   // Result construction menu
520   aList.append(action("HIDE_CMD"));
521   aList.append(action("SHOW_ONLY_CMD"));
522   aList.append(mySeparator);
523   aList.append(action("COLOR_CMD"));
524   aList.append(action("DEFLECTION_CMD"));
525   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
526   // Result part menu
527   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
528   //-------------------------------------
529   // Result body menu
530   aList.clear();
531   aList.append(action("WIREFRAME_CMD"));
532   aList.append(action("SHADING_CMD"));
533   aList.append(mySeparator);
534   aList.append(action("HIDE_CMD"));
535   aList.append(action("SHOW_ONLY_CMD"));
536   aList.append(mySeparator);
537   aList.append(action("COLOR_CMD"));
538   aList.append(action("DEFLECTION_CMD"));
539   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
540   // Group menu
541   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
542   myViewerMenu[ModelAPI_ResultField::group()] = aList;
543   //-------------------------------------
544 }
545
546
547 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
548 {
549   ModuleBase_IModule* aModule = myWorkshop->module();
550   if (aModule) {
551     theMenu->addSeparator();
552     aModule->addObjectBrowserMenu(theMenu);
553   }
554
555   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
556   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
557   int aSelected = aObjects.size();
558   QActionsList aActions;
559   if (aSelected == 1) {
560     ObjectPtr aObject = aObjects.first();
561     std::string aName = aObject->groupName();
562     if (myObjBrowserMenus.contains(aName))
563       aActions = myObjBrowserMenus[aName];
564   } else if (aSelected > 1) {
565       aActions.append(action("WIREFRAME_CMD"));
566       aActions.append(action("SHADING_CMD"));
567       aActions.append(mySeparator);
568       aActions.append(action("SHOW_CMD"));
569       aActions.append(action("HIDE_CMD"));
570       aActions.append(action("SHOW_ONLY_CMD"));
571       aActions.append(mySeparator);
572       //aActions.append(action("MOVE_CMD"));
573       aActions.append(action("COLOR_CMD"));
574       aActions.append(action("DEFLECTION_CMD"));
575
576       aActions.append(action("CLEAN_HISTORY_CMD"));
577       aActions.append(action("DELETE_CMD"));
578   }
579 #ifdef _DEBUG
580   if (aSelected == 0) {
581     #ifdef VINSPECTOR
582     aActions.append(action("VINSPECTOR_VIEW"));
583     #endif
584     #ifdef DFBROWSER
585     aActions.append(action("DFBROWSER_VIEW"));
586     #endif
587   }
588 #endif
589   theMenu->addActions(aActions);
590   addFeatures(theMenu);
591
592   // It is commented out because Object Browser does not have actions
593   //theMenu->addSeparator();
594   //theMenu->addActions(myWorkshop->objectBrowser()->actions());
595 }
596
597 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
598 {
599   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
600   QList<ModuleBase_ViewerPrsPtr> aPrsList =
601     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
602   int aSelected = aPrsList.size();
603   QActionsList aActions;
604
605   // Create selection menu
606   XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
607   QIntList aModes;
608   myWorkshop->module()->activeSelectionModes(aModes);
609   if ((!aOpMgr->hasOperation()) && aModes.isEmpty()) {
610     QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
611     aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
612     aSelMenu->addAction(action("SELECT_EDGE_CMD"));
613     aSelMenu->addAction(action("SELECT_FACE_CMD"));
614     //IMP: an attempt to use result selection with other selection modes
615     //aSelMenu->addSeparator();
616     aSelMenu->addAction(action("SELECT_RESULT_CMD"));
617     theMenu->addMenu(aSelMenu);
618     theMenu->addSeparator();
619   }
620   if (aSelected == 1) {
621     ObjectPtr aObject = aPrsList.first()->object();
622     if (aObject.get() != NULL) {
623       std::string aName = aObject->groupName();
624       if (myViewerMenu.contains(aName))
625         aActions = myViewerMenu[aName];
626     }
627   } else if (aSelected > 1) {
628     aActions.append(action("HIDE_CMD"));
629   }
630   // hide all is shown always even if selection in the viewer is empty
631   aActions.append(action("HIDEALL_CMD"));
632   aActions.append(action("COLOR_CMD"));
633   aActions.append(action("DEFLECTION_CMD"));
634
635   theMenu->addActions(aActions);
636
637   QMap<int, QAction*> aMenuActions;
638   ModuleBase_IModule* aModule = myWorkshop->module();
639   if (aModule) {
640     if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
641       theMenu->addSeparator();
642   }
643
644   // insert the module menu items on specific positions in the popup menu: some actions should be
645   // in the begin of the list, Delete action should be the last by #1343 issue
646   QList<QAction*> anActions = theMenu->actions();
647   int anActionsSize = anActions.size();
648   QAction* aFirstAction = anActions[0];
649   QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
650   for (; anIt != aLast; anIt++) {
651     if (anIt.key() > anActionsSize)
652       theMenu->addAction(anIt.value());
653     else
654       theMenu->insertAction(aFirstAction, *anIt);
655   }
656
657 #ifndef HAVE_SALOME
658   theMenu->addSeparator();
659   QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
660   if (aMDI->actions().size() > 0) {
661     QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
662     aSubMenu->addActions(aMDI->actions());
663   }
664 #endif
665 }
666
667 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
668 {
669   QStringList aGroups;
670
671   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
672                                                   aLast = myObjBrowserMenus.end();
673   for (; anIt != aLast; anIt++) {
674     QString aGroupName(anIt.key().c_str());
675     if (aGroups.contains(aGroupName))
676       continue;
677     QActionsList anActions = anIt.value();
678     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
679     bool aFound = false;
680     for (; anAIt != anALast && !aFound; anAIt++)
681       aFound = (*anAIt)->data().toString() == theName;
682     if (aFound)
683       aGroups.append(aGroupName);
684   }
685   return aGroups;
686 }
687
688 void XGUI_ContextMenuMgr::onRename()
689 {
690   QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
691   if (!myWorkshop->abortAllOperations())
692     return;
693   // restore selection in case if dialog box was shown
694   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
695   myWorkshop->objectBrowser()->onEditItem();
696 }
697
698 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
699 {
700   SessionPtr aMgr = ModelAPI_Session::get();
701   DocumentPtr aActiveDoc = aMgr->activeDocument();
702
703   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
704   XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
705   const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
706   QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
707
708   QString aName;
709   int aLen = 0;
710   bool aIsRoot = false;
711   foreach(QModelIndex aIdx, aSelectedIndexes) {
712     // Process only first column
713     if (aIdx.column() == 0) {
714       aIsRoot = !aIdx.parent().isValid();
715       // Exit if the selected index belongs to non active document
716       if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
717         return;
718       if ((!aIsRoot) && (aIdx.internalPointer() != aActiveDoc.get()))
719         return;
720
721       // Get name of the selected index
722       aName = aIdx.data().toString();
723       aLen = aName.indexOf('(');
724       if (aLen != -1) {
725         aName = aName.left(--aLen);
726       }
727       std::string aFeaturesStr = aIsRoot?
728         aDataModelXML->rootFolderFeatures(aName.toStdString()) :
729         aDataModelXML->subFolderFeatures(aName.toStdString());
730         if (aFeaturesStr.length() > 0) {
731           QStringList aFeturesList =
732             QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
733           foreach(QString aFea, aFeturesList) {
734             QAction* aAction = aActionMgr->action(aFea);
735             if (aAction)
736               theMenu->addAction(aAction);
737           }
738         }
739     }
740   }
741 }
742
743 #define UNCHECK_ACTION(NAME) \
744 { QAction* aAction = action(NAME); \
745 bool isBlock = aAction->signalsBlocked(); \
746 aAction->blockSignals(true); \
747 aAction->setChecked(false); \
748   aAction->blockSignals(isBlock); }
749
750
751 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
752 {
753   UNCHECK_ACTION("SELECT_VERTEX_CMD");
754   UNCHECK_ACTION("SELECT_EDGE_CMD");
755   UNCHECK_ACTION("SELECT_FACE_CMD");
756 }
757
758 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
759 {
760   UNCHECK_ACTION("SHOW_RESULTS_CMD");
761 }