Salome HOME
There is a problem with lost selection on an edge. Create a sketch, create two lines...
[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(anAIS);
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     if (isShading)
163       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
164     emit objectDisplayed(theObject, theAIS);
165
166     bool isCustomized = customizeObject(theObject);
167     if (isCustomized)
168       aContext->Redisplay(anAISIO, false);
169
170     if (aCanBeShaded) {
171       openLocalContext();
172       activateObjects(myActiveSelectionModes);
173       myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
174     }
175     else
176       activate(anAISIO, myActiveSelectionModes);
177  }
178   if (isUpdateViewer)
179     updateViewer();
180 }
181
182 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
183 {
184   if (!isVisible(theObject))
185     return;
186
187   Handle(AIS_InteractiveContext) aContext = AISContext();
188   if (aContext.IsNull())
189     return;
190   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
191   if (anObject) {
192     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
193     if (!anAIS.IsNull()) {
194       emit beforeObjectErase(theObject, anObject);
195       aContext->Remove(anAIS, isUpdateViewer);
196     }
197   }
198   myResult2AISObjectMap.remove(theObject);
199 }
200
201 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
202 {
203   if (!isVisible(theObject))
204     return;
205
206   AISObjectPtr aAISObj = getAISObject(theObject);
207   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
208
209   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
210   if (aPrs) {
211     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
212     if (!aAIS_Obj) {
213       erase(theObject, isUpdateViewer);
214       return;
215     }
216     if (aAIS_Obj != aAISObj) {
217       myResult2AISObjectMap[theObject] = aAIS_Obj;
218     }
219     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
220   }
221
222   if (!aAISIO.IsNull()) {
223     Handle(AIS_InteractiveContext) aContext = AISContext();
224     if (aContext.IsNull())
225       return;
226     // Check that the visualized shape is the same and the redisplay is not necessary
227     // Redisplay of AIS object leads to this object selection compute and the selection 
228     // in the browser is lost
229
230     // this check is not necessary anymore because the selection store/restore is realized
231     // before and after the values modification.
232     // Moreother, this check avoids customize and redisplay presentation if the presentable
233     // parameter is changed.
234     bool isEqualShapes = false;
235     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
236     if (aResult.get() != NULL) {
237       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
238       if (!aShapePrs.IsNull()) {
239         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
240         if (aShapePtr.get()) {
241           const TopoDS_Shape& aShape = aShapePrs->Shape();
242           std::shared_ptr<GeomAPI_Shape> anAISShapePtr(new GeomAPI_Shape());
243           anAISShapePtr->setImpl(new TopoDS_Shape(aShape));
244
245           isEqualShapes = aShapePtr->isEqual(anAISShapePtr);
246         }
247       }
248     }
249     // Customization of presentation
250     bool isCustomized = customizeObject(theObject);
251     if (!isEqualShapes || isCustomized) {
252       aContext->Redisplay(aAISIO, false);
253       if (isUpdateViewer)
254         updateViewer();
255     }
256   }
257 }
258
259 void XGUI_Displayer::deactivate(ObjectPtr theObject)
260 {
261   if (isVisible(theObject)) {
262     Handle(AIS_InteractiveContext) aContext = AISContext();
263     if (aContext.IsNull())
264       return;
265
266     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
267     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
268     aContext->Deactivate(anAIS);
269   }
270 }
271
272 /*void XGUI_Displayer::activate(ObjectPtr theFeature)
273 {
274   activate(theFeature, myActiveSelectionModes);
275 }
276
277 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
278 {
279 #ifdef DEBUG_ACTIVATE
280     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
281
282     if (aFeature.get() != NULL) {
283       QIntList aModes;
284       getModesOfActivation(theObject, aModes);
285
286
287       qDebug(QString("activate feature: %1, theModes: %2, myActiveSelectionModes: %3, getModesOf: %4").
288         arg(aFeature->data()->name().c_str()).
289         arg(theModes.size()).
290         arg(myActiveSelectionModes.size()).
291         arg(aModes.size()).toStdString().c_str());
292     }
293 #endif
294
295   if (isVisible(theObject)) {
296     Handle(AIS_InteractiveContext) aContext = AISContext();
297     if (aContext.IsNull())
298       return;
299
300     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
301     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
302
303     activate(anAIS, theModes);
304   }
305 }*/
306
307 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
308 {
309   if (!isVisible(theObject))
310     return;
311
312   Handle(AIS_InteractiveContext) aContext = AISContext();
313   if (aContext.IsNull())
314     return;
315
316   AISObjectPtr aAISObj = getAISObject(theObject);
317
318   if (aAISObj.get() != NULL) {
319     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
320     TColStd_ListOfInteger aTColModes;
321     aContext->ActivatedModes(anAISIO, aTColModes);
322     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
323     for (; itr.More(); itr.Next() ) {
324       theModes.append(itr.Value());
325     }
326   }
327 }
328
329 void XGUI_Displayer::activateObjects(const QIntList& theModes)
330 {
331 #ifdef DEBUG_ACTIVATE
332   qDebug(QString("activate all features: theModes: %2, myActiveSelectionModes: %3").
333     arg(theModes.size()).
334     arg(myActiveSelectionModes.size()).
335     toStdString().c_str());
336 #endif
337   // In order to avoid doblications of selection modes
338   QIntList aNewModes;
339   foreach (int aMode, theModes) {
340     if (!aNewModes.contains(aMode))
341       aNewModes.append(aMode);
342   }
343   myActiveSelectionModes = aNewModes;
344   Handle(AIS_InteractiveContext) aContext = AISContext();
345   if (aContext.IsNull())
346     return;
347   // Open local context if there is no one
348   if (!aContext->HasOpenedContext()) 
349     return;
350
351   //aContext->UseDisplayedObjects();
352   //myUseExternalObjects = true;
353
354   AIS_ListOfInteractive aPrsList;
355   ::displayedObjects(aContext, aPrsList);
356
357   Handle(AIS_Trihedron) aTrihedron;
358   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
359   Handle(AIS_InteractiveObject) anAISIO;
360   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
361     anAISIO = aLIt.Value();
362     activate(anAISIO, myActiveSelectionModes);
363   }
364 }
365
366
367 void XGUI_Displayer::deactivateObjects()
368 {
369   myActiveSelectionModes.clear();
370   Handle(AIS_InteractiveContext) aContext = AISContext();
371   // Open local context if there is no one
372   if (!aContext->HasOpenedContext()) 
373     return;
374
375   //aContext->NotUseDisplayedObjects();
376   AIS_ListOfInteractive aPrsList;
377   ::displayedObjects(aContext, aPrsList);
378
379   AIS_ListIteratorOfListOfInteractive aLIt;
380   //Handle(AIS_Trihedron) aTrihedron;
381   Handle(AIS_InteractiveObject) anAISIO;
382   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
383     anAISIO = aLIt.Value();
384     aContext->Deactivate(anAISIO);
385   }
386 }
387
388 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
389 {
390   Handle(AIS_InteractiveContext) aContext = AISContext();
391   if (aContext.IsNull())
392     return false;
393   if (!isVisible(theObject))
394     return false;
395     
396   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
397   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
398
399   TColStd_ListOfInteger aModes;
400   aContext->ActivatedModes(anAIS, aModes);
401   return aModes.Extent() > 0;
402 }
403
404 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
405 {
406   Handle(AIS_InteractiveContext) aContext = AISContext();
407   if (aContext.IsNull())
408     return;
409   if (aContext->HasOpenedContext()) {
410     aContext->UnhilightSelected();
411     aContext->ClearSelected();
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->SetSelected(anAIS, false);
418       }
419     }
420   } else {
421     aContext->UnhilightCurrents();
422     aContext->ClearCurrents();
423     foreach(ObjectPtr aResult, theResults) {
424       if (isVisible(aResult)) {
425         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
426         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
427         if (!anAIS.IsNull())
428           aContext->SetCurrentObject(anAIS, false);
429       }
430     }
431   }
432   if (isUpdateViewer)
433     updateViewer();
434 }
435
436
437 void XGUI_Displayer::clearSelected()
438 {
439   Handle(AIS_InteractiveContext) aContext = AISContext();
440   if (aContext) {
441     aContext->UnhilightCurrents(false);
442     aContext->ClearSelected();
443   }
444 }
445
446 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
447 {
448   Handle(AIS_InteractiveContext) aContext = AISContext();
449   if (!aContext.IsNull()) {
450     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
451       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
452       // erase an object
453       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
454       if (!anIO.IsNull()) {
455         emit beforeObjectErase(aObj, aAISObj);
456         aContext->Remove(anIO, false);
457       }
458     }
459     if (isUpdateViewer)
460       updateViewer();
461   }
462   myResult2AISObjectMap.clear();
463 }
464
465 void XGUI_Displayer::openLocalContext()
466 {
467   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
468   if (aContext.IsNull())
469     return;
470   // Open local context if there is no one
471   if (!aContext->HasOpenedContext()) {
472     // Preserve selected objects
473     //AIS_ListOfInteractive aAisList;
474     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
475     //  aAisList.Append(aContext->Current());
476
477     // get the filters from the global context and append them to the local context
478     // a list of filters in the global context is not cleared and should be cleared here
479     SelectMgr_ListOfFilter aFilters;
480     aFilters.Assign(aContext->Filters());
481     // it is important to remove the filters in the global context, because there is a code
482     // in the closeLocalContex, which restore the global context filters
483     aContext->RemoveFilters();
484
485     //aContext->ClearCurrents();
486     aContext->OpenLocalContext();
487     //aContext->NotUseDisplayedObjects();
488
489     //myUseExternalObjects = false;
490
491     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
492     for (;aIt.More(); aIt.Next()) {
493       aContext->AddFilter(aIt.Value());
494     }
495     // Restore selection
496     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
497     //for(; aIt2.More(); aIt2.Next()) {
498     //  aContext->SetSelected(aIt2.Value(), false);
499     //}
500   }
501 }
502
503 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
504 {
505   Handle(AIS_InteractiveContext) aContext = AISContext();
506   if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
507     // Preserve selected objects
508     //AIS_ListOfInteractive aAisList;
509     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
510     //  aAisList.Append(aContext->SelectedInteractive());
511
512     // get the filters from the local context and append them to the global context
513     // a list of filters in the local context is cleared
514     SelectMgr_ListOfFilter aFilters;
515     aFilters.Assign(aContext->Filters());
516
517     //aContext->ClearSelected();
518     aContext->CloseAllContexts(false);
519
520     // Redisplay all object if they were displayed in localContext
521     Handle(AIS_InteractiveObject) aAISIO;
522     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
523       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
524       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
525         aContext->Display(aAISIO, false);
526         aContext->SetDisplayMode(aAISIO, Shading, false);
527       }
528     }
529
530     // Append the filters from the local selection in the global selection context
531     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
532     for (;aIt.More(); aIt.Next()) {
533       Handle(SelectMgr_Filter) aFilter = aIt.Value();
534       aContext->AddFilter(aFilter);
535     }
536
537     if (isUpdateViewer)
538       updateViewer();
539     //myUseExternalObjects = false;
540
541     // Restore selection
542     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
543     //for(; aIt2.More(); aIt2.Next()) {
544     //  if (aContext->IsDisplayed(aIt2.Value()))
545     //    aContext->SetCurrentObject(aIt2.Value(), false);
546     //}
547   }
548 }
549
550 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
551 {
552   AISObjectPtr anIO;
553   if (myResult2AISObjectMap.contains(theObject))
554     anIO = myResult2AISObjectMap[theObject];
555   return anIO;
556 }
557
558 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
559 {
560   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
561   return getObject(aRefAIS);
562 }
563
564 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
565 {
566   ObjectPtr aFeature;
567   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
568     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
569     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
570     if (anAIS == theIO)
571       return anObj;
572   }
573   return aFeature;
574 }
575
576 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
577 {
578   bool aWasEnabled = myEnableUpdateViewer;
579
580   myEnableUpdateViewer = isEnabled;
581
582   return aWasEnabled;
583 }
584
585 void XGUI_Displayer::updateViewer()
586 {
587   Handle(AIS_InteractiveContext) aContext = AISContext();
588   if (!aContext.IsNull() && myEnableUpdateViewer)
589     aContext->UpdateCurrentViewer();
590 }
591
592 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
593 {
594   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
595   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
596     aContext->OpenLocalContext();
597   }
598   return aContext;
599 }
600
601 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
602 {
603   Handle(AIS_InteractiveContext) aContext = AISContext();
604   if (myAndFilter.IsNull() && !aContext.IsNull()) {
605     myAndFilter = new SelectMgr_AndFilter();
606     aContext->AddFilter(myAndFilter);
607   }
608   return myAndFilter;
609 }
610
611 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
612 {
613   Handle(AIS_InteractiveContext) aContext = AISContext();
614   if (aContext.IsNull())
615     return;
616   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
617   if (!anAISIO.IsNull()) {
618     aContext->Display(anAISIO, isUpdate);
619     if (aContext->HasOpenedContext()) {
620       //if (myUseExternalObjects) {
621         if (myActiveSelectionModes.size() == 0)
622           aContext->Activate(anAISIO);
623         else {
624           foreach(int aMode, myActiveSelectionModes) {
625             aContext->Activate(anAISIO, aMode);
626           }
627         }
628       //}
629     }
630   }
631 }
632
633 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
634 {
635   Handle(AIS_InteractiveContext) aContext = AISContext();
636   if (aContext.IsNull())
637     return;
638   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
639   if (!anAISIO.IsNull()) {
640     aContext->Remove(anAISIO, isUpdate);
641   }
642 }
643
644
645 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
646 {
647   if (theMode == NoMode)
648     return;
649
650   Handle(AIS_InteractiveContext) aContext = AISContext();
651   if (aContext.IsNull())
652     return;
653
654   AISObjectPtr aAISObj = getAISObject(theObject);
655   if (!aAISObj)
656     return;
657
658   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
659   bool aCanBeShaded = ::canBeShaded(aAISIO);
660   // In order to avoid extra closing/opening context
661   SelectMgr_IndexedMapOfOwner aSelectedOwners;
662   if (aCanBeShaded) {
663     myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
664     closeLocalContexts(false);
665   }
666   aContext->SetDisplayMode(aAISIO, theMode, false);
667   if (aCanBeShaded) {
668     openLocalContext();
669     activateObjects(myActiveSelectionModes);
670     myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
671   }
672   if (toUpdate)
673     updateViewer();
674 }
675
676 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
677 {
678   Handle(AIS_InteractiveContext) aContext = AISContext();
679   if (aContext.IsNull())
680     return NoMode;
681
682   AISObjectPtr aAISObj = getAISObject(theObject);
683   if (!aAISObj)
684     return NoMode;
685
686   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
687   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
688 }
689
690 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
691 {
692   Handle(AIS_InteractiveContext) aContext = AISContext();
693   if (aContext.IsNull())
694     return;
695   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
696   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
697   for (; aIt.More(); aIt.Next()) {
698     if (theFilter.Access() == aIt.Value().Access())
699       return;
700   }
701   GetFilter()->Add(theFilter);
702 }
703
704 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
705 {
706   Handle(AIS_InteractiveContext) aContext = AISContext();
707   if (aContext.IsNull())
708     return;
709   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
710   if (aCompositeFilter->IsIn(theFilter))
711     aCompositeFilter->Remove(theFilter);
712 }
713
714 void XGUI_Displayer::removeFilters()
715 {
716   Handle(AIS_InteractiveContext) aContext = AISContext();
717   if (aContext.IsNull())
718     return;
719   GetFilter()->Clear();
720 }
721
722 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
723 {
724   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
725   foreach(ObjectPtr aObj, aDispList) {
726     if (!theList.contains(aObj))
727       erase(aObj, false);
728   }
729   foreach(ObjectPtr aObj, theList) {
730     if (!isVisible(aObj))
731       display(aObj, false);
732   }
733   updateViewer();
734 }
735
736 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
737
738   if (!isVisible(theObject))
739     return false;
740
741   AISObjectPtr aAISObj = getAISObject(theObject);
742   if (aAISObj.get() == NULL)
743     return false;
744
745   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
746   return ::canBeShaded(anAIS);
747 }
748
749 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
750                               const QIntList& theModes) const
751 {
752   Handle(AIS_InteractiveContext) aContext = AISContext();
753   if (aContext.IsNull() || theIO.IsNull())
754     return;
755
756   // deactivate object in all modes, which are not in the list of activation
757   TColStd_ListOfInteger aTColModes;
758   aContext->ActivatedModes(theIO, aTColModes);
759   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
760   QIntList aModesActivatedForIO;
761   for (; itr.More(); itr.Next() ) {
762     Standard_Integer aMode = itr.Value();
763     if (!theModes.contains(aMode)) {
764 #ifdef DEBUG_ACTIVATE
765       qDebug(QString("deactivate: %1").arg(aMode).toStdString().c_str());
766 #endif
767       aContext->Deactivate(theIO, aMode);
768     }
769     else {
770       aModesActivatedForIO.append(aMode);
771 #ifdef DEBUG_ACTIVATE
772       qDebug(QString("  active: %1").arg(aMode).toStdString().c_str());
773 #endif
774     }
775   }
776   // loading the interactive object allowing the decomposition
777   if (aTColModes.IsEmpty())
778     aContext->Load(theIO, -1, true);
779
780   Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
781   //Deactivate trihedron which can be activated in local selector
782   if (aTrihedron.IsNull()) {
783     //aContext->Load(anAISIO, -1, true);
784     // In order to clear active modes list
785     if (theModes.size() == 0) {
786       //aContext->Load(anAISIO, 0, true);
787       aContext->Activate(theIO);
788 #ifdef DEBUG_ACTIVATE
789       qDebug("activate in all modes");
790 #endif
791     } else {
792       foreach(int aMode, theModes) {
793         //aContext->Load(anAISIO, aMode, true);
794         if (!aModesActivatedForIO.contains(aMode)) {
795           aContext->Activate(theIO, aMode);
796 #ifdef DEBUG_ACTIVATE
797           qDebug(QString("activate: %1").arg(aMode).toStdString().c_str());
798 #endif
799         }
800       }
801     }
802   }
803 }
804
805 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
806 {
807   AISObjectPtr anAISObj = getAISObject(theObject);
808   // correct the result's color it it has the attribute
809   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
810
811   // Customization of presentation
812   GeomCustomPrsPtr aCustomPrs;
813   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
814   if (aFeature.get() != NULL) {
815     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
816     if (aCustPrs.get() != NULL)
817       aCustomPrs = aCustPrs;
818   }
819   if (aCustomPrs.get() == NULL) {
820     // we ignore presentable not customized objects
821     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
822     if (aPrs.get() != NULL)
823       return false;
824     aCustomPrs = myCustomPrs;
825   }
826   return aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
827 }
828
829
830 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool toUpdate)
831 {
832   if (!isVisible(theObject))
833     return Qt::black;
834
835   AISObjectPtr anAISObj = getAISObject(theObject);
836   int aR, aG, aB;
837   anAISObj->getColor(aR, aG, aB);
838   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
839   if (toUpdate)
840     updateViewer();
841   return QColor(aR, aG, aB);
842 }