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