0027893: Visualization - AIS_InteractiveContext::SetSelected does not work
[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       for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
169       {
170         Load (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector, theMode);
171       }
172       if (!theObject->HasOwnPresentations())
173         return;
174
175       SelectMgr_SequenceOfSelector aSelectors;
176       aSelectors.Append (theSelector);
177       myLocal.Bind (theObject, aSelectors);
178     }
179   }
180 }
181
182
183 //==================================================
184 // Function: Remove
185 // Purpose :
186 //==================================================
187 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject)
188 {
189   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
190   {
191     Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()));
192   }
193
194   if (!theObject->HasOwnPresentations())
195     return;
196
197   if (myGlobal.Contains (theObject))
198   {
199     for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
200     {
201       Handle(SelectMgr_ViewerSelector) aCurSelector =
202         Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
203
204       if (!aCurSelector->Contains (theObject))
205         continue;
206
207       for (theObject->Init(); theObject->More(); theObject->Next())
208       {
209         aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
210         theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
211       }
212       aCurSelector->RemoveSelectableObject (theObject);
213     }
214
215     myGlobal.Remove (theObject);
216   }
217   else if (myLocal.IsBound (theObject))
218   {
219     SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
220     for (Standard_Integer aSelectorsIdx = 1; aSelectorsIdx <= aSelectors.Length(); aSelectorsIdx++)
221     {
222       Handle(SelectMgr_ViewerSelector) aCurSelector = aSelectors (aSelectorsIdx);
223       if (!aCurSelector->Contains (theObject))
224         continue;
225
226       for (theObject->Init(); theObject->More(); theObject->Next())
227       {
228         aCurSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
229         theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
230       }
231       aCurSelector->RemoveSelectableObject (theObject);
232     }
233
234     myLocal.UnBind (theObject);
235   }
236
237   theObject->ClearSelections();
238 }
239
240 //==================================================
241 // Function: Remove
242 // Purpose :
243 //==================================================
244 void SelectMgr_SelectionManager::Remove (const Handle(SelectMgr_SelectableObject)& theObject,
245                                          const Handle(SelectMgr_ViewerSelector)& theSelector)
246 {
247   if (!theSelector->Contains (theObject))
248     return;
249
250   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
251   {
252     Remove (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theSelector);
253   }
254
255   if (!theObject->HasOwnPresentations())
256     return;
257
258   for (theObject->Init(); theObject->More(); theObject->Next())
259   {
260     theSelector->RemoveSelectionOfObject (theObject, theObject->CurrentSelection());
261     theObject->CurrentSelection()->UpdateBVHStatus (SelectMgr_TBU_Remove);
262   }
263
264   theSelector->RemoveSelectableObject (theObject);
265
266   if (myLocal.IsBound (theObject))
267   {
268     SelectMgr_SequenceOfSelector& aSelectors = myLocal.ChangeFind (theObject);
269     for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); aSelectorIdx++)
270     {
271       if (aSelectors (aSelectorIdx) == theSelector)
272       {
273         aSelectors.Remove (aSelectorIdx);
274         break;
275       }
276     }
277
278     if (aSelectors.IsEmpty())
279     {
280       myLocal.UnBind (theObject);
281     }
282   }
283 }
284
285 //==================================================
286 // Function: Activate
287 // Purpose :
288 //==================================================
289 void SelectMgr_SelectionManager::Activate (const Handle(SelectMgr_SelectableObject)& theObject,
290                                            const Standard_Integer theMode,
291                                            const Handle(SelectMgr_ViewerSelector)& theSelector)
292 {
293   if (theMode == -1)
294     return;
295
296   if (!theSelector.IsNull() && !mySelectors.Contains (theSelector))
297     return;
298
299   for (PrsMgr_ListOfPresentableObjectsIter anChildIter (theObject->Children()); anChildIter.More(); anChildIter.Next())
300   {
301     Activate (Handle(SelectMgr_SelectableObject)::DownCast (anChildIter.Value()), theMode, theSelector);
302   }
303
304   if (!theObject->HasOwnPresentations())
305     return;
306
307   Standard_Boolean isComputed = Standard_False;
308   if (theObject->HasSelection (theMode))
309   {
310     isComputed = theObject->Selection (theMode)->IsEmpty() ? 0 : 1;
311   }
312
313   if (!isComputed)
314     loadMode (theObject, theMode);
315
316   if (theSelector.IsNull())
317   {
318     if (myGlobal.Contains (theObject))
319     {
320       for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
321       {
322         Handle(SelectMgr_ViewerSelector) aCurSelector =
323           Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
324         Activate (theObject, theMode, aCurSelector);
325       }
326     }
327     else if (myLocal.IsBound (theObject))
328     {
329       SelectMgr_SequenceOfSelector& theSelectors = myLocal.ChangeFind (theObject);
330       for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= theSelectors.Length(); aSelectorIdx++)
331       {
332         Handle(SelectMgr_ViewerSelector) aCurSelector = theSelectors (aSelectorIdx);
333         Activate (theObject, theMode, aCurSelector);
334       }
335     }
336   }
337
338   const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
339
340   switch (aSelection->UpdateStatus())
341   {
342   case SelectMgr_TOU_Full:
343     if (theObject->HasSelection (theMode))
344       theSelector->RemoveSelectionOfObject (theObject, aSelection);
345     theObject->RecomputePrimitives (theMode);
346   case SelectMgr_TOU_Partial:
347     if(theObject->HasTransformation())
348       theObject->UpdateTransformations (aSelection);
349     theSelector->RebuildObjectsTree();
350     break;
351   default:
352     break;
353   }
354   aSelection->UpdateStatus(SelectMgr_TOU_None);
355
356   switch (aSelection->BVHUpdateStatus())
357   {
358   case SelectMgr_TBU_Add:
359   case SelectMgr_TBU_Renew:
360     theSelector->AddSelectionToObject (theObject, aSelection);
361     break;
362   case SelectMgr_TBU_Remove:
363     if (aSelection->GetSelectionState() == SelectMgr_SOS_Deactivated)
364       theSelector->AddSelectionToObject (theObject, aSelection);
365     break;
366   default:
367     break;
368   }
369   aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
370
371   if (myGlobal.Contains (theObject))
372   {
373     const Standard_Integer aGlobalSelMode = theObject->GlobalSelectionMode();
374     if (theMode != aGlobalSelMode && theSelector->IsActive (theObject, aGlobalSelMode))
375     {
376       theSelector->Deactivate (theObject->Selection (aGlobalSelMode));
377     }
378     theSelector->Activate (theObject->Selection (theMode));
379   }
380   else
381   {
382     if (myLocal.IsBound (theObject))
383     {
384       if (FindIndex (myLocal.Find (theObject), theSelector) == 0)
385         (myLocal.ChangeFind (theObject)).Append (theSelector);
386       theSelector->Activate (theObject->Selection (theMode));
387     }
388   }
389 }
390
391 //==================================================
392 // Function: Deactivate
393 // Purpose :
394 //==================================================
395 void SelectMgr_SelectionManager::Deactivate (const Handle(SelectMgr_SelectableObject)& theObject,
396                                              const Standard_Integer theMode,
397                                              const Handle(SelectMgr_ViewerSelector)& theSelector)
398 {
399   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
400   {
401     Deactivate (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
402   }
403
404   if (!theObject->HasOwnPresentations())
405     return;
406
407   Standard_Boolean isInGlobal = myGlobal.Contains (theObject);
408   Standard_Boolean hasSelection = theMode == -1 ? Standard_True : theObject->HasSelection (theMode);
409
410   if (theSelector.IsNull())
411   {
412     Handle(SelectMgr_ViewerSelector) aSelector;
413     for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
414     {
415       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
416       if (isInGlobal || myLocal.IsBound (theObject))
417       {
418         if (theMode == -1)
419         {
420           for (theObject->Init(); theObject->More(); theObject->Next())
421           {
422             aSelector->Deactivate (theObject->CurrentSelection());
423           }
424         }
425         else
426         {
427           if (hasSelection)
428             aSelector->Deactivate (theObject->Selection (theMode));
429         }
430       }
431     }
432   }
433   else
434   {
435     if (theMode == -1)
436     {
437       for (theObject->Init(); theObject->More(); theObject->Next())
438       {
439         theSelector->Deactivate (theObject->CurrentSelection());
440       }
441     }
442     else
443       if (hasSelection)
444         theSelector->Deactivate (theObject->Selection (theMode));
445   }
446 }
447
448 //=======================================================================
449 //function : IsActivated
450 //purpose  :
451 //=======================================================================
452 Standard_Boolean SelectMgr_SelectionManager::IsActivated (const Handle(SelectMgr_SelectableObject)& theObject,
453                                                           const Standard_Integer theMode,
454                                                           const Handle(SelectMgr_ViewerSelector)& theSelector) const
455 {
456   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
457   {
458     if (IsActivated (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector))
459       return Standard_True;
460   }
461
462   if (!theObject->HasOwnPresentations())
463     return Standard_False;
464
465   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
466     return Standard_False;
467
468   if (theMode == -1 && theSelector.IsNull())
469   {
470     for (theObject->Init(); theObject->More(); theObject->Next())
471     {
472       if (IsActivated (theObject, theObject->CurrentSelection()->Mode()))
473         return Standard_True;
474     }
475
476     return Standard_False;
477   }
478
479   if (!theObject->HasSelection (theMode))
480     return Standard_False;
481
482   const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
483   if (theSelector.IsNull())
484   {
485     for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
486     {
487       Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
488       if (aSelector->Status (aSelection) == SelectMgr_SOS_Activated)
489         return Standard_True;
490     }
491   }
492   else
493   {
494     return theSelector->Status (aSelection) == SelectMgr_SOS_Activated;
495   }
496
497   return Standard_False;
498 }
499
500 //=======================================================================
501 //function : ClearSelectionStructures
502 //purpose  : Removes sensitive entities from all viewer selectors
503 //           after method Clear() was called to the selection they belonged to
504 //           or it was recomputed somehow
505 //=======================================================================
506 void SelectMgr_SelectionManager::ClearSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
507                                                            const Standard_Integer theMode,
508                                                            const Handle(SelectMgr_ViewerSelector)& theSelector)
509 {
510   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
511   {
512     ClearSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
513   }
514
515   if (!theObj->HasOwnPresentations())
516     return;
517
518   if (theSelector.IsNull())
519   {
520     if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
521       return;
522
523     TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
524     Handle(SelectMgr_ViewerSelector) aSelector;
525     for( ; aSelectorsIter.More(); aSelectorsIter.Next())
526     {
527       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
528       ClearSelectionStructures (theObj, theMode, aSelector);
529     }
530   }
531   else
532   {
533     if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
534       return;
535
536     if (theMode != -1)
537     {
538       if (theObj->HasSelection (theMode))
539       {
540         const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
541         if (theObj->HasSelection (theMode))
542         {
543           theSelector->RemoveSelectionOfObject (theObj, aSelection);
544           aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
545         }
546       }
547     }
548     else
549     {
550       for (theObj->Init(); theObj->More(); theObj->Next())
551       {
552         const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
553         theSelector->RemoveSelectionOfObject (theObj, aSelection);
554         aSelection->UpdateBVHStatus (SelectMgr_TBU_Add);
555       }
556     }
557     theSelector->RebuildObjectsTree();
558   }
559 }
560
561 //=======================================================================
562 //function : RestoreSelectionStructuress
563 //purpose  : Re-adds newely calculated sensitive  entities of recomputed selection
564 //           defined by mode theMode to all viewer selectors contained that selection.
565 //=======================================================================
566 void SelectMgr_SelectionManager::RestoreSelectionStructures (const Handle(SelectMgr_SelectableObject)& theObj,
567                                                              const Standard_Integer theMode,
568                                                              const Handle(SelectMgr_ViewerSelector)& theSelector)
569 {
570   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObj->Children()); anChildrenIter.More(); anChildrenIter.Next())
571   {
572     RestoreSelectionStructures (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theMode, theSelector);
573   }
574
575   if (!theObj->HasOwnPresentations())
576     return;
577
578   if (theSelector.IsNull())
579   {
580     if (!(myGlobal.Contains (theObj) || myLocal.IsBound(theObj)))
581       return;
582
583     TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors);
584     Handle(SelectMgr_ViewerSelector) aSelector;
585     for( ; aSelectorsIter.More(); aSelectorsIter.Next())
586     {
587       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
588       RestoreSelectionStructures (theObj, theMode, aSelector);
589     }
590   }
591   else
592   {
593     if (!(myGlobal.Contains (theObj) || myLocal.IsBound (theObj)))
594       return;
595
596     if (theMode != -1)
597     {
598       if (theObj->HasSelection (theMode))
599       {
600         const Handle(SelectMgr_Selection)& aSelection = theObj->Selection (theMode);
601         if (theObj->HasSelection (theMode))
602         {
603           theSelector->AddSelectionToObject (theObj, aSelection);
604           aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
605         }
606       }
607     }
608     else
609     {
610       for (theObj->Init(); theObj->More(); theObj->Next())
611       {
612         const Handle(SelectMgr_Selection)& aSelection = theObj->CurrentSelection();
613         theSelector->AddSelectionToObject (theObj, aSelection);
614         aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
615       }
616     }
617     theSelector->RebuildObjectsTree();
618   }
619 }
620
621 //=======================================================================
622 //function : rebuildSelectionStructures
623 //purpose  : Internal function that marks 1st level BVH of object theObj
624 //           as outdated
625 //=======================================================================
626 void SelectMgr_SelectionManager::rebuildSelectionStructures (const Handle(SelectMgr_ViewerSelector)& theSelector)
627 {
628   if (theSelector.IsNull())
629   {
630     Handle(SelectMgr_ViewerSelector) aSelector;
631     for(TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
632     {
633       aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
634       rebuildSelectionStructures (aSelector);
635     }
636   }
637   else
638   {
639     theSelector->RebuildObjectsTree();
640   }
641 }
642
643 //==================================================
644 // Function: recomputeSelectionMode
645 // Purpose :
646 //==================================================
647 void SelectMgr_SelectionManager::recomputeSelectionMode (const Handle(SelectMgr_SelectableObject)& theObject,
648                                                          const Handle(SelectMgr_Selection)& theSelection,
649                                                          const Standard_Integer theMode)
650 {
651   theSelection->UpdateStatus (SelectMgr_TOU_Full);
652
653   for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
654   {
655     Handle(SelectMgr_ViewerSelector) aCurSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
656
657     ClearSelectionStructures (theObject, theMode, aCurSelector);
658     theObject->RecomputePrimitives (theMode);
659     RestoreSelectionStructures (theObject, theMode, aCurSelector);
660     theSelection->UpdateStatus (SelectMgr_TOU_None);
661     theSelection->UpdateBVHStatus (SelectMgr_TBU_None);
662   }
663 }
664
665 //==================================================
666 // Function: Update
667 // Purpose :
668 //==================================================
669 void SelectMgr_SelectionManager::RecomputeSelection (const Handle(SelectMgr_SelectableObject)& theObject,
670                                                      const Standard_Boolean theIsForce,
671                                                      const Standard_Integer theMode)
672 {
673   if (theIsForce)
674   {
675     if (theMode == -1)
676     {
677       ClearSelectionStructures (theObject);
678       theObject->RecomputePrimitives();
679       theObject->UpdateTransformation();
680       RestoreSelectionStructures (theObject);
681     }
682     else if (theObject->HasSelection (theMode))
683     {
684       ClearSelectionStructures (theObject, theMode);
685       theObject->RecomputePrimitives (theMode);
686       theObject->UpdateTransformation();
687       RestoreSelectionStructures (theObject, theMode);
688     }
689     return;
690   }
691
692   for (PrsMgr_ListOfPresentableObjectsIter anChildrenIter (theObject->Children()); anChildrenIter.More(); anChildrenIter.Next())
693   {
694     RecomputeSelection (Handle(SelectMgr_SelectableObject)::DownCast (anChildrenIter.Value()), theIsForce, theMode);
695   }
696
697   if (!theObject->HasOwnPresentations())
698     return;
699
700   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
701     return;
702
703   if (theMode == -1)
704   {
705     for (theObject->Init(); theObject->More(); theObject->Next())
706     {
707       const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
708       Standard_Integer aSelMode = aSelection->Mode();
709       recomputeSelectionMode (theObject, aSelection, aSelMode);
710     }
711   }
712   else
713   {
714     if (!theObject->HasSelection (theMode))
715       return;
716
717     const Handle(SelectMgr_Selection)& aSelection = theObject->Selection (theMode);
718     recomputeSelectionMode (theObject, aSelection, theMode);
719   }
720 }
721
722 //=======================================================================
723 //function : Update
724 //purpose  : Selections are recalculated if they are flagged
725 //           "TO RECALCULATE" and activated in one of selectors.
726 //           If ForceUpdate = True, and they are "TO RECALCULATE"
727 //           This is done without caring for the state of activation.
728 //=======================================================================
729 void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
730                                          const Standard_Boolean theIsForce)
731 {
732   for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
733   {
734     Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theIsForce);
735   }
736
737   if (!theObject->HasOwnPresentations())
738     return;
739
740   for (theObject->Init(); theObject->More(); theObject->Next())
741   {
742     const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
743     if (theIsForce)
744     {
745       switch (aSelection->UpdateStatus())
746       {
747       case SelectMgr_TOU_Full:
748         ClearSelectionStructures (theObject, aSelection->Mode());
749         theObject->RecomputePrimitives (aSelection->Mode()); // no break on purpose...
750         RestoreSelectionStructures (theObject, aSelection->Mode());
751       case SelectMgr_TOU_Partial:
752         theObject->UpdateTransformations (aSelection);
753         rebuildSelectionStructures();
754         break;
755       default:
756         break;
757       }
758       aSelection->UpdateStatus (SelectMgr_TOU_None);
759       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
760     }
761
762     for (TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors); aSelectorIter.More(); aSelectorIter.Next())
763     {
764       Handle(SelectMgr_ViewerSelector) aSelector (Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key()));
765       Update (theObject, aSelector, Standard_False);
766     }
767   }
768 }
769
770 //==================================================
771 // Function: Update
772 // Purpose : Attention, it is required to know what is done...
773 //==================================================
774 void SelectMgr_SelectionManager::Update (const Handle(SelectMgr_SelectableObject)& theObject,
775                                          const Handle(SelectMgr_ViewerSelector)& theSelector,
776                                          const Standard_Boolean theIsForce)
777 {
778   if (!mySelectors.Contains (theSelector))
779     return;
780
781   Standard_Boolean isKnown = myGlobal.Contains (theObject);
782   if (!isKnown)
783     isKnown = (myLocal.IsBound (theObject) && (FindIndex (myLocal.Find (theObject), theSelector) != 0));
784   if (!isKnown)
785     return;
786
787   for (PrsMgr_ListOfPresentableObjectsIter aChildIter (theObject->Children()); aChildIter.More(); aChildIter.Next())
788   {
789     Update (Handle(SelectMgr_SelectableObject)::DownCast (aChildIter.Value()), theSelector, theIsForce);
790   }
791
792   if (!theObject->HasOwnPresentations())
793     return;
794
795   for (theObject->Init(); theObject->More(); theObject->Next())
796   {
797     const Handle(SelectMgr_Selection)& aSelection = theObject->CurrentSelection();
798     if (theIsForce)
799     {
800       switch (aSelection->UpdateStatus())
801       {
802       case SelectMgr_TOU_Full:
803         ClearSelectionStructures (theObject, aSelection->Mode());
804         theObject->RecomputePrimitives (aSelection->Mode());
805         RestoreSelectionStructures (theObject, aSelection->Mode());
806       case SelectMgr_TOU_Partial:
807         theObject->UpdateTransformations (aSelection);
808         rebuildSelectionStructures();
809         break;
810       default:
811         break;
812       }
813       aSelection->UpdateStatus (SelectMgr_TOU_None);
814       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
815     }
816
817     if (theSelector->Status (aSelection) == SelectMgr_SOS_Activated)
818     {
819       switch (aSelection->UpdateStatus())
820       {
821       case SelectMgr_TOU_Full:
822         ClearSelectionStructures (theObject, aSelection->Mode(), theSelector);
823         theObject->RecomputePrimitives (aSelection->Mode());
824         RestoreSelectionStructures (theObject, aSelection->Mode(), theSelector);
825       case SelectMgr_TOU_Partial:
826         if (theObject->HasTransformation())
827         {
828           theObject->UpdateTransformations (aSelection);
829           theSelector->RebuildObjectsTree();
830         }
831         break;
832       default:
833         break;
834       }
835
836       aSelection->UpdateStatus(SelectMgr_TOU_None);
837       aSelection->UpdateBVHStatus (SelectMgr_TBU_None);
838     }
839   }
840 }
841
842 //==================================================
843 // Function: loadMode
844 // Purpose : Private Method
845 //==================================================
846 void SelectMgr_SelectionManager::loadMode (const Handle(SelectMgr_SelectableObject)& theObject,
847                                            const Standard_Integer theMode,
848                                            const Handle(SelectMgr_ViewerSelector)& theSelector)
849 {
850   if (theMode == -1)
851     return;
852
853   if (!theObject->HasSelection (theMode))
854   {
855     Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
856     theObject->AddSelection (aNewSel, theMode);
857     if (theSelector.IsNull())
858     {
859       if (myGlobal.Contains (theObject))
860       {
861         TColStd_MapIteratorOfMapOfTransient aSelectorIter (mySelectors);
862         for ( ; aSelectorIter.More(); aSelectorIter.Next())
863         {
864           Handle(SelectMgr_ViewerSelector) aSelector =
865             Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorIter.Key());
866           aSelector->AddSelectionToObject (theObject, aNewSel);
867           aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
868         }
869       }
870       else if (myLocal.IsBound (theObject))
871       {
872         const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
873         for (Standard_Integer aSelectorIdx = 1; aSelectorIdx <= aSelectors.Length(); ++aSelectorIdx)
874         {
875           aSelectors (aSelectorIdx)->AddSelectionToObject (theObject, aNewSel);
876           aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
877         }
878       }
879     }
880     else
881     {
882       theSelector->AddSelectionToObject (theObject, aNewSel);
883       aNewSel->UpdateBVHStatus (SelectMgr_TBU_None);
884     }
885   }
886   else if (theObject->Selection (theMode)->IsEmpty())
887   {
888     if (theObject->Selection (theMode)->BVHUpdateStatus() == SelectMgr_TBU_Remove)
889     {
890       Handle(SelectMgr_Selection) aNewSel = new SelectMgr_Selection (theMode);
891       theObject->AddSelection (aNewSel, theMode);
892       theObject->Selection (theMode)->UpdateBVHStatus (SelectMgr_TBU_Remove);
893       theObject->Selection (theMode)->SetSelectionState (SelectMgr_SOS_Deactivated);
894     }
895   }
896 }
897
898 //=======================================================================
899 //function : SetUpdateMode
900 //purpose  :
901 //=======================================================================
902 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
903                                                 const SelectMgr_TypeOfUpdate theType)
904 {
905   for (theObject->Init(); theObject->More(); theObject->Next())
906     theObject->CurrentSelection()->UpdateStatus (theType);
907 }
908
909 //=======================================================================
910 //function : SetUpdateMode
911 //purpose  :
912 //=======================================================================
913 void SelectMgr_SelectionManager::SetUpdateMode (const Handle(SelectMgr_SelectableObject)& theObject,
914                                                 const Standard_Integer theMode,
915                                                 const SelectMgr_TypeOfUpdate theType)
916 {
917   if (theObject->HasSelection (theMode))
918     theObject->Selection (theMode)->UpdateStatus (theType);
919 }
920
921 //=======================================================================
922 //function : SetSelectionSensitivity
923 //purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject and
924 //           changes previous sensitivity value of all sensitive entities in selection with theMode
925 //           to the given theNewSensitivity.
926 //=======================================================================
927 void SelectMgr_SelectionManager::SetSelectionSensitivity (const Handle(SelectMgr_SelectableObject)& theObject,
928                                                           const Standard_Integer theMode,
929                                                           const Standard_Integer theNewSens)
930 {
931   Standard_ASSERT_RAISE (theNewSens > 0,
932     "Error! Selection sensitivity have positive value.");
933
934   if (theObject.IsNull() || !theObject->HasSelection (theMode))
935     return;
936
937   Handle(SelectMgr_Selection) aSel = theObject->Selection (theMode);
938   const Standard_Integer aPrevSens = aSel->Sensitivity();
939   aSel->SetSensitivity (theNewSens);
940
941   if (!(myGlobal.Contains (theObject) || myLocal.IsBound (theObject)))
942     return;
943
944   if (myGlobal.Contains (theObject))
945   {
946     for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
947     {
948       Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
949       if (aSelector->Contains (theObject))
950       {
951         aSelector->myTolerances.Decrement (aPrevSens);
952         aSelector->myTolerances.Add (theNewSens);
953         aSelector->myToUpdateTolerance = Standard_True;
954       }
955     }
956   }
957   if (myLocal.IsBound (theObject))
958   {
959     const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
960     for (SelectMgr_SequenceOfSelector::Iterator aLocalIter (aSelectors); aLocalIter.More(); aLocalIter.Next())
961     {
962       Handle(SelectMgr_ViewerSelector)& aCurSel = aLocalIter.ChangeValue();
963       aCurSel->myTolerances.Decrement (aPrevSens);
964       aCurSel->myTolerances.Add (theNewSens);
965       aCurSel->myToUpdateTolerance = Standard_True;
966     }
967   }
968 }
969
970 //=======================================================================
971 //function : UpdateSelection
972 //purpose  :
973 //=======================================================================
974 void SelectMgr_SelectionManager::UpdateSelection (const Handle(SelectMgr_SelectableObject)& theObject)
975 {
976   if (myGlobal.Contains (theObject))
977   {
978     for (TColStd_MapIteratorOfMapOfTransient aSelectorsIter (mySelectors); aSelectorsIter.More(); aSelectorsIter.Next())
979     {
980       Handle(SelectMgr_ViewerSelector) aSelector = Handle(SelectMgr_ViewerSelector)::DownCast (aSelectorsIter.Key());
981       if (aSelector->Contains (theObject))
982       {
983         aSelector->MoveSelectableObject (theObject);
984       }
985     }
986   }
987
988   if (myLocal.IsBound (theObject))
989   {
990     const SelectMgr_SequenceOfSelector& aSelectors = myLocal (theObject);
991     for (SelectMgr_SequenceOfSelector::Iterator aSelectorsIter (aSelectors); aSelectorsIter.More(); aSelectorsIter.Next())
992     {
993       Handle(SelectMgr_ViewerSelector)& aSelector = aSelectorsIter.ChangeValue();
994       if (aSelector->Contains (theObject))
995       {
996         aSelector->MoveSelectableObject (theObject);
997       }
998     }
999   }
1000 }