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