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