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