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