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