Salome HOME
Fix for selection of groups features
[modules/shaper.git] / src / XGUI / XGUI_ViewerProxy.cpp
index f97b3ce5360a63dd7408c5bc6fc6f15dbbf77995..fbff53c41f2180237f277379746c294925d51237 100644 (file)
@@ -1,4 +1,21 @@
-// Copyright (C) 2014-20xx 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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "XGUI_ViewerProxy.h"
 #include "XGUI_Workshop.h"
 #endif
 
 #include <ModuleBase_IViewWindow.h>
+#include <ModuleBase_Preferences.h>
+#include <GeomAPI_Shape.h>
+#include <ModelAPI_ResultConstruction.h>
+
+#include <Config_PropManager.h>
+
+#include <SUIT_ResourceMgr.h>
+#include <AIS_Shape.hxx>
+#include <StdSelect_BRepOwner.hxx>
 
 #include <QEvent>
+#include <QKeyEvent>
+
+
+#define HIGHLIGHT_COLOR Quantity_NOC_YELLOW
 
 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
     : ModuleBase_IViewer(theParent),
@@ -145,13 +175,13 @@ void XGUI_ViewerProxy::connectToViewer()
           SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
 
   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
-    this, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
+    this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
 
   connect(aViewer, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)),
-    this, SIGNAL(keyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
+    this, SLOT(onKeyPress(ModuleBase_IViewWindow*, QKeyEvent*)));
 
   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
-    this, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
+    this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
 
   connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
 
@@ -159,6 +189,7 @@ void XGUI_ViewerProxy::connectToViewer()
 
   connect(aViewer, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
           SIGNAL(contextMenuRequested(QContextMenuEvent*)));
+
 #else
   AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
 
@@ -214,9 +245,7 @@ bool XGUI_ViewerProxy::eventFilter(QObject *theObject, QEvent *theEvent)
 void XGUI_ViewerProxy::onViewCreated(ModuleBase_IViewWindow* theWnd)
 {
   theWnd->viewPort()->installEventFilter(this);
-
   myWindowScale.insert (theWnd->v3dView(), theWnd->v3dView()->Camera()->Scale());
-
   emit viewCreated(theWnd);
 }
 
@@ -267,16 +296,52 @@ void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouse
 
 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
 {
+  if (myIs2dMode) {
+    bool aHighlight2d =
+      ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "highlighting-2d", true);
+    if (aHighlight2d) {
+      if (myShowHighlight)
+        eraseHighlight();
+      else
+        updateHighlight();
+    }
+    else {
+      if (myShowHighlight)
+        updateHighlight();
+      else
+        eraseHighlight();
+    }
+  }
+  else {
+    bool aHighlight3d =
+      ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "highlighting-3d", false);
+    if (aHighlight3d) {
+      if (myShowHighlight)
+        eraseHighlight();
+      else
+        updateHighlight();
+    }
+    else {
+      if (myShowHighlight)
+        updateHighlight();
+      else
+        eraseHighlight();
+    }
+  }
   emit mouseMove(theWnd, theEvent);
 }
 
 void XGUI_ViewerProxy::onKeyPress(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
 {
+  myShowHighlight = theEvent->key() == Qt::Key_H;
   emit keyPress(theWnd, theEvent);
 }
 
 void XGUI_ViewerProxy::onKeyRelease(AppElements_ViewWindow* theWnd, QKeyEvent* theEvent)
 {
+  if (theEvent->key() == Qt::Key_H) {
+    myShowHighlight = false;
+  }
   emit keyRelease(theWnd, theEvent);
 }
 
@@ -379,6 +444,272 @@ bool XGUI_ViewerProxy::canDragByMouse() const
   }
 }
 
+//***************************************
+void XGUI_ViewerProxy::displayHighlight(FeaturePtr theFeature, const TopoDS_Shape& theIgnoreShape)
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+
+  double aDeflection;
+  if (myResult->groupName() == ModelAPI_ResultConstruction::group()) {
+    //FeaturePtr aFeature = ModelAPI_Feature::feature(myResult);
+    if (theFeature.get()) {
+      std::list<ResultPtr> aResults = theFeature->results();
+      std::list<ResultPtr>::const_iterator aIt;
+      ResultPtr aRes;
+      Handle(AIS_Shape) aAis;
+      for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
+        aRes = (*aIt);
+        TopoDS_Shape aTShape = aRes->shape()->impl<TopoDS_Shape>();
+        if (!aTShape.IsSame(theIgnoreShape)) {
+          aAis = new AIS_Shape(aTShape);
+          aAis->SetColor(HIGHLIGHT_COLOR);
+          aAis->SetZLayer(Graphic3d_ZLayerId_Top); //Graphic3d_ZLayerId_Topmost
+          aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
+          aAis->Attributes()->SetDeviationCoefficient(aDeflection);
+          myHighlights.Append(aAis);
+          aContext->Display(aAis, false);
+          aContext->Deactivate(aAis);
+        }
+      }
+    }
+  }
+  else {
+    TopoDS_Shape aTShape = myResult->shape()->impl<TopoDS_Shape>();
+    Handle(AIS_Shape) aAis = new AIS_Shape(aTShape);
+    aAis->SetColor(HIGHLIGHT_COLOR);
+    aAis->SetZLayer(Graphic3d_ZLayerId_Top); //Graphic3d_ZLayerId_Topmost
+    aDeflection = Config_PropManager::real("Visualization", "body_deflection");
+    aAis->Attributes()->SetDeviationCoefficient(aDeflection);
+    myHighlights.Append(aAis);
+    aContext->Display(aAis, false);
+    aContext->Deactivate(aAis);
+  }
+}
+
+bool XGUI_ViewerProxy::eraseHighlight()
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  Handle(AIS_InteractiveObject) anAISIO;
+  AIS_ListIteratorOfListOfInteractive aLIt;
+  bool isErased = myHighlights.Extent() > 0;
+  for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
+    anAISIO = aLIt.Value();
+    aContext->Remove(anAISIO, false);
+  }
+  myHighlights.Clear();
+  return isErased;
+}
+
+void XGUI_ViewerProxy::updateHighlight()
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  if (!aContext.IsNull()) {
+    Handle(StdSelect_BRepOwner) aOwner;
+    Handle(AIS_InteractiveObject) anIO;
+    bool isDisplayed = false;
+    TopoDS_Shape aShape, aShp;
+    ResultPtr aRes;
+    XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+    for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
+      aOwner = Handle(StdSelect_BRepOwner)::DownCast(aContext->DetectedOwner());
+      if ((!aOwner.IsNull()) && aOwner->HasShape()) {
+        aShape = aOwner->Shape();
+        anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
+        aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aDisplayer->getObject(anIO));
+        if (aRes.get() && (aRes != myResult)) {
+          eraseHighlight();
+          FeaturePtr aFeature = ModelAPI_Feature::feature(aRes);
+          aShp = aRes->shape()->impl<TopoDS_Shape>();
+          if ((aFeature->results().size() > 1) || (!aShp.IsSame(aShape))) {
+            myResult = aRes;
+            displayHighlight(aFeature, aShape);
+          }
+          else {
+            myResult = ResultPtr();
+          }
+          update();
+        }
+        isDisplayed = aRes.get();
+      }
+    }
+    if (!isDisplayed) {
+      if (eraseHighlight()) {
+        update();
+      }
+      myResult = ResultPtr();
+    }
+  }
+}
+
+#ifdef HAVE_SALOME
+void XGUI_ViewerProxy::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
+{
+  if (myIs2dMode) {
+    bool aHighlight2d =
+      ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "highlighting-2d", true);
+    if (aHighlight2d) {
+      if (myShowHighlight)
+        eraseHighlight();
+      else
+        updateHighlight();
+    }
+    else {
+      if (myShowHighlight)
+        updateHighlight();
+      else
+        eraseHighlight();
+    }
+  }
+  else {
+    bool aHighlight3d =
+      ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "highlighting-3d", false);
+    if (aHighlight3d) {
+      if (myShowHighlight)
+        eraseHighlight();
+      else
+        updateHighlight();
+    }
+    else {
+      if (myShowHighlight)
+        updateHighlight();
+      else
+        eraseHighlight();
+    }
+  }
+  emit mouseMove(theWnd, theEvent);
+}
+
+void XGUI_ViewerProxy::onKeyPress(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
+{
+  myShowHighlight = theEvent->key() == Qt::Key_H;
+  emit keyPress(theWnd, theEvent);
+}
+
+void XGUI_ViewerProxy::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
+{
+  if (theEvent->key() == Qt::Key_H) {
+    myShowHighlight = false;
+  }
+  emit keyRelease(theWnd, theEvent);
+}
+#endif
+
+
+bool XGUI_ViewerProxy::isColorScaleVisible() const
+{
+#ifdef HAVE_SALOME
+  return myWorkshop->salomeConnector()->viewer()->isColorScaleVisible();
+#else
+  return myWorkshop->mainWindow()->viewer()->isColorScaleVisible();
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScaleShown(bool on)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleShown(on);
+#else
+  myWorkshop->mainWindow()->viewer()->setColorScaleShown(on);
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScalePosition(double theX, double theY)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScalePosition(theX, theY);
+#else
+  QWidget* aWindow = activeViewPort();
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  aColorScale->SetPosition(aWindow->width() * theX, aWindow->height() * theY);
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScaleSize(double theW, double theH)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleSize(theW, theH);
+#else
+  QWidget* aWindow = activeViewPort();
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  aColorScale->SetSize(aWindow->width() * theW, aWindow->height() * theH);
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScaleRange(double theMin, double theMax)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleRange(theMin, theMax);
+#else
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  aColorScale->SetRange(theMin, theMax);
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScaleIntervals(int theNb)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleIntervals(theNb);
+#else
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  aColorScale->SetNumberOfIntervals(theNb);
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScaleTextColor(const QColor& theColor)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleTextColor(theColor);
+#else
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  Quantity_Color aColor(theColor.redF(), theColor.greenF(), theColor.blueF(), Quantity_TOC_RGB);
+  aColorScale->SetColor(aColor);
+#endif
+}
+
+
+void XGUI_ViewerProxy::setColorScaleTextHeigth(int theH)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleTextHeigth(theH);
+#else
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  aColorScale->SetTextHeight(theH);
+#endif
+}
+
+void XGUI_ViewerProxy::setColorScaleTitle(const QString& theText)
+{
+#ifdef HAVE_SALOME
+  myWorkshop->salomeConnector()->viewer()->setColorScaleTitle(theText);
+#else
+  Handle(AIS_ColorScale) aColorScale = myWorkshop->mainWindow()->viewer()->colorScale();
+  aColorScale->SetTitle(theText.toStdString().c_str());
+#endif
+}
+
+
+//******************************************************
+void XGUI_ViewerProxy::setupColorScale()
+{
+  SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
+  double aX = aResMgr->doubleValue("Viewer", "scalar_bar_x_position", 0.03);
+  double aY = aResMgr->doubleValue("Viewer", "scalar_bar_y_position", 0.35);
+  setColorScalePosition(aX, aY);
+
+  double aW = aResMgr->doubleValue("Viewer", "scalar_bar_width", 0.2);
+  double aH = aResMgr->doubleValue("Viewer", "scalar_bar_height", 0.5);
+  setColorScaleSize(aW, aH);
+
+  QColor aColor = aResMgr->colorValue("Viewer", "scalar_bar_text_color", Qt::black);
+  setColorScaleTextColor(aColor);
+
+  int aT = aResMgr->integerValue("Viewer", "scalar_bar_text_height", 14);
+  setColorScaleTextHeigth(aT);
+
+  int aN = aResMgr->integerValue("Viewer", "scalar_bar_nb_intervals", 20);
+  setColorScaleIntervals(aN);
+}
+
 
 //***************************************
 //void XGUI_ViewerProxy::Zfitall()
@@ -395,4 +726,27 @@ bool XGUI_ViewerProxy::canDragByMouse() const
 //      aView3d->DepthFitAll();
 //  }
 //#endif
-//}
\ No newline at end of file
+//}
+
+
+#ifdef HAVE_SALOME
+void XGUI_ViewerProxy::setFitter(OCCViewer_Fitter* theFitter)
+{
+  myWorkshop->salomeConnector()->viewer()->setFitter(theFitter);
+}
+
+OCCViewer_Fitter* XGUI_ViewerProxy::fitter() const
+{
+  return myWorkshop->salomeConnector()->viewer()->fitter();
+}
+#else
+void XGUI_ViewerProxy::setFitter(AppElements_Fitter* theFitter)
+{
+  myWorkshop->mainWindow()->viewer()->setFitter(theFitter);
+}
+
+AppElements_Fitter* XGUI_ViewerProxy::fitter() const
+{
+  return myWorkshop->mainWindow()->viewer()->fitter();
+}
+#endif