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