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