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