Salome HOME
Use activation only for objects which are not result of a current operation
[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, const QObjectPtrList& theObjList)
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   Handle(AIS_InteractiveObject) anAISIO;
360   AIS_ListOfInteractive aPrsList;
361   if (theObjList.isEmpty())
362     ::displayedObjects(aContext, aPrsList);
363   else {
364     foreach(ObjectPtr aObj, theObjList) {
365       if (myResult2AISObjectMap.contains(aObj))
366         aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
367     }
368   }
369
370   Handle(AIS_Trihedron) aTrihedron;
371   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
372   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
373     anAISIO = aLIt.Value();
374     aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
375     if (aTrihedron.IsNull())
376       activate(anAISIO, myActiveSelectionModes);
377   }
378 }
379
380
381 void XGUI_Displayer::deactivateObjects()
382 {
383   myActiveSelectionModes.clear();
384   Handle(AIS_InteractiveContext) aContext = AISContext();
385   // Open local context if there is no one
386   if (!aContext->HasOpenedContext()) 
387     return;
388
389   //aContext->NotUseDisplayedObjects();
390   AIS_ListOfInteractive aPrsList;
391   ::displayedObjects(aContext, aPrsList);
392
393   AIS_ListIteratorOfListOfInteractive aLIt;
394   //Handle(AIS_Trihedron) aTrihedron;
395   Handle(AIS_InteractiveObject) anAISIO;
396   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
397     anAISIO = aLIt.Value();
398     aContext->Deactivate(anAISIO);
399 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
400     aContext->LocalContext()->ClearOutdatedSelection(anAISIO, true);
401     updateViewer();
402 #endif
403   }
404 }
405
406 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
407 {
408   Handle(AIS_InteractiveContext) aContext = AISContext();
409   if (aContext.IsNull())
410     return false;
411   if (!isVisible(theObject))
412     return false;
413     
414   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
415   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
416
417   TColStd_ListOfInteger aModes;
418   aContext->ActivatedModes(anAIS, aModes);
419   return aModes.Extent() > 0;
420 }
421
422 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrs>& theValues, bool isUpdateViewer)
423 {
424   Handle(AIS_InteractiveContext) aContext = AISContext();
425   if (aContext.IsNull())
426     return;
427   if (aContext->HasOpenedContext()) {
428     aContext->UnhilightSelected();
429     aContext->ClearSelected();
430     //if (aSelected.size() > 0) {
431     foreach (ModuleBase_ViewerPrs aPrs, theValues) {
432       const TopoDS_Shape& aShape = aPrs.shape();
433       if (!aShape.IsNull()) {
434         aContext->AddOrRemoveSelected(aShape, false);
435       } else {
436         ObjectPtr anObject = aPrs.object();
437         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
438         if (aResult.get() && isVisible(aResult)) {
439           AISObjectPtr anObj = myResult2AISObjectMap[aResult];
440           Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
441           if (!anAIS.IsNull()) {
442             // The methods are replaced in order to provide multi-selection, e.g. restore selection
443             // by activating multi selector widget. It also gives an advantage that the multi
444             // selection in OB gives multi-selection in the viewer
445             //aContext->SetSelected(anAIS, false);
446             // The selection in the context was cleared, so the method sets the objects are selected
447             aContext->AddOrRemoveSelected(anAIS, false);
448           }
449         }
450       }
451     }
452   } else {
453     aContext->UnhilightCurrents();
454     aContext->ClearCurrents();
455     foreach (ModuleBase_ViewerPrs aPrs, theValues) {
456       ObjectPtr anObject = aPrs.object();
457       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
458       if (aResult.get() && isVisible(aResult)) {
459         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
460         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
461         if (!anAIS.IsNull())
462           aContext->SetCurrentObject(anAIS, false);
463       }
464     }
465   }
466   if (isUpdateViewer)
467     updateViewer();
468 }
469
470 void XGUI_Displayer::clearSelected()
471 {
472   Handle(AIS_InteractiveContext) aContext = AISContext();
473   if (aContext) {
474     aContext->UnhilightCurrents(false);
475     aContext->ClearSelected();
476   }
477 }
478
479 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
480 {
481   Handle(AIS_InteractiveContext) aContext = AISContext();
482   if (!aContext.IsNull()) {
483     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
484       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
485       // erase an object
486       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
487       if (!anIO.IsNull()) {
488         emit beforeObjectErase(aObj, aAISObj);
489         aContext->Remove(anIO, false);
490       }
491     }
492     if (isUpdateViewer)
493       updateViewer();
494   }
495   myResult2AISObjectMap.clear();
496 }
497
498 void XGUI_Displayer::openLocalContext()
499 {
500   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
501   if (aContext.IsNull())
502     return;
503   // Open local context if there is no one
504   if (!aContext->HasOpenedContext()) {
505     // Preserve selected objects
506     //AIS_ListOfInteractive aAisList;
507     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
508     //  aAisList.Append(aContext->Current());
509
510     // get the filters from the global context and append them to the local context
511     // a list of filters in the global context is not cleared and should be cleared here
512     SelectMgr_ListOfFilter aFilters;
513     aFilters.Assign(aContext->Filters());
514     // it is important to remove the filters in the global context, because there is a code
515     // in the closeLocalContex, which restore the global context filters
516     aContext->RemoveFilters();
517
518     //aContext->ClearCurrents();
519     aContext->OpenLocalContext();
520     //aContext->NotUseDisplayedObjects();
521
522     //myUseExternalObjects = false;
523
524     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
525     for (;aIt.More(); aIt.Next()) {
526       aContext->AddFilter(aIt.Value());
527     }
528     // Restore selection
529     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
530     //for(; aIt2.More(); aIt2.Next()) {
531     //  aContext->SetSelected(aIt2.Value(), false);
532     //}
533   }
534 }
535
536 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
537 {
538   Handle(AIS_InteractiveContext) aContext = AISContext();
539   if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
540     // Preserve selected objects
541     //AIS_ListOfInteractive aAisList;
542     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
543     //  aAisList.Append(aContext->SelectedInteractive());
544
545     // get the filters from the local context and append them to the global context
546     // a list of filters in the local context is cleared
547     SelectMgr_ListOfFilter aFilters;
548     aFilters.Assign(aContext->Filters());
549
550     //aContext->ClearSelected();
551     aContext->CloseAllContexts(false);
552
553     // Redisplay all object if they were displayed in localContext
554     Handle(AIS_InteractiveObject) aAISIO;
555     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
556       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
557       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
558         aContext->Display(aAISIO, false);
559         aContext->SetDisplayMode(aAISIO, Shading, false);
560       }
561     }
562
563     // Append the filters from the local selection in the global selection context
564     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
565     for (;aIt.More(); aIt.Next()) {
566       Handle(SelectMgr_Filter) aFilter = aIt.Value();
567       aContext->AddFilter(aFilter);
568     }
569
570     if (isUpdateViewer)
571       updateViewer();
572     //myUseExternalObjects = false;
573
574     // Restore selection
575     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
576     //for(; aIt2.More(); aIt2.Next()) {
577     //  if (aContext->IsDisplayed(aIt2.Value()))
578     //    aContext->SetCurrentObject(aIt2.Value(), false);
579     //}
580   }
581 }
582
583 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
584 {
585   AISObjectPtr anIO;
586   if (myResult2AISObjectMap.contains(theObject))
587     anIO = myResult2AISObjectMap[theObject];
588   return anIO;
589 }
590
591 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
592 {
593   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
594   return getObject(aRefAIS);
595 }
596
597 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
598 {
599   ObjectPtr aFeature;
600   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
601     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
602     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
603     if (anAIS == theIO)
604       return anObj;
605   }
606   return aFeature;
607 }
608
609 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
610 {
611   bool aWasEnabled = myEnableUpdateViewer;
612
613   myEnableUpdateViewer = isEnabled;
614
615   return aWasEnabled;
616 }
617
618 void XGUI_Displayer::updateViewer() const
619 {
620   Handle(AIS_InteractiveContext) aContext = AISContext();
621   if (!aContext.IsNull() && myEnableUpdateViewer)
622     aContext->UpdateCurrentViewer();
623 }
624
625 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
626 {
627   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
628   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
629     aContext->OpenLocalContext();
630   }
631   return aContext;
632 }
633
634 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
635 {
636   Handle(AIS_InteractiveContext) aContext = AISContext();
637   if (myAndFilter.IsNull() && !aContext.IsNull()) {
638     myAndFilter = new SelectMgr_AndFilter();
639     aContext->AddFilter(myAndFilter);
640   }
641   return myAndFilter;
642 }
643
644 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
645 {
646   Handle(AIS_InteractiveContext) aContext = AISContext();
647   if (aContext.IsNull())
648     return;
649   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
650   if (!anAISIO.IsNull()) {
651     aContext->Display(anAISIO, isUpdate);
652     if (aContext->HasOpenedContext()) {
653       //if (myUseExternalObjects) {
654         if (myActiveSelectionModes.size() == 0)
655           aContext->Activate(anAISIO);
656         else {
657           foreach(int aMode, myActiveSelectionModes) {
658             aContext->Activate(anAISIO, aMode);
659           }
660         }
661       //}
662     }
663   }
664 }
665
666 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
667 {
668   Handle(AIS_InteractiveContext) aContext = AISContext();
669   if (aContext.IsNull())
670     return;
671   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
672   if (!anAISIO.IsNull()) {
673     aContext->Remove(anAISIO, isUpdate);
674   }
675 }
676
677
678 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
679 {
680   if (theMode == NoMode)
681     return;
682
683   Handle(AIS_InteractiveContext) aContext = AISContext();
684   if (aContext.IsNull())
685     return;
686
687   AISObjectPtr aAISObj = getAISObject(theObject);
688   if (!aAISObj)
689     return;
690
691   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
692   bool aCanBeShaded = ::canBeShaded(aAISIO);
693   // In order to avoid extra closing/opening context
694   SelectMgr_IndexedMapOfOwner aSelectedOwners;
695   if (aCanBeShaded) {
696     myWorkshop->selector()->selection()->selectedOwners(aSelectedOwners);
697     closeLocalContexts(false);
698   }
699   aContext->SetDisplayMode(aAISIO, theMode, false);
700   // Redisplay in order to update new mode because it could be not computed before
701   aContext->Redisplay(aAISIO, false);
702   if (aCanBeShaded) {
703     openLocalContext();
704     activateObjects(myActiveSelectionModes);
705     myWorkshop->selector()->setSelectedOwners(aSelectedOwners, false);
706   }
707   if (toUpdate)
708     updateViewer();
709 }
710
711 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
712 {
713   Handle(AIS_InteractiveContext) aContext = AISContext();
714   if (aContext.IsNull())
715     return NoMode;
716
717   AISObjectPtr aAISObj = getAISObject(theObject);
718   if (!aAISObj)
719     return NoMode;
720
721   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
722   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
723 }
724
725 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
726 {
727   Handle(AIS_InteractiveContext) aContext = AISContext();
728   if (aContext.IsNull())
729     return;
730   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
731   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
732   for (; aIt.More(); aIt.Next()) {
733     if (theFilter.Access() == aIt.Value().Access())
734       return;
735   }
736   Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
737   const SelectMgr_ListOfFilter& aStoredFilters = aCompFilter->StoredFilters();
738   for (aIt.Initialize(aStoredFilters); aIt.More(); aIt.Next()) {
739     if (theFilter.Access() == aIt.Value().Access())
740       return;
741   }
742   aCompFilter->Add(theFilter);
743 #ifdef DEBUG_SELECTION_FILTERS
744   int aCount = GetFilter()->StoredFilters().Extent();
745   qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
746 #endif
747 }
748
749 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
750 {
751   Handle(AIS_InteractiveContext) aContext = AISContext();
752   if (aContext.IsNull())
753     return;
754   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
755   if (aCompositeFilter->IsIn(theFilter))
756     aCompositeFilter->Remove(theFilter);
757 #ifdef DEBUG_SELECTION_FILTERS
758   int aCount = GetFilter()->StoredFilters().Extent();
759   qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
760 #endif
761 }
762
763 void XGUI_Displayer::removeFilters()
764 {
765   Handle(AIS_InteractiveContext) aContext = AISContext();
766   if (aContext.IsNull())
767     return;
768   GetFilter()->Clear();
769 }
770
771 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
772 {
773   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
774   foreach(ObjectPtr aObj, aDispList) {
775     if (!theList.contains(aObj))
776       erase(aObj, false);
777   }
778   foreach(ObjectPtr aObj, theList) {
779     if (!isVisible(aObj))
780       display(aObj, false);
781   }
782   updateViewer();
783 }
784
785 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
786
787   if (!isVisible(theObject))
788     return false;
789
790   AISObjectPtr aAISObj = getAISObject(theObject);
791   if (aAISObj.get() == NULL)
792     return false;
793
794   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
795   return ::canBeShaded(anAIS);
796 }
797
798 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
799                               const QIntList& theModes) const
800 {
801   Handle(AIS_InteractiveContext) aContext = AISContext();
802   if (aContext.IsNull() || theIO.IsNull())
803     return;
804
805   // deactivate object in all modes, which are not in the list of activation
806   // It seems that after the IO deactivation the selected state of the IO's owners
807   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
808   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
809   // only if there is a difference in the current modes and the parameters modes.
810   // If the selection problem happens again, it is possible to write a test scenario and create
811   // a bug. The bug steps are the following:
812   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
813   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
814   TColStd_ListOfInteger aTColModes;
815   aContext->ActivatedModes(theIO, aTColModes);
816   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
817   QIntList aModesActivatedForIO;
818   //bool isDeactivated = false;
819   for (; itr.More(); itr.Next() ) {
820     Standard_Integer aMode = itr.Value();
821     if (!theModes.contains(aMode)) {
822 #ifdef DEBUG_ACTIVATE
823       qDebug(QString("deactivate: %1").arg(aMode).toStdString().c_str());
824 #endif
825       aContext->Deactivate(theIO, aMode);
826       //isDeactivated = true;
827     }
828     else {
829       aModesActivatedForIO.append(aMode);
830 #ifdef DEBUG_ACTIVATE
831       qDebug(QString("  active: %1").arg(aMode).toStdString().c_str());
832 #endif
833     }
834   }
835 #ifdef DEBUG_USE_CLEAR_OUTDATED_SELECTION
836   if (isDeactivated) {
837     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
838     updateViewer();
839   }
840 #endif
841   // loading the interactive object allowing the decomposition
842   if (aTColModes.IsEmpty())
843     aContext->Load(theIO, -1, true);
844
845     //aContext->Load(anAISIO, -1, true);
846     // In order to clear active modes list
847   if (theModes.size() == 0) {
848     //aContext->Load(anAISIO, 0, true);
849     aContext->Activate(theIO);
850 #ifdef DEBUG_ACTIVATE
851     qDebug("activate in all modes");
852 #endif
853   } else {
854     foreach(int aMode, theModes) {
855       //aContext->Load(anAISIO, aMode, true);
856       if (!aModesActivatedForIO.contains(aMode)) {
857         aContext->Activate(theIO, aMode);
858 #ifdef DEBUG_ACTIVATE
859         qDebug(QString("activate: %1").arg(aMode).toStdString().c_str());
860 #endif
861       }
862     }
863   }
864 }
865
866 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
867 {
868   AISObjectPtr anAISObj = getAISObject(theObject);
869   // correct the result's color it it has the attribute
870   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
871
872   // Customization of presentation
873   GeomCustomPrsPtr aCustomPrs;
874   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
875   if (aFeature.get() != NULL) {
876     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
877     if (aCustPrs.get() != NULL)
878       aCustomPrs = aCustPrs;
879   }
880   if (aCustomPrs.get() == NULL) {
881     // we ignore presentable not customized objects
882     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
883     if (aPrs.get() != NULL)
884       return false;
885     aCustomPrs = myCustomPrs;
886   }
887   return aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
888 }
889
890
891 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool toUpdate)
892 {
893   if (!isVisible(theObject))
894     return Qt::black;
895
896   AISObjectPtr anAISObj = getAISObject(theObject);
897   int aR, aG, aB;
898   anAISObj->getColor(aR, aG, aB);
899   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
900   if (toUpdate)
901     updateViewer();
902   return QColor(aR, aG, aB);
903 }
904
905 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
906 {
907   myResult2AISObjectMap[theObject] = theAIS;
908
909 #ifdef DEBUG_DISPLAY
910   std::ostringstream aPtrStr;
911   aPtrStr << theObject.get();
912   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
913   qDebug(getResult2AISObjectMapInfo().c_str());
914 #endif
915 }
916
917 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
918 {
919   QStringList aContent;
920   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
921     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
922     std::ostringstream aPtrStr;
923     aPtrStr << "aObj = " << aObj.get() << ":";
924     aPtrStr << "anAIS = " << aAISObj.get() << ":";
925     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
926     
927     aContent.append(aPtrStr.str().c_str());
928   }
929   return QString("myResult2AISObjectMap: size = %1\n%2").arg(myResult2AISObjectMap.size()).
930                                             arg(aContent.join("\n")).toStdString().c_str();
931 }