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