Salome HOME
Issue #2865: Do not highlight a pre-selected shape
[modules/shaper.git] / src / XGUI / XGUI_ViewerProxy.cpp
index 8687cba0648e66485909f7e2492ff3f831f82dd5..27277683abd64c271c897e27fda4f293b19cc78c 100644 (file)
 
 #include <ModuleBase_IViewWindow.h>
 #include <GeomAPI_Shape.h>
+#include <ModelAPI_ResultConstruction.h>
+
+#include <Config_PropManager.h>
 
 #include <AIS_Shape.hxx>
+#include <StdSelect_BRepOwner.hxx>
 
 #include <QEvent>
 
+
+#define HIGHLIGHT_COLOR Quantity_NOC_YELLOW
+
 XGUI_ViewerProxy::XGUI_ViewerProxy(XGUI_Workshop* theParent)
     : ModuleBase_IViewer(theParent),
       myWorkshop(theParent)
@@ -166,7 +173,7 @@ 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*)));
@@ -286,70 +293,9 @@ void XGUI_ViewerProxy::onMouseDoubleClick(AppElements_ViewWindow* theWnd, QMouse
   emit mouseDoubleClick(theWnd, theEvent);
 }
 
-void XGUI_ViewerProxy::displayHighlight()
-{
-  Handle(AIS_InteractiveContext) aContext = AISContext();
-  std::list<ResultPtr> aResults = myFeature->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>();
-    aAis = new AIS_Shape(aTShape);
-    aAis->SetColor(Quantity_NOC_CYAN3);
-    aAis->SetZLayer(Graphic3d_ZLayerId_Topmost);
-    myHighlights.Append(aAis);
-    aContext->Display(aAis, false);
-    aContext->Deactivate(aAis);
-  }
-}
-
-void XGUI_ViewerProxy::eraseHighlight()
-{
-  Handle(AIS_InteractiveContext) aContext = AISContext();
-  Handle(AIS_InteractiveObject) anAISIO;
-  AIS_ListIteratorOfListOfInteractive aLIt;
-  for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
-    anAISIO = aLIt.Value();
-    aContext->Remove(anAISIO, false);
-  }
-  myHighlights.Clear();
-}
-
 void XGUI_ViewerProxy::onMouseMove(AppElements_ViewWindow* theWnd, QMouseEvent* theEvent)
 {
-  // vsv: Display prehighlighting of detected object
-  //Handle(AIS_InteractiveContext) aContext = AISContext();
-  //if (!aContext.IsNull()) {
-  //  Handle(SelectMgr_EntityOwner) aOwner;
-  //  Handle(AIS_InteractiveObject) anIO;
-  //  ObjectPtr aObj;
-  //  bool isDisplayed = false;
-  //  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-  //  for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
-  //    aOwner = aContext->DetectedOwner();
-  //    anIO = Handle(AIS_InteractiveObject)::DownCast(aOwner->Selectable());
-  //    aObj = aDisplayer->getObject(anIO);
-  //    if (aObj.get()) {
-  //      FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
-  //      if (aFeature.get()) {
-  //        if (aFeature != myFeature) {
-  //          eraseHighlight();
-  //          myFeature = aFeature;
-  //          displayHighlight();
-  //          aContext->UpdateCurrentViewer();
-  //        }
-  //        isDisplayed = true;
-  //      }
-  //    }
-  //  }
-  //  if (!isDisplayed) {
-  //    eraseHighlight();
-  //    aContext->UpdateCurrentViewer();
-  //    myFeature = FeaturePtr();
-  //  }
-  //}
+  updateHighlight();
   emit mouseMove(theWnd, theEvent);
 }
 
@@ -462,6 +408,107 @@ 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(1); //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(1); //Graphic3d_ZLayerId_Topmost
+    aDeflection = Config_PropManager::real("Visualization", "body_deflection");
+    aAis->Attributes()->SetDeviationCoefficient(aDeflection);
+    myHighlights.Append(aAis);
+    aContext->Display(aAis, false);
+    aContext->Deactivate(aAis);
+  }
+}
+
+void XGUI_ViewerProxy::eraseHighlight()
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  Handle(AIS_InteractiveObject) anAISIO;
+  AIS_ListIteratorOfListOfInteractive aLIt;
+  for (aLIt.Initialize(myHighlights); aLIt.More(); aLIt.Next()) {
+    anAISIO = aLIt.Value();
+    aContext->Remove(anAISIO, false);
+  }
+  myHighlights.Clear();
+}
+
+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();
+          }
+          aContext->UpdateCurrentViewer();
+        }
+        isDisplayed = aRes.get();
+      }
+    }
+    if (!isDisplayed) {
+      eraseHighlight();
+      aContext->UpdateCurrentViewer();
+      myResult = ResultPtr();
+    }
+  }
+}
+
+#ifdef HAVE_SALOME
+void XGUI_ViewerProxy::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
+{
+  updateHighlight();
+  emit mouseMove(theWnd, theEvent);
+}
+#endif
 
 //***************************************
 //void XGUI_ViewerProxy::Zfitall()