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