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