Salome HOME
Merge branch 'Dev_GroupsRevision'
[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("DELETE_CMD"));
680   myObjBrowserMenus[ModelAPI_Folder::group()] = aList;
681
682 }
683
684 void XGUI_ContextMenuMgr::buildViewerMenu()
685 {
686   QActionsList aList;
687   // Result construction menu
688   aList.append(action("HIDE_CMD"));
689   aList.append(action("SHOW_ONLY_CMD"));
690   aList.append(mySeparator1);
691   aList.append(action("COLOR_CMD"));
692   aList.append(action("DEFLECTION_CMD"));
693 #ifdef USE_TRANSPARENCY
694   aList.append(action("TRANSPARENCY_CMD"));
695 #endif
696   myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
697   // Result part menu
698   myViewerMenu[ModelAPI_ResultPart::group()] = aList;
699   //-------------------------------------
700   // Result body menu
701   aList.clear();
702   aList.append(action("WIREFRAME_CMD"));
703   aList.append(action("SHADING_CMD"));
704   aList.append(mySeparator1);
705   aList.append(action("HIDE_CMD"));
706   aList.append(action("SHOW_ONLY_CMD"));
707   aList.append(mySeparator2);
708   aList.append(action("COLOR_CMD"));
709   aList.append(action("DEFLECTION_CMD"));
710 #ifdef USE_TRANSPARENCY
711   aList.append(action("TRANSPARENCY_CMD"));
712 #endif
713   myViewerMenu[ModelAPI_ResultBody::group()] = aList;
714   // Group menu
715   myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
716   myViewerMenu[ModelAPI_ResultField::group()] = aList;
717   //-------------------------------------
718 }
719
720
721 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
722 {
723   ModuleBase_IModule* aModule = myWorkshop->module();
724   if (aModule) {
725     theMenu->addSeparator();
726     aModule->addObjectBrowserMenu(theMenu);
727   }
728
729   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
730   QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
731   int aSelected = aObjects.size();
732   QActionsList aActions;
733   if (aSelected == 1) {
734     ObjectPtr aObject = aObjects.first();
735     std::string aName = aObject->groupName();
736     if (myObjBrowserMenus.contains(aName))
737       aActions = myObjBrowserMenus[aName];
738   } else if (aSelected > 1) {
739       aActions.append(action("WIREFRAME_CMD"));
740       aActions.append(action("SHADING_CMD"));
741       aActions.append(mySeparator1);
742       aActions.append(action("SHOW_CMD"));
743       aActions.append(action("HIDE_CMD"));
744       aActions.append(action("SHOW_ONLY_CMD"));
745       aActions.append(mySeparator2);
746       aActions.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
747       aActions.append(action("ADD_TO_FOLDER_AFTER_CMD"));
748       aActions.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
749       aActions.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
750       aActions.append(mySeparator3);
751       //aActions.append(action("MOVE_CMD"));
752       aActions.append(action("COLOR_CMD"));
753       aActions.append(action("DEFLECTION_CMD"));
754 #ifdef USE_TRANSPARENCY
755       aActions.append(action("TRANSPARENCY_CMD"));
756 #endif
757       aActions.append(action("CLEAN_HISTORY_CMD"));
758       aActions.append(action("DELETE_CMD"));
759   }
760 #ifdef _DEBUG
761   if (aSelected == 0) {
762     #ifdef TINSPECTOR
763     aActions.append(action("TINSPECTOR_VIEW"));
764     #endif
765   }
766 #endif
767   theMenu->addActions(aActions);
768   addFeatures(theMenu);
769
770   // It is commented out because Object Browser does not have actions
771   //theMenu->addSeparator();
772   //theMenu->addActions(myWorkshop->objectBrowser()->actions());
773 }
774
775 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
776 {
777   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
778   QList<ModuleBase_ViewerPrsPtr> aPrsList =
779     aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
780   int aSelected = aPrsList.size();
781   QActionsList aActions;
782
783   // Create selection menu
784   XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
785   if (!aOpMgr->hasOperation() &&
786       myWorkshop->selectionActivate()->activeSelectionPlace() == XGUI_SelectionActivate::Workshop) {
787     QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
788     aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
789     aSelMenu->addAction(action("SELECT_EDGE_CMD"));
790     aSelMenu->addAction(action("SELECT_FACE_CMD"));
791     //IMP: an attempt to use result selection with other selection modes
792     //aSelMenu->addSeparator();
793     aSelMenu->addAction(action("SELECT_RESULT_CMD"));
794     theMenu->addMenu(aSelMenu);
795     theMenu->addSeparator();
796   }
797   if (aSelected == 1) {
798     ObjectPtr aObject = aPrsList.first()->object();
799     if (aObject.get() != NULL) {
800       std::string aName = aObject->groupName();
801       if (myViewerMenu.contains(aName))
802         aActions = myViewerMenu[aName];
803     }
804   } else if (aSelected > 1) {
805     aActions.append(action("HIDE_CMD"));
806   }
807   // hide all is shown always even if selection in the viewer is empty
808   aActions.append(action("HIDEALL_CMD"));
809   aActions.append(action("COLOR_CMD"));
810   aActions.append(action("DEFLECTION_CMD"));
811 #ifdef USE_TRANSPARENCY
812   aActions.append(action("TRANSPARENCY_CMD"));
813 #endif
814   theMenu->addActions(aActions);
815
816   QMap<int, QAction*> aMenuActions;
817   ModuleBase_IModule* aModule = myWorkshop->module();
818   if (aModule) {
819     if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
820       theMenu->addSeparator();
821   }
822
823   // insert the module menu items on specific positions in the popup menu: some actions should be
824   // in the begin of the list, Delete action should be the last by #1343 issue
825   QList<QAction*> anActions = theMenu->actions();
826   int anActionsSize = anActions.size();
827   QAction* aFirstAction = anActions[0];
828   QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
829   for (; anIt != aLast; anIt++) {
830     if (anIt.key() > anActionsSize)
831       theMenu->addAction(anIt.value());
832     else
833       theMenu->insertAction(aFirstAction, *anIt);
834   }
835
836 #ifndef HAVE_SALOME
837   theMenu->addSeparator();
838   QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
839   if (aMDI->actions().size() > 0) {
840     QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
841     aSubMenu->addActions(aMDI->actions());
842   }
843 #endif
844 }
845
846 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
847 {
848   QStringList aGroups;
849
850   QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
851                                                   aLast = myObjBrowserMenus.end();
852   for (; anIt != aLast; anIt++) {
853     QString aGroupName(anIt.key().c_str());
854     if (aGroups.contains(aGroupName))
855       continue;
856     QActionsList anActions = anIt.value();
857     QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
858     bool aFound = false;
859     for (; anAIt != anALast && !aFound; anAIt++)
860       aFound = (*anAIt)->data().toString() == theName;
861     if (aFound)
862       aGroups.append(aGroupName);
863   }
864   return aGroups;
865 }
866
867 void XGUI_ContextMenuMgr::onRename()
868 {
869   QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
870   if (!myWorkshop->abortAllOperations())
871     return;
872   // restore selection in case if dialog box was shown
873   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
874   myWorkshop->objectBrowser()->onEditItem();
875 }
876
877 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
878 {
879   SessionPtr aMgr = ModelAPI_Session::get();
880   DocumentPtr aActiveDoc = aMgr->activeDocument();
881
882   XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
883   XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
884   const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
885   QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
886
887   QString aName;
888   int aLen = 0;
889   bool aIsRoot = false;
890   foreach(QModelIndex aIdx, aSelectedIndexes) {
891     // Process only first column
892     if (aIdx.column() == 1) {
893       aIsRoot = !aIdx.parent().isValid();
894       // Exit if the selected index belongs to non active document
895       if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
896         return;
897       if ((!aIsRoot) && (aIdx.internalPointer() != aActiveDoc.get()))
898         return;
899
900       // Get name of the selected index
901       aName = aIdx.data().toString();
902       aLen = aName.indexOf('(');
903       if (aLen != -1) {
904         aName = aName.left(--aLen);
905       }
906       std::string aFeaturesStr = aIsRoot?
907         aDataModelXML->rootFolderFeatures(aName.toStdString()) :
908         aDataModelXML->subFolderFeatures(aName.toStdString());
909         if (aFeaturesStr.length() > 0) {
910           QStringList aFeturesList =
911             QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
912           foreach(QString aFea, aFeturesList) {
913             QAction* aAction = aActionMgr->action(aFea);
914             if (aAction)
915               theMenu->addAction(aAction);
916           }
917         }
918     }
919   }
920 }
921
922 #define UNCHECK_ACTION(NAME) \
923 { QAction* aAction = action(NAME); \
924 bool isBlock = aAction->signalsBlocked(); \
925 aAction->blockSignals(true); \
926 aAction->setChecked(false); \
927   aAction->blockSignals(isBlock); }
928
929
930 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
931 {
932   UNCHECK_ACTION("SELECT_VERTEX_CMD");
933   UNCHECK_ACTION("SELECT_EDGE_CMD");
934   UNCHECK_ACTION("SELECT_FACE_CMD");
935 }
936
937 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
938 {
939   UNCHECK_ACTION("SHOW_RESULTS_CMD");
940 }