0027893: Visualization - AIS_InteractiveContext::SetSelected does not work
[occt.git] / src / SelectMgr / SelectMgr_SelectionManager.cxx
index 8aaffba..3e6de7b 100644 (file)
@@ -28,6 +28,8 @@
 #include <TColStd_ListOfInteger.hxx>
 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
 
+IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectionManager,MMgt_TShared)
+
 static Standard_Integer FindIndex (const SelectMgr_SequenceOfSelector& theSelectorsSeq,
                                    const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
@@ -163,19 +165,16 @@ void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)&
   {
     if (!myGlobal.Contains (theObject))
     {
-      if (theObject->HasOwnPresentations())
-      {
-        SelectMgr_SequenceOfSelector aSelectors;
-        aSelectors.Append (theSelector);
-        myLocal.Bind (theObject, aSelectors);
-      }
-      else
+      for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
       {
-        for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
-        {
-          Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
-        }
+        Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
       }
+      if (!theObject->HasOwnPresentations())
+        return;
+
+      SelectMgr_SequenceOfSelector aSelectors;
+      aSelectors.Append (theSelector);
+      myLocal.Bind (theObject, aSelectors);
     }
   }
 }
@@ -187,6 +186,14 @@ void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)&
 //==================================================
 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
 {
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
+  {
+    Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
+  }
+
+  if (!theObject->HasOwnPresentations())
+    return;
+
   if (myGlobal.Contains (theObject))
   {
     for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
@@ -212,8 +219,7 @@ void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject
     SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
     for (Standard_Integer aSelectorsIdx = 1; aSelectorsIdx <= aSelectors.Length(); aSelectorsIdx++)
     {
-      Handle(SelectMgr_ViewerSelector) aCurSelector =
-        Handle(SelectMgr_ViewerSelector)::DownCast (aSelectors (aSelectorsIdx));
+      Handle(SelectMgr_ViewerSelector) aCurSelector = aSelectors (aSelectorsIdx);
       if (!aCurSelector->Contains (theObject))
         continue;
 
@@ -227,13 +233,6 @@ void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject
 
     myLocal.UnBind (theObject);
   }
-  else if (!theObject->HasOwnPresentations())
-  {
-    for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
-    {
-      Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
-    }
-  }
 
   theObject->ClearSelections();
 }
@@ -297,15 +296,13 @@ void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObje
   if (!theSelector.IsNull() && !mySelectors.Contains (theSelector))
     return;
 
-  if (!theObject->HasOwnPresentations())
+  for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
   {
-    for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
-    {
-      Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
-    }
+    Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
+  }
 
+  if (!theObject->HasOwnPresentations())
     return;
-  }
 
   Standard_Boolean isComputed = Standard_False;
   if (theObject->HasSelection (theMode))
@@ -332,8 +329,7 @@ void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObje
       SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (theObject);
       for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= theSelectors.Length(); aSelectorIdx++)
       {
-        Handle(SelectMgr_ViewerSelector) aCurSelector =
-          Handle(SelectMgr_ViewerSelector)::DownCast (theSelectors (aSelectorIdx));
+        Handle(SelectMgr_ViewerSelector) aCurSelector = theSelectors (aSelectorIdx);
         Activate (theObject, theMode, aCurSelector);
       }
     }
@@ -374,9 +370,10 @@ void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObje
 
   if (myGlobal.Contains (theObject))
   {
-    if (theMode != 0 && theSelector->IsActive (theObject, 0))
+    const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
+    if (theMode != aGlobalSelMode && theSelector->IsActive (theObject, aGlobalSelMode))
     {
-      theSelector->Deactivate (theObject->Selection (0));
+      theSelector->Deactivate (theObject->Selection (aGlobalSelMode));
     }
     theSelector->Activate (theObject->Selection (theMode));
   }
@@ -399,16 +396,13 @@ void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableOb
                                              const Standard_Integer theMode,
                                              const Handle(SelectMgr_ViewerSelector)& theSelector)
 {
-
-  if (!theObject->HasOwnPresentations())
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
   {
-    for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
-    {
-      Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
-    }
+    Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
+  }
 
+  if (!theObject->HasOwnPresentations())
     return;
-  }
 
   Standard_Boolean isInGlobal = myGlobal.Contains (theObject);
   Standard_Boolean hasSelection = theMode == -1 ? Standard_True : theObject->HasSelection (theMode);
@@ -459,16 +453,14 @@ Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr
                                                           const Standard_Integer theMode,
                                                           const Handle(SelectMgr_ViewerSelector)& theSelector) const
 {
-  if (!theObject->HasOwnPresentations())
+  for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
   {
-    for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
-    {
-      if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector))
-        return Standard_True;
-    }
+    if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector))
+      return Standard_True;
+  }
 
+  if (!theObject->HasOwnPresentations())
     return Standard_False;
-  }
 
   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
     return Standard_False;
@@ -648,6 +640,28 @@ void SelectMgr_SelectionManager::rebuildSelectionStructures (const Handle(Select
   }
 }
 
+//==================================================
+// Function: recomputeSelectionMode
+// Purpose :
+//==================================================
+void SelectMgr_SelectionManager::recomputeSelectionMode (const Handle(SelectMgr_SelectableObject)& theObject,
+                                                         const Handle(SelectMgr_Selection)& theSelection,
+                                                         const Standard_Integer theMode)
+{
+  theSelection->UpdateStatus (SelectMgr_TOU_Full);
+
+  for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
+  {
+    Handle(SelectMgr_ViewerSelector) aCurSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
+
+    ClearSelectionStructures (theObject, theMode, aCurSelector);
+    theObject->RecomputePrimitives (theMode);
+    RestoreSelectionStructures (theObject, theMode, aCurSelector);
+    theSelection->UpdateStatus (SelectMgr_TOU_None);
+    theSelection->UpdateBVHStatus (SelectMgr_TBU_None);
+  }
+}
+
 //==================================================
 // Function: Update
 // Purpose :
@@ -686,25 +700,23 @@ void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_Sele
   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
     return;
 
-  for(theObject->Init(); theObject->More(); theObject->Next())
+  if (theMode == -1)
   {
-    const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
-    aSelection->UpdateStatus (SelectMgr_TOU_Full);
-    Standard_Integer aSelMode = aSelection->Mode();
-
-    for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
+    for (theObject->Init(); theObject->More(); theObject->Next())
     {
-      Handle(SelectMgr_ViewerSelector) aCurSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
-      if (aCurSelector->Status (aSelection) == SelectMgr_SOS_Activated)
-      {
-          ClearSelectionStructures (theObject, aSelMode, aCurSelector);
-          theObject->RecomputePrimitives(aSelMode);
-          RestoreSelectionStructures (theObject, aSelMode, aCurSelector);
-          aSelection->UpdateStatus (SelectMgr_TOU_None);
-          aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
-      }
+      const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
+      Standard_Integer aSelMode = aSelection->Mode();
+      recomputeSelectionMode (theObject, aSelection, aSelMode);
     }
   }
+  else
+  {
+    if (!theObject->HasSelection (theMode))
+      return;
+
+    const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
+    recomputeSelectionMode (theObject, aSelection, theMode);
+  }
 }
 
 //=======================================================================
@@ -954,3 +966,35 @@ void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr
     }
   }
 }
+
+//=======================================================================
+//function : UpdateSelection
+//purpose  :
+//=======================================================================
+void SelectMgr_SelectionManager::UpdateSelection (const Handle(SelectMgr_SelectableObject)& theObject)
+{
+  if (myGlobal.Contains (theObject))
+  {
+    for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
+      if (aSelector->Contains (theObject))
+      {
+        aSelector->MoveSelectableObject (theObject);
+      }
+    }
+  }
+
+  if (myLocal.IsBound (theObject))
+  {
+    const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
+    for (SelectMgr_SequenceOfSelector::Iterator aSelectorsIter (aSelectors); aSelectorsIter.More(); aSelectorsIter.Next())
+    {
+      Handle(SelectMgr_ViewerSelector)& aSelector = aSelectorsIter.ChangeValue();
+      if (aSelector->Contains (theObject))
+      {
+        aSelector->MoveSelectableObject (theObject);
+      }
+    }
+  }
+}