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