]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Issue #556: Make computation of flags more correct for root model
[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 #include <ModuleBase_Tools.h>
24
25 #include <GeomAPI_Shape.h>
26 #include <GeomAPI_IPresentable.h>
27 #include <GeomAPI_ICustomPrs.h>
28
29 #include <AIS_InteractiveContext.hxx>
30 #include <AIS_LocalContext.hxx>
31 #include <AIS_ListOfInteractive.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_DimensionSelectionMode.hxx>
34 #include <AIS_Shape.hxx>
35 #include <AIS_Dimension.hxx>
36 #include <TColStd_ListIteratorOfListOfInteger.hxx>
37 #include <SelectMgr_ListOfFilter.hxx>
38 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
39
40 #include <TColStd_MapOfTransient.hxx>
41 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
42
43 #include <set>
44
45 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse selection sensitivity
46
47 //#define DEBUG_DISPLAY
48 //#define DEBUG_ACTIVATE
49 //#define DEBUG_FEATURE_REDISPLAY
50 //#define DEBUG_SELECTION_FILTERS
51 //#define DEBUG_USE_CLEAR_OUTDATED_SELECTION
52
53 // Workaround for bug #25637
54 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
55 {
56   // Get from null point
57   theAIS->DisplayedObjects(theList, true);
58   if (theAIS->HasOpenedContext()) {
59     // get from local context
60     const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
61     TColStd_MapOfTransient aMap;
62     int NbDisp = aLC->DisplayedObjects(aMap);
63     TColStd_MapIteratorOfMapOfTransient aIt(aMap);
64
65     Handle(AIS_InteractiveObject) curIO;
66     Handle(Standard_Transient) Tr;
67     for(; aIt.More(); aIt.Next()){
68       Tr = aIt.Key();
69       curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
70       theList.Append(curIO);
71     }
72   }
73 }
74
75
76 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
77   : myWorkshop(theWorkshop)
78 {
79   enableUpdateViewer(true);
80   myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs());
81 }
82
83 XGUI_Displayer::~XGUI_Displayer()
84 {
85 }
86
87 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
88 {
89   return myResult2AISObjectMap.contains(theObject);
90 }
91
92 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
93 {
94   if (isVisible(theObject)) {
95     redisplay(theObject, isUpdateViewer);
96   } else {
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(anAIS);
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     appendResultObject(theObject, theAIS);
149
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     aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
159     if (isShading)
160       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
161     emit objectDisplayed(theObject, theAIS);
162
163     bool isCustomized = customizeObject(theObject);
164     if (isCustomized)
165       aContext->Redisplay(anAISIO, false);
166
167     if (aCanBeShaded) {
168       openLocalContext();
169       activateObjects(myActiveSelectionModes);
170       myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
171     }
172     else
173       activate(anAISIO, myActiveSelectionModes);
174  }
175   if (isUpdateViewer)
176     updateViewer();
177 }
178
179 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
180 {
181   if (!isVisible(theObject))
182     return;
183
184   Handle(AIS_InteractiveContext) aContext = AISContext();
185   if (aContext.IsNull())
186     return;
187   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
188   if (anObject) {
189     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
190     if (!anAIS.IsNull()) {
191       emit beforeObjectErase(theObject, anObject);
192       aContext->Remove(anAIS, isUpdateViewer);
193     }
194   }
195   myResult2AISObjectMap.remove(theObject);
196 }
197
198 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
199 {
200   if (!isVisible(theObject))
201     return;
202
203   AISObjectPtr aAISObj = getAISObject(theObject);
204   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
205
206   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
207   if (aPrs) {
208     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
209     if (!aAIS_Obj) {
210       erase(theObject, isUpdateViewer);
211       return;
212     }
213     if (aAIS_Obj != aAISObj) {
214       appendResultObject(theObject, aAIS_Obj);
215     }
216     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
217   }
218
219   if (!aAISIO.IsNull()) {
220     Handle(AIS_InteractiveContext) aContext = AISContext();
221     if (aContext.IsNull())
222       return;
223     // Check that the visualized shape is the same and the redisplay is not necessary
224     // Redisplay of AIS object leads to this object selection compute and the selection 
225     // in the browser is lost
226
227     // this check is not necessary anymore because the selection store/restore is realized
228     // before and after the values modification.
229     // Moreother, this check avoids customize and redisplay presentation if the presentable
230     // parameter is changed.
231     bool isEqualShapes = false;
232     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
233     if (aResult.get() != NULL) {
234       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
235       if (!aShapePrs.IsNull()) {
236         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
237         if (aShapePtr.get()) {
238           const TopoDS_Shape& aOldShape = aShapePrs->Shape();
239           isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
240         }
241       }
242     }
243     // Customization of presentation
244     bool isCustomized = customizeObject(theObject);
245     #ifdef DEBUG_FEATURE_REDISPLAY
246       //qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
247       //  arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
248     #endif
249     if (!isEqualShapes || isCustomized) {
250       aContext->Redisplay(aAISIO, false);
251       #ifdef DEBUG_FEATURE_REDISPLAY
252       //qDebug("  Redisplay happens");
253       #endif
254       if (isUpdateViewer)
255         updateViewer();
256     }
257   }
258 }
259
260 void XGUI_Displayer::deactivate(ObjectPtr theObject)
261 {
262   if (isVisible(theObject)) {
263     Handle(AIS_InteractiveContext) aContext = AISContext();
264     if (aContext.IsNull())
265       return;
266
267     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
268     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
269     aContext->Deactivate(anAIS);
270 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
271     aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
272     updateViewer();
273 #endif
274   }
275 }
276
277 /*void XGUI_Displayer::activate(ObjectPtr theFeature)
278 {
279   activate(theFeature, myActiveSelectionModes);
280 }
281
282 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
283 {
284 #ifdef DEBUG_ACTIVATE
285     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
286
287     if (aFeature.get() != NULL) {
288       QIntList aModes;
289       getModesOfActivation(theObject, aModes);
290
291
292       qDebug(QString("activate feature: %1, theModes: %2, myActiveSelectionModes: %3, getModesOf: %4").
293         arg(aFeature->data()->name().c_str()).
294         arg(theModes.size()).
295         arg(myActiveSelectionModes.size()).
296         arg(aModes.size()).toStdString().c_str());
297     }
298 #endif
299
300   if (isVisible(theObject)) {
301     Handle(AIS_InteractiveContext) aContext = AISContext();
302     if (aContext.IsNull())
303       return;
304
305     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
306     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
307
308     activate(anAIS, theModes);
309   }
310 }*/
311
312 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
313 {
314   if (!isVisible(theObject))
315     return;
316
317   Handle(AIS_InteractiveContext) aContext = AISContext();
318   if (aContext.IsNull())
319     return;
320
321   AISObjectPtr aAISObj = getAISObject(theObject);
322
323   if (aAISObj.get() != NULL) {
324     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
325     TColStd_ListOfInteger aTColModes;
326     aContext->ActivatedModes(anAISIO, aTColModes);
327     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
328     for (; itr.More(); itr.Next() ) {
329       theModes.append(itr.Value());
330     }
331   }
332 }
333
334 void XGUI_Displayer::activateObjects(const QIntList& theModes)
335 {
336 #ifdef DEBUG_ACTIVATE
337   qDebug(QString("activate all features: theModes: %2, myActiveSelectionModes: %3").
338     arg(theModes.size()).
339     arg(myActiveSelectionModes.size()).
340     toStdString().c_str());
341 #endif
342   // In order to avoid doblications of selection modes
343   QIntList aNewModes;
344   foreach (int aMode, theModes) {
345     if (!aNewModes.contains(aMode))
346       aNewModes.append(aMode);
347   }
348   myActiveSelectionModes = aNewModes;
349   Handle(AIS_InteractiveContext) aContext = AISContext();
350   if (aContext.IsNull())
351     return;
352   // Open local context if there is no one
353   if (!aContext->HasOpenedContext()) 
354     return;
355
356   //aContext->UseDisplayedObjects();
357   //myUseExternalObjects = true;
358
359   AIS_ListOfInteractive aPrsList;
360   ::displayedObjects(aContext, aPrsList);
361
362   Handle(AIS_Trihedron) aTrihedron;
363   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
364   Handle(AIS_InteractiveObject) anAISIO;
365   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
366     anAISIO = aLIt.Value();
367     activate(anAISIO, myActiveSelectionModes);
368   }
369 }
370
371
372 void XGUI_Displayer::deactivateObjects()
373 {
374   myActiveSelectionModes.clear();
375   Handle(AIS_InteractiveContext) aContext = AISContext();
376   // Open local context if there is no one
377   if (!aContext->HasOpenedContext()) 
378     return;
379
380   //aContext->NotUseDisplayedObjects();
381   AIS_ListOfInteractive aPrsList;
382   ::displayedObjects(aContext, aPrsList);
383
384   AIS_ListIteratorOfListOfInteractive aLIt;
385   //Handle(AIS_Trihedron) aTrihedron;
386   Handle(AIS_InteractiveObject) anAISIO;
387   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
388     anAISIO = aLIt.Value();
389     aContext->Deactivate(anAISIO);
390 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
391     aContext->LocalContext()->ClearOutdatedSelection(anAISIO, true);
392     updateViewer();
393 #endif
394   }
395 }
396
397 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
398 {
399   Handle(AIS_InteractiveContext) aContext = AISContext();
400   if (aContext.IsNull())
401     return false;
402   if (!isVisible(theObject))
403     return false;
404     
405   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
406   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
407
408   TColStd_ListOfInteger aModes;
409   aContext->ActivatedModes(anAIS, aModes);
410   return aModes.Extent() > 0;
411 }
412
413 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrs>& theValues, bool isUpdateViewer)
414 {
415   Handle(AIS_InteractiveContext) aContext = AISContext();
416   if (aContext.IsNull())
417     return;
418   if (aContext->HasOpenedContext()) {
419     aContext->UnhilightSelected();
420     aContext->ClearSelected();
421     //if (aSelected.size() > 0) {
422     foreach (ModuleBase_ViewerPrs aPrs, theValues) {
423       const TopoDS_Shape& aShape = aPrs.shape();
424       if (!aShape.IsNull()) {
425         aContext->AddOrRemoveSelected(aShape, false);
426       } else {
427         ObjectPtr anObject = aPrs.object();
428         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
429         if (aResult.get() && isVisible(aResult)) {
430           AISObjectPtr anObj = myResult2AISObjectMap[aResult];
431           Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
432           if (!anAIS.IsNull()) {
433             // The methods are replaced in order to provide multi-selection, e.g. restore selection
434             // by activating multi selector widget. It also gives an advantage that the multi
435             // selection in OB gives multi-selection in the viewer
436             //aContext->SetSelected(anAIS, false);
437             // The selection in the context was cleared, so the method sets the objects are selected
438             aContext->AddOrRemoveSelected(anAIS, false);
439           }
440         }
441       }
442     }
443   } else {
444     aContext->UnhilightCurrents();
445     aContext->ClearCurrents();
446     foreach (ModuleBase_ViewerPrs aPrs, theValues) {
447       ObjectPtr anObject = aPrs.object();
448       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
449       if (aResult.get() && isVisible(aResult)) {
450         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
451         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
452         if (!anAIS.IsNull())
453           aContext->SetCurrentObject(anAIS, false);
454       }
455     }
456   }
457   if (isUpdateViewer)
458     updateViewer();
459 }
460
461 void XGUI_Displayer::clearSelected()
462 {
463   Handle(AIS_InteractiveContext) aContext = AISContext();
464   if (aContext) {
465     aContext->UnhilightCurrents(false);
466     aContext->ClearSelected();
467   }
468 }
469
470 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
471 {
472   Handle(AIS_InteractiveContext) aContext = AISContext();
473   if (!aContext.IsNull()) {
474     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
475       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
476       // erase an object
477       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
478       if (!anIO.IsNull()) {
479         emit beforeObjectErase(aObj, aAISObj);
480         aContext->Remove(anIO, false);
481       }
482     }
483     if (isUpdateViewer)
484       updateViewer();
485   }
486   myResult2AISObjectMap.clear();
487 }
488
489 void XGUI_Displayer::openLocalContext()
490 {
491   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
492   if (aContext.IsNull())
493     return;
494   // Open local context if there is no one
495   if (!aContext->HasOpenedContext()) {
496     // Preserve selected objects
497     //AIS_ListOfInteractive aAisList;
498     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
499     //  aAisList.Append(aContext->Current());
500
501     // get the filters from the global context and append them to the local context
502     // a list of filters in the global context is not cleared and should be cleared here
503     SelectMgr_ListOfFilter aFilters;
504     aFilters.Assign(aContext->Filters());
505     // it is important to remove the filters in the global context, because there is a code
506     // in the closeLocalContex, which restore the global context filters
507     aContext->RemoveFilters();
508
509     //aContext->ClearCurrents();
510     aContext->OpenLocalContext();
511     //aContext->NotUseDisplayedObjects();
512
513     //myUseExternalObjects = false;
514
515     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
516     for (;aIt.More(); aIt.Next()) {
517       aContext->AddFilter(aIt.Value());
518     }
519     // Restore selection
520     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
521     //for(; aIt2.More(); aIt2.Next()) {
522     //  aContext->SetSelected(aIt2.Value(), false);
523     //}
524   }
525 }
526
527 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
528 {
529   Handle(AIS_InteractiveContext) aContext = AISContext();
530   if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
531     // Preserve selected objects
532     //AIS_ListOfInteractive aAisList;
533     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
534     //  aAisList.Append(aContext->SelectedInteractive());
535
536     // get the filters from the local context and append them to the global context
537     // a list of filters in the local context is cleared
538     SelectMgr_ListOfFilter aFilters;
539     aFilters.Assign(aContext->Filters());
540
541     //aContext->ClearSelected();
542     aContext->CloseAllContexts(false);
543
544     // Redisplay all object if they were displayed in localContext
545     Handle(AIS_InteractiveObject) aAISIO;
546     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
547       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
548       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
549         aContext->Display(aAISIO, false);
550         aContext->SetDisplayMode(aAISIO, Shading, false);
551       }
552     }
553
554     // Append the filters from the local selection in the global selection context
555     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
556     for (;aIt.More(); aIt.Next()) {
557       Handle(SelectMgr_Filter) aFilter = aIt.Value();
558       aContext->AddFilter(aFilter);
559     }
560
561     if (isUpdateViewer)
562       updateViewer();
563     //myUseExternalObjects = false;
564
565     // Restore selection
566     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
567     //for(; aIt2.More(); aIt2.Next()) {
568     //  if (aContext->IsDisplayed(aIt2.Value()))
569     //    aContext->SetCurrentObject(aIt2.Value(), false);
570     //}
571   }
572 }
573
574 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
575 {
576   AISObjectPtr anIO;
577   if (myResult2AISObjectMap.contains(theObject))
578     anIO = myResult2AISObjectMap[theObject];
579   return anIO;
580 }
581
582 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
583 {
584   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
585   return getObject(aRefAIS);
586 }
587
588 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
589 {
590   ObjectPtr aFeature;
591   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
592     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
593     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
594     if (anAIS == theIO)
595       return anObj;
596   }
597   return aFeature;
598 }
599
600 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
601 {
602   bool aWasEnabled = myEnableUpdateViewer;
603
604   myEnableUpdateViewer = isEnabled;
605
606   return aWasEnabled;
607 }
608
609 void XGUI_Displayer::updateViewer() const
610 {
611   Handle(AIS_InteractiveContext) aContext = AISContext();
612   if (!aContext.IsNull() && myEnableUpdateViewer)
613     aContext->UpdateCurrentViewer();
614 }
615
616 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
617 {
618   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
619   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
620     aContext->OpenLocalContext();
621   }
622   return aContext;
623 }
624
625 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
626 {
627   Handle(AIS_InteractiveContext) aContext = AISContext();
628   if (myAndFilter.IsNull() && !aContext.IsNull()) {
629     myAndFilter = new SelectMgr_AndFilter();
630     aContext->AddFilter(myAndFilter);
631   }
632   return myAndFilter;
633 }
634
635 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
636 {
637   Handle(AIS_InteractiveContext) aContext = AISContext();
638   if (aContext.IsNull())
639     return;
640   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
641   if (!anAISIO.IsNull()) {
642     aContext->Display(anAISIO, isUpdate);
643     if (aContext->HasOpenedContext()) {
644       //if (myUseExternalObjects) {
645         if (myActiveSelectionModes.size() == 0)
646           aContext->Activate(anAISIO);
647         else {
648           foreach(int aMode, myActiveSelectionModes) {
649             aContext->Activate(anAISIO, aMode);
650           }
651         }
652       //}
653     }
654   }
655 }
656
657 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
658 {
659   Handle(AIS_InteractiveContext) aContext = AISContext();
660   if (aContext.IsNull())
661     return;
662   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
663   if (!anAISIO.IsNull()) {
664     aContext->Remove(anAISIO, isUpdate);
665   }
666 }
667
668
669 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
670 {
671   if (theMode == NoMode)
672     return;
673
674   Handle(AIS_InteractiveContext) aContext = AISContext();
675   if (aContext.IsNull())
676     return;
677
678   AISObjectPtr aAISObj = getAISObject(theObject);
679   if (!aAISObj)
680     return;
681
682   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
683   bool aCanBeShaded = ::canBeShaded(aAISIO);
684   // In order to avoid extra closing/opening context
685   SelectMgr_IndexedMapOfOwner aSelectedOwners;
686   if (aCanBeShaded) {
687     myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
688     closeLocalContexts(false);
689   }
690   aContext->SetDisplayMode(aAISIO, theMode, false);
691   // Redisplay in order to update new mode because it could be not computed before
692   aContext->Redisplay(aAISIO, false);
693   if (aCanBeShaded) {
694     openLocalContext();
695     activateObjects(myActiveSelectionModes);
696     myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
697   }
698   if (toUpdate)
699     updateViewer();
700 }
701
702 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
703 {
704   Handle(AIS_InteractiveContext) aContext = AISContext();
705   if (aContext.IsNull())
706     return NoMode;
707
708   AISObjectPtr aAISObj = getAISObject(theObject);
709   if (!aAISObj)
710     return NoMode;
711
712   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
713   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
714 }
715
716 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
717 {
718   Handle(AIS_InteractiveContext) aContext = AISContext();
719   if (aContext.IsNull())
720     return;
721   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
722   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
723   for (; aIt.More(); aIt.Next()) {
724     if (theFilter.Access() == aIt.Value().Access())
725       return;
726   }
727   Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
728   const SelectMgr_ListOfFilter& aStoredFilters = aCompFilter->StoredFilters();
729   for (aIt.Initialize(aStoredFilters); aIt.More(); aIt.Next()) {
730     if (theFilter.Access() == aIt.Value().Access())
731       return;
732   }
733   aCompFilter->Add(theFilter);
734 #ifdef DEBUG_SELECTION_FILTERS
735   int aCount = GetFilter()->StoredFilters().Extent();
736   qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
737 #endif
738 }
739
740 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
741 {
742   Handle(AIS_InteractiveContext) aContext = AISContext();
743   if (aContext.IsNull())
744     return;
745   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
746   if (aCompositeFilter->IsIn(theFilter))
747     aCompositeFilter->Remove(theFilter);
748 #ifdef DEBUG_SELECTION_FILTERS
749   int aCount = GetFilter()->StoredFilters().Extent();
750   qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
751 #endif
752 }
753
754 void XGUI_Displayer::removeFilters()
755 {
756   Handle(AIS_InteractiveContext) aContext = AISContext();
757   if (aContext.IsNull())
758     return;
759   GetFilter()->Clear();
760 }
761
762 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
763 {
764   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
765   foreach(ObjectPtr aObj, aDispList) {
766     if (!theList.contains(aObj))
767       erase(aObj, false);
768   }
769   foreach(ObjectPtr aObj, theList) {
770     if (!isVisible(aObj))
771       display(aObj, false);
772   }
773   updateViewer();
774 }
775
776 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
777
778   if (!isVisible(theObject))
779     return false;
780
781   AISObjectPtr aAISObj = getAISObject(theObject);
782   if (aAISObj.get() == NULL)
783     return false;
784
785   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
786   return ::canBeShaded(anAIS);
787 }
788
789 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
790                               const QIntList& theModes) const
791 {
792   Handle(AIS_InteractiveContext) aContext = AISContext();
793   if (aContext.IsNull() || theIO.IsNull())
794     return;
795
796   // deactivate object in all modes, which are not in the list of activation
797   // It seems that after the IO deactivation the selected state of the IO's owners
798   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
799   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
800   // only if there is a difference in the current modes and the parameters modes.
801   // If the selection problem happens again, it is possible to write a test scenario and create
802   // a bug. The bug steps are the following:
803   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
804   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
805   TColStd_ListOfInteger aTColModes;
806   aContext->ActivatedModes(theIO, aTColModes);
807   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
808   QIntList aModesActivatedForIO;
809   //bool isDeactivated = false;
810   for (; itr.More(); itr.Next() ) {
811     Standard_Integer aMode = itr.Value();
812     if (!theModes.contains(aMode)) {
813 #ifdef DEBUG_ACTIVATE
814       qDebug(QString("deactivate: %1").arg(aMode).toStdString().c_str());
815 #endif
816       aContext->Deactivate(theIO, aMode);
817       //isDeactivated = true;
818     }
819     else {
820       aModesActivatedForIO.append(aMode);
821 #ifdef DEBUG_ACTIVATE
822       qDebug(QString("  active: %1").arg(aMode).toStdString().c_str());
823 #endif
824     }
825   }
826 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
827   if (isDeactivated) {
828     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
829     updateViewer();
830   }
831 #endif
832   // loading the interactive object allowing the decomposition
833   if (aTColModes.IsEmpty())
834     aContext->Load(theIO, -1, true);
835
836   Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
837   //Deactivate trihedron which can be activated in local selector
838   if (aTrihedron.IsNull()) {
839     //aContext->Load(anAISIO, -1, true);
840     // In order to clear active modes list
841     if (theModes.size() == 0) {
842       //aContext->Load(anAISIO, 0, true);
843       aContext->Activate(theIO);
844 #ifdef DEBUG_ACTIVATE
845       qDebug("activate in all modes");
846 #endif
847     } else {
848       foreach(int aMode, theModes) {
849         //aContext->Load(anAISIO, aMode, true);
850         if (!aModesActivatedForIO.contains(aMode)) {
851           aContext->Activate(theIO, aMode);
852 #ifdef DEBUG_ACTIVATE
853           qDebug(QString("activate: %1").arg(aMode).toStdString().c_str());
854 #endif
855         }
856       }
857     }
858   }
859 }
860
861 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
862 {
863   AISObjectPtr anAISObj = getAISObject(theObject);
864   // correct the result's color it it has the attribute
865   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
866
867   // Customization of presentation
868   GeomCustomPrsPtr aCustomPrs;
869   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
870   if (aFeature.get() != NULL) {
871     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
872     if (aCustPrs.get() != NULL)
873       aCustomPrs = aCustPrs;
874   }
875   if (aCustomPrs.get() == NULL) {
876     // we ignore presentable not customized objects
877     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
878     if (aPrs.get() != NULL)
879       return false;
880     aCustomPrs = myCustomPrs;
881   }
882   return aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
883 }
884
885
886 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool toUpdate)
887 {
888   if (!isVisible(theObject))
889     return Qt::black;
890
891   AISObjectPtr anAISObj = getAISObject(theObject);
892   int aR, aG, aB;
893   anAISObj->getColor(aR, aG, aB);
894   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
895   if (toUpdate)
896     updateViewer();
897   return QColor(aR, aG, aB);
898 }
899
900 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
901 {
902   myResult2AISObjectMap[theObject] = theAIS;
903
904 #ifdef DEBUG_DISPLAY
905   std::ostringstream aPtrStr;
906   aPtrStr << theObject.get();
907   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
908   qDebug(getResult2AISObjectMapInfo().c_str());
909 #endif
910 }
911
912 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
913 {
914   QStringList aContent;
915   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
916     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
917     std::ostringstream aPtrStr;
918     aPtrStr << "aObj = " << aObj.get() << ":";
919     aPtrStr << "anAIS = " << aAISObj.get() << ":";
920     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
921     
922     aContent.append(aPtrStr.str().c_str());
923   }
924   return QString("myResult2AISObjectMap: size = %1\n%2").arg(myResult2AISObjectMap.size()).
925                                             arg(aContent.join("\n")).toStdString().c_str();
926 }