Salome HOME
Merge remote-tracking branch 'origin/ngo/Lot5'
[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     } // end multi-selection
367
368     // Check folder management commands state if only features are selected
369     if ((!hasResult) && hasFeature && (!hasParameter) && (!hasCompositeOwner) &&
370       (!hasResultInHistory) && (!hasFolder)) {
371       std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
372       if (aFeatures.size() > 0) { // Check that features do not include Parts
373         QModelIndexList aIndexes = aSelMgr->selection()->selectedIndexes();
374         QModelIndex aFirstIdx = aIndexes.first();
375         QModelIndex aLastIdx = aIndexes.last();
376         QModelIndex aParentIdx = aFirstIdx.parent();
377
378         // if all selected are from the same level
379         bool isSameParent = true;
380         foreach(QModelIndex aIdx, aIndexes) {
381           if (aIdx.parent() != aParentIdx) {
382             isSameParent = false;
383             break;
384           }
385         }
386         if (isSameParent) {
387           // Check is selection continuous
388           XGUI_DataModel* aModel = myWorkshop->objectBrowser()->dataModel();
389           DocumentPtr aDoc = aMgr->activeDocument();
390           std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
391
392           bool isContinuos = true;
393           if (aSelected > 1) {
394             int aId = -1;
395             foreach(FeaturePtr aF, aFeatures) {
396               if (aId == -1)
397                 aId = aDoc->index(aF);
398               else {
399                 aId++;
400                 if (aId != aDoc->index(aF)) {
401                   isContinuos = false;
402                   break;
403                 }
404               }
405             }
406           }
407           if (isContinuos) {
408             ObjectPtr aDataObj = aModel->object(aParentIdx);
409
410             ObjectPtr aPrevObj;
411             if (aFirstIdx.row() > 0) {
412               QModelIndex aPrevIdx = aFirstIdx.sibling(aFirstIdx.row() - 1, 0);
413               aPrevObj = aModel->object(aPrevIdx);
414             }
415
416             ObjectPtr aNextObj;
417             if (aLastIdx.row() < (aModel->rowCount(aParentIdx) - 1)) {
418               QModelIndex aNextIdx = aFirstIdx.sibling(aLastIdx.row() + 1, 0);
419               aNextObj = aModel->object(aNextIdx);
420             }
421
422             bool isPrevFolder = (aPrevObj.get() &&
423               (aPrevObj->groupName() == ModelAPI_Folder::group()));
424             bool isNextFolder = (aNextObj.get() &&
425               (aNextObj->groupName() == ModelAPI_Folder::group()));
426             bool isInFolder = (aDataObj.get() &&
427               (aDataObj->groupName() == ModelAPI_Folder::group()));
428             bool isOutsideFolder = !isInFolder;
429
430             bool hasFirst = false;
431             bool hasLast = false;
432             if (isInFolder) {
433               FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aDataObj);
434               FeaturePtr aFirstFeatureInFolder;
435               AttributeReferencePtr aFirstFeatAttr =
436                   aFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
437               if (aFirstFeatAttr)
438                 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
439               hasFirst = (aFirstFeatureInFolder == aFeatures.front());
440
441               FeaturePtr aLastFeatureInFolder;
442               AttributeReferencePtr aLastFeatAttr =
443                   aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
444               if (aLastFeatAttr)
445                 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
446               hasLast = (aLastFeatureInFolder == aFeatures.back());
447             }
448             action("INSERT_FOLDER_CMD")->setEnabled(isOutsideFolder);
449             action("ADD_TO_FOLDER_BEFORE_CMD")->setEnabled(isOutsideFolder && isPrevFolder);
450             action("ADD_TO_FOLDER_AFTER_CMD")->setEnabled(isOutsideFolder && isNextFolder);
451             action("ADD_OUT_FOLDER_BEFORE_CMD")->setEnabled(isInFolder && hasFirst);
452             action("ADD_OUT_FOLDER_AFTER_CMD")->setEnabled(isInFolder && hasLast);
453           }
454         }
455       }
456     } // end folder management commands
457
458     bool allActive = true;
459     foreach( ObjectPtr aObject, aObjects )
460       if( aMgr->activeDocument() != aObject->document() )  {
461         allActive = false;
462         break;
463       }
464     if (!hasCompositeOwner && allActive ) {
465       if (hasResult || hasFeature || hasParameter)  // #2924 results can be erased
466         action("DELETE_CMD")->setEnabled(canBeDeleted);
467     }
468     if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
469       action("CLEAN_HISTORY_CMD")->setEnabled(true);
470
471     action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
472     action("SHOW_FEATURE_CMD")->setEnabled(hasResult && hasResultInHistory);
473   } // end selection processing
474
475   // Show/Hide command has to be disabled for objects from non active document
476   bool aDeactivate = false;
477   foreach (ObjectPtr aObj, aObjects) {
478     if (!aObj->document()->isActive()) {
479       if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
480            aObj->groupName() == ModelAPI_ResultPart::group()) {
481         aDeactivate = true;
482         break;
483       }
484     }
485   }
486   if (aDeactivate) {
487     // If at leas a one object can not be edited then Show/Hide has to be disabled
488     action("SHOW_CMD")->setEnabled(false);
489     action("HIDE_CMD")->setEnabled(false);
490     action("SHOW_ONLY_CMD")->setEnabled(false);
491   }
492
493   action("COLOR_CMD")->setEnabled(myWorkshop->canChangeProperty("COLOR_CMD"));
494   action("DEFLECTION_CMD")->setEnabled(myWorkshop->canChangeProperty("DEFLECTION_CMD"));
495   action("TRANSPARENCY_CMD")->setEnabled(myWorkshop->canChangeProperty("TRANSPARENCY_CMD"));
496   #ifdef _DEBUG
497     #ifdef TINSPECTOR
498       action("TINSPECTOR_VIEW")->setEnabled(true);
499     #endif
500   #endif
501
502
503   ModuleBase_IModule* aModule = myWorkshop->module();
504   if (aModule)
505     aModule->updateObjectBrowserMenu(myActions);
506 }
507
508 void XGUI_ContextMenuMgr::updateViewerMenu()
509 {
510   foreach(QAction* aAction, myActions)
511     aAction->setEnabled(false);
512
513   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
514   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
515   QList<ModuleBase_ViewerPrsPtr> aPrsList =
516     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
517   if (aPrsList.size() > 0) {
518     bool isVisible = false;
519     bool isShading = false;
520     bool canBeShaded = false;
521     bool hasPlanar = false;
522     ObjectPtr aObject;
523     foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
524       aObject = aPrs->object();
525       if (!aObject.get())
526         continue;
527       GeomShapePtr aShape = aPrs->shape();
528       if (aObject->isDisplayed()) {
529         isVisible = true;
530         canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
531         isShading =
532           (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
533       }
534       if (aShape.get()) {
535         if (aShape->isPlanar()) {
536           hasPlanar = true;
537         }
538       }
539     }
540     if (isVisible) {
541       if (canBeShaded) {
542         XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
543         if (aMode != XGUI_Displayer::NoMode) {
544           action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
545           action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
546         } else {
547           action("WIREFRAME_CMD")->setEnabled(true);
548           action("SHADING_CMD")->setEnabled(true);
549         }
550       }
551       action("SHOW_ONLY_CMD")->setEnabled(true);
552       action("HIDE_CMD")->setEnabled(true);
553     } else
554       action("SHOW_CMD")->setEnabled(true);
555
556     action("SET_VIEW_NORMAL_CMD")->setEnabled(hasPlanar);
557     action("SET_VIEW_INVERTEDNORMAL_CMD")->setEnabled(hasPlanar);
558   }
559   //issue #2159 Hide all incomplete behavior
560 #ifdef HAVE_SALOME
561     action("HIDEALL_CMD")->setEnabled(true);
562 #else
563   if (myWorkshop->displayer()->objectsCount() > 0)
564     action("HIDEALL_CMD")->setEnabled(true);
565 #endif
566
567   // Update selection menu
568   QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
569   action("SELECT_VERTEX_CMD")->setEnabled(true);
570   action("SELECT_EDGE_CMD")->setEnabled(true);
571   action("SELECT_FACE_CMD")->setEnabled(true);
572   action("SELECT_RESULT_CMD")->setEnabled(true);
573
574   action("SELECT_RESULT_CMD")->setChecked(false);
575   action("SELECT_VERTEX_CMD")->setChecked(false);
576   action("SELECT_EDGE_CMD")->setChecked(false);
577   action("SELECT_FACE_CMD")->setChecked(false);
578   action("SELECT_RESULT_CMD")->setChecked(false);
579   if (aModes.count() == 0) {
580     action("SELECT_RESULT_CMD")->setChecked(true);
581   } else {
582     foreach(int aMode, aModes) {
583       switch (aMode) {
584       case TopAbs_VERTEX:
585         action("SELECT_VERTEX_CMD")->setChecked(true);
586         break;
587       case TopAbs_EDGE:
588         action("SELECT_EDGE_CMD")->setChecked(true);
589         break;
590       case TopAbs_FACE:
591         action("SELECT_FACE_CMD")->setChecked(true);
592         break;
593       default:
594         action("SELECT_RESULT_CMD")->setChecked(true);
595       }
596     }
597   }
598
599   ModuleBase_IModule* aModule = myWorkshop->module();
600   if (aModule)
601     aModule->updateViewerMenu(myActions);
602
603   if (myWorkshop->canChangeProperty("COLOR_CMD"))
604     action("COLOR_CMD")->setEnabled(true);
605
606   if (myWorkshop->canChangeProperty("DEFLECTION_CMD"))
607     action("DEFLECTION_CMD")->setEnabled(true);
608
609   if (myWorkshop->canChangeProperty("TRANSPARENCY_CMD"))
610     action("TRANSPARENCY_CMD")->setEnabled(true);
611
612   // Delete command is not used in viewer pop-up menu
613   action("DELETE_CMD")->setEnabled(false);
614 }
615
616 void XGUI_ContextMenuMgr::connectObjectBrowser()
617 {
618   connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
619           SLOT(onContextMenuRequest(QContextMenuEvent*)));
620 }
621
622 void XGUI_ContextMenuMgr::connectViewer()
623 {
624   connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
625           SLOT(onContextMenuRequest(QContextMenuEvent*)));
626 }
627
628
629 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
630 {
631   QAction* aSeparator = ModuleBase_Tools::createAction(QIcon(), "", myWorkshop->desktop());
632   aSeparator->setSeparator(true);
633
634   QActionsList aList;
635
636   // Result construction menu
637   aList.append(action("SHOW_CMD"));
638   aList.append(action("HIDE_CMD"));
639   aList.append(action("SHOW_ONLY_CMD"));
640   aList.append(mySeparator1);
641   aList.append(action("RENAME_CMD"));
642   aList.append(action("COLOR_CMD"));
643   aList.append(action("DEFLECTION_CMD"));
644   aList.append(action("TRANSPARENCY_CMD"));
645   aList.append(action("SHOW_FEATURE_CMD"));
646   aList.append(mySeparator2);
647   aList.append(action("DELETE_CMD"));
648   myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
649
650   //-------------------------------------
651   // Result body menu
652   aList.clear();
653   aList.append(action("WIREFRAME_CMD"));
654   aList.append(action("SHADING_CMD"));
655   aList.append(mySeparator1); // this separator is not shown as this action is added after show only
656   // qt list container contains only one instance of the same action
657   aList.append(action("SHOW_CMD"));
658   aList.append(action("HIDE_CMD"));
659   aList.append(action("SHOW_ONLY_CMD"));
660   aList.append(mySeparator2);
661   aList.append(action("RENAME_CMD"));
662   aList.append(action("COLOR_CMD"));
663   aList.append(action("DEFLECTION_CMD"));
664   aList.append(action("TRANSPARENCY_CMD"));
665   aList.append(action("SHOW_FEATURE_CMD"));
666   aList.append(mySeparator3);
667   aList.append(action("DELETE_CMD"));
668   myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
669   // Group menu
670   myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
671   myObjBrowserMenus[ModelAPI_ResultField::group()] = aList;
672   // Result part menu
673   myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
674   //-------------------------------------
675   // Feature menu
676   aList.clear();
677   aList.append(action("RENAME_CMD"));
678   aList.append(action("SHOW_RESULTS_CMD"));
679   aList.append(action("MOVE_CMD"));
680   aList.append(mySeparator1);
681   aList.append(action("INSERT_FOLDER_CMD"));
682   aList.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
683   aList.append(action("ADD_TO_FOLDER_AFTER_CMD"));
684   aList.append(mySeparator2);
685   aList.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
686   aList.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
687   aList.append(mySeparator3);
688   aList.append(action("CLEAN_HISTORY_CMD"));
689   aList.append(action("DELETE_CMD"));
690   myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
691
692   aList.clear();
693   aList.append(action("RENAME_CMD"));
694   aList.append(mySeparator1);
695   aList.append(action("CLEAN_HISTORY_CMD"));
696   aList.append(action("DELETE_CMD"));
697   myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
698   //-------------------------------------
699
700   aList.clear();
701   aList.append(action("RENAME_CMD"));
702   aList.append(action("DELETE_CMD"));
703   myObjBrowserMenus[ModelAPI_Folder::group()] = aList;
704
705   //---------------------------------------
706   // Step objects menu
707   aList.clear();
708   aList.append(action("SHOW_CMD"));
709   aList.append(action("HIDE_CMD"));
710   aList.append(action("SHOW_ONLY_CMD"));
711   myObjBrowserMenus[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
712 }
713
714 void XGUI_ContextMenuMgr::buildViewerMenu()
715 {
716   QActionsList aList;
717   // Result construction menu
718   aList.append(action("COLOR_CMD"));
719   aList.append(action("DEFLECTION_CMD"));
720   aList.append(action("TRANSPARENCY_CMD"));
721   aList.append(mySeparator3);
722   aList.append(action("SET_VIEW_NORMAL_CMD"));
723   aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
724   aList.append(mySeparator1);
725   aList.append(action("SHOW_ONLY_CMD"));
726   aList.append(action("HIDE_CMD"));
727   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
728   // Result part menu
729   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
730   //-------------------------------------
731   // Result body menu
732   aList.clear();
733   aList.append(action("WIREFRAME_CMD"));
734   aList.append(action("SHADING_CMD"));
735   aList.append(mySeparator2);
736   aList.append(action("COLOR_CMD"));
737   aList.append(action("DEFLECTION_CMD"));
738   aList.append(action("TRANSPARENCY_CMD"));
739   aList.append(mySeparator3);
740   aList.append(action("SET_VIEW_NORMAL_CMD"));
741   aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
742   aList.append(mySeparator1);
743   aList.append(action("SHOW_ONLY_CMD"));
744   aList.append(action("HIDE_CMD"));
745   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
746   // Group menu
747   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
748   myViewerMenu[ModelAPI_ResultField::group()] = aList;
749   //-------------------------------------
750   // Step objects menu
751   aList.clear();
752   aList.append(action("HIDE_CMD"));
753   myViewerMenu[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
754 }
755
756
757 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
758 {
759   ModuleBase_IModule* aModule = myWorkshop->module();
760   if (aModule) {
761     theMenu->addSeparator();
762     aModule->addObjectBrowserMenu(theMenu);
763   }
764
765   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
766   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
767   int aSelected = aObjects.size();
768   QActionsList aActions;
769   if (aSelected == 1) {
770     ObjectPtr aObject = aObjects.first();
771     std::string aName = aObject->groupName();
772     if (myObjBrowserMenus.contains(aName))
773       aActions = myObjBrowserMenus[aName];
774   } else if (aSelected > 1) {
775       aActions.append(action("WIREFRAME_CMD"));
776       aActions.append(action("SHADING_CMD"));
777       aActions.append(mySeparator1);
778       aActions.append(action("SHOW_CMD"));
779       aActions.append(action("HIDE_CMD"));
780       aActions.append(action("SHOW_ONLY_CMD"));
781       aActions.append(mySeparator2);
782       aActions.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
783       aActions.append(action("ADD_TO_FOLDER_AFTER_CMD"));
784       aActions.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
785       aActions.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
786       aActions.append(mySeparator3);
787       //aActions.append(action("MOVE_CMD"));
788       aActions.append(action("COLOR_CMD"));
789       aActions.append(action("DEFLECTION_CMD"));
790       aActions.append(action("TRANSPARENCY_CMD"));
791       aActions.append(action("CLEAN_HISTORY_CMD"));
792       aActions.append(action("DELETE_CMD"));
793   }
794 #ifdef _DEBUG
795   if (aSelected == 0) {
796     #ifdef TINSPECTOR
797     aActions.append(action("TINSPECTOR_VIEW"));
798     #endif
799   }
800 #endif
801   theMenu->addActions(aActions);
802   addFeatures(theMenu);
803
804   // It is commented out because Object Browser does not have actions
805   //theMenu->addSeparator();
806   //theMenu->addActions(myWorkshop->objectBrowser()->actions());
807 }
808
809 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
810 {
811   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
812   QList<ModuleBase_ViewerPrsPtr> aPrsList =
813     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
814   int aSelected = aPrsList.size();
815   QActionsList aActions;
816
817   // Create selection menu
818   XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
819   if (!aOpMgr->hasOperation() &&
820       myWorkshop->selectionActivate()->activeSelectionPlace() == XGUI_SelectionActivate::Workshop) {
821     QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
822     aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
823     aSelMenu->addAction(action("SELECT_EDGE_CMD"));
824     aSelMenu->addAction(action("SELECT_FACE_CMD"));
825     //IMP: an attempt to use result selection with other selection modes
826     //aSelMenu->addSeparator();
827     aSelMenu->addAction(action("SELECT_RESULT_CMD"));
828     theMenu->addMenu(aSelMenu);
829     theMenu->addSeparator();
830   }
831   if (aSelected == 1) {
832     ObjectPtr aObject = aPrsList.first()->object();
833     if (aObject.get() != NULL) {
834       std::string aName = aObject->groupName();
835       if (myViewerMenu.contains(aName))
836         aActions = myViewerMenu[aName];
837     }
838   } else if (aSelected > 1) {
839     aActions.append(action("COLOR_CMD"));
840     aActions.append(action("DEFLECTION_CMD"));
841     aActions.append(action("TRANSPARENCY_CMD"));
842     aActions.append(mySeparator1);
843     aActions.append(action("SHOW_ONLY_CMD"));
844     aActions.append(action("HIDE_CMD"));
845   }
846   // hide all is shown always even if selection in the viewer is empty
847   aActions.append(action("HIDEALL_CMD"));
848   theMenu->addActions(aActions);
849
850   QMap<int, QAction*> aMenuActions;
851   ModuleBase_IModule* aModule = myWorkshop->module();
852   if (aModule) {
853     if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
854       theMenu->addSeparator();
855   }
856
857   // insert the module menu items on specific positions in the popup menu: some actions should be
858   // in the begin of the list, Delete action should be the last by #1343 issue
859   QList<QAction*> anActions = theMenu->actions();
860   int anActionsSize = anActions.size();
861   QAction* aFirstAction = anActions[0];
862   QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
863   for (; anIt != aLast; anIt++) {
864     if (anIt.key() > anActionsSize)
865       theMenu->addAction(anIt.value());
866     else
867       theMenu->insertAction(aFirstAction, *anIt);
868   }
869
870 #ifndef HAVE_SALOME
871   theMenu->addSeparator();
872   QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
873   if (aMDI->actions().size() > 0) {
874     QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
875     aSubMenu->addActions(aMDI->actions());
876   }
877 #endif
878 }
879
880 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
881 {
882   QStringList aGroups;
883
884   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
885                                                   aLast = myObjBrowserMenus.end();
886   for (; anIt != aLast; anIt++) {
887     QString aGroupName(anIt.key().c_str());
888     if (aGroups.contains(aGroupName))
889       continue;
890     QActionsList anActions = anIt.value();
891     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
892     bool aFound = false;
893     for (; anAIt != anALast && !aFound; anAIt++)
894       aFound = (*anAIt)->data().toString() == theName;
895     if (aFound)
896       aGroups.append(aGroupName);
897   }
898   return aGroups;
899 }
900
901 void XGUI_ContextMenuMgr::onRename()
902 {
903   QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
904   if (!myWorkshop->abortAllOperations())
905     return;
906   // restore selection in case if dialog box was shown
907   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
908   myWorkshop->objectBrowser()->onEditItem();
909 }
910
911 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
912 {
913   SessionPtr aMgr = ModelAPI_Session::get();
914   DocumentPtr aActiveDoc = aMgr->activeDocument();
915
916   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
917   XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
918   const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
919   QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
920
921   QString aName;
922   int aLen = 0;
923   bool aIsRoot = false;
924   foreach(QModelIndex aIdx, aSelectedIndexes) {
925     // Process only first column
926     if (aIdx.column() == 1) {
927       aIsRoot = !aIdx.parent().isValid();
928       // Exit if the selected index belongs to non active document
929       if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
930         return;
931
932       // Get name of the selected index
933       aName = aIdx.data().toString();
934       aLen = aName.indexOf('(');
935       if (aLen != -1) {
936         aName = aName.left(--aLen);
937       }
938       std::string aFeaturesStr = aIsRoot?
939         aDataModelXML->rootFolderFeatures(aName.toStdString()) :
940         aDataModelXML->subFolderFeatures(aName.toStdString());
941         if (aFeaturesStr.length() > 0) {
942           QStringList aFeturesList =
943             QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
944           foreach(QString aFea, aFeturesList) {
945             QAction* aAction = aActionMgr->action(aFea);
946             if (aAction)
947               theMenu->addAction(aAction);
948           }
949         }
950     }
951   }
952 }
953
954 #define UNCHECK_ACTION(NAME) \
955 { QAction* aAction = action(NAME); \
956 bool isBlock = aAction->signalsBlocked(); \
957 aAction->blockSignals(true); \
958 aAction->setChecked(false); \
959   aAction->blockSignals(isBlock); }
960
961
962 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
963 {
964   UNCHECK_ACTION("SELECT_VERTEX_CMD");
965   UNCHECK_ACTION("SELECT_EDGE_CMD");
966   UNCHECK_ACTION("SELECT_FACE_CMD");
967 }
968
969 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
970 {
971   UNCHECK_ACTION("SHOW_RESULTS_CMD");
972 }