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