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