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