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