Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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         const TopoDS_Shape& aShape = aShapePrs->Shape();
221         std::shared_ptr<GeomAPI_Shape> anAISShapePtr(new GeomAPI_Shape());
222         anAISShapePtr->setImpl(new TopoDS_Shape(aShape));
223
224         if (aShapePtr->isEqual(anAISShapePtr))
225           return;
226       }
227     }
228     aContext->Redisplay(aAISIO, false);
229     if (isUpdateViewer)
230       updateViewer();
231   }
232 }
233
234 void XGUI_Displayer::deactivate(ObjectPtr theObject)
235 {
236   if (isVisible(theObject)) {
237     Handle(AIS_InteractiveContext) aContext = AISContext();
238     if (aContext.IsNull())
239       return;
240
241     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
242     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
243     aContext->Deactivate(anAIS);
244     qDebug("### Deactivate obj %i", (long)anAIS.Access());
245   }
246 }
247
248 void XGUI_Displayer::activate(ObjectPtr theFeature)
249 {
250   activate(theFeature, myActiveSelectionModes);
251 }
252
253 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
254 {
255   if (isVisible(theObject)) {
256     Handle(AIS_InteractiveContext) aContext = AISContext();
257     if (aContext.IsNull())
258       return;
259
260     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
261     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
262     aContext->Deactivate(anAIS);
263     aContext->Load(anAIS, -1, true);
264     // In order to clear active modes list
265     if (theModes.size() > 0) {
266       foreach(int aMode, theModes) {
267         //aContext->Load(anAIS, aMode, true);
268         aContext->Activate(anAIS, aMode);
269         qDebug("### 1. Activate obj %i, %i", (long)anAIS.Access(), aMode);
270       }
271     } else {
272       //aContext->Load(anAIS, 0, true);
273       aContext->Activate(anAIS);
274       qDebug("### 2. Activate obj %i", (long)anAIS.Access());
275     }
276   }
277 }
278
279 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
280 {
281   if (!isVisible(theObject))
282     return;
283
284   Handle(AIS_InteractiveContext) aContext = AISContext();
285   if (aContext.IsNull())
286     return;
287
288   AISObjectPtr aAISObj = getAISObject(theObject);
289
290   if (aAISObj.get() != NULL) {
291     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
292     TColStd_ListOfInteger aTColModes;
293     aContext->ActivatedModes(anAISIO, aTColModes);
294     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
295     for (; itr.More(); itr.Next() ) {
296       theModes.append(itr.Value());
297     }
298   }
299 }
300
301 void XGUI_Displayer::activateObjects(const QIntList& theModes)
302 {
303   // In order to avoid doblications of selection modes
304   QIntList aNewModes;
305   foreach (int aMode, theModes) {
306     if (!aNewModes.contains(aMode))
307       aNewModes.append(aMode);
308   }
309   myActiveSelectionModes = aNewModes;
310   Handle(AIS_InteractiveContext) aContext = AISContext();
311   // Open local context if there is no one
312   if (!aContext->HasOpenedContext()) 
313     return;
314
315   //aContext->UseDisplayedObjects();
316   //myUseExternalObjects = true;
317
318   AIS_ListOfInteractive aPrsList;
319   ::displayedObjects(aContext, aPrsList);
320
321   Handle(AIS_Trihedron) aTrihedron;
322   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
323   Handle(AIS_InteractiveObject) anAISIO;
324   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
325     anAISIO = aLIt.Value();
326     aContext->Load(anAISIO, -1, true);
327     aContext->Deactivate(anAISIO);
328     aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
329     //Deactivate trihedron which can be activated in local selector
330     if (aTrihedron.IsNull()) {
331       //aContext->Load(anAISIO, -1, true);
332       // In order to clear active modes list
333       if (myActiveSelectionModes.size() == 0) {
334         //aContext->Load(anAISIO, 0, true);
335         aContext->Activate(anAISIO);
336         qDebug("### 2. Activate all %i", (long)anAISIO.Access());
337       } else {
338         foreach(int aMode, myActiveSelectionModes) {
339           //aContext->Load(anAISIO, aMode, true);
340           aContext->Activate(anAISIO, aMode);
341           qDebug("### 1. Activate all %i, %i", (long)anAISIO.Access(), aMode);
342         }
343       }
344     }
345   }
346 }
347
348
349 void XGUI_Displayer::deactivateObjects()
350 {
351   myActiveSelectionModes.clear();
352   Handle(AIS_InteractiveContext) aContext = AISContext();
353   // Open local context if there is no one
354   if (!aContext->HasOpenedContext()) 
355     return;
356
357   //aContext->NotUseDisplayedObjects();
358   AIS_ListOfInteractive aPrsList;
359   ::displayedObjects(aContext, aPrsList);
360
361   AIS_ListIteratorOfListOfInteractive aLIt;
362   //Handle(AIS_Trihedron) aTrihedron;
363   Handle(AIS_InteractiveObject) anAISIO;
364   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
365     anAISIO = aLIt.Value();
366     aContext->Deactivate(anAISIO);
367     //aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
368     //if (aTrihedron.IsNull()) {
369     //  qDebug("### Deactivate all %i", (long)anAISIO.Access());
370     //  //aContext->Activate(anAISIO);
371     //}
372   }
373 }
374
375 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
376 {
377   Handle(AIS_InteractiveContext) aContext = AISContext();
378   if (aContext.IsNull())
379     return false;
380   if (!isVisible(theObject))
381     return false;
382     
383   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
384   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
385
386   TColStd_ListOfInteger aModes;
387   aContext->ActivatedModes(anAIS, aModes);
388   return aModes.Extent() > 0;
389 }
390
391 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
392 {
393   Handle(AIS_InteractiveContext) aContext = AISContext();
394   if (aContext.IsNull())
395     return;
396   if (aContext->HasOpenedContext()) {
397     aContext->UnhilightSelected();
398     aContext->ClearSelected();
399     foreach(ObjectPtr aResult, theResults) {
400       if (isVisible(aResult)) {
401         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
402         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
403         if (!anAIS.IsNull())
404           aContext->SetSelected(anAIS, false);
405       }
406     }
407   } else {
408     aContext->UnhilightCurrents();
409     aContext->ClearCurrents();
410     foreach(ObjectPtr aResult, theResults) {
411       if (isVisible(aResult)) {
412         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
413         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
414         if (!anAIS.IsNull())
415           aContext->SetCurrentObject(anAIS, false);
416       }
417     }
418   }
419   if (isUpdateViewer)
420     updateViewer();
421 }
422
423
424 void XGUI_Displayer::clearSelected()
425 {
426   Handle(AIS_InteractiveContext) aContext = AISContext();
427   if (aContext) {
428     aContext->UnhilightCurrents(false);
429     aContext->ClearSelected();
430   }
431 }
432
433 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
434 {
435   Handle(AIS_InteractiveContext) aContext = AISContext();
436   if (aContext.IsNull())
437     return;
438
439    foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
440      // erase an object
441      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
442      if (!anIO.IsNull())
443        aContext->Remove(anIO, false);
444    }
445    myResult2AISObjectMap.clear();
446    if (isUpdateViewer)
447      updateViewer();
448  }
449
450 void XGUI_Displayer::openLocalContext()
451 {
452   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
453   if (aContext.IsNull())
454     return;
455   // Open local context if there is no one
456   if (!aContext->HasOpenedContext()) {
457     // Preserve selected objects
458     //AIS_ListOfInteractive aAisList;
459     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
460     //  aAisList.Append(aContext->Current());
461
462     // get the filters from the global context and append them to the local context
463     // a list of filters in the global context is not cleared and should be cleared here
464     SelectMgr_ListOfFilter aFilters;
465     aFilters.Assign(aContext->Filters());
466     // it is important to remove the filters in the global context, because there is a code
467     // in the closeLocalContex, which restore the global context filters
468     aContext->RemoveFilters();
469
470     //aContext->ClearCurrents();
471     aContext->OpenLocalContext();
472     qDebug("### Open context");
473     //aContext->NotUseDisplayedObjects();
474
475     //myUseExternalObjects = false;
476
477     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
478     for (;aIt.More(); aIt.Next()) {
479       aContext->AddFilter(aIt.Value());
480     }
481     // Restore selection
482     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
483     //for(; aIt2.More(); aIt2.Next()) {
484     //  aContext->SetSelected(aIt2.Value(), false);
485     //}
486   }
487 }
488
489 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
490 {
491   Handle(AIS_InteractiveContext) aContext = AISContext();
492   if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
493     // Preserve selected objects
494     //AIS_ListOfInteractive aAisList;
495     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
496     //  aAisList.Append(aContext->SelectedInteractive());
497
498     // get the filters from the local context and append them to the global context
499     // a list of filters in the local context is cleared
500     SelectMgr_ListOfFilter aFilters;
501     aFilters.Assign(aContext->Filters());
502
503     //aContext->ClearSelected();
504     aContext->CloseAllContexts(false);
505     qDebug("### Close context");
506
507     // Redisplay all object if they were displayed in localContext
508     Handle(AIS_InteractiveObject) aAISIO;
509     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
510       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
511       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
512         aContext->Display(aAISIO, false);
513         aContext->SetDisplayMode(aAISIO, Shading, false);
514       }
515     }
516
517     // Append the filters from the local selection in the global selection context
518     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
519     for (;aIt.More(); aIt.Next()) {
520       Handle(SelectMgr_Filter) aFilter = aIt.Value();
521       aContext->AddFilter(aFilter);
522     }
523
524     if (isUpdateViewer)
525       updateViewer();
526     //myUseExternalObjects = false;
527
528     // Restore selection
529     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
530     //for(; aIt2.More(); aIt2.Next()) {
531     //  if (aContext->IsDisplayed(aIt2.Value()))
532     //    aContext->SetCurrentObject(aIt2.Value(), false);
533     //}
534   }
535 }
536
537 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
538 {
539   AISObjectPtr anIO;
540   if (myResult2AISObjectMap.contains(theObject))
541     anIO = myResult2AISObjectMap[theObject];
542   return anIO;
543 }
544
545 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
546 {
547   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
548   return getObject(aRefAIS);
549 }
550
551 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
552 {
553   ObjectPtr aFeature;
554   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
555     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
556     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
557     if (anAIS == theIO)
558       return anObj;
559   }
560   return aFeature;
561 }
562
563 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
564 {
565   bool aWasEnabled = myEnableUpdateViewer;
566
567   myEnableUpdateViewer = isEnabled;
568
569   return aWasEnabled;
570 }
571
572 void XGUI_Displayer::updateViewer()
573 {
574   Handle(AIS_InteractiveContext) aContext = AISContext();
575   if (!aContext.IsNull() && myEnableUpdateViewer)
576     aContext->UpdateCurrentViewer();
577 }
578
579 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
580 {
581   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
582   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
583     aContext->OpenLocalContext();
584     qDebug("### Open context");
585   }
586   return aContext;
587 }
588
589 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
590 {
591   Handle(AIS_InteractiveContext) aContext = AISContext();
592   if (myAndFilter.IsNull() && !aContext.IsNull()) {
593     myAndFilter = new SelectMgr_AndFilter();
594     aContext->AddFilter(myAndFilter);
595   }
596   return myAndFilter;
597 }
598
599 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
600 {
601   Handle(AIS_InteractiveContext) aContext = AISContext();
602   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
603   if (!anAISIO.IsNull()) {
604     aContext->Display(anAISIO, isUpdate);
605     if (aContext->HasOpenedContext()) {
606       //if (myUseExternalObjects) {
607         if (myActiveSelectionModes.size() == 0)
608           aContext->Activate(anAISIO);
609         else {
610           foreach(int aMode, myActiveSelectionModes) {
611             aContext->Activate(anAISIO, aMode);
612           }
613         }
614       //}
615     }
616   }
617 }
618
619 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
620 {
621   Handle(AIS_InteractiveContext) aContext = AISContext();
622   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
623   if (!anAISIO.IsNull()) {
624     aContext->Remove(anAISIO, isUpdate);
625   }
626 }
627
628
629 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
630 {
631   if (theMode == NoMode)
632     return;
633
634   Handle(AIS_InteractiveContext) aContext = AISContext();
635   if (aContext.IsNull())
636     return;
637
638   AISObjectPtr aAISObj = getAISObject(theObject);
639   if (!aAISObj)
640     return;
641
642   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
643   bool aCanBeShaded = ::canBeShaded(aAISIO);
644   // In order to avoid extra closing/opening context
645   SelectMgr_IndexedMapOfOwner aSelectedOwners;
646   if (aCanBeShaded) {
647     myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
648     closeLocalContexts(false);
649   }
650   aContext->SetDisplayMode(aAISIO, theMode, false);
651   if (aCanBeShaded) {
652     openLocalContext();
653     activateObjects(myActiveSelectionModes);
654     myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
655   }
656   if (toUpdate)
657     updateViewer();
658 }
659
660 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
661 {
662   Handle(AIS_InteractiveContext) aContext = AISContext();
663   if (aContext.IsNull())
664     return NoMode;
665
666   AISObjectPtr aAISObj = getAISObject(theObject);
667   if (!aAISObj)
668     return NoMode;
669
670   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
671   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
672 }
673
674 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
675 {
676   Handle(AIS_InteractiveContext) aContext = AISContext();
677   if (aContext.IsNull())
678     return;
679   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
680   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
681   for (; aIt.More(); aIt.Next()) {
682     if (theFilter.Access() == aIt.Value().Access())
683       return;
684   }
685   GetFilter()->Add(theFilter);
686 }
687
688 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
689 {
690   Handle(AIS_InteractiveContext) aContext = AISContext();
691   if (aContext.IsNull())
692     return;
693   GetFilter()->Remove(theFilter);
694 }
695
696 void XGUI_Displayer::removeFilters()
697 {
698   Handle(AIS_InteractiveContext) aContext = AISContext();
699   if (aContext.IsNull())
700     return;
701   GetFilter()->Clear();
702 }
703
704 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
705 {
706   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
707   foreach(ObjectPtr aObj, aDispList) {
708     if (!theList.contains(aObj))
709       erase(aObj, false);
710   }
711   foreach(ObjectPtr aObj, theList) {
712     if (!isVisible(aObj))
713       display(aObj, false);
714   }
715   updateViewer();
716 }
717
718 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
719
720   if (!isVisible(theObject))
721     return false;
722
723   AISObjectPtr aAISObj = getAISObject(theObject);
724   if (aAISObj.get() == NULL)
725     return false;
726
727   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
728   return ::canBeShaded(anAIS);
729 }