Salome HOME
Fix for the issue #1695 : don't display circle on start point creation in arc by...
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
index 95d58996690c83bd5a5147984df7d7556a5b64d0..24e7b9eb6f740d8dce04b28d42172ace6b21faea 100644 (file)
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_IModule.h>
 #include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_Preferences.h>
 
 #include <GeomAPI_Shape.h>
 #include <GeomAPI_IPresentable.h>
 #include <GeomAPI_ICustomPrs.h>
 
+#include <SUIT_ResourceMgr.h>
+
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_LocalContext.hxx>
 #include <AIS_ListOfInteractive.hxx>
@@ -76,7 +79,7 @@ const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse
 
 //#define DEBUG_OCCT_SHAPE_SELECTION
 
-#define WORKAROUND_UNTIL_27523_IS_FIXED
+//#define WORKAROUND_UNTIL_27523_IS_FIXED
 
 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
 {
@@ -149,6 +152,18 @@ bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
     bool isShading = false;
     if (aPrs.get() != NULL) {
       anAIS = aPrs->getAISObject(anAIS);
+      if (anAIS.get()) {
+        // correct deviation coefficient for 
+        /*Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
+        if (!anAISPrs.IsNull()) {
+          Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
+          if (!aShapePrs.IsNull()) {
+            TopoDS_Shape aShape = aShapePrs->Shape();
+            if (!aShape.IsNull())
+              //ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, anAISPrs->Attributes());
+          }
+        }*/
+      }
     } else {
       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
       if (aResult.get() != NULL) {
@@ -171,6 +186,11 @@ bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
           Handle(AIS_InteractiveObject) anAISPrs = myWorkshop->module()->createPresentation(aResult);
           if (anAISPrs.IsNull())
             anAISPrs = new ModuleBase_ResultPrs(aResult);
+          else {
+            Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
+            if (!aShapePrs.IsNull())
+              ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*) aShapePrs.Access());
+          }
           anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
           //anAIS->createShape(aShapePtr);
           isShading = true;
@@ -228,15 +248,6 @@ bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
 
     emit objectDisplayed(theObject, theAIS);
     activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
-    // the fix from VPA for more suitable selection of sketcher lines
-    if(anAISIO->Width() > 1) {
-      for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
-        int aMode = myActiveSelectionModes.value(aModeIdx);
-        double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 : 
-                                                    (anAISIO->Width() + 2);
-        aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
-      }
-    }
   } 
   if (theUpdateViewer)
     updateViewer();
@@ -436,8 +447,8 @@ void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theMode
 
 int XGUI_Displayer::getSelectionMode(int theShapeType)
 {
-  return (theShapeType >= TopAbs_SHAPE)? theShapeType : 
-    AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
+  return (theShapeType > TopAbs_SHAPE) ? theShapeType : 
+                                         AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
 }
 
 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
@@ -465,7 +476,8 @@ bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& th
   if (!aVisible) {
     // check if all results of the feature are visible
     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
-    std::list<ResultPtr> aResults = aFeature->results();
+    std::list<ResultPtr> aResults;
+    ModelAPI_Tools::allResults(aFeature, aResults);
     std::list<ResultPtr>::const_iterator aIt;
     aVisible = !aResults.empty();
     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
@@ -575,13 +587,6 @@ void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrL
     if (activate(anAISIO, myActiveSelectionModes, false))
       isActivationChanged = true;
   }
-  if (!aTrihedron.IsNull()) {
-    foreach(int aMode, myActiveSelectionModes)
-      aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
-  }
-  // VSV It seems that there is no necessity to update viewer on activation
-  //if (theUpdateViewer && isActivationChanged)
-  //  updateViewer();
 }
 
 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
@@ -597,6 +602,8 @@ bool XGUI_Displayer::isActive(ObjectPtr theObject) const
   aContext->ActivatedModes(anAIS, aModes);
   return aModes.Extent() > 0;
 }
+
+
 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues, bool theUpdateViewer)
 {
   Handle(AIS_InteractiveContext) aContext = AISContext();
@@ -605,16 +612,25 @@ void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValue
   if (aContext->HasOpenedContext()) {
     aContext->UnhilightSelected(false);
     aContext->ClearSelected(false);
-    NCollection_Map<TopoDS_Shape> aShapesToBeSelected;
+    NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>> aShapesToBeSelected;
 
     foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
       const GeomShapePtr& aGeomShape = aPrs->shape();
       if (aGeomShape.get() && !aGeomShape->isNull()) {
         const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
 #ifdef DEBUG_OCCT_SHAPE_SELECTION
+        // problem 1: performance
+        // problem 2: IO is not specified, so the first found owner is selected, as a result
+        // it might belong to another result
         aContext->AddOrRemoveSelected(aShape, false);
 #else
-        aShapesToBeSelected.Add(aShape);
+        NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
+        if (aShapesToBeSelected.IsBound(aShape))
+          aPresentations = aShapesToBeSelected.Find(aShape);
+        ObjectPtr anObject = aPrs->object();
+        getPresentations(anObject, aPresentations);
+
+        aShapesToBeSelected.Bind(aShape, aPresentations);
 #endif
       } else {
         ObjectPtr anObject = aPrs->object();
@@ -654,12 +670,12 @@ void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValue
     updateViewer();
 }
 
-void XGUI_Displayer::clearSelected()
+void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
 {
   Handle(AIS_InteractiveContext) aContext = AISContext();
   if (!aContext.IsNull()) {
     aContext->UnhilightCurrents(false);
-    aContext->ClearSelected();
+    aContext->ClearSelected(theUpdateViewer);
   }
 }
 
@@ -891,6 +907,11 @@ void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
                                  const int theMode, const bool theUpdateViewer) const
 {
   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
+  if (!theIO.IsNull() && theIO == getTrihedron()) {
+    if (theMode != AIS_Shape::SelectionType(TopAbs_EDGE) &&
+        theMode != AIS_Shape::SelectionType(TopAbs_VERTEX))
+      return;
+  }
   if (!aContext.IsNull()) {
     if (myWorkshop->module()) {
       int aMode = (theMode > 8)? theMode : AIS_Shape::SelectionType(theMode);
@@ -898,6 +919,16 @@ void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
     } else
       aContext->Activate(theIO, theMode, false);
 
+    // the fix from VPA for more suitable selection of sketcher lines
+    if (theIO->Width() > 1) {
+      double aPrecision = theIO->Width() + 2;
+      if (theMode == getSelectionMode(TopAbs_VERTEX)) 
+        aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer", "point-selection-sensitivity", 20);
+      else if ((theMode == getSelectionMode(TopAbs_EDGE)) || (theMode == getSelectionMode(TopAbs_WIRE)))
+        aPrecision = theIO->Width() + 
+                     ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer", "edge-selection-sensitivity", 2);
+      aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
+    }
     ModuleBase_Tools::selectionInfo(aContext, "XGUI_Displayer::activateAIS -- Activate");
 
 #ifdef DEBUG_ACTIVATE_AIS
@@ -1283,6 +1314,42 @@ std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
                                             arg(aContent.join("\n")).toStdString().c_str();
 }
 
+void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
+                                  NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
+{
+  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+  if (aResult.get()) {
+    AISObjectPtr aAISObj = getAISObject(aResult);
+    if (aAISObj.get() != NULL) {
+      Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+      if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
+        thePresentations.Add(anAIS);
+    }
+  }
+  else {
+    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+    // find presentation of the feature
+    AISObjectPtr aAISObj = getAISObject(aFeature);
+    if (aAISObj.get() != NULL) {
+      Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+      if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
+        thePresentations.Add(anAIS);
+    }
+    // find presentations of the feature results
+    std::list<ResultPtr> aResults;
+    ModelAPI_Tools::allResults(aFeature, aResults);
+    std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
+    for (; anIt != aLast; ++anIt) {
+      AISObjectPtr aAISObj = getAISObject(*anIt);
+      if (aAISObj.get() != NULL) {
+        Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+        if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
+          thePresentations.Add(anAIS);
+      }
+    }
+  }
+}
+
 void XGUI_Displayer::activateTrihedron(bool theIsActive) 
 {  
   myIsTrihedronActive = theIsActive; 
@@ -1334,27 +1401,43 @@ QIntList XGUI_Displayer::activeSelectionModes() const
 }
 
 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
-                                      const NCollection_Map<TopoDS_Shape>& theShapesToBeSelected)
+ const NCollection_DataMap<TopoDS_Shape,
+                           NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
 {
   Handle(AIS_LocalContext) aLContext = theContext->LocalContext();
   TCollection_AsciiString aSelectionName = aLContext->SelectionName();
   aLContext->UnhilightPicked(Standard_False);
 
-  NCollection_Map<TopoDS_Shape> aShapesSelected;
-
   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
   aLContext->MainSelector()->ActiveOwners(anActiveOwners);
   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
   Handle(SelectMgr_EntityOwner) anOwner;
+
+  /// It is very important to check that the owner is processed only once and has a map of
+  /// processed owners because SetSelected works as a switch.
+  /// If count of calls setSelectec is even, the object stays in the previous state
+  /// (selected, deselected)
+  /// OCCT: to write about the problem that active owners method returns one owner several times
+  QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
   for (; anOwnersIt.More(); anOwnersIt.Next()) {
     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
+    if (aSelectedIds.contains((long)anOwner.Access()))
+      continue;
+    aSelectedIds.append((long)anOwner.Access());
+
     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
-    if (!BROwnr.IsNull() && BROwnr->HasShape() && theShapesToBeSelected.Contains(BROwnr->Shape())) {
-      if (aShapesSelected.Contains(BROwnr->Shape()))
-        continue;
-      AIS_Selection::Selection(aSelectionName.ToCString())->Select(anOwner);
-      anOwner->SetSelected (Standard_True);
-      aShapesSelected.Add(BROwnr->Shape());
+    if (!BROwnr.IsNull() && BROwnr->HasShape()) {
+      const TopoDS_Shape& aShape = BROwnr->Shape();
+      if (!aShape.IsNull() && theShapesToBeSelected.IsBound(aShape)) {
+        Handle(AIS_InteractiveObject) anOwnerPresentation =
+                                    Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
+        NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
+                                    theShapesToBeSelected.Find(aShape);
+        if (aPresentations.Contains(anOwnerPresentation)) {
+          AIS_Selection::Selection(aSelectionName.ToCString())->Select(anOwner);
+          anOwner->SetSelected (Standard_True);
+        }
+      }
     }
   }
   aLContext->HilightPicked(Standard_False);