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