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