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