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