]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_ContextMenuMgr.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into CEA_2019
[modules/shaper.git] / src / XGUI / XGUI_ContextMenuMgr.cpp
1 // Copyright (C) 2014-2019  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
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_SalomeConnector.h"
27 #include "XGUI_Selection.h"
28 #include "XGUI_SelectionActivate.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 #include <ModelAPI_Folder.h>
51 #include <ModelAPI_AttributeReference.h>
52 #include <ModelAPI_ResultField.h>
53
54 #include <Config_DataModelReader.h>
55
56 #include <ModuleBase_IModule.h>
57 #include <ModuleBase_Tools.h>
58 #include <ModuleBase_OperationAction.h>
59 #include <ModuleBase_ViewerPrs.h>
60
61 #include <QAction>
62 #include <QActionGroup>
63 #include <QContextMenuEvent>
64 #include <QMenu>
65 #include <QMdiArea>
66 #include <QMainWindow>
67 #include <QModelIndex>
68
69 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
70     : QObject(theParent),
71       myWorkshop(theParent),
72       mySeparator1(0), mySeparator2(0), mySeparator3(0)
73 {
74 }
75
76 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
77 {
78 }
79
80 void XGUI_ContextMenuMgr::createActions()
81 {
82 #ifdef HAVE_SALOME
83   QMainWindow* aDesktop = myWorkshop->salomeConnector()->desktop();
84 #else
85   QMainWindow* aDesktop = myWorkshop->mainWindow();
86 #endif
87
88   QAction* aAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
89                                                     aDesktop);
90   aDesktop->addAction(aAction);
91
92   addAction("DELETE_CMD", aAction);
93   aAction->setShortcutContext(Qt::ApplicationShortcut);
94
95   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/rename_edit.png"), tr("Rename"),
96                                            aDesktop, this, SLOT(onRename()));
97   addAction("RENAME_CMD", aAction);
98
99   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move.png"),
100                                            XGUI_Workshop::MOVE_TO_END_COMMAND, this);
101   addAction("MOVE_CMD", aAction);
102
103   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/clean_history.png"),
104                                            tr("Clean history"), aDesktop);
105   addAction("CLEAN_HISTORY_CMD", aAction);
106
107   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/color.png"), tr("Color..."), aDesktop);
108   addAction("COLOR_CMD", aAction);
109
110   aAction = ModuleBase_Tools::createAction(QIcon(""), tr("Deflection..."), aDesktop);
111   addAction("DEFLECTION_CMD", aAction);
112
113   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/transparency.png"),
114                                            tr("Transparency..."), aDesktop);
115   addAction("TRANSPARENCY_CMD", aAction);
116
117   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), aDesktop);
118   addAction("SHOW_CMD", aAction);
119
120   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"),
121                                            aDesktop);
122   addAction("SHOW_ONLY_CMD", aAction);
123
124   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"),
125                                            aDesktop);
126   addAction("HIDE_CMD", aAction);
127
128   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"),
129                                            aDesktop);
130   addAction("HIDEALL_CMD", aAction);
131
132   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/shading.png"), tr("Shading"), aDesktop);
133   addAction("SHADING_CMD", aAction);
134
135   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"),
136                                            aDesktop);
137   addAction("WIREFRAME_CMD", aAction);
138
139   mySeparator1 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
140   mySeparator1->setSeparator(true);
141
142   mySeparator2 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
143   mySeparator2->setSeparator(true);
144
145   mySeparator3 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
146   mySeparator3->setSeparator(true);
147
148   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/vertex.png"), tr("Vertices"), aDesktop,
149                                            this, SLOT(onShapeSelection(bool)));
150   aAction->setCheckable(true);
151   addAction("SELECT_VERTEX_CMD", aAction);
152
153   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/edge.png"), tr("Edges"), aDesktop,
154                                            this, SLOT(onShapeSelection(bool)));
155   aAction->setCheckable(true);
156   addAction("SELECT_EDGE_CMD", aAction);
157
158   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/face.png"), tr("Faces"), aDesktop,
159                                            this, SLOT(onShapeSelection(bool)));
160   aAction->setCheckable(true);
161   addAction("SELECT_FACE_CMD", aAction);
162
163   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/result.png"), tr("Results"), aDesktop,
164                                            this, SLOT(onResultSelection(bool)));
165   aAction->setCheckable(true);
166   addAction("SELECT_RESULT_CMD", aAction);
167
168   aAction->setChecked(true);
169
170   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
171                                            tr("Select results"), aDesktop);
172   addAction("SHOW_RESULTS_CMD", aAction);
173
174   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
175                                            tr("Select parent feature"), aDesktop);
176   addAction("SHOW_FEATURE_CMD", aAction);
177
178 #ifdef TINSPECTOR
179   aAction = ModuleBase_Tools::createAction(QIcon(), tr("TInspector"), aDesktop);
180   addAction("TINSPECTOR_VIEW", aAction);
181 #endif
182
183   // Features folders actions
184   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/create_folder.png"),
185                                            tr("Insert a folder before"), aDesktop);
186   addAction("INSERT_FOLDER_CMD", aAction);
187
188   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/insert_folder_before.png"),
189                                            tr("Move into the previous folder"), aDesktop);
190   addAction("ADD_TO_FOLDER_BEFORE_CMD", aAction);
191
192   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/insert_folder_after.png"),
193                                            tr("Move into the next folder"), aDesktop);
194   addAction("ADD_TO_FOLDER_AFTER_CMD", aAction);
195
196   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_out_before.png"),
197                                            tr("Move out before the folder"), aDesktop);
198   addAction("ADD_OUT_FOLDER_BEFORE_CMD", aAction);
199
200   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_out_after.png"),
201                                            tr("Move out after the folder"), aDesktop);
202   addAction("ADD_OUT_FOLDER_AFTER_CMD", aAction);
203
204   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/normal-view-inversed.png"),
205                                            tr("Set view by inverted normal to face"), aDesktop);
206   addAction("SET_VIEW_INVERTEDNORMAL_CMD", aAction);
207
208   aAction = ModuleBase_Tools::createAction(QIcon(":pictures/normal-view.png"),
209                                            tr("Set view by normal to face"), aDesktop);
210   addAction("SET_VIEW_NORMAL_CMD", aAction);
211
212   buildObjBrowserMenu();
213   buildViewerMenu();
214 }
215
216 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
217 {
218   if (myActions.contains(theId))
219     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
220   theAction->setData(theId);
221   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
222   myActions[theId] = theAction;
223 }
224
225 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
226 {
227   if (myActions.contains(theId))
228     return myActions[theId];
229   return 0;
230 }
231
232 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
233 {
234   foreach(QAction* eachAction, myActions) {
235     if (eachAction->text() == theName) {
236       return eachAction;
237     }
238   }
239   return NULL;
240 }
241
242 QStringList XGUI_ContextMenuMgr::actionIds() const
243 {
244   return myActions.keys();
245 }
246
247 void XGUI_ContextMenuMgr::onAction(bool isChecked)
248 {
249   QAction* aAction = static_cast<QAction*>(sender());
250   emit actionTriggered(aAction->data().toString(), isChecked);
251 }
252
253 void XGUI_ContextMenuMgr::updateCommandsStatus()
254 {
255 }
256
257 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
258 {
259   QMenu* aMenu = new QMenu();
260   if (sender() == myWorkshop->objectBrowser()) {
261     updateObjectBrowserMenu();
262     addObjBrowserMenu(aMenu);
263   } else if (sender() == myWorkshop->viewer()) {
264     updateViewerMenu();
265     addViewerMenu(aMenu);
266   }
267
268   if (aMenu && (aMenu->actions().size() > 0)) {
269     // it is possible that some objects should do something before and after the popup menu exec
270     // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
271     // a created entity
272     emit beforeContextMenu();
273     aMenu->exec(theEvent->globalPos());
274     emit afterContextMenu();
275     delete aMenu;
276   }
277 }
278
279 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
280 {
281   foreach(QAction* aAction, myActions)
282     aAction->setEnabled(false);
283
284   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
285   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
286   int aSelected = aObjects.size();
287   if (aSelected > 0) {
288     SessionPtr aMgr = ModelAPI_Session::get();
289     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
290     bool hasResult = false;
291     bool hasFeature = false;
292     bool hasParameter = false;
293     bool hasCompositeOwner = false;
294     bool hasResultInHistory = false;
295     bool hasFolder = false;
296     bool canBeDeleted = true;
297     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
298                                    hasCompositeOwner, hasResultInHistory, hasFolder);
299     //Process Feature
300     if (aSelected == 1) { // single selection
301       ObjectPtr aObject = aObjects.first();
302       if (aObject) {
303         if (hasResult && myWorkshop->canBeShaded(aObject)) {
304           XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
305           if (aMode != XGUI_Displayer::NoMode) {
306             action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
307             action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
308           } else {
309             action("WIREFRAME_CMD")->setEnabled(true);
310             action("SHADING_CMD")->setEnabled(true);
311           }
312         }
313         if (!hasFeature) {
314           bool aHasSubResults = ModelAPI_Tools::hasSubResults(
315                                             std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
316           if (aHasSubResults) {
317             action("HIDE_CMD")->setEnabled(true);
318             action("SHOW_CMD")->setEnabled(true);
319           }
320           else {
321             if (aObject->isDisplayed()) {
322               action("HIDE_CMD")->setEnabled(true);
323             } else if (hasResult && (!hasParameter)) {
324               action("SHOW_CMD")->setEnabled(true);
325             }
326           }
327           if (!(hasParameter || hasFeature))
328             action("SHOW_ONLY_CMD")->setEnabled(true);
329         }
330         else if (hasFeature && myWorkshop->canMoveFeature())
331           action("MOVE_CMD")->setEnabled(true);
332
333         if( aMgr->activeDocument() == aObject->document() )
334         {
335           action("RENAME_CMD")->setEnabled(true);
336           if (aObject->groupName() == ModelAPI_ResultConstruction::group()) {
337             FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
338             canBeDeleted = aFeature->isInHistory();
339             action("DELETE_CMD")->setEnabled(canBeDeleted);
340           }
341           else
342             action("DELETE_CMD")->setEnabled(!hasCompositeOwner);
343           action("CLEAN_HISTORY_CMD")->setEnabled(!hasCompositeOwner &&
344                                                   (hasFeature || hasParameter));
345         }
346       }
347       // end single selection
348     } else { // multi-selection
349       // parameter is commented because the actions are not in the list of result parameter actions
350       if (hasResult /*&& (!hasParameter)*/) {
351         action("SHOW_CMD")->setEnabled(true);
352         action("HIDE_CMD")->setEnabled(true);
353         action("SHOW_ONLY_CMD")->setEnabled(true);
354         action("SHADING_CMD")->setEnabled(true);
355         action("WIREFRAME_CMD")->setEnabled(true);
356
357         foreach(ObjectPtr aObj, aObjects) {
358           FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
359           if (!aFeature->isInHistory()) {
360             canBeDeleted = false;
361             break;
362           }
363         }
364         action("DELETE_CMD")->setEnabled(canBeDeleted);
365       }
366       if (hasFeature && myWorkshop->canMoveFeature())
367         action("MOVE_CMD")->setEnabled(true);
368     } // end multi-selection
369
370     // Check folder management commands state if only features are selected
371     if ((!hasResult) && hasFeature && (!hasParameter) && (!hasCompositeOwner) &&
372       (!hasResultInHistory) && (!hasFolder)) {
373       std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
374       if (aFeatures.size() > 0) { // Check that features do not include Parts
375         QModelIndexList aIndexes = aSelMgr->selection()->selectedIndexes();
376         QModelIndex aFirstIdx = aIndexes.first();
377         QModelIndex aLastIdx = aIndexes.last();
378         QModelIndex aParentIdx = aFirstIdx.parent();
379
380         // if all selected are from the same level
381         bool isSameParent = true;
382         foreach(QModelIndex aIdx, aIndexes) {
383           if (aIdx.parent() != aParentIdx) {
384             isSameParent = false;
385             break;
386           }
387         }
388         if (isSameParent) {
389           // Check is selection continuous
390           XGUI_DataModel* aModel = myWorkshop->objectBrowser()->dataModel();
391           DocumentPtr aDoc = aMgr->activeDocument();
392           std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
393
394           bool isContinuos = true;
395           if (aSelected > 1) {
396             int aId = -1;
397             foreach(FeaturePtr aF, aFeatures) {
398               if (aId == -1)
399                 aId = aDoc->index(aF);
400               else {
401                 aId++;
402                 if (aId != aDoc->index(aF)) {
403                   isContinuos = false;
404                   break;
405                 }
406               }
407             }
408           }
409           if (isContinuos) {
410             ObjectPtr aDataObj = aModel->object(aParentIdx);
411
412             ObjectPtr aPrevObj;
413             if (aFirstIdx.row() > 0) {
414               QModelIndex aPrevIdx = aFirstIdx.sibling(aFirstIdx.row() - 1, 0);
415               aPrevObj = aModel->object(aPrevIdx);
416             }
417
418             ObjectPtr aNextObj;
419             if (aLastIdx.row() < (aModel->rowCount(aParentIdx) - 1)) {
420               QModelIndex aNextIdx = aFirstIdx.sibling(aLastIdx.row() + 1, 0);
421               aNextObj = aModel->object(aNextIdx);
422             }
423
424             bool isPrevFolder = (aPrevObj.get() &&
425               (aPrevObj->groupName() == ModelAPI_Folder::group()));
426             bool isNextFolder = (aNextObj.get() &&
427               (aNextObj->groupName() == ModelAPI_Folder::group()));
428             bool isInFolder = (aDataObj.get() &&
429               (aDataObj->groupName() == ModelAPI_Folder::group()));
430             bool isOutsideFolder = !isInFolder;
431
432             bool hasFirst = false;
433             bool hasLast = false;
434             if (isInFolder) {
435               FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aDataObj);
436               FeaturePtr aFirstFeatureInFolder;
437               AttributeReferencePtr aFirstFeatAttr =
438                   aFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
439               if (aFirstFeatAttr)
440                 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
441               hasFirst = (aFirstFeatureInFolder == aFeatures.front());
442
443               FeaturePtr aLastFeatureInFolder;
444               AttributeReferencePtr aLastFeatAttr =
445                   aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
446               if (aLastFeatAttr)
447                 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
448               hasLast = (aLastFeatureInFolder == aFeatures.back());
449             }
450             action("INSERT_FOLDER_CMD")->setEnabled(isOutsideFolder);
451             action("ADD_TO_FOLDER_BEFORE_CMD")->setEnabled(isOutsideFolder && isPrevFolder);
452             action("ADD_TO_FOLDER_AFTER_CMD")->setEnabled(isOutsideFolder && isNextFolder);
453             action("ADD_OUT_FOLDER_BEFORE_CMD")->setEnabled(isInFolder && hasFirst);
454             action("ADD_OUT_FOLDER_AFTER_CMD")->setEnabled(isInFolder && hasLast);
455           }
456         }
457       }
458     } // end folder management commands
459
460     bool allActive = true;
461     foreach( ObjectPtr aObject, aObjects )
462       if( aMgr->activeDocument() != aObject->document() )  {
463         allActive = false;
464         break;
465       }
466     if (!hasCompositeOwner && allActive ) {
467       if (hasResult || hasFeature || hasParameter)  // #2924 results can be erased
468         action("DELETE_CMD")->setEnabled(canBeDeleted);
469     }
470     if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
471       action("CLEAN_HISTORY_CMD")->setEnabled(true);
472
473     action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
474     action("SHOW_FEATURE_CMD")->setEnabled(hasResult && hasResultInHistory);
475   } // end selection processing
476
477   // Show/Hide command has to be disabled for objects from non active document
478   bool aDeactivate = false;
479   foreach (ObjectPtr aObj, aObjects) {
480     if (!aObj->document()->isActive()) {
481       if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
482            aObj->groupName() == ModelAPI_ResultPart::group()) {
483         aDeactivate = true;
484         break;
485       }
486     }
487   }
488   if (aDeactivate) {
489     // If at leas a one object can not be edited then Show/Hide has to be disabled
490     action("SHOW_CMD")->setEnabled(false);
491     action("HIDE_CMD")->setEnabled(false);
492     action("SHOW_ONLY_CMD")->setEnabled(false);
493   }
494
495   action("COLOR_CMD")->setEnabled(myWorkshop->canChangeProperty("COLOR_CMD"));
496   action("DEFLECTION_CMD")->setEnabled(myWorkshop->canChangeProperty("DEFLECTION_CMD"));
497   action("TRANSPARENCY_CMD")->setEnabled(myWorkshop->canChangeProperty("TRANSPARENCY_CMD"));
498   #ifdef _DEBUG
499     #ifdef TINSPECTOR
500       action("TINSPECTOR_VIEW")->setEnabled(true);
501     #endif
502   #endif
503
504
505   ModuleBase_IModule* aModule = myWorkshop->module();
506   if (aModule)
507     aModule->updateObjectBrowserMenu(myActions);
508 }
509
510 void XGUI_ContextMenuMgr::updateViewerMenu()
511 {
512   foreach(QAction* aAction, myActions)
513     aAction->setEnabled(false);
514
515   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
516   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
517   QList<ModuleBase_ViewerPrsPtr> aPrsList =
518     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
519   if (aPrsList.size() > 0) {
520     bool isVisible = false;
521     bool isShading = false;
522     bool canBeShaded = false;
523     bool hasPlanar = false;
524     ObjectPtr aObject;
525     foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
526       aObject = aPrs->object();
527       if (!aObject.get())
528         continue;
529       GeomShapePtr aShape = aPrs->shape();
530       if (aObject->isDisplayed()) {
531         isVisible = true;
532         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
533         isShading =
534           (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
535       }
536       if (aShape.get()) {
537         if (aShape->isPlanar()) {
538           hasPlanar = true;
539         }
540       }
541     }
542     if (isVisible) {
543       if (canBeShaded) {
544         XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
545         if (aMode != XGUI_Displayer::NoMode) {
546           action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
547           action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
548         } else {
549           action("WIREFRAME_CMD")->setEnabled(true);
550           action("SHADING_CMD")->setEnabled(true);
551         }
552       }
553       action("SHOW_ONLY_CMD")->setEnabled(true);
554       action("HIDE_CMD")->setEnabled(true);
555     } else
556       action("SHOW_CMD")->setEnabled(true);
557
558     action("SET_VIEW_NORMAL_CMD")->setEnabled(hasPlanar);
559     action("SET_VIEW_INVERTEDNORMAL_CMD")->setEnabled(hasPlanar);
560   }
561   //issue #2159 Hide all incomplete behavior
562 #ifdef HAVE_SALOME
563     action("HIDEALL_CMD")->setEnabled(true);
564 #else
565   if (myWorkshop->displayer()->objectsCount() > 0)
566     action("HIDEALL_CMD")->setEnabled(true);
567 #endif
568
569   // Update selection menu
570   QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
571   action("SELECT_VERTEX_CMD")->setEnabled(true);
572   action("SELECT_EDGE_CMD")->setEnabled(true);
573   action("SELECT_FACE_CMD")->setEnabled(true);
574   action("SELECT_RESULT_CMD")->setEnabled(true);
575
576   action("SELECT_RESULT_CMD")->setChecked(false);
577   action("SELECT_VERTEX_CMD")->setChecked(false);
578   action("SELECT_EDGE_CMD")->setChecked(false);
579   action("SELECT_FACE_CMD")->setChecked(false);
580   action("SELECT_RESULT_CMD")->setChecked(false);
581   if (aModes.count() == 0) {
582     action("SELECT_RESULT_CMD")->setChecked(true);
583   } else {
584     foreach(int aMode, aModes) {
585       switch (aMode) {
586       case TopAbs_VERTEX:
587         action("SELECT_VERTEX_CMD")->setChecked(true);
588         break;
589       case TopAbs_EDGE:
590         action("SELECT_EDGE_CMD")->setChecked(true);
591         break;
592       case TopAbs_FACE:
593         action("SELECT_FACE_CMD")->setChecked(true);
594         break;
595       default:
596         action("SELECT_RESULT_CMD")->setChecked(true);
597       }
598     }
599   }
600
601   ModuleBase_IModule* aModule = myWorkshop->module();
602   if (aModule)
603     aModule->updateViewerMenu(myActions);
604
605   if (myWorkshop->canChangeProperty("COLOR_CMD"))
606     action("COLOR_CMD")->setEnabled(true);
607
608   if (myWorkshop->canChangeProperty("DEFLECTION_CMD"))
609     action("DEFLECTION_CMD")->setEnabled(true);
610
611   if (myWorkshop->canChangeProperty("TRANSPARENCY_CMD"))
612     action("TRANSPARENCY_CMD")->setEnabled(true);
613
614   // Delete command is not used in viewer pop-up menu
615   action("DELETE_CMD")->setEnabled(false);
616 }
617
618 void XGUI_ContextMenuMgr::connectObjectBrowser()
619 {
620   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
621           SLOT(onContextMenuRequest(QContextMenuEvent*)));
622 }
623
624 void XGUI_ContextMenuMgr::connectViewer()
625 {
626   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
627           SLOT(onContextMenuRequest(QContextMenuEvent*)));
628 }
629
630
631 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
632 {
633   QAction* aSeparator = ModuleBase_Tools::createAction(QIcon(), "", myWorkshop->desktop());
634   aSeparator->setSeparator(true);
635
636   QActionsList aList;
637
638   // Result construction menu
639   aList.append(action("SHOW_CMD"));
640   aList.append(action("HIDE_CMD"));
641   aList.append(action("SHOW_ONLY_CMD"));
642   aList.append(mySeparator1);
643   aList.append(action("RENAME_CMD"));
644   aList.append(action("COLOR_CMD"));
645   aList.append(action("DEFLECTION_CMD"));
646   aList.append(action("TRANSPARENCY_CMD"));
647   aList.append(action("SHOW_FEATURE_CMD"));
648   aList.append(mySeparator2);
649   aList.append(action("DELETE_CMD"));
650   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
651
652   //-------------------------------------
653   // Result body menu
654   aList.clear();
655   aList.append(action("WIREFRAME_CMD"));
656   aList.append(action("SHADING_CMD"));
657   aList.append(mySeparator1); // this separator is not shown as this action is added after show only
658   // qt list container contains only one instance of the same action
659   aList.append(action("SHOW_CMD"));
660   aList.append(action("HIDE_CMD"));
661   aList.append(action("SHOW_ONLY_CMD"));
662   aList.append(mySeparator2);
663   aList.append(action("RENAME_CMD"));
664   aList.append(action("COLOR_CMD"));
665   aList.append(action("DEFLECTION_CMD"));
666   aList.append(action("TRANSPARENCY_CMD"));
667   aList.append(action("SHOW_FEATURE_CMD"));
668   aList.append(mySeparator3);
669   aList.append(action("DELETE_CMD"));
670   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
671   // Group menu
672   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
673   myObjBrowserMenus[ModelAPI_ResultField::group()] = aList;
674   // Result part menu
675   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
676   //-------------------------------------
677   // Feature menu
678   aList.clear();
679   aList.append(action("RENAME_CMD"));
680   aList.append(action("SHOW_RESULTS_CMD"));
681   aList.append(action("MOVE_CMD"));
682   aList.append(mySeparator1);
683   aList.append(action("INSERT_FOLDER_CMD"));
684   aList.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
685   aList.append(action("ADD_TO_FOLDER_AFTER_CMD"));
686   aList.append(mySeparator2);
687   aList.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
688   aList.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
689   aList.append(mySeparator3);
690   aList.append(action("CLEAN_HISTORY_CMD"));
691   aList.append(action("DELETE_CMD"));
692   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
693
694   aList.clear();
695   aList.append(action("RENAME_CMD"));
696   aList.append(mySeparator1);
697   aList.append(action("CLEAN_HISTORY_CMD"));
698   aList.append(action("DELETE_CMD"));
699   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
700   //-------------------------------------
701
702   aList.clear();
703   aList.append(action("RENAME_CMD"));
704   aList.append(action("DELETE_CMD"));
705   myObjBrowserMenus[ModelAPI_Folder::group()] = aList;
706
707   //---------------------------------------
708   // Step objects menu
709   aList.clear();
710   aList.append(action("SHOW_CMD"));
711   aList.append(action("HIDE_CMD"));
712   aList.append(action("SHOW_ONLY_CMD"));
713   myObjBrowserMenus[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
714 }
715
716 void XGUI_ContextMenuMgr::buildViewerMenu()
717 {
718   QActionsList aList;
719   // Result construction menu
720   aList.append(action("COLOR_CMD"));
721   aList.append(action("DEFLECTION_CMD"));
722   aList.append(action("TRANSPARENCY_CMD"));
723   aList.append(mySeparator3);
724   aList.append(action("SET_VIEW_NORMAL_CMD"));
725   aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
726   aList.append(mySeparator1);
727   aList.append(action("SHOW_ONLY_CMD"));
728   aList.append(action("HIDE_CMD"));
729   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
730   // Result part menu
731   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
732   //-------------------------------------
733   // Result body menu
734   aList.clear();
735   aList.append(action("WIREFRAME_CMD"));
736   aList.append(action("SHADING_CMD"));
737   aList.append(mySeparator2);
738   aList.append(action("COLOR_CMD"));
739   aList.append(action("DEFLECTION_CMD"));
740   aList.append(action("TRANSPARENCY_CMD"));
741   aList.append(mySeparator3);
742   aList.append(action("SET_VIEW_NORMAL_CMD"));
743   aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
744   aList.append(mySeparator1);
745   aList.append(action("SHOW_ONLY_CMD"));
746   aList.append(action("HIDE_CMD"));
747   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
748   // Group menu
749   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
750   myViewerMenu[ModelAPI_ResultField::group()] = aList;
751   //-------------------------------------
752   // Step objects menu
753   aList.clear();
754   aList.append(action("HIDE_CMD"));
755   myViewerMenu[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
756 }
757
758
759 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
760 {
761   ModuleBase_IModule* aModule = myWorkshop->module();
762   if (aModule) {
763     theMenu->addSeparator();
764     aModule->addObjectBrowserMenu(theMenu);
765   }
766
767   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
768   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
769   int aSelected = aObjects.size();
770   QActionsList aActions;
771   if (aSelected == 1) {
772     ObjectPtr aObject = aObjects.first();
773     std::string aName = aObject->groupName();
774     if (myObjBrowserMenus.contains(aName))
775       aActions = myObjBrowserMenus[aName];
776   } else if (aSelected > 1) {
777       aActions.append(action("WIREFRAME_CMD"));
778       aActions.append(action("SHADING_CMD"));
779       aActions.append(mySeparator1);
780       aActions.append(action("SHOW_CMD"));
781       aActions.append(action("HIDE_CMD"));
782       aActions.append(action("SHOW_ONLY_CMD"));
783       aActions.append(mySeparator2);
784       aActions.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
785       aActions.append(action("ADD_TO_FOLDER_AFTER_CMD"));
786       aActions.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
787       aActions.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
788       aActions.append(mySeparator3);
789       aActions.append(action("MOVE_CMD"));
790       aActions.append(action("COLOR_CMD"));
791       aActions.append(action("DEFLECTION_CMD"));
792       aActions.append(action("TRANSPARENCY_CMD"));
793       aActions.append(action("CLEAN_HISTORY_CMD"));
794       aActions.append(action("DELETE_CMD"));
795   }
796 #ifdef _DEBUG
797   if (aSelected == 0) {
798     #ifdef TINSPECTOR
799     aActions.append(action("TINSPECTOR_VIEW"));
800     #endif
801   }
802 #endif
803   theMenu->addActions(aActions);
804   addFeatures(theMenu);
805
806   // It is commented out because Object Browser does not have actions
807   //theMenu->addSeparator();
808   //theMenu->addActions(myWorkshop->objectBrowser()->actions());
809 }
810
811 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
812 {
813   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
814   QList<ModuleBase_ViewerPrsPtr> aPrsList =
815     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
816   int aSelected = aPrsList.size();
817   QActionsList aActions;
818
819   // Create selection menu
820   XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
821   if (!aOpMgr->hasOperation() &&
822       myWorkshop->selectionActivate()->activeSelectionPlace() == XGUI_SelectionActivate::Workshop) {
823     QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
824     aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
825     aSelMenu->addAction(action("SELECT_EDGE_CMD"));
826     aSelMenu->addAction(action("SELECT_FACE_CMD"));
827     //IMP: an attempt to use result selection with other selection modes
828     //aSelMenu->addSeparator();
829     aSelMenu->addAction(action("SELECT_RESULT_CMD"));
830     theMenu->addMenu(aSelMenu);
831     theMenu->addSeparator();
832   }
833   if (aSelected == 1) {
834     ObjectPtr aObject = aPrsList.first()->object();
835     if (aObject.get() != NULL) {
836       std::string aName = aObject->groupName();
837       if (myViewerMenu.contains(aName))
838         aActions = myViewerMenu[aName];
839     }
840   } else if (aSelected > 1) {
841     aActions.append(action("COLOR_CMD"));
842     aActions.append(action("DEFLECTION_CMD"));
843     aActions.append(action("TRANSPARENCY_CMD"));
844     aActions.append(mySeparator1);
845     aActions.append(action("SHOW_ONLY_CMD"));
846     aActions.append(action("HIDE_CMD"));
847   }
848   // hide all is shown always even if selection in the viewer is empty
849   aActions.append(action("HIDEALL_CMD"));
850   theMenu->addActions(aActions);
851
852   QMap<int, QAction*> aMenuActions;
853   ModuleBase_IModule* aModule = myWorkshop->module();
854   if (aModule) {
855     if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
856       theMenu->addSeparator();
857   }
858
859   // insert the module menu items on specific positions in the popup menu: some actions should be
860   // in the begin of the list, Delete action should be the last by #1343 issue
861   QList<QAction*> anActions = theMenu->actions();
862   int anActionsSize = anActions.size();
863   QAction* aFirstAction = anActions[0];
864   QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
865   for (; anIt != aLast; anIt++) {
866     if (anIt.key() > anActionsSize)
867       theMenu->addAction(anIt.value());
868     else
869       theMenu->insertAction(aFirstAction, *anIt);
870   }
871
872 #ifndef HAVE_SALOME
873   theMenu->addSeparator();
874   QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
875   if (aMDI->actions().size() > 0) {
876     QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
877     aSubMenu->addActions(aMDI->actions());
878   }
879 #endif
880 }
881
882 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
883 {
884   QStringList aGroups;
885
886   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
887                                                   aLast = myObjBrowserMenus.end();
888   for (; anIt != aLast; anIt++) {
889     QString aGroupName(anIt.key().c_str());
890     if (aGroups.contains(aGroupName))
891       continue;
892     QActionsList anActions = anIt.value();
893     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
894     bool aFound = false;
895     for (; anAIt != anALast && !aFound; anAIt++)
896       aFound = (*anAIt)->data().toString() == theName;
897     if (aFound)
898       aGroups.append(aGroupName);
899   }
900   return aGroups;
901 }
902
903 void XGUI_ContextMenuMgr::onRename()
904 {
905   QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
906   if (!myWorkshop->abortAllOperations())
907     return;
908   // restore selection in case if dialog box was shown
909   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
910   myWorkshop->objectBrowser()->onEditItem();
911 }
912
913 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
914 {
915   SessionPtr aMgr = ModelAPI_Session::get();
916   DocumentPtr aActiveDoc = aMgr->activeDocument();
917
918   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
919   XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
920   const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
921   QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
922
923   QString aName;
924   int aLen = 0;
925   bool aIsRoot = false;
926   foreach(QModelIndex aIdx, aSelectedIndexes) {
927     // Process only first column
928     if (aIdx.column() == 1) {
929       aIsRoot = !aIdx.parent().isValid();
930       // Exit if the selected index belongs to non active document
931       if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
932         return;
933
934       // Get name of the selected index
935       aName = aIdx.data().toString();
936       aLen = aName.indexOf('(');
937       if (aLen != -1) {
938         aName = aName.left(--aLen);
939       }
940       std::string aFeaturesStr = aIsRoot?
941         aDataModelXML->rootFolderFeatures(aName.toStdString()) :
942         aDataModelXML->subFolderFeatures(aName.toStdString());
943         if (aFeaturesStr.length() > 0) {
944           QStringList aFeturesList =
945             QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
946           foreach(QString aFea, aFeturesList) {
947             QAction* aAction = aActionMgr->action(aFea);
948             if (aAction)
949               theMenu->addAction(aAction);
950           }
951         }
952     }
953   }
954 }
955
956 #define UNCHECK_ACTION(NAME) \
957 { QAction* aAction = action(NAME); \
958 bool isBlock = aAction->signalsBlocked(); \
959 aAction->blockSignals(true); \
960 aAction->setChecked(false); \
961   aAction->blockSignals(isBlock); }
962
963
964 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
965 {
966   UNCHECK_ACTION("SELECT_VERTEX_CMD");
967   UNCHECK_ACTION("SELECT_EDGE_CMD");
968   UNCHECK_ACTION("SELECT_FACE_CMD");
969 }
970
971 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
972 {
973   UNCHECK_ACTION("SHOW_RESULTS_CMD");
974 }