0027285: Visualization - selection of AIS_MultipleConnectedInteractive is broken
[occt.git] / src / AIS / AIS_InteractiveContext_1.cxx
index 47cc984..eaa9d9f 100644 (file)
@@ -52,7 +52,6 @@
 #include <V3d_SpotLight.hxx>
 #include <V3d_View.hxx>
 #include <V3d_Viewer.hxx>
-#include <Visual3d_View.hxx>
 
 typedef NCollection_DataMap<Handle(AIS_InteractiveObject), NCollection_Handle<SelectMgr_SequenceOfOwner> > AIS_MapOfObjSelectedOwners;
 
@@ -109,6 +108,56 @@ void AIS_InteractiveContext::highlightSelected (const Handle(SelectMgr_EntityOwn
   }
 }
 
+//=======================================================================
+//function : unhighlightSelected
+//purpose  :
+//=======================================================================
+void AIS_InteractiveContext::unhighlightSelected (const Standard_Boolean theIsToHilightSubIntensity)
+{
+  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
+  Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
+  NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
+  for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
+  {
+    const Handle(SelectMgr_EntityOwner) anOwner =
+      Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
+    if (anOwner.IsNull() || !anOwner->HasSelectable())
+      continue;
+
+    const Handle(AIS_InteractiveObject) anInteractive =
+      Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
+    if (anOwner->IsAutoHilight())
+    {
+      const Standard_Integer aHiMode = anInteractive->HasHilightMode() ? anInteractive->HilightMode() : 0;
+      anOwner->Unhilight (myMainPM, aHiMode);
+      if (theIsToHilightSubIntensity)
+      {
+        if (myObjects.IsBound (anInteractive) && myObjects (anInteractive)->IsSubIntensityOn())
+        {
+          anOwner->HilightWithColor (myMainPM, mySubIntensity, aHiMode);
+        }
+      }
+    }
+    else
+    {
+      if (!anObjToClear.Contains (anInteractive))
+        anObjToClear.Add (anInteractive);
+    }
+    anOwner->State (0);
+    if (anOwner == anInteractive->GlobalSelOwner())
+    {
+      myObjects.ChangeFind (anInteractive)->SetHilightStatus (Standard_False);
+    }
+  }
+  for (NCollection_IndexedMap<Handle(AIS_InteractiveObject)>::Iterator anIter (anObjToClear); anIter.More(); anIter.Next())
+  {
+    const Handle(AIS_InteractiveObject)& anObj = anIter.Value();
+    const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
+    myMainPM->Unhighlight (anObj, aHiMode);
+    anObj->ClearSelected();
+  }
+}
+
 //=======================================================================
 //function : MoveTo
 //purpose  :
@@ -245,16 +294,10 @@ AIS_StatusOfDetection AIS_InteractiveContext::MoveTo (const Standard_Integer  th
     myLastPicked.Nullify();
   }
 
-  if (toUpdateViewer)
+  if (toUpdateViewer
+   && theToRedrawOnUpdate)
   {
-    if (theToRedrawOnUpdate)
-    {
-      theView->Viewer()->Update();
-    }
-    else
-    {
-      theView->Viewer()->Invalidate();
-    }
+    theView->Viewer()->Update();
   }
 
   mylastmoveview = theView;
@@ -346,8 +389,7 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const TColgp_Array1OfPnt2d& the
 
   for (aSelector->Init(); aSelector->More(); aSelector->Next())
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked();
     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
@@ -398,47 +440,7 @@ AIS_StatusOfPick AIS_InteractiveContext::Select (const Standard_Boolean toUpdate
   }
   else
   {
-    AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-    Handle(AIS_Selection) aSelection = AIS_Selection::CurrentSelection();
-    NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
-    for (aSelection->Init(); aSelection->More(); aSelection->Next())
-    {
-      const Handle(SelectMgr_EntityOwner) anOwner
-        = Handle(SelectMgr_EntityOwner)::DownCast (aSelection->Value());
-
-      if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
-        continue;
-
-      const Handle(AIS_InteractiveObject) anObject =
-        Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-      anOwner->State (0);
-      if (anOwner == anObject->GlobalSelOwner())
-      {
-        Handle(AIS_GlobalStatus) aStatus = myObjects (anObject);
-        aStatus->SetHilightStatus (Standard_False);
-        aStatus->SetHilightColor (Quantity_NOC_WHITE);
-        anObject->State (0);
-      }
-      if (!anOwner->IsAutoHilight())
-      {
-        if (!anObjToClear.Contains (anObject))
-          anObjToClear.Add (anObject);
-      }
-      else
-      {
-        const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
-        anOwner->Unhilight (myMainPM, aHiMode);
-        if (myObjects.IsBound (anObject) && myObjects(anObject)->IsSubIntensityOn())
-        {
-          anOwner->HilightWithColor (myMainPM, mySubIntensity, aHiMode);
-        }
-      }
-    }
-    while (!anObjToClear.IsEmpty())
-    {
-      anObjToClear.FindKey (anObjToClear.Size())->ClearSelected();
-      anObjToClear.RemoveLast();
-    }
+    unhighlightSelected (Standard_True);
 
     AIS_Selection::Select();
     if (toUpdateViewer && myWasLastMain)
@@ -524,7 +526,7 @@ AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const Standard_Integer the
   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
   for (aSelector->Init(); aSelector->More(); aSelector->Next())
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =  Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked();
     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
@@ -575,7 +577,7 @@ AIS_StatusOfPick AIS_InteractiveContext::ShiftSelect (const TColgp_Array1OfPnt2d
   AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
   for (aSelector->Init(); aSelector->More(); aSelector->Next())
   {
-    const Handle(SelectMgr_EntityOwner) anOwner =  Handle(SelectMgr_EntityOwner)::DownCast (aSelector->Picked());
+    const Handle(SelectMgr_EntityOwner) anOwner = aSelector->Picked();
     if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
@@ -762,7 +764,9 @@ void AIS_InteractiveContext::HilightSelected (const Standard_Boolean theToUpdate
         Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
       if (anOwner == anObj->GlobalSelOwner())
       {
-        anObj->State (1);
+        Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObj);
+        aState->SetHilightStatus (Standard_True);
+        aState->SetHilightColor (mySelectionColor);
       }
       anOwner->State (1);
       if (!anOwner->IsAutoHilight())
@@ -823,7 +827,7 @@ void AIS_InteractiveContext::UnhilightSelected (const Standard_Boolean theToUpda
         Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
       if (anOwner == anObj->GlobalSelOwner())
       {
-        anObj->State (0);
+        myObjects.ChangeFind (anObj)->SetHilightStatus (Standard_False);
       }
       anOwner->State (0);
       const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HasHilightMode() : 0;
@@ -848,40 +852,7 @@ void AIS_InteractiveContext::ClearSelected (const Standard_Boolean theToUpdateVi
   if (NbSelected() == 0)
     return;
 
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aSel = AIS_Selection::CurrentSelection();
-  NCollection_IndexedMap<Handle(AIS_InteractiveObject)> anObjToClear;
-  for (aSel->Init(); aSel->More(); aSel->Next())
-  {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aSel->Value());
-    if (!anOwner.IsNull() && anOwner->HasSelectable())
-    {
-      const Handle(AIS_InteractiveObject) anObj =
-        Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
-      if (anOwner == anObj->GlobalSelOwner())
-      {
-        anObj->State (0);
-      }
-      anOwner->State (0);
-      if (!anOwner->IsAutoHilight())
-      {
-        if (!anObjToClear.Contains (anObj))
-          anObjToClear.Add (anObj);
-      }
-      else
-      {
-        const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
-        anOwner->Unhilight (myMainPM, aHiMode);
-      }
-    }
-  }
-
-  while (!anObjToClear.IsEmpty())
-  {
-    anObjToClear.FindKey (anObjToClear.Size())->ClearSelected();
-    anObjToClear.RemoveLast();
-  }
+  unhighlightSelected();
 
   AIS_Selection::Select();
   myMainPM->ClearImmediateDraw();
@@ -923,7 +894,7 @@ void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& t
   if (theObject->HasSelection (0))
     return;
 
-  if (NbSelected() == 1 && theObject->State() == 1)
+  if (NbSelected() == 1 && myObjects (theObject)->IsHilighted())
   {
     Quantity_NameOfColor aHiCol;
     Standard_Boolean hasHiCol = Standard_False;
@@ -943,14 +914,17 @@ void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& t
   {
     const Handle(SelectMgr_EntityOwner) anOwner =
       Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
-    if (anOwner.IsNull() || !anOwner->HasSelectable())
+    if (anOwner.IsNull() || !anOwner->HasSelectable() || !myFilters->IsOk (anOwner))
       continue;
 
     Handle(AIS_InteractiveObject) aSelectable =
       Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
     Unhilight (aSelectable, Standard_False);
     anOwner->State (0);
-    aSelectable->State (0);
+    if (anOwner == aSelectable->GlobalSelOwner())
+    {
+      myObjects.ChangeFind (aSelectable)->SetHilightStatus (Standard_False);
+    }
   }
 
   // added to avoid untimely viewer update...
@@ -961,8 +935,13 @@ void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& t
   Handle(SelectMgr_EntityOwner) anOwner =
     Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
   AIS_Selection::ClearAndSelect (anOwner);
-  theObject->State (1);
   anOwner->State (1);
+  if (anOwner == theObject->GlobalSelOwner())
+  {
+    Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (theObject);
+    aState->SetHilightStatus (Standard_True);
+    aState->SetHilightColor (mySelectionColor);
+  }
   Quantity_NameOfColor aHiCol;
   Standard_Boolean hasHiCol = Standard_False;
   if (IsHilighted (theObject, hasHiCol, aHiCol))
@@ -988,7 +967,7 @@ void AIS_InteractiveContext::SetSelected (const Handle(AIS_InteractiveObject)& t
 void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& theOwner,
                                           const Standard_Boolean theToUpdateViewer)
 {
-  if (theOwner.IsNull() || !theOwner->HasSelectable())
+  if (theOwner.IsNull() || !theOwner->HasSelectable() || !myFilters->IsOk (theOwner))
     return;
 
   const Handle(AIS_InteractiveObject) anObject =
@@ -1012,18 +991,7 @@ void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& t
   if (!myObjects.IsBound (anObject))
     Display (anObject, Standard_False);
 
-  AIS_Selection::SetCurrentSelection (myCurrentName.ToCString());
-  Handle(AIS_Selection) aCurSel = AIS_Selection::Selection (myCurrentName.ToCString());
-  for (aCurSel->Init(); aCurSel->More(); aCurSel->Next())
-  {
-    const Handle(SelectMgr_EntityOwner) anOwner =
-      Handle(SelectMgr_EntityOwner)::DownCast (aCurSel->Value());
-    if (!anOwner->HasSelectable())
-      continue;
-    const Standard_Integer aHiMode = anObject->HasHilightMode() ? anObject->HilightMode() : 0;
-    anOwner->Unhilight (myMainPM, aHiMode);
-    anOwner->State (0);
-  }
+  unhighlightSelected();
 
   AIS_Selection::ClearAndSelect (theOwner);
   theOwner->State (1);
@@ -1034,6 +1002,13 @@ void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& t
     highlightSelected (theOwner, mySelectionColor);
   }
 
+  if (theOwner == anObject->GlobalSelOwner())
+  {
+    Handle(AIS_GlobalStatus)& aState = myObjects.ChangeFind (anObject);
+    aState->SetHilightStatus (Standard_True);
+    aState->SetHilightColor (mySelectionColor);
+  }
+
   if (theToUpdateViewer)
     UpdateCurrentViewer();
 }
@@ -1047,23 +1022,20 @@ void AIS_InteractiveContext::SetSelected (const Handle(SelectMgr_EntityOwner)& t
 void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(AIS_InteractiveObject)& theObject,
                                                   const Standard_Boolean theToUpdateViewer)
 {
-  if (!theObject->HasInteractiveContext())
-    theObject->SetContext (this);
+  if (theObject.IsNull())
+    return;
 
   if (HasOpenedContext())
     return myLocalContexts (myCurLocalIndex)->AddOrRemoveSelected (theObject, theToUpdateViewer);
 
-  if (theObject.IsNull() || !myObjects.IsBound (theObject) || !theObject->HasSelection (0))
+  const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
+  if (!myObjects.IsBound (theObject) || !theObject->HasSelection (aGlobalSelMode))
     return;
 
-  const Handle(SelectMgr_Selection)& aSel = theObject->Selection (0);
-
-  if (aSel->IsEmpty())
-    return;
+  if (!theObject->HasInteractiveContext())
+    theObject->SetContext (this);
 
-  aSel->Init();
-  const Handle(SelectMgr_EntityOwner) anOwner =
-    Handle(SelectMgr_EntityOwner)::DownCast (aSel->Sensitive()->BaseSensitive()->OwnerId());
+  const Handle(SelectMgr_EntityOwner) anOwner = theObject->GlobalSelOwner();
 
   if (anOwner.IsNull() || !anOwner->HasSelectable())
     return;
@@ -1114,9 +1086,7 @@ void AIS_InteractiveContext::AddOrRemoveSelected (const Handle(SelectMgr_EntityO
   const Handle(AIS_InteractiveObject) anObj =
     Handle(AIS_InteractiveObject)::DownCast (theOwner->Selectable());
   const Standard_Boolean isGlobal = anObj->GlobalSelOwner() == theOwner;
-  if (isGlobal)
-    anObj->State (aState);
-  const Handle(AIS_GlobalStatus)& aStatus = myObjects (anObj);
+  Handle(AIS_GlobalStatus)& aStatus = myObjects.ChangeFind (anObj);
   const Standard_Integer aHiMode = anObj->HasHilightMode() ? anObj->HilightMode() : 0;
   if (aState == 1)
   {
@@ -1165,10 +1135,10 @@ Standard_Boolean AIS_InteractiveContext::IsSelected (const Handle(AIS_Interactiv
       break;
     }
   }
-  if (!theObj->HasSelection (aGlobalSelMode) || !isGlobalModeActivated)
+  if (!theObj->HasSelection (aGlobalSelMode) || !isGlobalModeActivated || theObj->GlobalSelOwner().IsNull())
     return Standard_False;
 
-  return theObj->State() == 1;
+  return theObj->GlobalSelOwner()->State() == 1;
 }
 
 //=======================================================================