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