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