0028832: MMgt_TShared can be replaced by Standard_Transient
[occt.git] / src / AIS / AIS_LocalContext.cxx
1 // Created on: 1997-01-17
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-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 //Modified by ROB : Traque des UpdateConversion intempestifs.
18
19 #include <AIS_DataMapIteratorOfDataMapOfSelStat.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <AIS_ListIteratorOfListOfInteractive.hxx>
23 #include <AIS_ListOfInteractive.hxx>
24 #include <AIS_LocalContext.hxx>
25 #include <AIS_LocalStatus.hxx>
26 #include <AIS_Shape.hxx>
27 #include <Aspect_TypeOfMarker.hxx>
28 #include <Graphic3d_Structure.hxx>
29 #include <Prs3d_LineAspect.hxx>
30 #include <Prs3d_PlaneAspect.hxx>
31 #include <Prs3d_PointAspect.hxx>
32 #include <Prs3d_Presentation.hxx>
33 #include <SelectMgr_CompositionFilter.hxx>
34 #include <SelectMgr_EntityOwner.hxx>
35 #include <SelectMgr_Filter.hxx>
36 #include <SelectMgr_OrFilter.hxx>
37 #include <SelectMgr_SelectableObject.hxx>
38 #include <SelectMgr_SelectionManager.hxx>
39 #include <Standard_Transient.hxx>
40 #include <Standard_Type.hxx>
41 #include <StdSelect_ShapeTypeFilter.hxx>
42 #include <StdSelect_ViewerSelector3d.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TColStd_ListIteratorOfListOfInteger.hxx>
45 #include <TopAbs_ShapeEnum.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <V3d_View.hxx>
48 #include <V3d_Viewer.hxx>
49
50 #include <stdio.h>
51 IMPLEMENT_STANDARD_RTTIEXT(AIS_LocalContext,Standard_Transient)
52
53 //=======================================================================
54 //function : AIS_LocalContext
55 //purpose  : 
56 //=======================================================================
57
58
59 AIS_LocalContext::AIS_LocalContext(){}
60
61 AIS_LocalContext::AIS_LocalContext(const Handle(AIS_InteractiveContext)& aCtx,
62                                    const Standard_Integer Index,
63                                    const Standard_Boolean LoadDisplayed,
64                                    const Standard_Boolean AcceptStandardModes,
65                                    const Standard_Boolean AcceptEraseOfTemp,
66                                    const Standard_Boolean /*BothViewers*/):
67 myCTX(aCtx),
68 myLoadDisplayed(LoadDisplayed),
69 myAcceptStdMode(AcceptStandardModes),
70 myAcceptErase(AcceptEraseOfTemp),
71 mySM(aCtx->SelectionManager()),
72 myMainVS(aCtx->MainSelector()),
73 myFilters(new SelectMgr_OrFilter()),
74 myAutoHilight(Standard_True),
75 myMapOfOwner (new SelectMgr_IndexedMapOfOwner()),
76 mySelection(new AIS_Selection()),
77 mylastindex(0),
78 mylastgood(0),
79 myCurDetected(0),
80 myAISCurDetected(0),
81 mySubintStyle (new Prs3d_Drawer())
82 {
83   mySubintStyle->Link (aCtx->SelectionStyle());
84   mySubintStyle->SetColor        (aCtx->SelectionStyle()->Color());
85   mySubintStyle->SetTransparency (aCtx->SelectionStyle()->Transparency());
86   mySubintStyle->SetMethod       (aCtx->SelectionStyle()->Method());
87
88   // bind self to AIS_InteractiveContext::myLocalContexts. Further, the
89   // constructor executes logic that implies that the context is already
90   // created and mapped.
91   aCtx->myLocalContexts.Bind (Index, this);
92
93   myMainVS->ResetSelectionActivationStatus();
94   myMainPM = aCtx->MainPrsMgr();
95
96   mySM->Add(myMainVS);
97   if(myLoadDisplayed) LoadContextObjects();
98   Process();
99
100 }
101
102
103 //=======================================================================
104 //function : SetContext
105 //purpose  : 
106 //=======================================================================
107
108 void AIS_LocalContext::SetContext(const Handle(AIS_InteractiveContext)& aCtx)
109 {  myCTX = aCtx;}
110
111 //=======================================================================
112 //function : Display
113 //purpose  : 
114 //=======================================================================
115
116 Standard_Boolean AIS_LocalContext::Display(const Handle(AIS_InteractiveObject)& anInteractive,
117                                            const Standard_Integer WhichMode,
118                                            const Standard_Boolean AllowShapeDecomposition,
119                                            const Standard_Integer ActivationMode)
120 {
121   if(myActiveObjects.IsBound(anInteractive)){
122     const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
123     
124     if(STAT->DisplayMode() == -1){
125       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
126         myMainPM->Display(anInteractive,WhichMode);
127       if(STAT->IsTemporary())
128         STAT->SetDisplayMode(WhichMode);
129     }
130     else if(STAT->DisplayMode()!=WhichMode && STAT->IsTemporary()){
131       myMainPM->Erase(anInteractive,STAT->DisplayMode());
132       STAT->SetDisplayMode(WhichMode);
133       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
134         myMainPM->Display(anInteractive,WhichMode);
135     }
136     
137     if(ActivationMode!=-1){
138       if(!STAT->IsActivated(ActivationMode)){
139         STAT->ClearSelectionModes();
140         mySM->Load(anInteractive,myMainVS);
141         STAT->AddSelectionMode(ActivationMode);
142         mySM->Activate(anInteractive,ActivationMode,myMainVS);
143       }
144     }
145   }
146   else {
147     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
148     
149     if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
150       Att->SetDecomposition(Standard_True);
151     else 
152       Att->SetDecomposition(Standard_False);
153     // status temporary or not
154     if(myCTX->DisplayStatus(anInteractive) == AIS_DS_None ||
155        myCTX->DisplayStatus(anInteractive) == AIS_DS_Temporary)
156       Att->SetTemporary(Standard_True);
157     else
158       Att->SetTemporary(Standard_False); 
159
160
161     
162     if(!myCTX->IsDisplayed(anInteractive,WhichMode)){
163       
164       //storing information....
165       Att->SetDisplayMode(WhichMode);
166       if (ActivationMode!=-1)
167         Att->AddSelectionMode(ActivationMode);
168       Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode(): WhichMode;
169       Att->SetHilightMode(HiMod);
170
171       if(!myMainPM->IsDisplayed(anInteractive,WhichMode))
172         myMainPM->Display(anInteractive,WhichMode);
173       
174       if(ActivationMode!=-1){
175         mySM->Load(anInteractive,myMainVS);
176         mySM->Activate(anInteractive,ActivationMode,myMainVS);
177       }
178     }
179     else{
180       Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode(): WhichMode;
181       Att->SetHilightMode(HiMod);
182     }
183     myActiveObjects.Bind(anInteractive,Att);
184   }  
185   Process(anInteractive);
186
187   
188   
189
190   return Standard_True;
191 }
192
193 //=======================================================================
194 //function : Load
195 //purpose  : 
196 //=======================================================================
197
198 Standard_Boolean AIS_LocalContext::
199 Load(const Handle(AIS_InteractiveObject)& anInteractive,
200      const Standard_Boolean AllowShapeDecomposition,
201      const Standard_Integer ActivationMode)
202 {
203   if (myActiveObjects.IsBound (anInteractive))
204   {
205     if (anInteractive->HasSelection (ActivationMode))
206     {
207       const Handle(SelectMgr_Selection)& aSel = anInteractive->Selection (ActivationMode);
208       if (aSel->GetSelectionState() != SelectMgr_SOS_Activated)
209       {
210         if (!myMainVS->Contains (anInteractive))
211         {
212           mySM->Load (anInteractive, myMainVS);
213         }
214         mySM->Activate (anInteractive, ActivationMode, myMainVS);
215         return Standard_True;
216       }
217     }
218     return Standard_False;
219   }
220
221   Handle(AIS_LocalStatus) Att = new AIS_LocalStatus();
222   
223   if(anInteractive->AcceptShapeDecomposition() && AllowShapeDecomposition)
224     Att->SetDecomposition(Standard_True);
225   else 
226     Att->SetDecomposition(Standard_False);
227   
228   if(!myCTX->IsDisplayed(anInteractive))
229     Att->SetTemporary(Standard_True);
230   else
231     Att->SetTemporary(Standard_False);
232   Att->SetDisplayMode(-1);
233   
234   //storing information....
235   if(ActivationMode!=-1)
236     Att->AddSelectionMode(ActivationMode);
237   Standard_Integer HiMod = anInteractive->HasHilightMode()? anInteractive->HilightMode():0;
238   Att->SetHilightMode(HiMod);
239   //Action
240   
241   mySM->Load(anInteractive,myMainVS);
242   if(ActivationMode != -1){
243     mySM->Activate(anInteractive,ActivationMode,myMainVS);
244   }
245   myActiveObjects.Bind(anInteractive,Att);
246   Process(anInteractive);
247   return Standard_True;
248 }
249
250 //=======================================================================
251 //function : ClearPrs
252 //purpose  : 
253 //=======================================================================
254
255 Standard_Boolean AIS_LocalContext::
256 ClearPrs(const Handle(AIS_InteractiveObject)& anInteractive,
257          const Standard_Integer aMode)
258 {
259   if(!myActiveObjects.IsBound(anInteractive))
260     return Standard_False;
261
262   Standard_Boolean jobdone(Standard_False);
263   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
264   
265   //Display step
266   if(STAT->IsSubIntensityOn()) {
267     STAT->SubIntensityOff();
268     if(STAT->HilightMode()==aMode)
269       myMainPM->Unhighlight(anInteractive);
270   }
271   myMainPM->Clear(anInteractive,aMode); // correction connexions 23/09/97
272   jobdone = Standard_True;
273   if(STAT->DisplayMode()==aMode)
274     STAT->SetDisplayMode(-1);
275   return jobdone;
276 }
277 //=======================================================================
278 //function : Erase
279 //purpose  : 
280 //=======================================================================
281
282 Standard_Boolean AIS_LocalContext::
283 Erase(const Handle(AIS_InteractiveObject)& anInteractive)
284 {
285   if(!myActiveObjects.IsBound(anInteractive))
286     return Standard_False;
287   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anInteractive);
288   
289   //Display step
290   if(STAT->IsSubIntensityOn()) {
291     STAT->SubIntensityOff();
292     myMainPM->Unhighlight (anInteractive);
293   }
294
295   Standard_Boolean status(Standard_False);
296
297   if(STAT->DisplayMode()!=-1) {
298     if(IsSelected(anInteractive))
299       AddOrRemoveSelected(anInteractive);
300     if(myMainPM->IsHighlighted(anInteractive,STAT->HilightMode()))
301     {
302       myMainPM->Unhighlight (anInteractive);
303     }
304     myMainPM->SetVisibility (anInteractive, STAT->DisplayMode(), Standard_False);
305     STAT->SetDisplayMode(-1);
306     status = Standard_True;
307   }
308   if(STAT->IsTemporary()){
309     if(myMainPM->IsDisplayed(anInteractive,STAT->HilightMode()))
310       myMainPM->SetVisibility (anInteractive, STAT->HilightMode(), Standard_False);
311   }
312
313   // Deactivate selectable entities of interactive object
314   const Handle(SelectMgr_SelectableObject)& anObj = anInteractive; // to avoid ambiguity
315   if (mySM->Contains (anObj))
316   {
317     while (!STAT->SelectionModes().IsEmpty())
318     {
319       mySM->Deactivate (anInteractive, STAT->SelectionModes().Last(), myMainVS);
320       STAT->RemoveSelectionMode (STAT->SelectionModes().Last());
321     }
322   }
323
324   ClearOutdatedSelection (anInteractive, Standard_True);
325
326   return status;
327 }
328
329
330 //=======================================================================
331 //function : SetShapeDecomposition
332 //purpose  : 
333 //=======================================================================
334
335 void AIS_LocalContext::SetShapeDecomposition(const Handle(AIS_InteractiveObject)& aStoredObject, 
336                                                 const Standard_Boolean aStatus)
337 {
338   if(!myActiveObjects.IsBound(aStoredObject)) return;
339   
340   if(aStatus == myActiveObjects(aStoredObject)->Decomposed()) 
341     return;
342   
343   myActiveObjects(aStoredObject)->SetDecomposition(aStatus);
344
345   Process(aStoredObject);
346 }
347
348 //=======================================================================
349 //function : Clear
350 //purpose  : 
351 //=======================================================================
352
353 void AIS_LocalContext::Clear(const AIS_ClearMode aType)
354 {
355   switch (aType){
356   case AIS_CM_All:
357     {
358       ClearObjects();
359       myFilters->Clear();
360       while(!myListOfStandardMode.IsEmpty())
361         DeactivateStandardMode(AIS_Shape::SelectionType(myListOfStandardMode.Last()));
362       break;
363     }
364   case AIS_CM_Interactive:
365     ClearObjects();
366     break;
367   case AIS_CM_Filters:
368     myFilters->Clear();
369     break;
370   case AIS_CM_StandardModes:
371     {
372       while(!myListOfStandardMode.IsEmpty())
373         DeactivateStandardMode(AIS_Shape::SelectionType(myListOfStandardMode.Last()));
374       break;
375     }
376   case AIS_CM_TemporaryShapePrs:
377     ClearDetected();
378   }
379 }
380 //=======================================================================
381 //function : ActivateMode
382 //purpose  : 
383 //=======================================================================
384
385 void AIS_LocalContext::ActivateMode(const Handle(AIS_InteractiveObject)& aSelectable,
386                                        const Standard_Integer aMode)
387 {
388   if(!myActiveObjects.IsBound(aSelectable)) return;
389 //  if(myActiveObjects(aSelectable)->SelectionMode()!=aMode)
390 //    mySM->Deactivate(aSelectable,aMode,myMainVS);
391   if(aMode != -1){
392     myActiveObjects(aSelectable)->AddSelectionMode(aMode);
393     mySM->Activate(aSelectable,aMode,myMainVS);
394   }
395 }
396 //=======================================================================
397 //function : ActivateMode
398 //purpose  : 
399 //=======================================================================
400
401 void AIS_LocalContext::DeactivateMode(const Handle(AIS_InteractiveObject)& aSelectable,
402                                          const Standard_Integer aMode)
403 {
404   if(!myActiveObjects.IsBound(aSelectable)) return;
405   
406   if(aMode==-1) return;
407   
408   myActiveObjects(aSelectable)->RemoveSelectionMode(aMode);
409   mySM->Deactivate(aSelectable,aMode,myMainVS);
410 }
411 //=======================================================================
412 //function : ActivateMode
413 //purpose  : 
414 //=======================================================================
415
416 void AIS_LocalContext::Deactivate(const Handle(AIS_InteractiveObject)& aSelectable)
417 {
418   if(!myActiveObjects.IsBound(aSelectable)) return;
419   
420   mySM->Deactivate(aSelectable, -1, myMainVS);
421   myActiveObjects(aSelectable)->ClearSelectionModes();
422 }
423
424 //=======================================================================
425 //function : Remove
426 //purpose  : 
427 //=======================================================================
428
429 Standard_Boolean AIS_LocalContext::Remove(const Handle(AIS_InteractiveObject)& aSelectable)
430 {
431   if(!myActiveObjects.IsBound (aSelectable))
432   {
433     return Standard_False;
434   }
435
436   if (IsSelected (aSelectable))
437   {
438     AddOrRemoveSelected (aSelectable, Standard_False);
439   }
440
441   const Handle(AIS_LocalStatus)& Att = myActiveObjects (aSelectable);
442
443   TColStd_ListIteratorOfListOfInteger It;
444
445   // Deactivate standard modes
446   if (Att->Decomposed())
447   {
448     for (It.Initialize (myListOfStandardMode); It.More(); It.Next())
449     {
450       mySM->Deactivate (aSelectable, It.Value(), myMainVS);
451     }
452   }
453
454   // If object or temporary presentations
455   if (Att->IsTemporary())
456   {
457     if (Att->IsSubIntensityOn())
458     {
459       myMainPM->Unhighlight (aSelectable);
460     }
461
462     myMainPM->Erase (aSelectable, Att->DisplayMode());
463     if (myMainPM->IsDisplayed (aSelectable, Att->HilightMode()))
464     {
465       myMainPM->Erase (aSelectable, Att->HilightMode());
466     }
467   }
468   // If subintensity used
469   else if (Att->IsSubIntensityOn())
470   {
471     myCTX->SubIntensityOff (aSelectable, Standard_False);
472   }
473
474   // Deactivate stored selection modes
475   for (It.Initialize (Att->SelectionModes()); It.More(); It.Next())
476   {
477     mySM->Deactivate (aSelectable, It.Value(), myMainVS);
478   }
479
480   // Remove the interactive object from selection manager
481   const Handle(SelectMgr_SelectableObject)& anObj = aSelectable; // to avoid ambiguity
482   if (mySM->Contains (anObj))
483   {
484     mySM->Remove (anObj);
485   }
486   ClearOutdatedSelection (aSelectable, Standard_True);
487
488   // This should be done at the very end because most methods use
489   // myActiveObjects even during clean-up
490   myActiveObjects.UnBind (aSelectable);
491   return Standard_True;
492 }
493
494 //=======================================================================
495 //function : ActivateStandardMode
496 //purpose  : 
497 //=======================================================================
498
499 void AIS_LocalContext::ActivateStandardMode(const TopAbs_ShapeEnum aType)
500 {
501   
502   //check if it is not in the list
503   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
504   for(;It.More();It.Next())
505     if(It.Value()==aType)  return;
506   Standard_Integer IMode = AIS_Shape::SelectionMode(aType);
507   
508
509   // create a hidden filter answering ok to the type except for :
510   // if the type is shape...
511   // if the filters already impact at the type <aType>
512   if(aType != TopAbs_SHAPE){
513     if(myStdFilters[IMode].IsNull())
514       myStdFilters[IMode] = new StdSelect_ShapeTypeFilter(aType);
515     if(!HasFilters(aType))
516       myFilters->Add(myStdFilters[IMode]);
517   }
518   
519   // the mode is activated for all objects of type Shape 
520   // accepting the decomposition in standard mode.
521   myListOfStandardMode.Append(IMode);
522   
523   AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
524
525   for(;ItM.More();ItM.Next()){
526     if(ItM.Value()->Decomposed())
527       myCTX->SelectionManager()->Activate(ItM.Key(),
528                                           IMode,
529                                           myMainVS);
530     ItM.Value()->AddSelectionMode (IMode);
531   }
532   
533 }
534
535 //=======================================================================
536 //function : DeActivateStandardMode
537 //purpose  : 
538 //=======================================================================
539
540 void AIS_LocalContext::DeactivateStandardMode(const TopAbs_ShapeEnum aType)
541 {
542   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
543   Standard_Integer IMode = AIS_Shape::SelectionMode(aType);
544   for(;It.More();It.Next())
545     if(It.Value()==IMode) {
546       AIS_DataMapIteratorOfDataMapOfSelStat ItM(myActiveObjects);
547       
548       for(;ItM.More();ItM.Next()){
549         if(ItM.Value()->Decomposed()){
550           myCTX->SelectionManager()->Deactivate(ItM.Key(),
551                                                 IMode,
552                                                 myMainVS);
553           ItM.Value()->RemoveSelectionMode(IMode);
554         }
555       }
556       myListOfStandardMode.Remove(It);
557       if(myFilters->IsIn(myStdFilters[IMode]))
558         myFilters->Remove(myStdFilters[IMode]);
559       return;
560     }   
561 }
562
563 //=======================================================================
564 //function : AddFilter
565 //purpose  : 
566 //=======================================================================
567
568 void AIS_LocalContext::AddFilter(const Handle(SelectMgr_Filter)& aFilter)
569 {
570   // it is checked if the filter impacts at the type of active sub-shape 
571   // for which a filter of type has been already implemented...
572
573   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
574   
575   for(;It.More();It.Next()){
576     if(aFilter->ActsOn(AIS_Shape::SelectionType(It.Value())))
577       if(myFilters->IsIn(myStdFilters[It.Value()]))
578         myFilters->Remove(myStdFilters[It.Value()]);
579   } 
580   myFilters->Add(aFilter);
581 }
582
583 //=======================================================================
584 //function : RemoveFilter
585 //purpose  : 
586 //=======================================================================
587
588 void AIS_LocalContext::RemoveFilter(const Handle(SelectMgr_Filter)& aFilter)
589 {
590   if(myFilters->IsIn(aFilter)) myFilters->Remove(aFilter);
591   
592   // it is checked if the filter for type standard is active.
593   // if yes, it is checked there are still similarities among the
594   // remaining filters...
595   //     otherwise, the standard filter is restored to
596   //     continu selecting active modes...
597   TColStd_ListIteratorOfListOfInteger It(myListOfStandardMode);
598   TopAbs_ShapeEnum SE;
599   for(;It.More();It.Next()){
600     SE = AIS_Shape::SelectionType(It.Value());
601     if(aFilter->ActsOn(SE))
602       if(!HasFilters(SE))
603         myFilters->Add(myStdFilters[It.Value()]);
604   }
605 }
606
607 //=======================================================================
608 //function : Terminate
609 //purpose  :
610 //=======================================================================
611
612 void AIS_LocalContext::Terminate (const Standard_Boolean theToUpdate)
613 {
614   ClearDetected();
615   Clear();
616   myMapOfOwner->Clear();
617   
618   mylastindex=0;
619   // clear the selector...
620   myMainVS->Clear();
621   
622   for (AIS_NListOfEntityOwner::Iterator aSelIter (mySelection->Objects()); aSelIter.More(); aSelIter.Next())
623   {
624     aSelIter.Value()->SetSelected (Standard_False);
625   }
626   mySelection->Clear();
627
628   Handle(V3d_View) aDummyView;
629   myMainVS->ClearSensitive (aDummyView);
630
631   if (theToUpdate)
632   {
633     myCTX->UpdateCurrentViewer();
634   }
635 }
636
637
638 //=======================================================================
639 //function : SubIntensity
640 //purpose  : 
641 //=======================================================================
642
643 void AIS_LocalContext::SubIntensityOn(const Handle(AIS_InteractiveObject)& anObject)
644 {
645   if(!myActiveObjects.IsBound(anObject)) return;
646   mySubintStyle->SetColor (myCTX->SubIntensityColor());
647   
648   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
649
650   if(Att->IsTemporary())
651   {
652     myMainPM->Color (anObject, mySubintStyle, Att->DisplayMode());
653   }
654   
655   Att->SubIntensityOn();
656 }
657 //=======================================================================
658 //function : SubIntensity
659 //purpose  : 
660 //=======================================================================
661
662 void AIS_LocalContext::SubIntensityOff(const Handle(AIS_InteractiveObject)& anObject)
663 {
664   if(!myActiveObjects.IsBound(anObject)) return;
665   
666   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
667
668   if(Att->IsTemporary()) 
669     myMainPM->Unhighlight(anObject);
670   Att->SubIntensityOff();
671 }
672
673
674 //=======================================================================
675 //function : Hilight
676 //purpose  : 
677 //=======================================================================
678
679 void AIS_LocalContext::Hilight(const  Handle(AIS_InteractiveObject)& anObject)
680 {
681   if(!myActiveObjects.IsBound(anObject)){
682     Standard_Integer HiMod = anObject->HasHilightMode()? anObject->HilightMode() : 0; 
683     Handle(AIS_LocalStatus) Att = new AIS_LocalStatus(Standard_True,
684                                                       Standard_False,
685                                                       -1,-1,HiMod);
686     myActiveObjects.Bind(anObject,Att);
687     
688   }
689   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
690   myMainPM->Color(anObject, myCTX->getHiStyle (anObject, anObject->GlobalSelOwner()), Att->HilightMode());
691   Att->SubIntensityOn();
692 }
693 //=======================================================================
694 //function : Hilight
695 //purpose  : 
696 //=======================================================================
697
698 void AIS_LocalContext::Hilight (const Handle(AIS_InteractiveObject)& theObj,
699                                 const Handle(Prs3d_Drawer)& theStyle)
700 {
701   if (!myActiveObjects.IsBound (theObj))
702   {
703     Handle(AIS_LocalStatus) aStatus = new AIS_LocalStatus
704       (Standard_True, Standard_False, -1, -1, theObj->HasHilightMode() ? theObj->HilightMode() : 0);
705     myActiveObjects.Bind (theObj, aStatus);
706   }
707   const Handle(AIS_LocalStatus)& aStatus = myActiveObjects (theObj);
708   myMainPM->Color (theObj, theStyle, aStatus->HilightMode());
709   aStatus->SubIntensityOn();
710   aStatus->SetHilightStyle (theStyle);
711 }
712
713 //=======================================================================
714 //function : Unhilight
715 //purpose  : 
716 //=======================================================================
717
718 void AIS_LocalContext::Unhilight(const Handle(AIS_InteractiveObject)& anObject)
719 {
720   if(!myActiveObjects.IsBound(anObject)) return;
721   
722   // chieck if by hazard the object is somewhere else...
723   Standard_Integer Indx;
724   Standard_DISABLE_DEPRECATION_WARNINGS
725   Standard_Boolean IsSomeWhereElse  = 
726     myCTX->IsInLocal(anObject,Indx) && Indx != myCTX->IndexOfCurrentLocal();
727   Standard_ENABLE_DEPRECATION_WARNINGS
728   
729   const Handle(AIS_LocalStatus)& Att = myActiveObjects(anObject);
730   myMainPM->Unhighlight (anObject);
731   if(Att->IsTemporary() && Att->DisplayMode()==-1)
732     if(!IsSomeWhereElse)
733       myMainPM->SetVisibility (anObject, Att->HilightMode(), Standard_False);
734
735   Att->SubIntensityOff();
736   Att->SetHilightStyle (Handle(Prs3d_Drawer)());
737 }
738
739
740 //=======================================================================
741 //function : IsIn
742 //purpose  : 
743 //=======================================================================
744
745 Standard_Boolean AIS_LocalContext::
746 IsIn(const Handle(AIS_InteractiveObject)& anObject) const 
747 {
748   return myActiveObjects.IsBound(anObject);
749 }
750
751 //=======================================================================
752 //function : IsHilighted
753 //purpose  : 
754 //=======================================================================
755
756 Standard_Boolean AIS_LocalContext::IsHilighted(const Handle(AIS_InteractiveObject)& anObject) const 
757 {
758   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
759   return myActiveObjects(anObject)->IsSubIntensityOn();
760 }
761
762 Standard_Boolean AIS_LocalContext::HighlightStyle (const Handle(AIS_InteractiveObject)& theObject,
763                                                    Handle(Prs3d_Drawer)& theStyle) const
764 {
765   if (!myActiveObjects.IsBound (theObject))
766     return Standard_False;
767
768   if (myActiveObjects (theObject)->IsSubIntensityOn())
769   {
770     theStyle = myActiveObjects (theObject)->HilightStyle();
771     return Standard_True;
772   }
773
774   return Standard_False;
775 }
776
777
778 void AIS_LocalContext::SetDisplayPriority(const Handle(AIS_InteractiveObject)& anObject,
779                                           const Standard_Integer Prior)
780 {
781   if(!myActiveObjects.IsBound(anObject)) return;
782   const Handle(AIS_LocalStatus)& STAT = myActiveObjects(anObject);
783   if(STAT->DisplayMode()==-1) return;
784   myMainPM->SetDisplayPriority(anObject,STAT->DisplayMode(),Prior);
785   if(STAT->IsSubIntensityOn())
786     myMainPM->SetDisplayPriority(anObject,STAT->HilightMode(),Prior);
787   
788   
789 }
790
791 //=======================================================================
792 //function : DisplayedObjects
793 //purpose  : 
794 //=======================================================================
795 Standard_Integer AIS_LocalContext::DisplayedObjects(TColStd_MapOfTransient& theMap) const
796 {
797   Standard_Integer NbDisp(0);
798   for(AIS_DataMapIteratorOfDataMapOfSelStat it(myActiveObjects);it.More();it.Next()){
799     const Handle(SelectMgr_SelectableObject)& SO = it.Key();
800     if(!theMap.Contains(SO))
801       if(it.Value()->DisplayMode()!=-1){
802         theMap.Add(SO);
803         NbDisp++;
804       }
805   }
806   return NbDisp;
807 }
808
809
810 //=======================================================================
811 //function : IsDisplayed
812 //purpose  : 
813 //=======================================================================
814
815 Standard_Boolean AIS_LocalContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject) const 
816 {
817   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
818   return (myActiveObjects(anObject)->DisplayMode()!=-1);
819 }
820
821 //=======================================================================
822 //function : IsDisplayed
823 //purpose  : 
824 //=======================================================================
825
826 Standard_Boolean AIS_LocalContext::IsDisplayed(const Handle(AIS_InteractiveObject)& anObject,
827                                                   const Standard_Integer aMode) const 
828 {
829   if(!myActiveObjects.IsBound(anObject)) return Standard_False;
830   return (myActiveObjects(anObject)->DisplayMode()==aMode);
831 }
832
833 //=======================================================================
834 //function : SelectionModes
835 //purpose  : 
836 //=======================================================================
837
838 const TColStd_ListOfInteger& AIS_LocalContext::
839 SelectionModes(const Handle(AIS_InteractiveObject)& anObject) const 
840 {
841   return myActiveObjects(anObject)->SelectionModes(); 
842 }
843
844 //=======================================================================
845 //function : Status
846 //purpose  : 
847 //=======================================================================
848
849 TCollection_AsciiString AIS_LocalContext::Status() const 
850 {
851   TCollection_AsciiString t;
852   return t;
853 }
854
855 const Handle(AIS_LocalStatus)& AIS_LocalContext::Status(const Handle(AIS_InteractiveObject)& anObject) const 
856 {
857   return myActiveObjects(anObject);
858 }
859
860 //=======================================================================
861 //function : LoadContextObjects
862 //purpose  : 
863 //=======================================================================
864
865 void AIS_LocalContext::LoadContextObjects()
866 {
867   AIS_ListIteratorOfListOfInteractive It;
868   if(myLoadDisplayed) {
869     AIS_ListOfInteractive LL;
870     myCTX->DisplayedObjects(LL,Standard_True);
871     Handle(AIS_LocalStatus) Att;
872     for (It.Initialize(LL);It.More();It.Next()){
873       const Handle(AIS_InteractiveObject)& anObj = It.Value();
874       Att= new AIS_LocalStatus();
875       Att->SetDecomposition((anObj->AcceptShapeDecomposition() && myAcceptStdMode));
876       Att->SetTemporary(Standard_False);
877       Att->SetHilightMode(anObj->HasHilightMode()? anObj->HilightMode(): 0);
878       for (anObj->Init(); anObj->More(); anObj->Next())
879       {
880         const Handle(SelectMgr_Selection)& aSel = anObj->CurrentSelection();
881         aSel->SetSelectionState (SelectMgr_SOS_Deactivated);
882       }
883       myActiveObjects.Bind(anObj,Att);
884     }
885   }
886 }
887
888 void AIS_LocalContext::UnloadContextObjects()
889 {
890   AIS_ListIteratorOfListOfInteractive It;
891   if(myLoadDisplayed) 
892   {
893     AIS_ListOfInteractive LL;
894     myCTX->DisplayedObjects(LL,Standard_True);
895     
896     for (It.Initialize(LL);It.More();It.Next())
897     {
898       myActiveObjects.UnBind(It.Value());
899     }
900   }
901 }
902 //=======================================================================
903 //function : Process
904 //purpose  : 
905 //=======================================================================
906
907 void AIS_LocalContext::Process(const Handle(SelectMgr_SelectableObject)& anObject)
908
909   if(!myActiveObjects.IsBound(anObject)) return;
910   if(myActiveObjects(anObject)->Decomposed())
911     ActivateStandardModes(anObject);
912   else
913     {
914       TColStd_ListIteratorOfListOfInteger It(myActiveObjects(anObject)->SelectionModes());
915       for(;It.More();It.Next())
916         myCTX->SelectionManager()->Activate(anObject,It.Value(),myMainVS);
917     }
918 }
919
920 //=======================================================================
921 //function : Process
922 //purpose  : 
923 //=======================================================================
924
925 void AIS_LocalContext::Process()
926
927
928   myMainVS->Clear();
929   
930   AIS_DataMapIteratorOfDataMapOfSelStat It(myActiveObjects);
931   
932   for(;It.More();It.Next()){
933     myCTX->SelectionManager()->Load(It.Key(),myMainVS);
934     if(It.Value()->Decomposed()) 
935       ActivateStandardModes(It.Key());
936     else if( myCTX->GetAutoActivateSelection() )
937     {
938       It.Value()->AddSelectionMode(0);
939       myCTX->SelectionManager()->Activate(It.Key(),0,myMainVS);
940     }
941   }
942
943 }
944
945 //=======================================================================
946 //function : ActivateModes
947 //purpose  : 
948 //=======================================================================
949
950 void AIS_LocalContext::ActivateStandardModes(const Handle(SelectMgr_SelectableObject)& anObject)
951
952   if(!myActiveObjects.IsBound(anObject)) return;
953   
954   TColStd_ListIteratorOfListOfInteger itl (myListOfStandardMode);
955
956   const Handle(AIS_LocalStatus)&  LS = myActiveObjects(anObject);
957   if(LS->Decomposed()){
958     for(;itl.More();itl.Next()){
959       myCTX->SelectionManager()->Activate(anObject,itl.Value(),myMainVS);
960       LS->AddSelectionMode(itl.Value());
961     }
962   }
963 }
964
965
966 //=======================================================================
967 //function : ClearObjects
968 //purpose  : 
969 //=======================================================================
970
971 void AIS_LocalContext::ClearObjects()
972 {
973   AIS_DataMapIteratorOfDataMapOfSelStat It(myActiveObjects);
974   for(;It.More();It.Next())
975     {
976       Handle(AIS_InteractiveObject) SO =
977         Handle(AIS_InteractiveObject)::DownCast(It.Key());
978       
979       const Handle(AIS_LocalStatus)& CurAtt = It.Value();
980       //TColStd_ListIteratorOfListOfInteger ItL;
981       // if object is temporary the presentations managed by myMainPM are removed
982       AIS_DisplayStatus TheDS = myCTX->DisplayStatus(SO);
983       
984       if(TheDS != AIS_DS_Displayed)
985       {
986         if (myMainPM->IsDisplayed(SO,CurAtt->DisplayMode()))
987         {
988           if (CurAtt->IsSubIntensityOn() && myMainPM->IsHighlighted (SO, CurAtt->HilightMode()))
989           {
990             myMainPM->Unhighlight (SO);
991           }
992           myMainPM->Erase (SO, CurAtt->DisplayMode());
993         }
994
995         if (CurAtt->IsTemporary())
996         {
997           myMainPM->Erase (SO, CurAtt->DisplayMode());
998           //myMainPM->Clear(SO,CurAtt->DisplayMode());
999         }
1000       }
1001       else {
1002         if (CurAtt->IsSubIntensityOn())
1003         {
1004           myCTX->SubIntensityOff(SO, Standard_False);
1005         }
1006         Standard_Integer DiMo = SO->HasDisplayMode()?
1007           SO->DisplayMode():myCTX->DisplayMode();
1008         if(CurAtt->DisplayMode()!=-1 &&
1009            CurAtt->DisplayMode()!= DiMo)
1010           myMainPM->Erase(SO,CurAtt->DisplayMode());
1011       }
1012       
1013       TColStd_ListIteratorOfListOfInteger aSelModeIter (CurAtt->SelectionModes());
1014       for ( ; aSelModeIter.More(); aSelModeIter.Next())
1015       {
1016         Standard_Integer aSelMode = aSelModeIter.Value();
1017         mySM->Deactivate (SO, aSelMode, myMainVS);
1018       }
1019
1020     }
1021   ClearSelected( Standard_False );
1022
1023   // Clear selection structures for temporary objects, created in local context
1024   for (AIS_DataMapIteratorOfDataMapOfSelStat anIter (myActiveObjects); anIter.More(); anIter.Next())
1025   {
1026     if (anIter.Value()->IsTemporary())
1027     {
1028       mySM->Remove (anIter.Key(), myMainVS);
1029     }
1030   }
1031
1032   myActiveObjects.Clear();
1033 }
1034
1035
1036 Standard_Boolean AIS_LocalContext::IsDecompositionOn() const 
1037 {return !myListOfStandardMode.IsEmpty();}
1038
1039
1040
1041
1042 //=======================================================================
1043 //function : HasAlreadyFilters
1044 //purpose  : 
1045 //=======================================================================
1046
1047 Standard_Boolean AIS_LocalContext::
1048 HasFilters(const TopAbs_ShapeEnum aType) const 
1049 {
1050   return myFilters->ActsOn(aType);
1051 }
1052
1053 void AIS_LocalContext::ClearDetected()
1054 {
1055   for(Standard_Integer I=1;I<=myMapOfOwner->Extent();I++)
1056   {
1057     if(!myMapOfOwner->FindKey (I).IsNull())
1058     {
1059       if(myMapOfOwner->FindKey (I)->IsHilighted(myMainPM))
1060         myMapOfOwner->FindKey (I)->Unhilight(myMainPM);
1061       else
1062       {
1063         const Handle(SelectMgr_SelectableObject)& SO = 
1064           myMapOfOwner->FindKey (I)->Selectable();
1065         if(myActiveObjects.IsBound(SO))
1066         {
1067           const Handle(AIS_LocalStatus)& Att = myActiveObjects(SO);
1068
1069           if(Att->IsTemporary() &&
1070              Att->DisplayMode()==-1 && 
1071              Att->SelectionModes().IsEmpty())
1072           {
1073             myMapOfOwner->FindKey (I)->Clear(myMainPM);
1074           }
1075         }
1076       }
1077     }
1078   }
1079 }
1080
1081 //=======================================================================
1082 //function : BeginImmediateDraw
1083 //purpose  :
1084 //=======================================================================
1085 Standard_Boolean AIS_LocalContext::BeginImmediateDraw()
1086 {
1087   if (myMainPM->IsImmediateModeOn())
1088   {
1089     myMainPM->BeginImmediateDraw();
1090     return Standard_True;
1091   }
1092   return Standard_False;
1093 }
1094
1095 //=======================================================================
1096 //function : ImmediateAdd
1097 //purpose  :
1098 //=======================================================================
1099 Standard_Boolean AIS_LocalContext::ImmediateAdd (const Handle(AIS_InteractiveObject)& theObj,
1100                                                  const Standard_Integer               theMode)
1101 {
1102   if (!myMainPM->IsImmediateModeOn())
1103   {
1104     return Standard_False;
1105   }
1106
1107   myMainPM->AddToImmediateList (myMainPM->Presentation (theObj, theMode)->Presentation());
1108   return Standard_True;
1109 }
1110
1111 //=======================================================================
1112 //function : EndImmediateDraw
1113 //purpose  :
1114 //=======================================================================
1115 Standard_Boolean AIS_LocalContext::EndImmediateDraw (const Handle(V3d_Viewer)& theViewer)
1116 {
1117   if (!myMainPM->IsImmediateModeOn())
1118   {
1119     return Standard_False;
1120   }
1121
1122   myMainPM->EndImmediateDraw (theViewer);
1123   return Standard_True;
1124 }
1125
1126 // =======================================================================
1127 // function : ClearImmediateDraw
1128 // purpose  :
1129 // =======================================================================
1130 void AIS_LocalContext::ClearImmediateDraw()
1131 {
1132   myMainPM->ClearImmediateDraw();
1133 }
1134
1135 //=======================================================================
1136 //function : IsImmediateModeOn
1137 //purpose  :
1138 //=======================================================================
1139 Standard_Boolean AIS_LocalContext::IsImmediateModeOn() const
1140 {
1141   return myMainPM->IsImmediateModeOn();
1142 }
1143
1144 void AIS_LocalContext::SetPixelTolerance(const Standard_Integer aPrecision) {
1145
1146   myMainVS->SetPixelTolerance(aPrecision);
1147 }
1148
1149 Standard_Integer AIS_LocalContext::PixelTolerance() const {
1150
1151   return myMainVS->PixelTolerance();
1152 }
1153
1154 //=======================================================================
1155 //function : SetSelectionSensitivity
1156 //purpose  : Allows to manage sensitivity of a particular selection of interactive object theObject
1157 //=======================================================================
1158 void AIS_LocalContext::SetSelectionSensitivity (const Handle(AIS_InteractiveObject)& theObject,
1159                                                 const Standard_Integer theMode,
1160                                                 const Standard_Integer theNewSensitivity)
1161 {
1162   mySM->SetSelectionSensitivity (theObject, theMode, theNewSensitivity);
1163 }