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