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