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