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