Salome HOME
Merge branch 'occ/shaper2smesh'
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI.cpp
index 1a1004970a11405ff923ee909e8bbfc9529c04c7..1ac461065e957c789673cdf65b4cd0e760221c6e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "SHAPERGUI.h"
 #include <XGUI_FacesPanel.h>
 #include <XGUI_SelectionActivate.h>
 #include <XGUI_InspectionPanel.h>
+#include <XGUI_ViewerProxy.h>
 
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_Preferences.h>
 #include <ModuleBase_ActionInfo.h>
 #include <ModuleBase_IModule.h>
 
+#include <ModelAPI_Tools.h>
+
 #include <LightApp_Application.h>
 #include <LightApp_SelectionMgr.h>
 #include <LightApp_OCCSelector.h>
 #include <LightApp_Study.h>
+
 #include <OCCViewer_ViewModel.h>
+#include <OCCViewer_ViewPort3d.h>
 
 #include <SUIT_Selector.h>
 #include <SUIT_Desktop.h>
 #include <SUIT_ViewManager.h>
+#include <SUIT_ViewWindow.h>
 #include <SUIT_ResourceMgr.h>
 #include <SUIT_DataBrowser.h>
 
@@ -69,7 +74,9 @@
 #include <QMenu>
 #include <QToolBar>
 
-#define SALOME_PATCH_FOR_CTRL_WHEEL
+#if OCC_VERSION_HEX < 0x070400
+  #define SALOME_PATCH_FOR_CTRL_WHEEL
+#endif
 
 extern "C" {
 SHAPERGUI_EXPORT CAM_Module* createModule()
@@ -83,6 +90,11 @@ SHAPERGUI_EXPORT char* getModuleVersion()
 }
 } // extern "C"
 
+
+static const QString ToolbarsSection("SHAPER_Toolbars");
+static const QString FreeCommandsParam("OutOFToolbars");
+
+
 /** 
 * Class for preferences management
 */
@@ -133,7 +145,9 @@ SHAPERGUI::SHAPERGUI()
   myProxyViewer = new SHAPERGUI_SalomeViewer(this);
 
   ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
-  ModuleBase_Preferences::loadCustomProps();
+
+  // It will be called in XGUI_Workshop::startApplication
+  // ModuleBase_Preferences::loadCustomProps();
 }
 
 //******************************************************
@@ -156,7 +170,7 @@ void SHAPERGUI::initialize(CAM_Application* theApp)
   }
 
   int aMenu = createMenu(tr("Inspection"), -1, -1, 30);
-  int aSubMenu = createMenu(tr("Information"), aMenu);
+  int aSubMenu = createMenu(tr("Information"), aMenu, -1, -1, 0);
 
   int aId = getNextCommandId();
   myActionsList.append(aId);
@@ -168,7 +182,7 @@ void SHAPERGUI::initialize(CAM_Application* theApp)
   myWhatIsAction->setData("INSPECTION_CMD");
   createMenu(aId, aSubMenu, 0);
 
-  QString aToolName = tr("Inspection tool");
+  QString aToolName = tr("Inspection");
   int aTool = createTool(aToolName);
   int aToolId = createTool(myWhatIsAction, aTool);
   registerCommandToolbar(aToolName, aId);
@@ -181,6 +195,26 @@ void SHAPERGUI::initialize(CAM_Application* theApp)
     tr("Edit toolbars..."), aTip, QKeySequence(), aDesk, false, this, SLOT(onEditToolbars()));
   int aEditMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1, 30);
   int aEditItem = createMenu(aId, aEditMenu);
+
+  // Initialize viewer proxy if OCC viewer is already exist
+  ViewManagerList aOCCViewManagers;
+  application()->viewManagers(OCCViewer_Viewer::Type(), aOCCViewManagers);
+  if (aOCCViewManagers.size() > 0) {
+    SUIT_ViewManager* aMgr = aOCCViewManagers.first();
+    SUIT_ViewWindow* aWnd = aMgr->getActiveView();
+    if (aWnd) {
+      OCCViewer_ViewWindow* aOccWnd = static_cast<OCCViewer_ViewWindow*>(aWnd);
+      OCCViewer_ViewPort3d* aViewPort = aOccWnd->getViewPort();
+      if (aViewPort) {
+        XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
+        aViewPort->installEventFilter(aViewer);
+        Handle(V3d_View) aView = aViewPort->getView();
+        aViewer->SetScale(aView, aView->Camera()->Scale());
+        // We can not create selector here because other modules will be deactivated later
+        //onViewManagerAdded(aMgr);
+      }
+    }
+  }
 }
 
 //******************************************************
@@ -202,7 +236,7 @@ void SHAPERGUI::viewManagers(QStringList& theList) const
 //void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
 //{
 //  // if there are created viewer managers, we should try to create viewer
-//  // selector and initialize viewer with it. It sets interactive contect to the
+//  // selector and initialize viewer with it. It sets interactive context to the
 //  // proxy viewer. If study is opened, CAM application calls this method before the open()
 //  // of data model
 //  // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
@@ -253,10 +287,10 @@ bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
       ViewManagerList OCCViewManagers;
       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
       if (OCCViewManagers.size() > 0) {
-        mySelector = createSelector(OCCViewManagers.first());
+        onViewManagerAdded(OCCViewManagers.first());
       }
     }
-    // it should be pefromed after the selector creation in order to have AISContext
+    // it should be performed after the selector creation in order to have AISContext
     myWorkshop->activateModule();
     //action(myEraseAll)->setEnabled(false);
 
@@ -290,6 +324,14 @@ bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
     XGUI_Displayer* aDisp = myWorkshop->displayer();
     QObjectPtrList aObjList = aDisp->displayedObjects();
 
+    //if (myHighlightPointAspect.IsNull()) {
+    //  Handle(AIS_Trihedron) aTrihedron = mySelector->viewer()->getTrihedron();
+    //  myHighlightPointAspect =
+    //    new Graphic3d_AspectMarker3d(aTrihedron->getHighlightPointAspect()->Aspect().operator*());
+    //}
+    if (myOldSelectionColor.size() == 0)
+      myOldSelectionColor = aDisp->selectionColor();
+
     AIS_ListOfInteractive aList;
     aContext->DisplayedObjects(aList);
     AIS_ListIteratorOfListOfInteractive aLIt;
@@ -314,7 +356,7 @@ bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
   }
   myProxyViewer->activateViewer(true);
 
-  // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
+  // Post-processing for LoadScriptId to remove created(if it was created) SALOME Object Browser
   connect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
           this, SLOT(onScriptLoaded()));
 
@@ -368,7 +410,21 @@ bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
   }
   // Delete selector because it has to be redefined on next activation
   if (mySelector) {
+    //if (!myHighlightPointAspect.IsNull()) {
+    //  Handle(AIS_Trihedron) aTrihedron = mySelector->viewer()->getTrihedron();
+    //  aTrihedron->getHighlightPointAspect()->SetAspect(myHighlightPointAspect);
+    //  myHighlightPointAspect.Nullify();
+    //}
+    myWorkshop->displayer()->setSelectionColor(myOldSelectionColor);
     myProxyViewer->setSelector(0);
+
+    LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
+    QList<SUIT_Selector*> aList;
+    aMgr->selectors(aList);
+    foreach(SUIT_Selector* aSel, aList) {
+      aSel->setEnabled(aSel != mySelector);
+    }
+
     delete mySelector;
     mySelector = 0;
   }
@@ -380,7 +436,9 @@ bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
   getApp()->setEditEnabled(myIsEditEnabled);
 
-  // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
+  myOldSelectionColor.clear();
+
+  // Post-processing for LoadScriptId to remove created(if it was created) SALOME Object Browser
   disconnect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
              this, SLOT(onScriptLoaded()));
 
@@ -394,6 +452,8 @@ bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
   connect(getApp()->action(LightApp_Application::FileSaveAsId), SIGNAL(triggered(bool)),
           getApp(), SLOT(onSaveAsDoc()));
 
+  publishToStudy();
+
   return LightApp_Module::deactivateModule(theStudy);
 }
 
@@ -417,8 +477,16 @@ void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
       if (mySelector->viewer() == aViewer) {
         XGUI_Displayer* aDisp = myWorkshop->displayer();
         QObjectPtrList aObjects = aDisp->displayedObjects();
-        foreach(ObjectPtr aObj, aObjects)
+        ResultPtr aRes;
+        foreach(ObjectPtr aObj, aObjects) {
           aObj->setDisplayed(false);
+          aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+          if (aRes.get()) {
+            while (aRes = ModelAPI_Tools::bodyOwner(aRes)) {
+              aRes->setDisplayed(false);
+            }
+          }
+        }
         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
         myProxyViewer->setSelector(0);
         delete mySelector;
@@ -460,6 +528,8 @@ void SHAPERGUI::onScriptLoaded()
   SUIT_DataBrowser* aBrowser = getApp()->objectBrowser();
   if (aBrowser)
     delete aBrowser;
+  myWorkshop->displayer()->updateViewer();
+  myWorkshop->updateCommandStatus();
 }
 
 //******************************************************
@@ -491,6 +561,12 @@ SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
 {
   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
+
+    //if (myHighlightPointAspect.IsNull()) {
+    //  Handle(AIS_Trihedron) aTrihedron = aViewer->getTrihedron();
+    //  myHighlightPointAspect =
+    //    new Graphic3d_AspectMarker3d(aTrihedron->getHighlightPointAspect()->Aspect().operator*());
+    //}
     SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer,
                                                                  getApp()->selectionMgr());
 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
@@ -504,6 +580,12 @@ SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
       aSel->setEnabled(aSel == aSelector);
     }
     myProxyViewer->setSelector(aSelector);
+
+    if (myOldSelectionColor.size() == 0)
+      myOldSelectionColor = myWorkshop->displayer()->selectionColor();
+
+    std::vector<int> aColor = Config_PropManager::color("Visualization", "selection_color");
+    myWorkshop->displayer()->setSelectionColor(aColor);
     return aSelector;
   }
   return 0;
@@ -752,6 +834,49 @@ void SHAPERGUI::createPreferences()
                 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
   pref->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
                 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
+
+  int highlightGroup = pref->addItem(tr("Additional highlighting"), viewTab);
+  pref->setItemProperty("columns", 2, highlightGroup);
+  pref->addItem(tr("In 3d mode"), highlightGroup,
+    SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
+  pref->addItem(tr("In 2d mode"), highlightGroup,
+    SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
+
+  int colorScaleGroup = pref->addItem(tr("Color scale"), viewTab);
+  pref->setItemProperty("columns", 4, colorScaleGroup);
+  int aItem = aMgr.addPreference(tr("X position"), colorScaleGroup,
+    SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
+  pref->setItemProperty("min", 0, aItem);
+  pref->setItemProperty("max", 1, aItem);
+
+  aItem = aMgr.addPreference(tr("Y position"), colorScaleGroup,
+    SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
+  pref->setItemProperty("min", 0, aItem);
+  pref->setItemProperty("max", 1, aItem);
+
+  aItem = aMgr.addPreference(tr("Width"), colorScaleGroup,
+    SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
+  pref->setItemProperty("min", 0, aItem);
+  pref->setItemProperty("max", 1, aItem);
+
+  aItem = aMgr.addPreference(tr("Height"), colorScaleGroup,
+    SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
+  pref->setItemProperty("min", 0, aItem);
+  pref->setItemProperty("max", 1, aItem);
+
+  aItem = aMgr.addPreference(tr("Intervals number"), colorScaleGroup,
+    SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
+  pref->setItemProperty("min", 0, aItem);
+  pref->setItemProperty("max", 100, aItem);
+
+  aItem = aMgr.addPreference(tr("Text height"), colorScaleGroup,
+    SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
+  pref->setItemProperty("min", 0, aItem);
+  pref->setItemProperty("max", 100, aItem);
+
+  aItem = aMgr.addPreference(tr("Text color"), colorScaleGroup,
+    SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
+
   pref->retrieve();
 }
 
@@ -773,6 +898,11 @@ void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& the
   }
   aProp->setValue(aValue);
 
+  if ((theSection == "Visualization") && (theParam == "selection_color")) {
+    std::vector<int> aColor = Config_PropManager::color("Visualization", "selection_color");
+    myWorkshop->displayer()->setSelectionColor(aColor);
+  }
+
   myWorkshop->displayer()->redisplayObjects();
 }
 
@@ -818,7 +948,10 @@ void SHAPERGUI::onEditToolbars()
 {
   SHAPERGUI_ToolbarsDlg aDlg(this);
   if (aDlg.exec() == QDialog::Accepted) {
-    updateToolbars(aDlg.result());
+    if (aDlg.isReset())
+      resetToolbars();
+    else
+      updateToolbars(aDlg.result());
   }
 }
 
@@ -841,6 +974,10 @@ int SHAPERGUI::getNextCommandId() const
 
 void SHAPERGUI::updateToolbars(const QMap<QString, QIntList>& theNewToolbars)
 {
+  // Store default toolbars
+  if (myDefaultToolbars.size() == 0)
+    myDefaultToolbars = myToolbars;
+
   QtxActionToolMgr* aMgr = toolMgr();
   QStringList aToolbars = theNewToolbars.keys();
   QIntList aCommands, aOldCmd;
@@ -902,7 +1039,7 @@ void SHAPERGUI::saveToolbarsConfig()
 {
   if (!myIsToolbarsModified)
     return;
-  // Set toolbars config
+  // Save toolbars configuration into map
   QMap<QString, QStringList> aToolbarsConfig;
   QtxActionToolMgr* aMgr = toolMgr();
   QStringList aToolbars = myToolbars.keys();
@@ -918,30 +1055,39 @@ void SHAPERGUI::saveToolbarsConfig()
     }
     aToolbarsConfig[aName] = aContent;
   }
-
+  // Store the configuration into resources
   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
   QStringList aNames = aToolbarsConfig.keys();
   QStringList aValues;
-  const QString aSection("SHAPER_Toolbars");
   foreach(QString aToolbar, aNames) {
-    aResMgr->setValue(aSection, aToolbar, aToolbarsConfig[aToolbar].join(","));
+    aResMgr->setValue(ToolbarsSection, aToolbar, aToolbarsConfig[aToolbar].join(","));
   }
-  QStringList aOldParams = aResMgr->parameters(aSection);
+  // Remove obsolete parameters from resources
+  QStringList aOldParams = aResMgr->parameters(ToolbarsSection);
   foreach(QString aName, aOldParams) {
     if (!aToolbars.contains(aName))
-      aResMgr->remove(aSection, aName);
+      aResMgr->remove(ToolbarsSection, aName);
   }
+  // Store current list of free commands
+  QIntList aFreeCommands = getFreeCommands();
+  QStringList aFreeList;
+  foreach(int aId, aFreeCommands) {
+    aFreeList.append(action(aId)->data().toString());
+  }
+  if (aFreeList.size() > 0)
+    aResMgr->setValue(ToolbarsSection, FreeCommandsParam, aFreeList.join(","));
+
   myIsToolbarsModified = false;
 }
 
 void SHAPERGUI::loadToolbarsConfig()
 {
-  const QString aSection("SHAPER_Toolbars");
   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
-  QStringList aToolbarNames = aResMgr->parameters(aSection);
+  QStringList aToolbarNames = aResMgr->parameters(ToolbarsSection);
   if (aToolbarNames.size() == 0)
     return;
 
+  // Create commands map
   QMap<QString, int> aCommandsMap;
   QString aCmdIdStr;
   foreach(int aId, myActionsList) {
@@ -949,19 +1095,65 @@ void SHAPERGUI::loadToolbarsConfig()
     aCommandsMap[aCmdIdStr] = aId;
   }
 
+  // Create new toolbars structure
   QMap<QString, QIntList> aToolbars;
   QStringList aCommands;
+  QIntList aKnownCommands;
   QList<QAction*> aActions;
   foreach(QString aName, aToolbarNames) {
-    aCommands = aResMgr->stringValue(aSection, aName).split(",");
-
-    aToolbars[aName] = QIntList();
-    if (aCommands.size() > 0) {
+    aCommands = aResMgr->stringValue(ToolbarsSection, aName).split(",");
+    if (aName == FreeCommandsParam) {
+      // The value is a list of free commands
       foreach(QString aCommand, aCommands) {
-        if (aCommand.isEmpty())
-          aToolbars[aName].append(-1);
-        else if (aCommandsMap.contains(aCommand)) {
-          aToolbars[aName].append(aCommandsMap[aCommand]);
+        aKnownCommands.append(aCommandsMap[aCommand]);
+      }
+    }
+    else {
+      aToolbars[aName] = QIntList();
+      if (aCommands.size() > 0) {
+        foreach(QString aCommand, aCommands) {
+          if (aCommand.isEmpty())
+            aToolbars[aName].append(-1);
+          else if (aCommandsMap.contains(aCommand)) {
+            int aId = aCommandsMap[aCommand];
+            aToolbars[aName].append(aId);
+            aKnownCommands.append(aId);
+          }
+        }
+      }
+    }
+  }
+  // Find new and obsolete commands
+  QIntList aNewCommands = myActionsList;
+  foreach(int aId, myActionsList) {
+    if (aKnownCommands.contains(aId)) {
+      aKnownCommands.removeAll(aId);
+      aNewCommands.removeAll(aId);
+    }
+  }
+  if (aNewCommands.size() > 0) {
+    // Add new commands to toolbars structure
+    QStringList aKeys = myToolbars.keys();
+    foreach(int aNewId, aNewCommands) {
+      foreach(QString aName, aKeys) {
+        if (myToolbars[aName].contains(aNewId)) {
+          if (!aToolbars.contains(aName)) {
+            aToolbars[aName] = QIntList();
+          }
+          aToolbars[aName].append(aNewId);
+        }
+      }
+    }
+  }
+  if (aKnownCommands.size() > 0) {
+    // Remove obsolete commands from the toolbars structure
+    QStringList aKeys = aToolbars.keys();
+    foreach(int aOldId, aKnownCommands) {
+      foreach(QString aName, aKeys) {
+        if (aToolbars[aName].contains(aOldId)) {
+          aToolbars[aName].removeAll(aOldId);
+          if (aToolbars[aName].size() == 0)
+            aToolbars.remove(aName);
         }
       }
     }
@@ -969,3 +1161,36 @@ void SHAPERGUI::loadToolbarsConfig()
   updateToolbars(aToolbars);
   myIsToolbarsModified = false;
 }
+
+
+QIntList SHAPERGUI::getFreeCommands() const
+{
+  QIntList aFreeCommands;
+  QtxActionToolMgr* aMgr = toolMgr();
+  QAction* anAction;
+  int aId;
+  QMap<QString, QIntList>::const_iterator aIt;
+  QIntList aShaperActions = shaperActions();
+  foreach(int aCmd, aShaperActions) {
+    anAction = action(aCmd);
+    aId = aMgr->actionId(anAction);
+    if (!aMgr->containsAction(aId))
+      aFreeCommands.append(aCmd);
+  }
+  return aFreeCommands;
+}
+
+void SHAPERGUI::resetToolbars()
+{
+  if (!myDefaultToolbars.isEmpty())
+    updateToolbars(myDefaultToolbars);
+  myIsToolbarsModified = false;
+  SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
+  aResMgr->remove(ToolbarsSection);
+}
+
+void SHAPERGUI::publishToStudy()
+{
+  if (isActiveModule())
+    myWorkshop->module()->launchOperation("PublishToStudy", false);
+}