0027523: Visualization - selection owner contains obsolete shape
[occt.git] / src / SelectMgr / SelectMgr_SelectionManager.cxx
1 // Created on: 1995-02-13
2 // Created by: Mister rmi
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <OSD_Environment.hxx>
19 #include <SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors.hxx>
20 #include <SelectMgr_SelectableObject.hxx>
21 #include <SelectMgr_Selection.hxx>
22 #include <SelectMgr_SelectionManager.hxx>
23 #include <SelectMgr_SequenceOfSelector.hxx>
24 #include <SelectMgr_ViewerSelector.hxx>
25 #include <Standard_Type.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28 #include <TColStd_ListOfInteger.hxx>
29 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_SelectionManager,MMgt_TShared)
32
33 static Standard_Integer FindIndex (const SelectMgr_SequenceOfSelector& theSelectorsSeq,
34                                    const Handle(SelectMgr_ViewerSelector)& theSelector)
35 {
36   Standard_Integer aFoundIdx = 0;
37
38   for (Standard_Integer anIdx = 1; anIdx <= theSelectorsSeq.Length() && aFoundIdx==0; anIdx++)
39   {
40     if (theSelector == theSelectorsSeq.Value (anIdx))
41       aFoundIdx = anIdx;
42   }
43
44   return aFoundIdx;
45 }
46
47 //==================================================
48 // Function: Create
49 // Purpose :
50 //==================================================
51 SelectMgr_SelectionManager::SelectMgr_SelectionManager() {}
52
53 //==================================================
54 // Function: Add
55 // Purpose :
56 //==================================================
57 void SelectMgr_SelectionManager::Add (const Handle(SelectMgr_ViewerSelector)& theSelector)
58 {
59   mySelectors.Add (theSelector);
60 }
61
62 //==================================================
63 // Function: Remove
64 // Purpose :
65 //==================================================
66 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_ViewerSelector)& theSelector)
67 {
68   for (SelectMgr_DataMapIteratorOfDataMapOfObjectSelectors aSelIter (myLocal); aSelIter.More(); aSelIter.Next())
69   {
70     SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (aSelIter.Key());
71     Standard_Integer aRank = FindIndex (theSelectors, theSelector);
72     if (aRank != 0 && aRank <= theSelectors.Length())
73       theSelectors.Remove (aRank);
74   }
75
76   if (mySelectors.Contains (theSelector))
77     mySelectors.Remove (theSelector);
78 }
79
80 //==================================================
81 // Function: Contains
82 // Purpose :
83 //==================================================
84 Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_ViewerSelector)& theSelector) const
85 {
86   return mySelectors.Contains (theSelector);
87 }
88
89 //==================================================
90 // Function: Contains
91 // Purpose :
92 //==================================================
93 Standard_Boolean SelectMgr_SelectionManager::Contains (const Handle(SelectMgr_SelectableObject)& theObject) const
94 {
95   if (myGlobal.Contains (theObject))
96     return Standard_True;
97
98   if (myLocal.IsBound (theObject))
99     return Standard_True;
100
101   return Standard_False;
102 }
103
104 //==================================================
105 // Function: Load
106 // Purpose :
107 //==================================================
108 void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
109                                        const Standard_Integer theMode)
110 {
111   if (myGlobal.Contains(theObject))
112     return;
113
114   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
115   {
116     Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode);
117   }
118
119   if (!theObject->HasOwnPresentations())
120     return;
121
122   myGlobal.Add(theObject);
123   for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
124   {
125     Handle(SelectMgr_ViewerSelector) aSelector =
126       Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
127     if (!aSelector->Contains (theObject) && theObject->HasOwnPresentations())
128     {
129       aSelector->AddSelectableObject (theObject);
130     }
131   }
132   if (theMode != -1)
133     loadMode (theObject, theMode);
134 }
135
136
137 //==================================================
138 // Function: Load
139 // Purpose :
140 //==================================================
141 void SelectMgr_SelectionManager::Load (const Handle(SelectMgr_SelectableObject)& theObject,
142                                        const Handle(SelectMgr_ViewerSelector)& theSelector,
143                                        const Standard_Integer theMode)
144 {
145   if (!mySelectors.Contains (theSelector))
146   {
147     mySelectors.Add (theSelector);
148   }
149
150   if (theMode != -1)
151     loadMode (theObject, theMode, theSelector);
152
153   if (theObject->HasOwnPresentations())
154     theSelector->AddSelectableObject (theObject);
155
156   if (myLocal.IsBound (theObject))
157   {
158     SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
159     if (FindIndex (aSelectors, theSelector) == 0)
160     {
161         aSelectors.Append (theSelector);
162     }
163   }
164   else
165   {
166     if (!myGlobal.Contains (theObject))
167     {
168       if (theObject->HasOwnPresentations())
169       {
170         SelectMgr_SequenceOfSelector aSelectors;
171         aSelectors.Append (theSelector);
172         myLocal.Bind (theObject, aSelectors);
173       }
174       else
175       {
176         for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
177         {
178           Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
179         }
180       }
181     }
182   }
183 }
184
185
186 //==================================================
187 // Function: Remove
188 // Purpose :
189 //==================================================
190 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
191 {
192   if (myGlobal.Contains (theObject))
193   {
194     for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
195     {
196       Handle(SelectMgr_ViewerSelector) aCurSelector =
197         Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
198
199       if (!aCurSelector->Contains (theObject))
200         continue;
201
202       for (theObject->Init(); theObject->More(); theObject->Next())
203       {
204         aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
205         theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
206       }
207       aCurSelector->RemoveSelectableObject (theObject);
208     }
209
210     myGlobal.Remove (theObject);
211   }
212   else if (myLocal.IsBound (theObject))
213   {
214     SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
215     for (Standard_Integer aSelectorsIdx = 1; aSelectorsIdx <= aSelectors.Length(); aSelectorsIdx++)
216     {
217       Handle(SelectMgr_ViewerSelector) aCurSelector = aSelectors (aSelectorsIdx);
218       if (!aCurSelector->Contains (theObject))
219         continue;
220
221       for (theObject->Init(); theObject->More(); theObject->Next())
222       {
223         aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
224         theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
225       }
226       aCurSelector->RemoveSelectableObject (theObject);
227     }
228
229     myLocal.UnBind (theObject);
230   }
231   else if (!theObject->HasOwnPresentations())
232   {
233     for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
234     {
235       Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
236     }
237   }
238
239   theObject->ClearSelections();
240 }
241
242 //==================================================
243 // Function: Remove
244 // Purpose :
245 //==================================================
246 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject,
247                                          const Handle(SelectMgr_ViewerSelector)& theSelector)
248 {
249   if (!theSelector->Contains (theObject))
250     return;
251
252   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
253   {
254     Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector);
255   }
256
257   if (!theObject->HasOwnPresentations())
258     return;
259
260   for (theObject->Init(); theObject->More(); theObject->Next())
261   {
262     theSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
263     theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
264   }
265
266   theSelector->RemoveSelectableObject (theObject);
267
268   if (myLocal.IsBound (theObject))
269   {
270     SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
271     for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); aSelectorIdx++)
272     {
273       if (aSelectors (aSelectorIdx) == theSelector)
274       {
275         aSelectors.Remove (aSelectorIdx);
276         break;
277       }
278     }
279
280     if (aSelectors.IsEmpty())
281     {
282       myLocal.UnBind (theObject);
283     }
284   }
285 }
286
287 //==================================================
288 // Function: Activate
289 // Purpose :
290 //==================================================
291 void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
292                                            const Standard_Integer theMode,
293                                            const Handle(SelectMgr_ViewerSelector)& theSelector)
294 {
295   if (theMode == -1)
296     return;
297
298   if (!theSelector.IsNull() && !mySelectors.Contains (theSelector))
299     return;
300
301   if (!theObject->HasOwnPresentations())
302   {
303     for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
304     {
305       Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
306     }
307
308     return;
309   }
310
311   Standard_Boolean isComputed = Standard_False;
312   if (theObject->HasSelection (theMode))
313   {
314     isComputed = theObject->Selection (theMode)->IsEmpty() ? 0 : 1;
315   }
316
317   if (!isComputed)
318     loadMode (theObject, theMode);
319
320   if (theSelector.IsNull())
321   {
322     if (myGlobal.Contains (theObject))
323     {
324       for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
325       {
326         Handle(SelectMgr_ViewerSelector) aCurSelector =
327           Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
328         Activate (theObject, theMode, aCurSelector);
329       }
330     }
331     else if (myLocal.IsBound (theObject))
332     {
333       SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (theObject);
334       for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= theSelectors.Length(); aSelectorIdx++)
335       {
336         Handle(SelectMgr_ViewerSelector) aCurSelector = theSelectors (aSelectorIdx);
337         Activate (theObject, theMode, aCurSelector);
338       }
339     }
340   }
341
342   const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
343
344   switch (aSelection->UpdateStatus())
345   {
346   case SelectMgr_TOU_Full:
347     if (theObject->HasSelection (theMode))
348       theSelector->RemoveSelectionOfObject (theObject, aSelection);
349     theObject->RecomputePrimitives (theMode);
350   case SelectMgr_TOU_Partial:
351     if(theObject->HasTransformation())
352       theObject->UpdateTransformations (aSelection);
353     theSelector->RebuildObjectsTree();
354     break;
355   default:
356     break;
357   }
358   aSelection->UpdateStatus(SelectMgr_TOU_None);
359
360   switch (aSelection->BVHUpdateStatus())
361   {
362   case SelectMgr_TBU_Add:
363   case SelectMgr_TBU_Renew:
364     theSelector->AddSelectionToObject (theObject, aSelection);
365     break;
366   case SelectMgr_TBU_Remove:
367     if (aSelection->GetSelectionState() == SelectMgr_SOS_Deactivated)
368       theSelector->AddSelectionToObject (theObject, aSelection);
369     break;
370   default:
371     break;
372   }
373   aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
374
375   if (myGlobal.Contains (theObject))
376   {
377     if (theMode != 0 && theSelector->IsActive (theObject, 0))
378     {
379       theSelector->Deactivate (theObject->Selection (0));
380     }
381     theSelector->Activate (theObject->Selection (theMode));
382   }
383   else
384   {
385     if (myLocal.IsBound (theObject))
386     {
387       if (FindIndex (myLocal.Find (theObject), theSelector) == 0)
388         (myLocal.ChangeFind (theObject)).Append (theSelector);
389       theSelector->Activate (theObject->Selection (theMode));
390     }
391   }
392 }
393
394 //==================================================
395 // Function: Deactivate
396 // Purpose :
397 //==================================================
398 void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableObject)& theObject,
399                                              const Standard_Integer theMode,
400                                              const Handle(SelectMgr_ViewerSelector)& theSelector)
401 {
402
403   if (!theObject->HasOwnPresentations())
404   {
405     for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
406     {
407       Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
408     }
409
410     return;
411   }
412
413   Standard_Boolean isInGlobal = myGlobal.Contains (theObject);
414   Standard_Boolean hasSelection = theMode == -1 ? Standard_True : theObject->HasSelection (theMode);
415
416   if (theSelector.IsNull())
417   {
418     Handle(SelectMgr_ViewerSelector) aSelector;
419     for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
420     {
421       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
422       if (isInGlobal || myLocal.IsBound (theObject))
423       {
424         if (theMode == -1)
425         {
426           for (theObject->Init(); theObject->More(); theObject->Next())
427           {
428             aSelector->Deactivate (theObject->CurrentSelection());
429           }
430         }
431         else
432         {
433           if (hasSelection)
434             aSelector->Deactivate (theObject->Selection (theMode));
435         }
436       }
437     }
438   }
439   else
440   {
441     if (theMode == -1)
442     {
443       for (theObject->Init(); theObject->More(); theObject->Next())
444       {
445         theSelector->Deactivate (theObject->CurrentSelection());
446       }
447     }
448     else
449       if (hasSelection)
450         theSelector->Deactivate (theObject->Selection (theMode));
451   }
452 }
453
454 //=======================================================================
455 //function : IsActivated
456 //purpose  :
457 //=======================================================================
458 Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr_SelectableObject)& theObject,
459                                                           const Standard_Integer theMode,
460                                                           const Handle(SelectMgr_ViewerSelector)& theSelector) const
461 {
462   if (!theObject->HasOwnPresentations())
463   {
464     for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
465     {
466       if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector))
467         return Standard_True;
468     }
469
470     return Standard_False;
471   }
472
473   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
474     return Standard_False;
475
476   if (theMode == -1 && theSelector.IsNull())
477   {
478     for (theObject->Init(); theObject->More(); theObject->Next())
479     {
480       if (IsActivated (theObject, theObject->CurrentSelection()->Mode()))
481         return Standard_True;
482     }
483
484     return Standard_False;
485   }
486
487   if (!theObject->HasSelection (theMode))
488     return Standard_False;
489
490   const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
491   if (theSelector.IsNull())
492   {
493     for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
494     {
495       Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
496       if (aSelector->Status (aSelection) == SelectMgr_SOS_Activated)
497         return Standard_True;
498     }
499   }
500   else
501   {
502     return theSelector->Status (aSelection) == SelectMgr_SOS_Activated;
503   }
504
505   return Standard_False;
506 }
507
508 //=======================================================================
509 //function : ClearSelectionStructures
510 //purpose  : Removes sensitive entities from all viewer selectors
511 //           after method Clear() was called to the selection they belonged to
512 //           or it was recomputed somehow
513 //=======================================================================
514 void SelectMgr_SelectionManager::ClearSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
515                                                            const Standard_Integer theMode,
516                                                            const Handle(SelectMgr_ViewerSelector)& theSelector)
517 {
518   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
519   {
520     ClearSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
521   }
522
523   if (!theObj->HasOwnPresentations())
524     return;
525
526   if (theSelector.IsNull())
527   {
528     if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
529       return;
530
531     TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
532     Handle(SelectMgr_ViewerSelector) aSelector;
533     for( ; aSelectorsIter.More(); aSelectorsIter.Next())
534     {
535       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
536       ClearSelectionStructures (theObj, theMode, aSelector);
537     }
538   }
539   else
540   {
541     if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
542       return;
543
544     if (theMode != -1)
545     {
546       if (theObj->HasSelection (theMode))
547       {
548         const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
549         if (theObj->HasSelection (theMode))
550         {
551           theSelector->RemoveSelectionOfObject (theObj, aSelection);
552           aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
553         }
554       }
555     }
556     else
557     {
558       for (theObj->Init(); theObj->More(); theObj->Next())
559       {
560         const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
561         theSelector->RemoveSelectionOfObject (theObj, aSelection);
562         aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
563       }
564     }
565     theSelector->RebuildObjectsTree();
566   }
567 }
568
569 //=======================================================================
570 //function : RestoreSelectionStructuress
571 //purpose  : Re-adds newely calculated sensitive  entities of recomputed selection
572 //           defined by mode theMode to all viewer selectors contained that selection.
573 //=======================================================================
574 void SelectMgr_SelectionManager::RestoreSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
575                                                              const Standard_Integer theMode,
576                                                              const Handle(SelectMgr_ViewerSelector)& theSelector)
577 {
578   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
579   {
580     RestoreSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
581   }
582
583   if (!theObj->HasOwnPresentations())
584     return;
585
586   if (theSelector.IsNull())
587   {
588     if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
589       return;
590
591     TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
592     Handle(SelectMgr_ViewerSelector) aSelector;
593     for( ; aSelectorsIter.More(); aSelectorsIter.Next())
594     {
595       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
596       RestoreSelectionStructures (theObj, theMode, aSelector);
597     }
598   }
599   else
600   {
601     if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
602       return;
603
604     if (theMode != -1)
605     {
606       if (theObj->HasSelection (theMode))
607       {
608         const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
609         if (theObj->HasSelection (theMode))
610         {
611           theSelector->AddSelectionToObject (theObj, aSelection);
612           aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
613         }
614       }
615     }
616     else
617     {
618       for (theObj->Init(); theObj->More(); theObj->Next())
619       {
620         const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
621         theSelector->AddSelectionToObject (theObj, aSelection);
622         aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
623       }
624     }
625     theSelector->RebuildObjectsTree();
626   }
627 }
628
629 //=======================================================================
630 //function : rebuildSelectionStructures
631 //purpose  : Internal function that marks 1st level BVH of object theObj
632 //           as outdated
633 //=======================================================================
634 void SelectMgr_SelectionManager::rebuildSelectionStructures (const Handle(SelectMgr_ViewerSelector)& theSelector)
635 {
636   if (theSelector.IsNull())
637   {
638     Handle(SelectMgr_ViewerSelector) aSelector;
639     for(TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
640     {
641       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
642       rebuildSelectionStructures (aSelector);
643     }
644   }
645   else
646   {
647     theSelector->RebuildObjectsTree();
648   }
649 }
650
651 //==================================================
652 // Function: recomputeSelectionMode
653 // Purpose :
654 //==================================================
655 void SelectMgr_SelectionManager::recomputeSelectionMode (const Handle(SelectMgr_SelectableObject)& theObject,
656                                                          const Handle(SelectMgr_Selection)& theSelection,
657                                                          const Standard_Integer theMode)
658 {
659   theSelection->UpdateStatus (SelectMgr_TOU_Full);
660
661   for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
662   {
663     Handle(SelectMgr_ViewerSelector) aCurSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
664
665     ClearSelectionStructures (theObject, theMode, aCurSelector);
666     theObject->RecomputePrimitives (theMode);
667     RestoreSelectionStructures (theObject, theMode, aCurSelector);
668     theSelection->UpdateStatus (SelectMgr_TOU_None);
669     theSelection->UpdateBVHStatus (SelectMgr_TBU_None);
670   }
671 }
672
673 //==================================================
674 // Function: Update
675 // Purpose :
676 //==================================================
677 void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
678                                                      const Standard_Boolean theIsForce,
679                                                      const Standard_Integer theMode)
680 {
681   if (theIsForce)
682   {
683     if (theMode == -1)
684     {
685       ClearSelectionStructures (theObject);
686       theObject->RecomputePrimitives();
687       theObject->UpdateTransformation();
688       RestoreSelectionStructures (theObject);
689     }
690     else if (theObject->HasSelection (theMode))
691     {
692       ClearSelectionStructures (theObject, theMode);
693       theObject->RecomputePrimitives (theMode);
694       theObject->UpdateTransformation();
695       RestoreSelectionStructures (theObject, theMode);
696     }
697     return;
698   }
699
700   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
701   {
702     RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
703   }
704
705   if (!theObject->HasOwnPresentations())
706     return;
707
708   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
709     return;
710
711   if (theMode == -1)
712   {
713     for (theObject->Init(); theObject->More(); theObject->Next())
714     {
715       const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
716       Standard_Integer aSelMode = aSelection->Mode();
717       recomputeSelectionMode (theObject, aSelection, aSelMode);
718     }
719   }
720   else
721   {
722     if (!theObject->HasSelection (theMode))
723       return;
724
725     const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
726     recomputeSelectionMode (theObject, aSelection, theMode);
727   }
728 }
729
730 //=======================================================================
731 //function : Update
732 //purpose  : Selections are recalculated if they are flagged
733 //           "TO RECALCULATE" and activated in one of selectors.
734 //           If ForceUpdate = True, and they are "TO RECALCULATE"
735 //           This is done without caring for the state of activation.
736 //=======================================================================
737 void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
738                                          const Standard_Boolean theIsForce)
739 {
740   for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
741   {
742     Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
743   }
744
745   if (!theObject->HasOwnPresentations())
746     return;
747
748   for (theObject->Init(); theObject->More(); theObject->Next())
749   {
750     const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
751     if (theIsForce)
752     {
753       switch (aSelection->UpdateStatus())
754       {
755       case SelectMgr_TOU_Full:
756         ClearSelectionStructures (theObject, aSelection->Mode());
757         theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
758         RestoreSelectionStructures (theObject, aSelection->Mode());
759       case SelectMgr_TOU_Partial:
760         theObject->UpdateTransformations (aSelection);
761         rebuildSelectionStructures();
762         break;
763       default:
764         break;
765       }
766       aSelection->UpdateStatus (SelectMgr_TOU_None);
767       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
768     }
769
770     for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
771     {
772       Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
773       Update (theObject, aSelector, Standard_False);
774     }
775   }
776 }
777
778 //==================================================
779 // Function: Update
780 // Purpose : Attention, it is required to know what is done...
781 //==================================================
782 void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
783                                          const Handle(SelectMgr_ViewerSelector)& theSelector,
784                                          const Standard_Boolean theIsForce)
785 {
786   if (!mySelectors.Contains (theSelector))
787     return;
788
789   Standard_Boolean isKnown = myGlobal.Contains (theObject);
790   if (!isKnown)
791     isKnown = (myLocal.IsBound (theObject) && (FindIndex (myLocal.Find (theObject), theSelector) != 0));
792   if (!isKnown)
793     return;
794
795   for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
796   {
797     Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theSelector, theIsForce);
798   }
799
800   if (!theObject->HasOwnPresentations())
801     return;
802
803   for (theObject->Init(); theObject->More(); theObject->Next())
804   {
805     const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
806     if (theIsForce)
807     {
808       switch (aSelection->UpdateStatus())
809       {
810       case SelectMgr_TOU_Full:
811         ClearSelectionStructures (theObject, aSelection->Mode());
812         theObject->RecomputePrimitives (aSelection->Mode());
813         RestoreSelectionStructures (theObject, aSelection->Mode());
814       case SelectMgr_TOU_Partial:
815         theObject->UpdateTransformations (aSelection);
816         rebuildSelectionStructures();
817         break;
818       default:
819         break;
820       }
821       aSelection->UpdateStatus (SelectMgr_TOU_None);
822       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
823     }
824
825     if (theSelector->Status (aSelection) == SelectMgr_SOS_Activated)
826     {
827       switch (aSelection->UpdateStatus())
828       {
829       case SelectMgr_TOU_Full:
830         ClearSelectionStructures (theObject, aSelection->Mode(), theSelector);
831         theObject->RecomputePrimitives (aSelection->Mode());
832         RestoreSelectionStructures (theObject, aSelection->Mode(), theSelector);
833       case SelectMgr_TOU_Partial:
834         if (theObject->HasTransformation())
835         {
836           theObject->UpdateTransformations (aSelection);
837           theSelector->RebuildObjectsTree();
838         }
839         break;
840       default:
841         break;
842       }
843
844       aSelection->UpdateStatus(SelectMgr_TOU_None);
845       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
846     }
847   }
848 }
849
850 //==================================================
851 // Function: loadMode
852 // Purpose : Private Method
853 //==================================================
854 void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
855                                            const Standard_Integer theMode,
856                                            const Handle(SelectMgr_ViewerSelector)& theSelector)
857 {
858   if (theMode == -1)
859     return;
860
861   if (!theObject->HasSelection (theMode))
862   {
863     Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
864     theObject->AddSelection (aNewSel, theMode);
865     if (theSelector.IsNull())
866     {
867       if (myGlobal.Contains (theObject))
868       {
869         TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors);
870         for ( ; aSelectorIter.More(); aSelectorIter.Next())
871         {
872           Handle(SelectMgr_ViewerSelector) aSelector =
873             Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
874           aSelector->AddSelectionToObject (theObject, aNewSel);
875           aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
876         }
877       }
878       else if (myLocal.IsBound (theObject))
879       {
880         const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
881         for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); ++aSelectorIdx)
882         {
883           aSelectors (aSelectorIdx)->AddSelectionToObject (theObject, aNewSel);
884           aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
885         }
886       }
887     }
888     else
889     {
890       theSelector->AddSelectionToObject (theObject, aNewSel);
891       aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
892     }
893   }
894   else if (theObject->Selection (theMode)->IsEmpty())
895   {
896     if (theObject->Selection (theMode)->BVHUpdateStatus() == SelectMgr_TBU_Remove)
897     {
898       Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
899       theObject->AddSelection (aNewSel, theMode);
900       theObject->Selection (theMode)->UpdateBVHStatus (SelectMgr_TBU_Remove);
901       theObject->Selection (theMode)->SetSelectionState (SelectMgr_SOS_Deactivated);
902     }
903   }
904 }
905
906 //=======================================================================
907 //function : SetUpdateMode
908 //purpose  :
909 //=======================================================================
910 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
911                                                 const SelectMgr_TypeOfUpdate theType)
912 {
913   for (theObject->Init(); theObject->More(); theObject->Next())
914     theObject->CurrentSelection()->UpdateStatus (theType);
915 }
916
917 //=======================================================================
918 //function : SetUpdateMode
919 //purpose  :
920 //=======================================================================
921 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
922                                                 const Standard_Integer theMode,
923                                                 const SelectMgr_TypeOfUpdate theType)
924 {
925   if (theObject->HasSelection (theMode))
926     theObject->Selection (theMode)->UpdateStatus (theType);
927 }
928
929 //=======================================================================
930 //function : SetSelectionSensitivity
931 //purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject and
932 //           changes previous sensitivity value of all sensitive entities in selection with theMode
933 //           to the given theNewSensitivity.
934 //=======================================================================
935 void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
936                                                           const Standard_Integer theMode,
937                                                           const Standard_Integer theNewSens)
938 {
939   Standard_ASSERT_RAISE (theNewSens > 0,
940     "Error! Selection sensitivity have positive value.");
941
942   if (theObject.IsNull() || !theObject->HasSelection (theMode))
943     return;
944
945   Handle(SelectMgr_Selection) aSel = theObject->Selection (theMode);
946   const Standard_Integer aPrevSens = aSel->Sensitivity();
947   aSel->SetSensitivity (theNewSens);
948
949   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
950     return;
951
952   if (myGlobal.Contains (theObject))
953   {
954     for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
955     {
956       Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
957       if (aSelector->Contains (theObject))
958       {
959         aSelector->myTolerances.Decrement (aPrevSens);
960         aSelector->myTolerances.Add (theNewSens);
961         aSelector->myToUpdateTolerance = Standard_True;
962       }
963     }
964   }
965   if (myLocal.IsBound (theObject))
966   {
967     const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
968     for (SelectMgr_SequenceOfSelector::Iterator aLocalIter (aSelectors); aLocalIter.More(); aLocalIter.Next())
969     {
970       Handle(SelectMgr_ViewerSelector)& aCurSel = aLocalIter.ChangeValue();
971       aCurSel->myTolerances.Decrement (aPrevSens);
972       aCurSel->myTolerances.Add (theNewSens);
973       aCurSel->myToUpdateTolerance = Standard_True;
974     }
975   }
976 }