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