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