Salome HOME
cab65bc08482e14ab69d345d0552761245f01289
[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
149     aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
150     // Customization of presentation
151     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
152     if (aFeature.get() != NULL) {
153       GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
154       if (aCustPrs.get() != NULL)
155         aCustPrs->customisePresentation(theAIS);
156     }
157     if (aCanBeShaded) {
158       openLocalContext();
159       activateObjects(myActiveSelectionModes);
160       myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
161     }
162   }
163   if (isUpdateViewer)
164     updateViewer();
165 }
166
167 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
168 {
169   if (!isVisible(theObject))
170     return;
171
172   Handle(AIS_InteractiveContext) aContext = AISContext();
173   if (aContext.IsNull())
174     return;
175   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
176   if (anObject) {
177     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
178     if (!anAIS.IsNull()) {
179       aContext->Remove(anAIS, isUpdateViewer);
180     }
181   }
182   myResult2AISObjectMap.remove(theObject);
183 }
184
185 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
186 {
187   if (!isVisible(theObject))
188     return;
189
190   AISObjectPtr aAISObj = getAISObject(theObject);
191   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
192
193   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
194   if (aPrs) {
195     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
196     if (!aAIS_Obj) {
197       erase(theObject, isUpdateViewer);
198       return;
199     }
200     if (aAIS_Obj != aAISObj) {
201       myResult2AISObjectMap[theObject] = aAIS_Obj;
202     }
203     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
204   }
205
206   if (!aAISIO.IsNull()) {
207     Handle(AIS_InteractiveContext) aContext = AISContext();
208     if (aContext.IsNull())
209       return;
210     // Check that the visualized shape is the same and the redisplay is not necessary
211     // Redisplay of AIS object leads to this object selection compute and the selection 
212     // in the browser is lost
213     // become
214     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
215     if (aResult.get() != NULL) {
216       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
217       if (!aShapePrs.IsNull()) {
218         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
219         if (aShapePtr.get()) {
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     }
229     aContext->Redisplay(aAISIO, false);
230     if (isUpdateViewer)
231       updateViewer();
232   }
233 }
234
235 void XGUI_Displayer::deactivate(ObjectPtr theObject)
236 {
237   if (isVisible(theObject)) {
238     Handle(AIS_InteractiveContext) aContext = AISContext();
239     if (aContext.IsNull())
240       return;
241
242     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
243     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
244     aContext->Deactivate(anAIS);
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       }
270     } else {
271       //aContext->Load(anAIS, 0, true);
272       aContext->Activate(anAIS);
273     }
274   }
275 }
276
277 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
278 {
279   if (!isVisible(theObject))
280     return;
281
282   Handle(AIS_InteractiveContext) aContext = AISContext();
283   if (aContext.IsNull())
284     return;
285
286   AISObjectPtr aAISObj = getAISObject(theObject);
287
288   if (aAISObj.get() != NULL) {
289     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
290     TColStd_ListOfInteger aTColModes;
291     aContext->ActivatedModes(anAISIO, aTColModes);
292     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
293     for (; itr.More(); itr.Next() ) {
294       theModes.append(itr.Value());
295     }
296   }
297 }
298
299 void XGUI_Displayer::activateObjects(const QIntList& theModes)
300 {
301   // In order to avoid doblications of selection modes
302   QIntList aNewModes;
303   foreach (int aMode, theModes) {
304     if (!aNewModes.contains(aMode))
305       aNewModes.append(aMode);
306   }
307   myActiveSelectionModes = aNewModes;
308   Handle(AIS_InteractiveContext) aContext = AISContext();
309   if (aContext.IsNull())
310     return;
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       } else {
337         foreach(int aMode, myActiveSelectionModes) {
338           //aContext->Load(anAISIO, aMode, true);
339           aContext->Activate(anAISIO, aMode);
340         }
341       }
342     }
343   }
344 }
345
346
347 void XGUI_Displayer::deactivateObjects()
348 {
349   myActiveSelectionModes.clear();
350   Handle(AIS_InteractiveContext) aContext = AISContext();
351   // Open local context if there is no one
352   if (!aContext->HasOpenedContext()) 
353     return;
354
355   //aContext->NotUseDisplayedObjects();
356   AIS_ListOfInteractive aPrsList;
357   ::displayedObjects(aContext, aPrsList);
358
359   AIS_ListIteratorOfListOfInteractive aLIt;
360   //Handle(AIS_Trihedron) aTrihedron;
361   Handle(AIS_InteractiveObject) anAISIO;
362   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
363     anAISIO = aLIt.Value();
364     aContext->Deactivate(anAISIO);
365   }
366 }
367
368 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
369 {
370   Handle(AIS_InteractiveContext) aContext = AISContext();
371   if (aContext.IsNull())
372     return false;
373   if (!isVisible(theObject))
374     return false;
375     
376   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
377   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
378
379   TColStd_ListOfInteger aModes;
380   aContext->ActivatedModes(anAIS, aModes);
381   return aModes.Extent() > 0;
382 }
383
384 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
385 {
386   Handle(AIS_InteractiveContext) aContext = AISContext();
387   if (aContext.IsNull())
388     return;
389   if (aContext->HasOpenedContext()) {
390     aContext->UnhilightSelected();
391     aContext->ClearSelected();
392     foreach(ObjectPtr aResult, theResults) {
393       if (isVisible(aResult)) {
394         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
395         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
396         if (!anAIS.IsNull())
397           aContext->SetSelected(anAIS, false);
398       }
399     }
400   } else {
401     aContext->UnhilightCurrents();
402     aContext->ClearCurrents();
403     foreach(ObjectPtr aResult, theResults) {
404       if (isVisible(aResult)) {
405         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
406         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
407         if (!anAIS.IsNull())
408           aContext->SetCurrentObject(anAIS, false);
409       }
410     }
411   }
412   if (isUpdateViewer)
413     updateViewer();
414 }
415
416
417 void XGUI_Displayer::clearSelected()
418 {
419   Handle(AIS_InteractiveContext) aContext = AISContext();
420   if (aContext) {
421     aContext->UnhilightCurrents(false);
422     aContext->ClearSelected();
423   }
424 }
425
426 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
427 {
428   Handle(AIS_InteractiveContext) aContext = AISContext();
429   if (!aContext.IsNull()) {
430    foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
431      // erase an object
432      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
433      if (!anIO.IsNull())
434        aContext->Remove(anIO, false);
435    }
436    if (isUpdateViewer)
437      updateViewer();
438   }
439   myResult2AISObjectMap.clear();
440 }
441
442 void XGUI_Displayer::openLocalContext()
443 {
444   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
445   if (aContext.IsNull())
446     return;
447   // Open local context if there is no one
448   if (!aContext->HasOpenedContext()) {
449     // Preserve selected objects
450     //AIS_ListOfInteractive aAisList;
451     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
452     //  aAisList.Append(aContext->Current());
453
454     // get the filters from the global context and append them to the local context
455     // a list of filters in the global context is not cleared and should be cleared here
456     SelectMgr_ListOfFilter aFilters;
457     aFilters.Assign(aContext->Filters());
458     // it is important to remove the filters in the global context, because there is a code
459     // in the closeLocalContex, which restore the global context filters
460     aContext->RemoveFilters();
461
462     //aContext->ClearCurrents();
463     aContext->OpenLocalContext();
464     //aContext->NotUseDisplayedObjects();
465
466     //myUseExternalObjects = false;
467
468     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
469     for (;aIt.More(); aIt.Next()) {
470       aContext->AddFilter(aIt.Value());
471     }
472     // Restore selection
473     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
474     //for(; aIt2.More(); aIt2.Next()) {
475     //  aContext->SetSelected(aIt2.Value(), false);
476     //}
477   }
478 }
479
480 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
481 {
482   Handle(AIS_InteractiveContext) aContext = AISContext();
483   if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
484     // Preserve selected objects
485     //AIS_ListOfInteractive aAisList;
486     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
487     //  aAisList.Append(aContext->SelectedInteractive());
488
489     // get the filters from the local context and append them to the global context
490     // a list of filters in the local context is cleared
491     SelectMgr_ListOfFilter aFilters;
492     aFilters.Assign(aContext->Filters());
493
494     //aContext->ClearSelected();
495     aContext->CloseAllContexts(false);
496
497     // Redisplay all object if they were displayed in localContext
498     Handle(AIS_InteractiveObject) aAISIO;
499     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
500       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
501       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
502         aContext->Display(aAISIO, false);
503         aContext->SetDisplayMode(aAISIO, Shading, false);
504       }
505     }
506
507     // Append the filters from the local selection in the global selection context
508     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
509     for (;aIt.More(); aIt.Next()) {
510       Handle(SelectMgr_Filter) aFilter = aIt.Value();
511       aContext->AddFilter(aFilter);
512     }
513
514     if (isUpdateViewer)
515       updateViewer();
516     //myUseExternalObjects = false;
517
518     // Restore selection
519     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
520     //for(; aIt2.More(); aIt2.Next()) {
521     //  if (aContext->IsDisplayed(aIt2.Value()))
522     //    aContext->SetCurrentObject(aIt2.Value(), false);
523     //}
524   }
525 }
526
527 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
528 {
529   AISObjectPtr anIO;
530   if (myResult2AISObjectMap.contains(theObject))
531     anIO = myResult2AISObjectMap[theObject];
532   return anIO;
533 }
534
535 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
536 {
537   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
538   return getObject(aRefAIS);
539 }
540
541 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
542 {
543   ObjectPtr aFeature;
544   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
545     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
546     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
547     if (anAIS == theIO)
548       return anObj;
549   }
550   return aFeature;
551 }
552
553 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
554 {
555   bool aWasEnabled = myEnableUpdateViewer;
556
557   myEnableUpdateViewer = isEnabled;
558
559   return aWasEnabled;
560 }
561
562 void XGUI_Displayer::updateViewer()
563 {
564   Handle(AIS_InteractiveContext) aContext = AISContext();
565   if (!aContext.IsNull() && myEnableUpdateViewer)
566     aContext->UpdateCurrentViewer();
567 }
568
569 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
570 {
571   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
572   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
573     aContext->OpenLocalContext();
574   }
575   return aContext;
576 }
577
578 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
579 {
580   Handle(AIS_InteractiveContext) aContext = AISContext();
581   if (myAndFilter.IsNull() && !aContext.IsNull()) {
582     myAndFilter = new SelectMgr_AndFilter();
583     aContext->AddFilter(myAndFilter);
584   }
585   return myAndFilter;
586 }
587
588 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
589 {
590   Handle(AIS_InteractiveContext) aContext = AISContext();
591   if (aContext.IsNull())
592     return;
593   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
594   if (!anAISIO.IsNull()) {
595     aContext->Display(anAISIO, isUpdate);
596     if (aContext->HasOpenedContext()) {
597       //if (myUseExternalObjects) {
598         if (myActiveSelectionModes.size() == 0)
599           aContext->Activate(anAISIO);
600         else {
601           foreach(int aMode, myActiveSelectionModes) {
602             aContext->Activate(anAISIO, aMode);
603           }
604         }
605       //}
606     }
607   }
608 }
609
610 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
611 {
612   Handle(AIS_InteractiveContext) aContext = AISContext();
613   if (aContext.IsNull())
614     return;
615   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
616   if (!anAISIO.IsNull()) {
617     aContext->Remove(anAISIO, isUpdate);
618   }
619 }
620
621
622 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
623 {
624   if (theMode == NoMode)
625     return;
626
627   Handle(AIS_InteractiveContext) aContext = AISContext();
628   if (aContext.IsNull())
629     return;
630
631   AISObjectPtr aAISObj = getAISObject(theObject);
632   if (!aAISObj)
633     return;
634
635   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
636   bool aCanBeShaded = ::canBeShaded(aAISIO);
637   // In order to avoid extra closing/opening context
638   SelectMgr_IndexedMapOfOwner aSelectedOwners;
639   if (aCanBeShaded) {
640     myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
641     closeLocalContexts(false);
642   }
643   aContext->SetDisplayMode(aAISIO, theMode, false);
644   if (aCanBeShaded) {
645     openLocalContext();
646     activateObjects(myActiveSelectionModes);
647     myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
648   }
649   if (toUpdate)
650     updateViewer();
651 }
652
653 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
654 {
655   Handle(AIS_InteractiveContext) aContext = AISContext();
656   if (aContext.IsNull())
657     return NoMode;
658
659   AISObjectPtr aAISObj = getAISObject(theObject);
660   if (!aAISObj)
661     return NoMode;
662
663   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
664   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
665 }
666
667 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
668 {
669   Handle(AIS_InteractiveContext) aContext = AISContext();
670   if (aContext.IsNull())
671     return;
672   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
673   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
674   for (; aIt.More(); aIt.Next()) {
675     if (theFilter.Access() == aIt.Value().Access())
676       return;
677   }
678   GetFilter()->Add(theFilter);
679 }
680
681 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
682 {
683   Handle(AIS_InteractiveContext) aContext = AISContext();
684   if (aContext.IsNull())
685     return;
686   GetFilter()->Remove(theFilter);
687 }
688
689 void XGUI_Displayer::removeFilters()
690 {
691   Handle(AIS_InteractiveContext) aContext = AISContext();
692   if (aContext.IsNull())
693     return;
694   GetFilter()->Clear();
695 }
696
697 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
698 {
699   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
700   foreach(ObjectPtr aObj, aDispList) {
701     if (!theList.contains(aObj))
702       erase(aObj, false);
703   }
704   foreach(ObjectPtr aObj, theList) {
705     if (!isVisible(aObj))
706       display(aObj, false);
707   }
708   updateViewer();
709 }
710
711 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
712
713   if (!isVisible(theObject))
714     return false;
715
716   AISObjectPtr aAISObj = getAISObject(theObject);
717   if (aAISObj.get() == NULL)
718     return false;
719
720   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
721   return ::canBeShaded(anAIS);
722 }