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