Salome HOME
Porting on OCCT 7.4.0
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI.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 "SHAPERGUI.h"
21 #include "SHAPERGUI_DataModel.h"
22 #include "SHAPERGUI_OCCSelector.h"
23 #include "SHAPERGUI_NestedButton.h"
24 #include "SHAPERGUI_ToolbarsMgr.h"
25
26 #include <XGUI_Workshop.h>
27 #include <XGUI_PropertyPanel.h>
28 #include <XGUI_ContextMenuMgr.h>
29 #include <XGUI_ObjectsBrowser.h>
30 #include <XGUI_OperationMgr.h>
31 #include <XGUI_Displayer.h>
32 #include <XGUI_MenuMgr.h>
33 #include <XGUI_FacesPanel.h>
34 #include <XGUI_SelectionActivate.h>
35 #include <XGUI_InspectionPanel.h>
36 #include <XGUI_ViewerProxy.h>
37
38 #include <ModuleBase_Operation.h>
39 #include <ModuleBase_Preferences.h>
40 #include <ModuleBase_ActionInfo.h>
41 #include <ModuleBase_IModule.h>
42
43 #include <ModelAPI_Tools.h>
44
45 #include <LightApp_Application.h>
46 #include <LightApp_SelectionMgr.h>
47 #include <LightApp_OCCSelector.h>
48 #include <LightApp_Study.h>
49
50 #include <OCCViewer_ViewModel.h>
51 #include <OCCViewer_ViewPort3d.h>
52
53 #include <SUIT_Selector.h>
54 #include <SUIT_Desktop.h>
55 #include <SUIT_ViewManager.h>
56 #include <SUIT_ViewWindow.h>
57 #include <SUIT_ResourceMgr.h>
58 #include <SUIT_DataBrowser.h>
59
60 #include <QtxPopupMgr.h>
61 #include <QtxActionMenuMgr.h>
62 #include <QtxActionToolMgr.h>
63 #include <QtxResourceMgr.h>
64
65 #include <Config_PropManager.h>
66 #include <Config_ModuleReader.h>
67
68 #include <AIS_ListOfInteractive.hxx>
69 #include <AIS_ListIteratorOfListOfInteractive.hxx>
70
71 #include <QDockWidget>
72 #include <QAction>
73 #include <QTimer>
74 #include <QMenu>
75 #include <QToolBar>
76
77 #if OCC_VERSION_HEX < 0x070400
78   #define SALOME_PATCH_FOR_CTRL_WHEEL
79 #endif
80
81 extern "C" {
82 SHAPERGUI_EXPORT CAM_Module* createModule()
83 {
84   return new SHAPERGUI();
85 }
86
87 SHAPERGUI_EXPORT char* getModuleVersion()
88 {
89   return (char*)"0.0";
90 }
91 } // extern "C"
92
93
94 static const QString ToolbarsSection("SHAPER_Toolbars");
95 static const QString FreeCommandsParam("OutOFToolbars");
96
97
98 /** 
99 * Class for preferences management
100 */
101 class SHAPERGUI_PrefMgr: public ModuleBase_IPrefMgr
102 {
103 public:
104   /// Constructor
105   /// \param theMgr preferences manager of SALOME
106   /// \param theModName name of the module
107   SHAPERGUI_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):
108     myMgr(theMgr), myModName(theModName) {}
109
110   virtual int addPreference(const QString& theLbl, int pId,
111                             SUIT_PreferenceMgr::PrefItemType theType,
112                             const QString& theSection, const QString& theName )
113   {
114     return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
115   }
116
117   virtual void setItemProperty(const QString& thePropName,
118                                const QVariant& theValue,
119                                const int theId = -1)
120   {
121     myMgr->setItemProperty(thePropName, theValue, theId);
122   }
123
124
125   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
126
127 private:
128   LightApp_Preferences* myMgr;
129   QString myModName;
130 };
131
132
133
134
135 //******************************************************
136 SHAPERGUI::SHAPERGUI()
137     : LightApp_Module("SHAPER"),
138       mySelector(0), myIsOpened(0), myPopupMgr(0), myIsInspectionVisible(false),
139   myInspectionPanel(0), myIsToolbarsModified(false)
140 {
141   myWorkshop = new XGUI_Workshop(this);
142   connect(myWorkshop, SIGNAL(commandStatusUpdated()),
143           this, SLOT(onUpdateCommandStatus()));
144
145   myProxyViewer = new SHAPERGUI_SalomeViewer(this);
146
147   ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
148
149   // It will be called in XGUI_Workshop::startApplication
150   // ModuleBase_Preferences::loadCustomProps();
151 }
152
153 //******************************************************
154 SHAPERGUI::~SHAPERGUI()
155 {
156   delete myWorkshop;
157   delete myProxyViewer;
158 }
159
160 //******************************************************
161 void SHAPERGUI::initialize(CAM_Application* theApp)
162 {
163   LightApp_Module::initialize(theApp);
164
165   myWorkshop->startApplication();
166   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
167   if (anApp)
168   {
169     connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
170   }
171
172   int aMenu = createMenu(tr("Inspection"), -1, -1, 30);
173   int aSubMenu = createMenu(tr("Information"), aMenu, -1, -1, 0);
174
175   int aId = getNextCommandId();
176   myActionsList.append(aId);
177   SUIT_Desktop* aDesk = application()->desktop();
178   QString aTip = tr("Show inspection window");
179   myWhatIsAction = createAction(aId, aTip, QIcon(":pictures/whatis.png"), tr("What Is"),
180     aTip, QKeySequence(), aDesk, true, this, SLOT(onWhatIs(bool)));
181   myWhatIsAction->setStatusTip(aTip);
182   myWhatIsAction->setData("INSPECTION_CMD");
183   createMenu(aId, aSubMenu, 0);
184
185   QString aToolName = tr("Inspection");
186   int aTool = createTool(aToolName);
187   int aToolId = createTool(myWhatIsAction, aTool);
188   registerCommandToolbar(aToolName, aId);
189
190   // Define Edit toolbars command
191   aId = getNextCommandId();
192   //myActionsList.append(aId); Do not use it for editing of toolbars
193   aTip = tr("Edit toolbars of the module");
194   QAction* aAction = createAction(aId, aTip, QIcon(":pictures/configure_toolbars.png"),
195     tr("Edit toolbars..."), aTip, QKeySequence(), aDesk, false, this, SLOT(onEditToolbars()));
196   int aEditMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1, 30);
197   int aEditItem = createMenu(aId, aEditMenu);
198
199   // Initialize viewer proxy if OCC viewer is already exist
200   ViewManagerList aOCCViewManagers;
201   application()->viewManagers(OCCViewer_Viewer::Type(), aOCCViewManagers);
202   if (aOCCViewManagers.size() > 0) {
203     SUIT_ViewManager* aMgr = aOCCViewManagers.first();
204     SUIT_ViewWindow* aWnd = aMgr->getActiveView();
205     if (aWnd) {
206       OCCViewer_ViewWindow* aOccWnd = static_cast<OCCViewer_ViewWindow*>(aWnd);
207       OCCViewer_ViewPort3d* aViewPort = aOccWnd->getViewPort();
208       if (aViewPort) {
209         XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
210         aViewPort->installEventFilter(aViewer);
211         Handle(V3d_View) aView = aViewPort->getView();
212         aViewer->SetScale(aView, aView->Camera()->Scale());
213         // We can not create selector here because other modules will be deactivated later
214         //onViewManagerAdded(aMgr);
215       }
216     }
217   }
218 }
219
220 //******************************************************
221 void SHAPERGUI::windows(QMap<int, int>& theWndMap) const
222 {
223   theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
224 }
225
226 //******************************************************
227 void SHAPERGUI::viewManagers(QStringList& theList) const
228 {
229   theList.append(OCCViewer_Viewer::Type());
230 }
231
232 //******************************************************
233 // We can not create selector in this method because it can be called when
234 // SHAPER module is not active. Take into account that creation of our selector
235 // leads to switching OFF all other selectors
236 //void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
237 //{
238 //  // if there are created viewer managers, we should try to create viewer
239 //  // selector and initialize viewer with it. It sets interactive context to the
240 //  // proxy viewer. If study is opened, CAM application calls this method before the open()
241 //  // of data model
242 //  // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
243 //  // we need to connect to the viewer before it. Here,
244 //  // it seems the most appropriate place for this
245 //  // according to SALOME architecture.
246 //  if (!mySelector) {
247 //    ViewManagerList OCCViewManagers;
248 //    application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
249 //    if (OCCViewManagers.size() > 0) {
250 //      mySelector = createSelector(OCCViewManagers.first());
251 //    }
252 //  }
253 //  LightApp_Module::connectToStudy(theStudy);
254 //}
255
256 //******************************************************
257 bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
258 {
259   bool isDone = LightApp_Module::activateModule(theStudy);
260   loadToolbarsConfig();
261
262   SHAPERGUI_DataModel* aDataModel = dynamic_cast<SHAPERGUI_DataModel*>(dataModel());
263   aDataModel->initRootObject();
264
265   if (isDone) {
266     setMenuShown(true);
267     setToolShown(true);
268
269     QObject* aObj = myWorkshop->objectBrowser()->parent();
270     QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
271     if (aObjDoc) {
272       QAction* aViewAct = aObjDoc->toggleViewAction();
273       aViewAct->setEnabled(true);
274       myWorkshop->objectBrowser()->setVisible(true);
275       aObjDoc->setVisible(true);
276       desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
277     }
278
279     if (!myInspectionPanel) {
280       myInspectionPanel = myWorkshop->inspectionPanel();
281       QAction* aViewAct = myInspectionPanel->toggleViewAction();
282       connect(aViewAct, SIGNAL(toggled(bool)), this, SLOT(onWhatIs(bool)));
283     }
284     myInspectionPanel->toggleViewAction()->setEnabled(true);
285
286     if (!mySelector) {
287       ViewManagerList OCCViewManagers;
288       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
289       if (OCCViewManagers.size() > 0) {
290         onViewManagerAdded(OCCViewManagers.first());
291       }
292     }
293     // it should be performed after the selector creation in order to have AISContext
294     myWorkshop->activateModule();
295     //action(myEraseAll)->setEnabled(false);
296
297     if (myIsOpened) {
298       myWorkshop->objectBrowser()->rebuildDataTree();
299       myWorkshop->updateCommandStatus();
300       myIsOpened = false;
301     }
302     else
303       myWorkshop->updateCommandStatus();
304   }
305   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
306   myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
307   myIsEditEnabled = getApp()->isEditEnabled();
308   getApp()->setEditEnabled(false);
309
310   // this following row is caused by #187 bug.
311   // SALOME saves the dock widget positions before deactivateModule() and
312   // load it after the module activation. So, if the panel is visible before
313   // deactivate, it becomes visible after activate.
314   // In order to avoid the visible property panel, the widget position save is
315   // switch off in this module
316   aResMgr->setValue("Study", "store_positions", false);
317
318   // Synchronize displayed objects
319   Handle(AIS_InteractiveContext) aContext;
320   if (mySelector && mySelector->viewer())
321     aContext = mySelector->viewer()->getAISContext();
322
323   if (!aContext.IsNull()) {
324     XGUI_Displayer* aDisp = myWorkshop->displayer();
325     QObjectPtrList aObjList = aDisp->displayedObjects();
326
327     //if (myHighlightPointAspect.IsNull()) {
328     //  Handle(AIS_Trihedron) aTrihedron = mySelector->viewer()->getTrihedron();
329     //  myHighlightPointAspect =
330     //    new Graphic3d_AspectMarker3d(aTrihedron->getHighlightPointAspect()->Aspect().operator*());
331     //}
332     if (myOldSelectionColor.size() == 0)
333       myOldSelectionColor = aDisp->selectionColor();
334
335     AIS_ListOfInteractive aList;
336     aContext->DisplayedObjects(aList);
337     AIS_ListIteratorOfListOfInteractive aLIt;
338     Handle(AIS_InteractiveObject) anAISIO;
339     foreach (ObjectPtr aObj, aObjList) {
340       AISObjectPtr aPrs = aDisp->getAISObject(aObj);
341       Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
342       bool aFound = false;
343       for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
344         anAISIO = aLIt.Value();
345         if (anAISIO.get() == aAIS.get()) {
346           aFound = true;
347           break;
348         }
349       }
350       if (!aFound) {
351         aObj->setDisplayed(false);
352         //aDisp->erase(aObj, false);
353       }
354     }
355     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
356   }
357   myProxyViewer->activateViewer(true);
358
359   // Post-processing for LoadScriptId to remove created(if it was created) SALOME Object Browser
360   connect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
361           this, SLOT(onScriptLoaded()));
362
363   disconnect(getApp()->action(LightApp_Application::FileSaveId), SIGNAL(triggered(bool)),
364              getApp(), SLOT(onSaveDoc()));
365   disconnect(getApp()->action(LightApp_Application::FileSaveAsId), SIGNAL(triggered(bool)),
366              getApp(), SLOT(onSaveAsDoc()));
367
368   connect(getApp()->action(LightApp_Application::FileSaveId), SIGNAL(triggered(bool)),
369           this, SLOT(onSaveDocByShaper()));
370   connect(getApp()->action(LightApp_Application::FileSaveAsId), SIGNAL(triggered(bool)),
371           this, SLOT(onSaveAsDocByShaper()));
372
373   return isDone;
374 }
375
376 //******************************************************
377 bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
378 {
379   saveToolbarsConfig();
380
381   myProxyViewer->activateViewer(false);
382   setMenuShown(false);
383   setToolShown(false);
384
385   myWorkshop->deactivateModule();
386
387   QObject* aObj = myWorkshop->objectBrowser()->parent();
388   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
389   if (aObjDoc) {
390     aObjDoc->setVisible(false);
391     myWorkshop->objectBrowser()->setVisible(false);
392     QAction* aViewAct = aObjDoc->toggleViewAction();
393     aViewAct->setEnabled(false);
394   }
395
396   myIsInspectionVisible = myInspectionPanel->isVisible();
397   myInspectionPanel->hide();
398   QAction* aViewAct = myInspectionPanel->toggleViewAction();
399   aViewAct->setEnabled(false);
400
401   // the active operation should be stopped for the next activation.
402   // There should not be active operation and visualized preview.
403   // Abort operation should be performed before the selection's remove
404   // because the displayed objects should be removed from the viewer, but
405   // the AIS context is obtained from the selector.
406   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
407   while (anOperation) {
408     anOperation->abort();
409     anOperation = myWorkshop->operationMgr()->currentOperation();
410   }
411   // Delete selector because it has to be redefined on next activation
412   if (mySelector) {
413     //if (!myHighlightPointAspect.IsNull()) {
414     //  Handle(AIS_Trihedron) aTrihedron = mySelector->viewer()->getTrihedron();
415     //  aTrihedron->getHighlightPointAspect()->SetAspect(myHighlightPointAspect);
416     //  myHighlightPointAspect.Nullify();
417     //}
418     myWorkshop->displayer()->setSelectionColor(myOldSelectionColor);
419     myProxyViewer->setSelector(0);
420     delete mySelector;
421     mySelector = 0;
422   }
423
424   myWorkshop->hidePanel(myWorkshop->facesPanel());
425   //myWorkshop->contextMenuMgr()->disconnectViewer();
426
427   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
428   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
429   getApp()->setEditEnabled(myIsEditEnabled);
430
431   myOldSelectionColor.clear();
432
433   // Post-processing for LoadScriptId to remove created(if it was created) SALOME Object Browser
434   disconnect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
435              this, SLOT(onScriptLoaded()));
436
437   disconnect(getApp()->action(LightApp_Application::FileSaveId), SIGNAL(triggered(bool)),
438              this, SLOT(onSaveDocByShaper()));
439   disconnect(getApp()->action(LightApp_Application::FileSaveAsId), SIGNAL(triggered(bool)),
440              this, SLOT(onSaveAsDocByShaper()));
441
442   connect(getApp()->action(LightApp_Application::FileSaveId), SIGNAL(triggered(bool)),
443           getApp(), SLOT(onSaveDoc()));
444   connect(getApp()->action(LightApp_Application::FileSaveAsId), SIGNAL(triggered(bool)),
445           getApp(), SLOT(onSaveAsDoc()));
446
447   return LightApp_Module::deactivateModule(theStudy);
448 }
449
450 //******************************************************
451 void SHAPERGUI::onViewManagerAdded(SUIT_ViewManager* theMgr)
452 {
453   if (!mySelector) {
454     mySelector = createSelector(theMgr);
455     myWorkshop->selectionActivate()->updateSelectionFilters();
456     myWorkshop->selectionActivate()->updateSelectionModes();
457     myWorkshop->synchronizeViewer();
458   }
459 }
460
461 //******************************************************
462 void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
463 {
464   if (mySelector) {
465     if (theMgr->getType() == OCCViewer_Viewer::Type()) {
466       OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
467       if (mySelector->viewer() == aViewer) {
468         XGUI_Displayer* aDisp = myWorkshop->displayer();
469         QObjectPtrList aObjects = aDisp->displayedObjects();
470         ResultPtr aRes;
471         foreach(ObjectPtr aObj, aObjects) {
472           aObj->setDisplayed(false);
473           aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
474           if (aRes.get()) {
475             while (aRes = ModelAPI_Tools::bodyOwner(aRes)) {
476               aRes->setDisplayed(false);
477             }
478           }
479         }
480         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
481         myProxyViewer->setSelector(0);
482         delete mySelector;
483         mySelector = 0;
484
485         myWorkshop->module()->clearViewer();
486       }
487     }
488   }
489 }
490
491 //******************************************************
492 QtxPopupMgr* SHAPERGUI::popupMgr()
493 {
494   if (!myPopupMgr)
495     myPopupMgr = new QtxPopupMgr( 0, this );
496   return myPopupMgr;
497 }
498
499 //******************************************************
500 void SHAPERGUI::onDefaultPreferences()
501 {
502   // reset main resources
503   ModuleBase_Preferences::resetResourcePreferences(preferences());
504   // reset plugin's resources
505   ModuleBase_Preferences::resetConfigPropPreferences(preferences());
506
507   myWorkshop->displayer()->redisplayObjects();
508 }
509
510 //******************************************************
511 void SHAPERGUI::onScriptLoaded()
512 {
513   // this slot is called after processing of the LoadScriptId action of SalomeApp Application
514   // Each dumped script contains updateObjBrowser() that creates a new instance of Object
515   // Browser. When SHAPER module is active, this browser should not be used. It might be removed
516   // as hidden by means of updateWindows() of SalomeApp_Application or to remove
517   // it manually (because this method of application is protected)
518   SUIT_DataBrowser* aBrowser = getApp()->objectBrowser();
519   if (aBrowser)
520     delete aBrowser;
521   myWorkshop->displayer()->updateViewer();
522   myWorkshop->updateCommandStatus();
523 }
524
525 //******************************************************
526 void SHAPERGUI::onSaveDocByShaper()
527 {
528   if(!workshop()->operationMgr()->abortAllOperations(XGUI_OperationMgr::XGUI_InformationMessage))
529     return;
530
531   getApp()->onSaveDoc();
532 }
533
534 //******************************************************
535 void SHAPERGUI::onSaveAsDocByShaper()
536 {
537   if(!workshop()->operationMgr()->abortAllOperations(XGUI_OperationMgr::XGUI_InformationMessage))
538     return;
539
540   getApp()->onSaveAsDoc();
541 }
542
543 //******************************************************
544 void SHAPERGUI::onUpdateCommandStatus()
545 {
546   getApp()->updateActions();
547 }
548
549 //******************************************************
550 SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
551 {
552   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
553     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
554
555     //if (myHighlightPointAspect.IsNull()) {
556     //  Handle(AIS_Trihedron) aTrihedron = aViewer->getTrihedron();
557     //  myHighlightPointAspect =
558     //    new Graphic3d_AspectMarker3d(aTrihedron->getHighlightPointAspect()->Aspect().operator*());
559     //}
560     SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer,
561                                                                  getApp()->selectionMgr());
562 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
563     aViewer->setUseLocalSelection(true);
564 #endif
565     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
566     QList<SUIT_Selector*> aList;
567     aMgr->selectors(aList);
568     foreach(SUIT_Selector* aSel, aList)
569     {
570       aSel->setEnabled(aSel == aSelector);
571     }
572     myProxyViewer->setSelector(aSelector);
573
574     if (myOldSelectionColor.size() == 0)
575       myOldSelectionColor = myWorkshop->displayer()->selectionColor();
576
577     std::vector<int> aColor = Config_PropManager::color("Visualization", "selection_color");
578     myWorkshop->displayer()->setSelectionColor(aColor);
579     return aSelector;
580   }
581   return 0;
582 }
583
584 //******************************************************
585 CAM_DataModel* SHAPERGUI::createDataModel()
586 {
587   return new SHAPERGUI_DataModel(this);
588 }
589
590 QAction* SHAPERGUI::addFeature(const QString& theWBName, const ActionInfo& theInfo,
591                                const bool isAddSeparator)
592 {
593   return addFeature(theWBName,
594                     theInfo.toolBar,
595                     theInfo.id,
596                     theInfo.text,
597                     //Issue #650: in the SALOME mode the tooltip should be same as text
598                     theInfo.text,
599                     theInfo.icon,
600                     theInfo.shortcut,
601                     theInfo.checkable,
602                     isAddSeparator,
603                     theInfo.toolTip);
604 }
605
606 //******************************************************
607 QAction* SHAPERGUI::addFeature(const QString& theWBName, const QString& theTBName,
608                                const QString& theId, const QString& theTitle, const QString& theTip,
609                                const QIcon& theIcon, const QKeySequence& theKeys,
610                                bool isCheckable, const bool isAddSeparator,
611                                const QString& theStatusTip)
612 {
613   static QString aLastTool = "";
614   static int aNb = 0;
615   if (aLastTool.isEmpty())
616     aLastTool = theWBName;
617   else if (theWBName != aLastTool) {
618     aLastTool = theWBName;
619     if (aNb > 20) {
620       desktop()->addToolBarBreak();
621       aNb = 0;
622     }
623   }
624   aNb++;
625
626   int aId = getNextCommandId();
627   myActionsList.append(aId);
628   SUIT_Desktop* aDesk = application()->desktop();
629   int aKeys = 0;
630   for (int i = 0; i < theKeys.count(); i++)
631     aKeys += theKeys[i];
632   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
633                                   isCheckable);
634   aAction->setStatusTip(theStatusTip);
635
636   aAction->setData(theId);
637
638   int aWBMenu = createMenu(theWBName, -1, -1, 30/*10-Window, 1000 - Help*/);
639   int aItemId = createMenu(aId, aWBMenu);
640   if (isAddSeparator)
641     createMenu(separator(), aWBMenu);
642
643   int aWBTool = createTool(theTBName, theTBName);
644   int aToolId = createTool(aId, aWBTool);
645   registerCommandToolbar(theTBName, aId);
646   if (isAddSeparator) {
647     createTool(separator(), aWBTool);
648     registerCommandToolbar(theTBName, -1);
649   }
650   return aAction;
651 }
652
653 bool SHAPERGUI::isFeatureOfNested(const QAction* theAction)
654 {
655   return dynamic_cast<const SHAPERGUI_NestedButton*>(theAction);
656 }
657
658 QAction* SHAPERGUI::addFeatureOfNested(const QString& theWBName,
659                                        const ActionInfo& theInfo,
660                                        const QList<QAction*>& theNestedActions)
661 {
662   SUIT_Desktop* aDesk = application()->desktop();
663   SHAPERGUI_NestedButton* anAction = new SHAPERGUI_NestedButton(aDesk, theNestedActions);
664   anAction->setData(theInfo.id);
665   anAction->setCheckable(theInfo.checkable);
666   anAction->setChecked(theInfo.checked);
667   anAction->setEnabled(theInfo.enabled);
668   anAction->setVisible(theInfo.visible);
669   anAction->setIcon(theInfo.icon);
670   anAction->setText(theInfo.text);
671   anAction->setToolTip(theInfo.toolTip);
672   anAction->setShortcut(theInfo.shortcut);
673   anAction->setFont(theInfo.font);
674
675   int aWBMenu = createMenu(theWBName, -1, -1, 30);
676   int aItemId = createMenu(anAction, aWBMenu);
677   myActionsList.append(aItemId);
678   createMenu(separator(), aWBMenu); /// nested action is always separated of others
679
680   int aWBTool = createTool(theWBName, theWBName);
681   int aToolId = createTool(anAction, aWBTool);
682   registerCommandToolbar(theWBName, aItemId);
683   createTool(separator(), aWBTool); /// nested action is always separated of others
684   registerCommandToolbar(theWBName, -1);
685
686   return anAction;
687 }
688
689
690 //******************************************************
691 QAction* SHAPERGUI::addDesktopCommand(const QString& theId, const QString& theTitle,
692                                            const QString& theTip, const QIcon& theIcon,
693                                            const QKeySequence& theKeys, bool isCheckable,
694                                            const char* theMenuSourceText, const int theMenuPosition)
695 {
696   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
697
698   int aId = getNextCommandId();
699   myActionsList.append(aId);
700   SUIT_Desktop* aDesk = application()->desktop();
701   int aKeys = 0;
702   for (int i = 0; i < theKeys.count(); i++)
703     aKeys += theKeys[i];
704   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
705                                   isCheckable);
706   aAction->setStatusTip(theTip);
707   aAction->setData(theId);
708   createMenu(aId, aMenu, theMenuPosition);
709   return aAction;
710 }
711
712 //******************************************************
713 void SHAPERGUI::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
714 {
715   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
716   createMenu(separator(), aMenu, -1, theMenuPosition);
717 }
718
719 //******************************************************
720 bool SHAPERGUI::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
721 {
722   if( !theAction )
723     return false;
724
725   SUIT_Desktop* aDesktop = application()->desktop();
726   if( !aDesktop )
727     return false;
728
729   QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
730   if( !aToolMgr )
731     return false;
732
733   aToolMgr->append( theAction, theToolBarTitle );
734   return true;
735 }
736
737 //******************************************************
738 QList<QAction*> SHAPERGUI::commandList() const
739 {
740   QList<QAction*> aActions;
741   foreach (int aId, myActionsList) {
742     QAction* aCmd = action(aId);
743     if (aCmd)
744       aActions.append(aCmd);
745   }
746
747   return aActions;
748 }
749
750 //******************************************************
751 QMainWindow* SHAPERGUI::desktop() const
752 {
753   return application()->desktop();
754 }
755
756 void SHAPERGUI::setFeatureInfo(const QString& theFeatureId,
757                                const std::shared_ptr<Config_FeatureMessage>& theMessage)
758 {
759   myFeaturesInfo.insert(theFeatureId, theMessage);
760 }
761
762 std::shared_ptr<Config_FeatureMessage> SHAPERGUI::featureInfo(const QString& theFeatureId)
763 {
764   std::shared_ptr<Config_FeatureMessage> aMessage;
765   if (myFeaturesInfo.contains(theFeatureId))
766     aMessage =  myFeaturesInfo[theFeatureId];
767   return aMessage;
768 }
769
770 //******************************************************
771 void SHAPERGUI::selectionChanged()
772 {
773   LightApp_Module::selectionChanged();
774   myWorkshop->salomeViewerSelectionChanged();
775 }
776
777 //******************************************************
778 void SHAPERGUI::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
779 {
780   myWorkshop->contextMenuMgr()->updateViewerMenu();
781   myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
782   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
783 }
784
785
786 //******************************************************
787 void SHAPERGUI::createPreferences()
788 {
789   LightApp_Preferences* pref = preferences();
790   if (!pref)
791     return;
792   ModuleBase_Preferences::updateConfigByResources();
793   QString aModName = moduleName();
794
795   QtxPreferenceItem* item = pref->findItem(aModName, true );
796   if ( item && (!item->isEmpty() )) {
797     item->parentItem()->removeItem(item);
798     delete item;
799   }
800
801   int catId = pref->addPreference(aModName, -1 );
802   if ( catId == -1 )
803     return;
804   SHAPERGUI_PrefMgr aMgr(pref, aModName);
805   ModuleBase_Preferences::createEditContent(&aMgr, catId);
806
807   int viewTab = pref->addItem(tr("Viewer"), catId);
808   // Create other parameters group in viewer tab
809   int otherGroup = pref->addItem(tr("Default selection"), viewTab);
810   pref->setItemProperty("columns", 3, otherGroup);
811   pref->addItem(tr("Faces"), otherGroup,
812                          SUIT_PreferenceMgr::Bool,
813                          ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
814   pref->addItem(tr("Edges"), otherGroup,
815                          SUIT_PreferenceMgr::Bool,
816                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
817   pref->addItem(tr("Vertices"), otherGroup,
818                          SUIT_PreferenceMgr::Bool,
819                          ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
820
821   int sensitivityGroup = pref->addItem(tr("Selection sensitivity"), viewTab);
822   pref->setItemProperty("columns", 2, sensitivityGroup);
823   pref->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
824                 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
825   pref->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
826                 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
827
828   int highlightGroup = pref->addItem(tr("Additional highlighting"), viewTab);
829   pref->setItemProperty("columns", 2, highlightGroup);
830   pref->addItem(tr("In 3d mode"), highlightGroup,
831     SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
832   pref->addItem(tr("In 2d mode"), highlightGroup,
833     SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
834
835   int colorScaleGroup = pref->addItem(tr("Color scale"), viewTab);
836   pref->setItemProperty("columns", 4, colorScaleGroup);
837   int aItem = aMgr.addPreference(tr("X position"), colorScaleGroup,
838     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
839   pref->setItemProperty("min", 0, aItem);
840   pref->setItemProperty("max", 1, aItem);
841
842   aItem = aMgr.addPreference(tr("Y position"), colorScaleGroup,
843     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
844   pref->setItemProperty("min", 0, aItem);
845   pref->setItemProperty("max", 1, aItem);
846
847   aItem = aMgr.addPreference(tr("Width"), colorScaleGroup,
848     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
849   pref->setItemProperty("min", 0, aItem);
850   pref->setItemProperty("max", 1, aItem);
851
852   aItem = aMgr.addPreference(tr("Height"), colorScaleGroup,
853     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
854   pref->setItemProperty("min", 0, aItem);
855   pref->setItemProperty("max", 1, aItem);
856
857   aItem = aMgr.addPreference(tr("Intervals number"), colorScaleGroup,
858     SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
859   pref->setItemProperty("min", 0, aItem);
860   pref->setItemProperty("max", 100, aItem);
861
862   aItem = aMgr.addPreference(tr("Text height"), colorScaleGroup,
863     SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
864   pref->setItemProperty("min", 0, aItem);
865   pref->setItemProperty("max", 100, aItem);
866
867   aItem = aMgr.addPreference(tr("Text color"), colorScaleGroup,
868     SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
869
870   pref->retrieve();
871 }
872
873 //******************************************************
874 void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& theParam)
875 {
876   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
877   QString aVal = aResMgr->stringValue(theSection, theParam);
878   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(),
879                                                     theParam.toStdString());
880   std::string aValue = aVal.toStdString();
881   if (aValue.empty()) {
882     aValue = aProp->defaultValue();
883     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
884
885     LightApp_Preferences* pref = preferences();
886     if (pref)
887       pref->retrieve();
888   }
889   aProp->setValue(aValue);
890
891   if ((theSection == "Visualization") && (theParam == "selection_color")) {
892     std::vector<int> aColor = Config_PropManager::color("Visualization", "selection_color");
893     myWorkshop->displayer()->setSelectionColor(aColor);
894   }
895
896   myWorkshop->displayer()->redisplayObjects();
897 }
898
899 void SHAPERGUI::putInfo(const QString& theInfo, const int theMSecs)
900 {
901   application()->putInfo(theInfo, theMSecs);
902 }
903
904 bool SHAPERGUI::abortAllOperations()
905 {
906   return workshop()->operationMgr()->abortAllOperations();
907 }
908
909 void SHAPERGUI::createFeatureActions()
910 {
911   myWorkshop->menuMgr()->createFeatureActions();
912 }
913
914 void SHAPERGUI::onWhatIs(bool isToggled)
915 {
916   if (sender() == myWhatIsAction) {
917     QAction* aViewAct = myInspectionPanel->toggleViewAction();
918     aViewAct->blockSignals(true);
919     aViewAct->setChecked(isToggled);
920     aViewAct->blockSignals(false);
921     myInspectionPanel->setVisible(isToggled);
922   }
923   else {
924     myWhatIsAction->blockSignals(true);
925     myWhatIsAction->setChecked(isToggled);
926     myWhatIsAction->blockSignals(false);
927     myInspectionPanel->setVisible(isToggled);
928   }
929 }
930
931 void SHAPERGUI::updateModuleVisibilityState()
932 {
933   LightApp_Module::updateModuleVisibilityState();
934   onWhatIs(myIsInspectionVisible);
935 }
936
937 void SHAPERGUI::onEditToolbars()
938 {
939   SHAPERGUI_ToolbarsDlg aDlg(this);
940   if (aDlg.exec() == QDialog::Accepted) {
941     if (aDlg.isReset())
942       resetToolbars();
943     else
944       updateToolbars(aDlg.result());
945   }
946 }
947
948 void SHAPERGUI::registerCommandToolbar(const QString& theToolName, int theCommandId)
949 {
950   if (!myToolbars.contains(theToolName))
951     myToolbars[theToolName] = QList<int>();
952   myToolbars[theToolName].append(theCommandId);
953 }
954
955 int SHAPERGUI::getNextCommandId() const
956 {
957   QtxActionMenuMgr* aMenuMgr = menuMgr();
958   QIntList aIds = aMenuMgr->idList();
959   int aId = aIds.count();
960   while (action(aId) || myActionsList.contains(aId))
961     aId++;
962   return aId;
963 }
964
965 void SHAPERGUI::updateToolbars(const QMap<QString, QIntList>& theNewToolbars)
966 {
967   // Store default toolbars
968   if (myDefaultToolbars.size() == 0)
969     myDefaultToolbars = myToolbars;
970
971   QtxActionToolMgr* aMgr = toolMgr();
972   QStringList aToolbars = theNewToolbars.keys();
973   QIntList aCommands, aOldCmd;
974   int aToolbarId;
975   QAction* aAction;
976   int aActionId;
977   foreach(QString aName, aToolbars) {
978     aCommands = theNewToolbars[aName];
979     // Find or create toolbar
980     if (aMgr->hasToolBar(aName)) {
981       aToolbarId = aMgr->find(aMgr->toolBar(aName));
982       aOldCmd = myToolbars[aName];
983     }
984     else {
985       aToolbarId = aMgr->createToolBar(aName);
986     }
987     int aPos = 0;
988     foreach(int aCmd, aCommands) {
989       // Find action
990       if (aCmd == -1)
991         aAction = separator();
992       else
993         aAction = action(aCmd);
994       aActionId = aMgr->actionId(aAction);
995       if (aActionId == -1) {
996         // Add new action
997         aMgr->insert(aAction, aToolbarId, aPos);
998       }
999       else {
1000         // Change position of action
1001         if (aMgr->index(aActionId, aToolbarId) != aPos) {
1002           if (aMgr->containsAction(aActionId, aToolbarId))
1003             aMgr->remove(aActionId, aToolbarId);
1004           aMgr->insert(aActionId, aToolbarId, aPos);
1005         }
1006       }
1007       aOldCmd.removeAll(aCmd);
1008       aPos++;
1009     }
1010     // remove extra actions
1011     foreach(int aCmd, aOldCmd) {
1012       aAction = action(aCmd);
1013       aActionId = aMgr->actionId(aAction);
1014       aMgr->remove(aActionId, aToolbarId);
1015     }
1016     myToolbars.remove(aName);
1017   }
1018   // Remove extra toolbars
1019   aToolbars = myToolbars.keys();
1020   foreach(QString aName, aToolbars) {
1021     aMgr->removeToolBar(aName);
1022   }
1023   // Set new toolbars structure
1024   myToolbars = theNewToolbars;
1025   myIsToolbarsModified = true;
1026 }
1027
1028 void SHAPERGUI::saveToolbarsConfig()
1029 {
1030   if (!myIsToolbarsModified)
1031     return;
1032   // Save toolbars configuration into map
1033   QMap<QString, QStringList> aToolbarsConfig;
1034   QtxActionToolMgr* aMgr = toolMgr();
1035   QStringList aToolbars = myToolbars.keys();
1036   QIntList aActionsIds;
1037   foreach(QString aName, aToolbars) {
1038     aActionsIds = myToolbars[aName];
1039     QStringList aContent;
1040     foreach(int aId, aActionsIds) {
1041       if (aId == -1)
1042         aContent.append("");
1043       else
1044         aContent.append(action(aId)->data().toString());
1045     }
1046     aToolbarsConfig[aName] = aContent;
1047   }
1048   // Store the configuration into resources
1049   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
1050   QStringList aNames = aToolbarsConfig.keys();
1051   QStringList aValues;
1052   foreach(QString aToolbar, aNames) {
1053     aResMgr->setValue(ToolbarsSection, aToolbar, aToolbarsConfig[aToolbar].join(","));
1054   }
1055   // Remove obsolete parameters from resources
1056   QStringList aOldParams = aResMgr->parameters(ToolbarsSection);
1057   foreach(QString aName, aOldParams) {
1058     if (!aToolbars.contains(aName))
1059       aResMgr->remove(ToolbarsSection, aName);
1060   }
1061   // Store current list of free commands
1062   QIntList aFreeCommands = getFreeCommands();
1063   QStringList aFreeList;
1064   foreach(int aId, aFreeCommands) {
1065     aFreeList.append(action(aId)->data().toString());
1066   }
1067   if (aFreeList.size() > 0)
1068     aResMgr->setValue(ToolbarsSection, FreeCommandsParam, aFreeList.join(","));
1069
1070   myIsToolbarsModified = false;
1071 }
1072
1073 void SHAPERGUI::loadToolbarsConfig()
1074 {
1075   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
1076   QStringList aToolbarNames = aResMgr->parameters(ToolbarsSection);
1077   if (aToolbarNames.size() == 0)
1078     return;
1079
1080   // Create commands map
1081   QMap<QString, int> aCommandsMap;
1082   QString aCmdIdStr;
1083   foreach(int aId, myActionsList) {
1084     aCmdIdStr = action(aId)->data().toString();
1085     aCommandsMap[aCmdIdStr] = aId;
1086   }
1087
1088   // Create new toolbars structure
1089   QMap<QString, QIntList> aToolbars;
1090   QStringList aCommands;
1091   QIntList aKnownCommands;
1092   QList<QAction*> aActions;
1093   foreach(QString aName, aToolbarNames) {
1094     aCommands = aResMgr->stringValue(ToolbarsSection, aName).split(",");
1095     if (aName == FreeCommandsParam) {
1096       // The value is a list of free commands
1097       foreach(QString aCommand, aCommands) {
1098         aKnownCommands.append(aCommandsMap[aCommand]);
1099       }
1100     }
1101     else {
1102       aToolbars[aName] = QIntList();
1103       if (aCommands.size() > 0) {
1104         foreach(QString aCommand, aCommands) {
1105           if (aCommand.isEmpty())
1106             aToolbars[aName].append(-1);
1107           else if (aCommandsMap.contains(aCommand)) {
1108             int aId = aCommandsMap[aCommand];
1109             aToolbars[aName].append(aId);
1110             aKnownCommands.append(aId);
1111           }
1112         }
1113       }
1114     }
1115   }
1116   // Find new and obsolete commands
1117   QIntList aNewCommands = myActionsList;
1118   foreach(int aId, myActionsList) {
1119     if (aKnownCommands.contains(aId)) {
1120       aKnownCommands.removeAll(aId);
1121       aNewCommands.removeAll(aId);
1122     }
1123   }
1124   if (aNewCommands.size() > 0) {
1125     // Add new commands to toolbars structure
1126     QStringList aKeys = myToolbars.keys();
1127     foreach(int aNewId, aNewCommands) {
1128       foreach(QString aName, aKeys) {
1129         if (myToolbars[aName].contains(aNewId)) {
1130           if (!aToolbars.contains(aName)) {
1131             aToolbars[aName] = QIntList();
1132           }
1133           aToolbars[aName].append(aNewId);
1134         }
1135       }
1136     }
1137   }
1138   if (aKnownCommands.size() > 0) {
1139     // Remove obsolete commands from the toolbars structure
1140     QStringList aKeys = aToolbars.keys();
1141     foreach(int aOldId, aKnownCommands) {
1142       foreach(QString aName, aKeys) {
1143         if (aToolbars[aName].contains(aOldId)) {
1144           aToolbars[aName].removeAll(aOldId);
1145           if (aToolbars[aName].size() == 0)
1146             aToolbars.remove(aName);
1147         }
1148       }
1149     }
1150   }
1151   updateToolbars(aToolbars);
1152   myIsToolbarsModified = false;
1153 }
1154
1155
1156 QIntList SHAPERGUI::getFreeCommands() const
1157 {
1158   QIntList aFreeCommands;
1159   QtxActionToolMgr* aMgr = toolMgr();
1160   QAction* anAction;
1161   int aId;
1162   QMap<QString, QIntList>::const_iterator aIt;
1163   QIntList aShaperActions = shaperActions();
1164   foreach(int aCmd, aShaperActions) {
1165     anAction = action(aCmd);
1166     aId = aMgr->actionId(anAction);
1167     if (!aMgr->containsAction(aId))
1168       aFreeCommands.append(aCmd);
1169   }
1170   return aFreeCommands;
1171 }
1172
1173 void SHAPERGUI::resetToolbars()
1174 {
1175   if (!myDefaultToolbars.isEmpty())
1176     updateToolbars(myDefaultToolbars);
1177   myIsToolbarsModified = false;
1178   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
1179   aResMgr->remove(ToolbarsSection);
1180 }