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