Salome HOME
Issue #591 - Highlight of the first argument of constraints
[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 anObject;
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       anObject = anObj;
584     if (anObject.get())
585       break;
586   }
587   if (!anObject.get()) {
588     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
589     if (!theIO.IsNull()) {
590       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
591     }
592     anObject = myWorkshop->module()->findPresentedObject(anAISObj);
593   }
594   return anObject;
595 }
596
597 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
598 {
599   bool aWasEnabled = myEnableUpdateViewer;
600
601   myEnableUpdateViewer = isEnabled;
602
603   return aWasEnabled;
604 }
605
606 void XGUI_Displayer::updateViewer() const
607 {
608   Handle(AIS_InteractiveContext) aContext = AISContext();
609   if (!aContext.IsNull() && myEnableUpdateViewer) {
610     myWorkshop->viewer()->Zfitall();
611     aContext->UpdateCurrentViewer();
612   }
613 }
614
615 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
616                                  const int theMode, const bool theUpdateViewer) const
617 {
618   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
619   aContext->Activate(theIO, theMode, theUpdateViewer);
620
621 #ifdef DEBUG_ACTIVATE_AIS
622   ObjectPtr anObject = getObject(theIO);
623   anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
624   qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
625 #endif
626 }
627
628 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
629 {
630   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
631   if (theMode == -1)
632     aContext->Deactivate(theIO);
633   else
634     aContext->Deactivate(theIO, theMode);
635
636 #ifdef DEBUG_DEACTIVATE_AIS
637   ObjectPtr anObject = getObject(theIO);
638   anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
639   qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
640 #endif
641 }
642
643 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
644 {
645   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
646   if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
647     aContext->OpenLocalContext();
648     deactivateTrihedron();
649   }
650   return aContext;
651 }
652
653 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
654 {
655   Handle(AIS_InteractiveContext) aContext = AISContext();
656   if (myAndFilter.IsNull() && !aContext.IsNull()) {
657     myAndFilter = new SelectMgr_AndFilter();
658     aContext->AddFilter(myAndFilter);
659   }
660   return myAndFilter;
661 }
662
663 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool theUpdateViewer)
664 {
665   Handle(AIS_InteractiveContext) aContext = AISContext();
666   if (aContext.IsNull())
667     return;
668   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
669   if (!anAISIO.IsNull()) {
670     aContext->Display(anAISIO, theUpdateViewer);
671     if (aContext->HasOpenedContext()) {
672       if (myActiveSelectionModes.size() == 0)
673         activateAIS(anAISIO, 0, theUpdateViewer);
674       else {
675         foreach(int aMode, myActiveSelectionModes) {
676           activateAIS(anAISIO, aMode, theUpdateViewer);
677         }
678       }
679     }
680   }
681 }
682
683 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
684 {
685   Handle(AIS_InteractiveContext) aContext = AISContext();
686   if (aContext.IsNull())
687     return;
688   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
689   if (!anAISIO.IsNull()) {
690     aContext->Remove(anAISIO, theUpdateViewer);
691   }
692 }
693
694
695 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
696 {
697   if (theMode == NoMode)
698     return;
699
700   Handle(AIS_InteractiveContext) aContext = AISContext();
701   if (aContext.IsNull())
702     return;
703
704   AISObjectPtr aAISObj = getAISObject(theObject);
705   if (!aAISObj)
706     return;
707
708   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
709   aContext->SetDisplayMode(aAISIO, theMode, false);
710   // Redisplay in order to update new mode because it could be not computed before
711   if (theUpdateViewer)
712     updateViewer();
713 }
714
715 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
716 {
717   Handle(AIS_InteractiveContext) aContext = AISContext();
718   if (aContext.IsNull())
719     return NoMode;
720
721   AISObjectPtr aAISObj = getAISObject(theObject);
722   if (!aAISObj)
723     return NoMode;
724
725   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
726   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
727 }
728
729 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
730 {
731   Handle(AIS_InteractiveContext) aContext = AISContext();
732   if (aContext.IsNull() || hasSelectionFilter(theFilter))
733     return;
734
735   Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
736   aCompFilter->Add(theFilter);
737 #ifdef DEBUG_SELECTION_FILTERS
738   int aCount = GetFilter()->StoredFilters().Extent();
739   qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
740 #endif
741 }
742
743 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
744 {
745   Handle(AIS_InteractiveContext) aContext = AISContext();
746   if (aContext.IsNull())
747     return;
748   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
749   if (aCompositeFilter->IsIn(theFilter))
750     aCompositeFilter->Remove(theFilter);
751 #ifdef DEBUG_SELECTION_FILTERS
752   int aCount = GetFilter()->StoredFilters().Extent();
753   qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
754 #endif
755 }
756
757 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
758 {
759   bool aFilterFound = false;
760
761   Handle(AIS_InteractiveContext) aContext = AISContext();
762   if (aContext.IsNull())
763     return aFilterFound;
764   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
765   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
766   for (; aIt.More() && !aFilterFound; aIt.Next()) {
767     if (theFilter.Access() == aIt.Value().Access())
768       aFilterFound = true;
769   }
770   Handle(SelectMgr_CompositionFilter) aCompFilter = GetFilter();
771   const SelectMgr_ListOfFilter& aStoredFilters = aCompFilter->StoredFilters();
772   for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
773     if (theFilter.Access() == aIt.Value().Access())
774       aFilterFound = true;
775   }
776   return aFilterFound;
777 }
778
779 void XGUI_Displayer::removeFilters()
780 {
781   Handle(AIS_InteractiveContext) aContext = AISContext();
782   if (aContext.IsNull())
783     return;
784   GetFilter()->Clear();
785 }
786
787 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
788 {
789   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
790   foreach(ObjectPtr aObj, aDispList) {
791     if (!theList.contains(aObj))
792       erase(aObj, false);
793   }
794   foreach(ObjectPtr aObj, theList) {
795     if (!isVisible(aObj))
796       display(aObj, false);
797   }
798   updateViewer();
799 }
800
801 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
802
803   if (!isVisible(theObject))
804     return false;
805
806   AISObjectPtr aAISObj = getAISObject(theObject);
807   if (aAISObj.get() == NULL)
808     return false;
809
810   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
811   return ::canBeShaded(anAIS);
812 }
813
814 void XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
815                               const QIntList& theModes,
816                               const bool theUpdateViewer) const
817 {
818   Handle(AIS_InteractiveContext) aContext = AISContext();
819   if (aContext.IsNull() || theIO.IsNull())
820     return;
821
822   // deactivate object in all modes, which are not in the list of activation
823   // It seems that after the IO deactivation the selected state of the IO's owners
824   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
825   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
826   // only if there is a difference in the current modes and the parameters modes.
827   // If the selection problem happens again, it is possible to write a test scenario and create
828   // a bug. The bug steps are the following:
829   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
830   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
831   TColStd_ListOfInteger aTColModes;
832   aContext->ActivatedModes(theIO, aTColModes);
833   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
834   QIntList aModesActivatedForIO;
835   bool isDeactivated = false;
836   for (; itr.More(); itr.Next() ) {
837     Standard_Integer aMode = itr.Value();
838     if (!theModes.contains(aMode)) {
839       deactivateAIS(theIO, aMode);
840       isDeactivated = true;
841     }
842     else {
843       aModesActivatedForIO.append(aMode);
844     }
845   }
846   if (isDeactivated) {
847     // the selection from the previous activation modes should be cleared manually (#26172)
848     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
849     if (theUpdateViewer)
850       updateViewer();
851   }
852
853   // loading the interactive object allowing the decomposition
854   if (aTColModes.IsEmpty()) {
855     aContext->Load(theIO, -1, true);
856   }
857
858   // trihedron AIS check should be after the AIS loading.
859   // If it is not loaded, it is steel selectable in the viewer.
860   Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
861   if (aTrihedron.IsNull()) {
862       //aContext->Load(anAISIO, -1, true);
863       // In order to clear active modes list
864     if (theModes.size() == 0) {
865       //aContext->Load(anAISIO, 0, true);
866       activateAIS(theIO, 0, theUpdateViewer);
867     } else {
868       foreach(int aMode, theModes) {
869         //aContext->Load(anAISIO, aMode, true);
870         if (!aModesActivatedForIO.contains(aMode)) {
871           activateAIS(theIO, aMode, theUpdateViewer);
872         }
873       }
874     }
875   }
876 }
877
878 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
879 {
880   AISObjectPtr anAISObj = getAISObject(theObject);
881   // correct the result's color it it has the attribute
882   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
883
884   // Customization of presentation
885   GeomCustomPrsPtr aCustomPrs;
886   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
887   if (aFeature.get() != NULL) {
888     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
889     if (aCustPrs.get() != NULL)
890       aCustomPrs = aCustPrs;
891   }
892   if (aCustomPrs.get() == NULL) {
893     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
894     // we ignore presentable not customized objects
895     if (aPrs.get() == NULL)
896       aCustomPrs = myCustomPrs;
897   }
898   bool isCustomized = aCustomPrs.get() &&
899                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
900   myWorkshop->module()->customizeObject(theObject);
901   return isCustomized;
902 }
903
904
905 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
906 {
907   if (!isVisible(theObject))
908     return Qt::black;
909
910   AISObjectPtr anAISObj = getAISObject(theObject);
911   int aR, aG, aB;
912   anAISObj->getColor(aR, aG, aB);
913   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
914   if (theUpdateViewer)
915     updateViewer();
916   return QColor(aR, aG, aB);
917 }
918
919 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
920 {
921   myResult2AISObjectMap[theObject] = theAIS;
922
923 #ifdef DEBUG_DISPLAY
924   std::ostringstream aPtrStr;
925   aPtrStr << theObject.get();
926   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
927   qDebug(getResult2AISObjectMapInfo().c_str());
928 #endif
929 }
930
931 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
932 {
933   QStringList aContent;
934   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
935     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
936     std::ostringstream aPtrStr;
937     aPtrStr << "aObj = " << aObj.get() << ":";
938     aPtrStr << "anAIS = " << aAISObj.get() << ":";
939     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
940     
941     aContent.append(aPtrStr.str().c_str());
942   }
943   return QString("myResult2AISObjectMap: size = %1\n%2").arg(myResult2AISObjectMap.size()).
944                                             arg(aContent.join("\n")).toStdString().c_str();
945 }