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