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