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