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