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