Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
index bf5a0516545685504f0a484595aa68553c742673..a8f9dcad990baa4b7a0fe56252615dd0231c3458 100644 (file)
@@ -68,6 +68,7 @@ void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfIn
 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
   : myWorkshop(theWorkshop)
 {
+  enableUpdateViewer(true);
 }
 
 XGUI_Displayer::~XGUI_Displayer()
@@ -115,8 +116,13 @@ bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
     TopAbs_ShapeEnum aType = aShape.ShapeType();
     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
       return false;
-    else
+    else {
+      // Check that the presentation is not a sketch
+      Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
+      if (!aPrs.IsNull()) 
+        return !aPrs->isSketchMode();
       return true;
+    }
   }
   return false;
 }
@@ -131,7 +137,7 @@ void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
   if (!anAISIO.IsNull()) {
     myResult2AISObjectMap[theObject] = theAIS;
-    bool aCanBeShaded = canBeShaded(anAISIO);
+    bool aCanBeShaded = ::canBeShaded(anAISIO);
     // In order to avoid extra closing/opening context
     SelectMgr_IndexedMapOfOwner aSelectedOwners;
     if (aCanBeShaded) {
@@ -152,7 +158,7 @@ void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
     if (aCanBeShaded) {
       openLocalContext();
       activateObjects(myActiveSelectionModes);
-      myWorkshop->selector()->setSelectedOwners(aSelectedOwners);
+      myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
     }
   }
   if (isUpdateViewer)
@@ -202,11 +208,24 @@ void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
     Handle(AIS_InteractiveContext) aContext = AISContext();
     if (aContext.IsNull())
       return;
-    bool aToSelect = aContext->IsSelected(aAISIO);
+    // Check that the visualized shape is the same and the redisplay is not necessary
+    // Redisplay of AIS object leads to this object selection compute and the selection 
+    // in the browser is lost
+    // become
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+    if (aResult.get() != NULL) {
+      Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
+      if (!aShapePrs.IsNull()) {
+        std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
+        const TopoDS_Shape& aShape = aShapePrs->Shape();
+        std::shared_ptr<GeomAPI_Shape> anAISShapePtr(new GeomAPI_Shape());
+        anAISShapePtr->setImpl(new TopoDS_Shape(aShape));
+
+        if (aShapePtr->isEqual(anAISShapePtr))
+          return;
+      }
+    }
     aContext->Redisplay(aAISIO, false);
-    // Restore selection state after redisplay
-    if (aToSelect) 
-      aContext->SetSelected(aAISIO);
     if (isUpdateViewer)
       updateViewer();
   }
@@ -297,7 +316,7 @@ void XGUI_Displayer::activateObjects(const QIntList& theModes)
   //myUseExternalObjects = true;
 
   AIS_ListOfInteractive aPrsList;
-  displayedObjects(aContext, aPrsList);
+  ::displayedObjects(aContext, aPrsList);
 
   Handle(AIS_Trihedron) aTrihedron;
   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
@@ -337,7 +356,7 @@ void XGUI_Displayer::deactivateObjects()
 
   //aContext->NotUseDisplayedObjects();
   AIS_ListOfInteractive aPrsList;
-  displayedObjects(aContext, aPrsList);
+  ::displayedObjects(aContext, aPrsList);
 
   AIS_ListIteratorOfListOfInteractive aLIt;
   //Handle(AIS_Trihedron) aTrihedron;
@@ -430,7 +449,7 @@ void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
 
 void XGUI_Displayer::openLocalContext()
 {
-  Handle(AIS_InteractiveContext) aContext = AISContext();
+  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
   if (aContext.IsNull())
     return;
   // Open local context if there is no one
@@ -541,10 +560,19 @@ ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO)
   return aFeature;
 }
 
+bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
+{
+  bool aWasEnabled = myEnableUpdateViewer;
+
+  myEnableUpdateViewer = isEnabled;
+
+  return aWasEnabled;
+}
+
 void XGUI_Displayer::updateViewer()
 {
   Handle(AIS_InteractiveContext) aContext = AISContext();
-  if (!aContext.IsNull())
+  if (!aContext.IsNull() && myEnableUpdateViewer)
     aContext->UpdateCurrentViewer();
 }
 
@@ -612,7 +640,21 @@ void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bo
     return;
 
   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
-  aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
+  bool aCanBeShaded = ::canBeShaded(aAISIO);
+  // In order to avoid extra closing/opening context
+  SelectMgr_IndexedMapOfOwner aSelectedOwners;
+  if (aCanBeShaded) {
+    myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
+    closeLocalContexts(false);
+  }
+  aContext->SetDisplayMode(aAISIO, theMode, false);
+  if (aCanBeShaded) {
+    openLocalContext();
+    activateObjects(myActiveSelectionModes);
+    myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
+  }
+  if (toUpdate)
+    updateViewer();
 }
 
 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
@@ -672,3 +714,16 @@ void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
   }
   updateViewer();
 }
+
+bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
+{ 
+  if (!isVisible(theObject))
+    return false;
+
+  AISObjectPtr aAISObj = getAISObject(theObject);
+  if (aAISObj.get() == NULL)
+    return false;
+
+  Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+  return ::canBeShaded(anAIS);
+}