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