]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Fix for crash of double-click in one point during creation of linear segment in a...
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_Displayer.cpp
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "XGUI_Displayer.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_ViewerProxy.h"
10 #include "XGUI_SelectionMgr.h"
11 #include "XGUI_Selection.h"
12
13 #include <AppElements_Viewer.h>
14
15 #include <ModelAPI_Document.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_Tools.h>
19
20 #include <ModuleBase_ResultPrs.h>
21
22 #include <GeomAPI_Shape.h>
23 #include <GeomAPI_IPresentable.h>
24 #include <GeomAPI_ICustomPrs.h>
25
26 #include <AIS_InteractiveContext.hxx>
27 #include <AIS_LocalContext.hxx>
28 #include <AIS_ListOfInteractive.hxx>
29 #include <AIS_ListIteratorOfListOfInteractive.hxx>
30 #include <AIS_DimensionSelectionMode.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_Dimension.hxx>
33 #include <TColStd_ListIteratorOfListOfInteger.hxx>
34 #include <SelectMgr_ListOfFilter.hxx>
35 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
36
37 #include <TColStd_MapOfTransient.hxx>
38 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
39
40 #include <set>
41
42 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse selection sensitivity
43
44
45 // Workaround for bug #25637
46 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
47 {
48   // Get from null point
49   theAIS->DisplayedObjects(theList, true);
50   if (theAIS->HasOpenedContext()) {
51     // get from local context
52     const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
53     TColStd_MapOfTransient aMap;
54     int NbDisp = aLC->DisplayedObjects(aMap);
55     TColStd_MapIteratorOfMapOfTransient aIt(aMap);
56
57     Handle(AIS_InteractiveObject) curIO;
58     Handle(Standard_Transient) Tr;
59     for(; aIt.More(); aIt.Next()){
60       Tr = aIt.Key();
61       curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
62       theList.Append(curIO);
63     }
64   }
65 }
66
67
68 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
69   : myWorkshop(theWorkshop)
70 {
71   enableUpdateViewer(true);
72 }
73
74 XGUI_Displayer::~XGUI_Displayer()
75 {
76 }
77
78 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
79 {
80   return myResult2AISObjectMap.contains(theObject);
81 }
82
83 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
84 {
85   if (isVisible(theObject)) {
86     redisplay(theObject, isUpdateViewer);
87   } else {
88     AISObjectPtr anAIS;
89
90     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
91     bool isShading = false;
92     if (aPrs.get() != NULL) {
93       anAIS = aPrs->getAISObject(AISObjectPtr());
94     } else {
95       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
96       if (aResult.get() != NULL) {
97         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
98         if (aShapePtr.get() != NULL) {
99           anAIS = AISObjectPtr(new GeomAPI_AISObject());
100           anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
101           //anAIS->createShape(aShapePtr);
102           isShading = true;
103         }
104       }
105     }
106     if (anAIS)
107       display(theObject, anAIS, isShading, isUpdateViewer);
108   }
109 }
110
111 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS)
112 {
113   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
114   if (!aShapePrs.IsNull()) {
115     TopoDS_Shape aShape = aShapePrs->Shape();
116     TopAbs_ShapeEnum aType = aShape.ShapeType();
117     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
118       return false;
119     else {
120       // Check that the presentation is not a sketch
121       Handle(ModuleBase_ResultPrs) aPrs = Handle(ModuleBase_ResultPrs)::DownCast(theAIS);
122       if (!aPrs.IsNull()) 
123         return !aPrs->isSketchMode();
124       return true;
125     }
126   }
127   return false;
128 }
129
130 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS, 
131                              bool isShading, bool isUpdateViewer)
132 {
133   Handle(AIS_InteractiveContext) aContext = AISContext();
134   if (aContext.IsNull())
135     return;
136
137   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
138   if (!anAISIO.IsNull()) {
139     myResult2AISObjectMap[theObject] = theAIS;
140     bool aCanBeShaded = ::canBeShaded(anAISIO);
141     // In order to avoid extra closing/opening context
142     SelectMgr_IndexedMapOfOwner aSelectedOwners;
143     if (aCanBeShaded) {
144       myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
145       closeLocalContexts(false);
146     }
147     aContext->Display(anAISIO, false);
148     qDebug("### Display %i", (long)anAISIO.Access());
149
150     aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
151     // Customization of presentation
152     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
153     if (aFeature.get() != NULL) {
154       GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
155       if (aCustPrs.get() != NULL)
156         aCustPrs->customisePresentation(theAIS);
157     }
158     if (aCanBeShaded) {
159       openLocalContext();
160       activateObjects(myActiveSelectionModes);
161       myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
162     }
163   }
164   if (isUpdateViewer)
165     updateViewer();
166 }
167
168 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
169 {
170   if (!isVisible(theObject))
171     return;
172
173   Handle(AIS_InteractiveContext) aContext = AISContext();
174   if (aContext.IsNull())
175     return;
176   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
177   if (anObject) {
178     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
179     if (!anAIS.IsNull()) {
180       aContext->Remove(anAIS, isUpdateViewer);
181     }
182   }
183   myResult2AISObjectMap.remove(theObject);
184 }
185
186 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
187 {
188   if (!isVisible(theObject))
189     return;
190
191   AISObjectPtr aAISObj = getAISObject(theObject);
192   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
193
194   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
195   if (aPrs) {
196     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
197     if (!aAIS_Obj) {
198       erase(theObject, isUpdateViewer);
199       return;
200     }
201     if (aAIS_Obj != aAISObj) {
202       myResult2AISObjectMap[theObject] = aAIS_Obj;
203     }
204     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
205   }
206
207   if (!aAISIO.IsNull()) {
208     Handle(AIS_InteractiveContext) aContext = AISContext();
209     if (aContext.IsNull())
210       return;
211     // Check that the visualized shape is the same and the redisplay is not necessary
212     // Redisplay of AIS object leads to this object selection compute and the selection 
213     // in the browser is lost
214     // become
215     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
216     if (aResult.get() != NULL) {
217       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
218       if (!aShapePrs.IsNull()) {
219         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
220         if (aShapePtr.get()) {
221           const TopoDS_Shape& aShape = aShapePrs->Shape();
222           std::shared_ptr<GeomAPI_Shape> anAISShapePtr(new GeomAPI_Shape());
223           anAISShapePtr->setImpl(new TopoDS_Shape(aShape));
224
225           if (aShapePtr->isEqual(anAISShapePtr))
226             return;
227         }
228       }
229     }
230     aContext->Redisplay(aAISIO, false);
231     if (isUpdateViewer)
232       updateViewer();
233   }
234 }
235
236 void XGUI_Displayer::deactivate(ObjectPtr theObject)
237 {
238   if (isVisible(theObject)) {
239     Handle(AIS_InteractiveContext) aContext = AISContext();
240     if (aContext.IsNull())
241       return;
242
243     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
244     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
245     aContext->Deactivate(anAIS);
246     qDebug("### Deactivate obj %i", (long)anAIS.Access());
247   }
248 }
249
250 void XGUI_Displayer::activate(ObjectPtr theFeature)
251 {
252   activate(theFeature, myActiveSelectionModes);
253 }
254
255 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
256 {
257   if (isVisible(theObject)) {
258     Handle(AIS_InteractiveContext) aContext = AISContext();
259     if (aContext.IsNull())
260       return;
261
262     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
263     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
264     aContext->Deactivate(anAIS);
265     aContext->Load(anAIS, -1, true);
266     // In order to clear active modes list
267     if (theModes.size() > 0) {
268       foreach(int aMode, theModes) {
269         //aContext->Load(anAIS, aMode, true);
270         aContext->Activate(anAIS, aMode);
271         qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
272       }
273     } else {
274       //aContext->Load(anAIS, 0, true);
275       aContext->Activate(anAIS);
276       qDebug("### 2. Activate obj %i", (long)anAIS.Access());
277     }
278   }
279 }
280
281 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
282 {
283   if (!isVisible(theObject))
284     return;
285
286   Handle(AIS_InteractiveContext) aContext = AISContext();
287   if (aContext.IsNull())
288     return;
289
290   AISObjectPtr aAISObj = getAISObject(theObject);
291
292   if (aAISObj.get() != NULL) {
293     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
294     TColStd_ListOfInteger aTColModes;
295     aContext->ActivatedModes(anAISIO, aTColModes);
296     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
297     for (; itr.More(); itr.Next() ) {
298       theModes.append(itr.Value());
299     }
300   }
301 }
302
303 void XGUI_Displayer::activateObjects(const QIntList& theModes)
304 {
305   // In order to avoid doblications of selection modes
306   QIntList aNewModes;
307   foreach (int aMode, theModes) {
308     if (!aNewModes.contains(aMode))
309       aNewModes.append(aMode);
310   }
311   myActiveSelectionModes = aNewModes;
312   Handle(AIS_InteractiveContext) aContext = AISContext();
313   // Open local context if there is no one
314   if (!aContext->HasOpenedContext()) 
315     return;
316
317   //aContext->UseDisplayedObjects();
318   //myUseExternalObjects = true;
319
320   AIS_ListOfInteractive aPrsList;
321   ::displayedObjects(aContext, aPrsList);
322
323   Handle(AIS_Trihedron) aTrihedron;
324   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
325   Handle(AIS_InteractiveObject) anAISIO;
326   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
327     anAISIO = aLIt.Value();
328     aContext->Load(anAISIO, -1, true);
329     aContext->Deactivate(anAISIO);
330     aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
331     //Deactivate trihedron which can be activated in local selector
332     if (aTrihedron.IsNull()) {
333       //aContext->Load(anAISIO, -1, true);
334       // In order to clear active modes list
335       if (myActiveSelectionModes.size() == 0) {
336         //aContext->Load(anAISIO, 0, true);
337         aContext->Activate(anAISIO);
338         qDebug("### 2. Activate all %i", (long)anAISIO.Access());
339       } else {
340         foreach(int aMode, myActiveSelectionModes) {
341           //aContext->Load(anAISIO, aMode, true);
342           aContext->Activate(anAISIO, aMode);
343           qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
344         }
345       }
346     }
347   }
348 }
349
350
351 void XGUI_Displayer::deactivateObjects()
352 {
353   myActiveSelectionModes.clear();
354   Handle(AIS_InteractiveContext) aContext = AISContext();
355   // Open local context if there is no one
356   if (!aContext->HasOpenedContext()) 
357     return;
358
359   //aContext->NotUseDisplayedObjects();
360   AIS_ListOfInteractive aPrsList;
361   ::displayedObjects(aContext, aPrsList);
362
363   AIS_ListIteratorOfListOfInteractive aLIt;
364   //Handle(AIS_Trihedron) aTrihedron;
365   Handle(AIS_InteractiveObject) anAISIO;
366   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
367     anAISIO = aLIt.Value();
368     aContext->Deactivate(anAISIO);
369     //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
370     //if (aTrihedron.IsNull()) {
371     //  qDebug("### Deactivate all %i", (long)anAISIO.Access());
372     //  //aContext->Activate(anAISIO);
373     //}
374   }
375 }
376
377 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
378 {
379   Handle(AIS_InteractiveContext) aContext = AISContext();
380   if (aContext.IsNull())
381     return false;
382   if (!isVisible(theObject))
383     return false;
384     
385   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
386   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
387
388   TColStd_ListOfInteger aModes;
389   aContext->ActivatedModes(anAIS, aModes);
390   return aModes.Extent() > 0;
391 }
392
393 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
394 {
395   Handle(AIS_InteractiveContext) aContext = AISContext();
396   if (aContext.IsNull())
397     return;
398   if (aContext->HasOpenedContext()) {
399     aContext->UnhilightSelected();
400     aContext->ClearSelected();
401     foreach(ObjectPtr aResult, theResults) {
402       if (isVisible(aResult)) {
403         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
404         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
405         if (!anAIS.IsNull())
406           aContext->SetSelected(anAIS, false);
407       }
408     }
409   } else {
410     aContext->UnhilightCurrents();
411     aContext->ClearCurrents();
412     foreach(ObjectPtr aResult, theResults) {
413       if (isVisible(aResult)) {
414         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
415         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
416         if (!anAIS.IsNull())
417           aContext->SetCurrentObject(anAIS, false);
418       }
419     }
420   }
421   if (isUpdateViewer)
422     updateViewer();
423 }
424
425
426 void XGUI_Displayer::clearSelected()
427 {
428   Handle(AIS_InteractiveContext) aContext = AISContext();
429   if (aContext) {
430     aContext->UnhilightCurrents(false);
431     aContext->ClearSelected();
432   }
433 }
434
435 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
436 {
437   Handle(AIS_InteractiveContext) aContext = AISContext();
438   if (aContext.IsNull())
439     return;
440
441    foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
442      // erase an object
443      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
444      if (!anIO.IsNull())
445        aContext->Remove(anIO, false);
446    }
447    myResult2AISObjectMap.clear();
448    if (isUpdateViewer)
449      updateViewer();
450  }
451
452 void XGUI_Displayer::openLocalContext()
453 {
454   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
455   if (aContext.IsNull())
456     return;
457   // Open local context if there is no one
458   if (!aContext->HasOpenedContext()) {
459     // Preserve selected objects
460     //AIS_ListOfInteractive aAisList;
461     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
462     //  aAisList.Append(aContext->Current());
463
464     // get the filters from the global context and append them to the local context
465     // a list of filters in the global context is not cleared and should be cleared here
466     SelectMgr_ListOfFilter aFilters;
467     aFilters.Assign(aContext->Filters());
468     // it is important to remove the filters in the global context, because there is a code
469     // in the closeLocalContex, which restore the global context filters
470     aContext->RemoveFilters();
471
472     //aContext->ClearCurrents();
473     aContext->OpenLocalContext();
474     qDebug("### Open context");
475     //aContext->NotUseDisplayedObjects();
476
477     //myUseExternalObjects = false;
478
479     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
480     for (;aIt.More(); aIt.Next()) {
481       aContext->AddFilter(aIt.Value());
482     }
483     // Restore selection
484     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
485     //for(; aIt2.More(); aIt2.Next()) {
486     //  aContext->SetSelected(aIt2.Value(), false);
487     //}
488   }
489 }
490
491 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
492 {
493   Handle(AIS_InteractiveContext) aContext = AISContext();
494   if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
495     // Preserve selected objects
496     //AIS_ListOfInteractive aAisList;
497     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
498     //  aAisList.Append(aContext->SelectedInteractive());
499
500     // get the filters from the local context and append them to the global context
501     // a list of filters in the local context is cleared
502     SelectMgr_ListOfFilter aFilters;
503     aFilters.Assign(aContext->Filters());
504
505     //aContext->ClearSelected();
506     aContext->CloseAllContexts(false);
507     qDebug("### Close context");
508
509     // Redisplay all object if they were displayed in localContext
510     Handle(AIS_InteractiveObject) aAISIO;
511     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
512       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
513       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
514         aContext->Display(aAISIO, false);
515         aContext->SetDisplayMode(aAISIO, Shading, false);
516       }
517     }
518
519     // Append the filters from the local selection in the global selection context
520     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
521     for (;aIt.More(); aIt.Next()) {
522       Handle(SelectMgr_Filter) aFilter = aIt.Value();
523       aContext->AddFilter(aFilter);
524     }
525
526     if (isUpdateViewer)
527       updateViewer();
528     //myUseExternalObjects = false;
529
530     // Restore selection
531     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
532     //for(; aIt2.More(); aIt2.Next()) {
533     //  if (aContext->IsDisplayed(aIt2.Value()))
534     //    aContext->SetCurrentObject(aIt2.Value(), false);
535     //}
536   }
537 }
538
539 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
540 {
541   AISObjectPtr anIO;
542   if (myResult2AISObjectMap.contains(theObject))
543     anIO = myResult2AISObjectMap[theObject];
544   return anIO;
545 }
546
547 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
548 {
549   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
550   return getObject(aRefAIS);
551 }
552
553 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
554 {
555   ObjectPtr aFeature;
556   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
557     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
558     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
559     if (anAIS == theIO)
560       return anObj;
561   }
562   return aFeature;
563 }
564
565 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
566 {
567   bool aWasEnabled = myEnableUpdateViewer;
568
569   myEnableUpdateViewer = isEnabled;
570
571   return aWasEnabled;
572 }
573
574 void XGUI_Displayer::updateViewer()
575 {
576   Handle(AIS_InteractiveContext) aContext = AISContext();
577   if (!aContext.IsNull() && myEnableUpdateViewer)
578     aContext->UpdateCurrentViewer();
579 }
580
581 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
582 {
583   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
584   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
585     aContext->OpenLocalContext();
586     qDebug("### Open context");
587   }
588   return aContext;
589 }
590
591 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
592 {
593   Handle(AIS_InteractiveContext) aContext = AISContext();
594   if (myAndFilter.IsNull() && !aContext.IsNull()) {
595     myAndFilter = new SelectMgr_AndFilter();
596     aContext->AddFilter(myAndFilter);
597   }
598   return myAndFilter;
599 }
600
601 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
602 {
603   Handle(AIS_InteractiveContext) aContext = AISContext();
604   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
605   if (!anAISIO.IsNull()) {
606     aContext->Display(anAISIO, isUpdate);
607     if (aContext->HasOpenedContext()) {
608       //if (myUseExternalObjects) {
609         if (myActiveSelectionModes.size() == 0)
610           aContext->Activate(anAISIO);
611         else {
612           foreach(int aMode, myActiveSelectionModes) {
613             aContext->Activate(anAISIO, aMode);
614           }
615         }
616       //}
617     }
618   }
619 }
620
621 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
622 {
623   Handle(AIS_InteractiveContext) aContext = AISContext();
624   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
625   if (!anAISIO.IsNull()) {
626     aContext->Remove(anAISIO, isUpdate);
627   }
628 }
629
630
631 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
632 {
633   if (theMode == NoMode)
634     return;
635
636   Handle(AIS_InteractiveContext) aContext = AISContext();
637   if (aContext.IsNull())
638     return;
639
640   AISObjectPtr aAISObj = getAISObject(theObject);
641   if (!aAISObj)
642     return;
643
644   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
645   bool aCanBeShaded = ::canBeShaded(aAISIO);
646   // In order to avoid extra closing/opening context
647   SelectMgr_IndexedMapOfOwner aSelectedOwners;
648   if (aCanBeShaded) {
649     myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
650     closeLocalContexts(false);
651   }
652   aContext->SetDisplayMode(aAISIO, theMode, false);
653   if (aCanBeShaded) {
654     openLocalContext();
655     activateObjects(myActiveSelectionModes);
656     myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
657   }
658   if (toUpdate)
659     updateViewer();
660 }
661
662 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
663 {
664   Handle(AIS_InteractiveContext) aContext = AISContext();
665   if (aContext.IsNull())
666     return NoMode;
667
668   AISObjectPtr aAISObj = getAISObject(theObject);
669   if (!aAISObj)
670     return NoMode;
671
672   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
673   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
674 }
675
676 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
677 {
678   Handle(AIS_InteractiveContext) aContext = AISContext();
679   if (aContext.IsNull())
680     return;
681   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
682   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
683   for (; aIt.More(); aIt.Next()) {
684     if (theFilter.Access() == aIt.Value().Access())
685       return;
686   }
687   GetFilter()->Add(theFilter);
688 }
689
690 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
691 {
692   Handle(AIS_InteractiveContext) aContext = AISContext();
693   if (aContext.IsNull())
694     return;
695   GetFilter()->Remove(theFilter);
696 }
697
698 void XGUI_Displayer::removeFilters()
699 {
700   Handle(AIS_InteractiveContext) aContext = AISContext();
701   if (aContext.IsNull())
702     return;
703   GetFilter()->Clear();
704 }
705
706 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
707 {
708   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
709   foreach(ObjectPtr aObj, aDispList) {
710     if (!theList.contains(aObj))
711       erase(aObj, false);
712   }
713   foreach(ObjectPtr aObj, theList) {
714     if (!isVisible(aObj))
715       display(aObj, false);
716   }
717   updateViewer();
718 }
719
720 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
721
722   if (!isVisible(theObject))
723     return false;
724
725   AISObjectPtr aAISObj = getAISObject(theObject);
726   if (aAISObj.get() == NULL)
727     return false;
728
729   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
730   return ::canBeShaded(anAIS);
731 }