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