Salome HOME
be653fd0ed79c101ac0b2f83efcca3891804a2fe
[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 #ifndef HAVE_SALOME
15 #include <AppElements_Viewer.h>
16 #endif
17
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeIntArray.h>
23 #include <ModelAPI_ResultCompSolid.h>
24
25 #include <ModuleBase_ResultPrs.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_ViewerPrs.h>
29
30 #include <GeomAPI_Shape.h>
31 #include <GeomAPI_IPresentable.h>
32 #include <GeomAPI_ICustomPrs.h>
33
34 #include <AIS_InteractiveContext.hxx>
35 #include <AIS_LocalContext.hxx>
36 #include <AIS_ListOfInteractive.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
38 #include <AIS_DimensionSelectionMode.hxx>
39 #include <AIS_Shape.hxx>
40 #include <AIS_Dimension.hxx>
41 #include <AIS_Trihedron.hxx>
42 #include <AIS_Axis.hxx>
43 #include <AIS_Plane.hxx>
44 #include <AIS_Point.hxx>
45 #include <AIS_Selection.hxx>
46 #include <TColStd_ListIteratorOfListOfInteger.hxx>
47 #include <SelectMgr_ListOfFilter.hxx>
48 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
49 #include <Prs3d_Drawer.hxx>
50 #include <Prs3d_IsoAspect.hxx>
51 #include <SelectMgr_SelectionManager.hxx>
52
53 #include <StdSelect_ViewerSelector3d.hxx>
54
55 #include <TColStd_MapOfTransient.hxx>
56 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
57
58 #include <Events_Loop.h>
59 #include <ModelAPI_Events.h>
60
61 #include <set>
62
63 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse selection sensitivity
64
65 //#define DEBUG_ACTIVATE_OBJECTS
66 //#define DEBUG_DEACTIVATE
67 //#define DEBUG_ACTIVATE_AIS
68 //#define DEBUG_DEACTIVATE_AIS
69
70 //#define DEBUG_DISPLAY
71 //#define DEBUG_FEATURE_REDISPLAY
72 //#define DEBUG_SELECTION_FILTERS
73
74 //#define DEBUG_COMPOSILID_DISPLAY
75 // Workaround for bug #25637
76
77 //#define DEBUG_OCCT_SHAPE_SELECTION
78
79 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
80 {
81   // Get from null point
82   theAIS->DisplayedObjects(theList, true);
83   if (theAIS->HasOpenedContext()) {
84     // get from local context
85     const Handle(AIS_LocalContext)& aLC = theAIS->LocalContext();
86     TColStd_MapOfTransient aMap;
87     int NbDisp = aLC->DisplayedObjects(aMap);
88     TColStd_MapIteratorOfMapOfTransient aIt(aMap);
89
90     Handle(AIS_InteractiveObject) curIO;
91     Handle(Standard_Transient) Tr;
92     for(; aIt.More(); aIt.Next()){
93       Tr = aIt.Key();
94       curIO = *((Handle(AIS_InteractiveObject)*) &Tr);
95       theList.Append(curIO);
96     }
97   }
98 }
99
100 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
101 {
102   QStringList anInfo;
103   QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
104   for (; anIt != aLast; anIt++) {
105     anInfo.append(QString::number(*anIt));
106   }
107   return anInfo.join(theSeparator);
108 }
109
110 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
111   : myWorkshop(theWorkshop), myNeedUpdate(false),
112   myIsTrihedronActive(true), myViewerBlockedRecursiveCount(0)
113 {
114   myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
115 }
116
117 XGUI_Displayer::~XGUI_Displayer()
118 {
119 }
120
121 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
122 {
123   return myResult2AISObjectMap.contains(theObject);
124 }
125
126 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
127 {
128   bool aDisplayed = false;
129   if (isVisible(theObject)) {
130 #ifdef DEBUG_COMPOSILID_DISPLAY
131     ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
132     if (aCompsolidResult.get()) {
133       for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
134         ResultPtr aSubResult = aCompsolidResult->subResult(i);
135         if (aSubResult.get())
136           redisplay(aSubResult, false);
137       }
138       if (theUpdateViewer)
139         updateViewer();
140     }
141     else
142 #endif
143     aDisplayed = redisplay(theObject, theUpdateViewer);
144   } else {
145     AISObjectPtr anAIS;
146     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
147     bool isShading = false;
148     if (aPrs.get() != NULL) {
149       anAIS = aPrs->getAISObject(anAIS);
150     } else {
151       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
152       if (aResult.get() != NULL) {
153 #ifdef DEBUG_COMPOSILID_DISPLAY
154         ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
155         if (aCompsolidResult.get()) {
156           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
157             ResultPtr aSubResult = aCompsolidResult->subResult(i);
158             if (aSubResult.get())
159               display(aSubResult, false);
160           }
161           if (theUpdateViewer)
162             updateViewer();
163         }
164         else {
165 #endif
166         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
167         if (aShapePtr.get() != NULL) {
168           anAIS = AISObjectPtr(new GeomAPI_AISObject());
169           Handle(AIS_InteractiveObject) anAISPrs = myWorkshop->module()->createPresentation(aResult);
170           if (anAISPrs.IsNull())
171             anAISPrs = new ModuleBase_ResultPrs(aResult);
172           anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
173           //anAIS->createShape(aShapePtr);
174           isShading = true;
175         }
176 #ifdef DEBUG_COMPOSILID_DISPLAY
177         } // close else
178 #endif
179       }
180     }
181     if (anAIS)
182       aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
183   }
184   return aDisplayed;
185 }
186
187 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
188 {
189   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
190   if (!aShapePrs.IsNull()) {
191     TopoDS_Shape aShape = aShapePrs->Shape();
192     if (aShape.IsNull())
193       return false;
194     TopAbs_ShapeEnum aType = aShape.ShapeType();
195     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
196       return false;
197     else {
198       // Check that the presentation is not a sketch
199       return theModule->canBeShaded(theAIS);
200     }
201   }
202   return false;
203 }
204
205 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS, 
206                              bool isShading, bool theUpdateViewer)
207 {
208   bool aDisplayed = false;
209
210   Handle(AIS_InteractiveContext) aContext = AISContext();
211   if (aContext.IsNull())
212     return aDisplayed;
213
214   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
215   if (!anAISIO.IsNull()) {
216     appendResultObject(theObject, theAIS);
217
218     bool isCustomized = customizeObject(theObject);
219
220     int aDispMode = isShading? Shading : Wireframe;
221     if (isShading)
222       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
223     anAISIO->SetDisplayMode(aDispMode);
224     aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed); 
225     aDisplayed = true;
226
227     emit objectDisplayed(theObject, theAIS);
228     activate(anAISIO, myActiveSelectionModes, theUpdateViewer);
229     // the fix from VPA for more suitable selection of sketcher lines
230     if(anAISIO->Width() > 1) {
231       for(int aModeIdx = 0; aModeIdx < myActiveSelectionModes.length(); ++aModeIdx) {
232         int aMode = myActiveSelectionModes.value(aModeIdx);
233         double aPrecision = (aMode == getSelectionMode(TopAbs_VERTEX))? 20 : 
234                                                     (anAISIO->Width() + 2);
235         aContext->SetSelectionSensitivity(anAISIO, aMode, aPrecision);
236       }
237     }
238   } 
239   if (theUpdateViewer)
240     updateViewer();
241
242   return aDisplayed;
243 }
244
245 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
246 {
247   bool aErased = false;
248   if (!isVisible(theObject))
249     return aErased;
250
251   Handle(AIS_InteractiveContext) aContext = AISContext();
252   if (aContext.IsNull())
253     return aErased;
254
255   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
256   if (anObject) {
257     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
258     if (!anAIS.IsNull()) {
259       emit beforeObjectErase(theObject, anObject);
260       aContext->Remove(anAIS, false/*update viewer*/);
261       aErased = true;
262     }
263   }
264   myResult2AISObjectMap.remove(theObject);
265
266 #ifdef DEBUG_DISPLAY
267   std::ostringstream aPtrStr;
268   aPtrStr << theObject.get();
269   qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
270   qDebug(getResult2AISObjectMapInfo().c_str());
271 #endif
272
273   if (theUpdateViewer)
274     updateViewer();
275
276   return aErased;
277 }
278
279 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
280 {
281   bool aRedisplayed = false;
282   if (!isVisible(theObject))
283     return aRedisplayed;
284
285   AISObjectPtr aAISObj = getAISObject(theObject);
286   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
287
288   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
289   if (aPrs) {
290     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
291     if (!aAIS_Obj) {
292       aRedisplayed = erase(theObject, theUpdateViewer);
293       return aRedisplayed;
294     }
295     if (aAIS_Obj != aAISObj) {
296       appendResultObject(theObject, aAIS_Obj);
297     }
298     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
299   }
300
301   Handle(AIS_InteractiveContext) aContext = AISContext();
302   if (!aContext.IsNull() && !aAISIO.IsNull()) {
303     // Check that the visualized shape is the same and the redisplay is not necessary
304     // Redisplay of AIS object leads to this object selection compute and the selection 
305     // in the browser is lost
306     // this check is not necessary anymore because the selection store/restore is realized
307     // before and after the values modification.
308     // Moreother, this check avoids customize and redisplay presentation if the presentable
309     // parameter is changed.
310     bool isEqualShapes = false;
311     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
312     if (aResult.get() != NULL) {
313       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
314       if (!aShapePrs.IsNull()) {
315         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
316         if (aShapePtr.get()) {
317           const TopoDS_Shape& aOldShape = aShapePrs->Shape();
318           if (!aOldShape.IsNull())
319             isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
320         }
321       }
322     }
323     // Customization of presentation
324     bool isCustomized = customizeObject(theObject);
325     #ifdef DEBUG_FEATURE_REDISPLAY
326       qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
327         arg(!isEqualShapes || isCustomized).arg(isEqualShapes).arg(isCustomized).toStdString().c_str());
328     #endif
329     if (!isEqualShapes || isCustomized) {
330       /// if shapes are equal and presentation are customized, selection should be restored
331       bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
332       if (aNeedToRestoreSelection)
333         myWorkshop->module()->storeSelection();
334
335       aContext->Redisplay(aAISIO, false);
336
337       if (aNeedToRestoreSelection)
338         myWorkshop->module()->restoreSelection();
339
340       aRedisplayed = true;
341       #ifdef DEBUG_FEATURE_REDISPLAY
342         qDebug("  Redisplay happens");
343       #endif
344       if (theUpdateViewer)
345         updateViewer();
346     }
347   }
348   return aRedisplayed;
349 }
350
351 void XGUI_Displayer::redisplayObjects()
352 {
353   // redisplay objects visualized in the viewer
354   static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
355   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
356   QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
357   QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
358   for (; anIt != aLast; anIt++) {
359     aECreator->sendUpdated(*anIt, EVENT_DISP);
360   }
361   Events_Loop::loop()->flush(EVENT_DISP);
362 }
363
364 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
365 {
366 #ifdef DEBUG_DEACTIVATE
367   QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
368   qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
369     arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
370     arg(anInfoStr).
371     toStdString().c_str());
372 #endif
373   Handle(AIS_InteractiveContext) aContext = AISContext();
374   if (!aContext.IsNull() && isVisible(theObject)) {
375     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
376     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
377
378     deactivateAIS(anAIS);
379     // the selection from the previous activation modes should be cleared manually (#26172)
380     aContext->LocalContext()->ClearOutdatedSelection(anAIS, true);
381     if (theUpdateViewer)
382       updateViewer();
383   }
384 }
385
386 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
387                                        const bool theUpdateViewer)
388 {
389   //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
390   //if (!aTrihedron.IsNull())
391   //  deactivateAIS(aTrihedron);
392
393   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
394   for (; anIt != aLast; anIt++) {
395     deactivate(*anIt, false);
396   }
397   //VSV It seems that there is no necessity to update viewer on deactivation
398   //if (theUpdateViewer)
399   //  updateViewer();
400 }
401
402 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
403 {
404   Handle(AIS_InteractiveContext) aContext = AISContext();
405   if (aContext.IsNull() || !isVisible(theObject))
406     return;
407
408   AISObjectPtr aAISObj = getAISObject(theObject);
409
410   if (aAISObj.get() != NULL) {
411     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
412     TColStd_ListOfInteger aTColModes;
413     aContext->ActivatedModes(anAISIO, aTColModes);
414     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
415     for (; itr.More(); itr.Next() ) {
416       theModes.append(itr.Value());
417     }
418   }
419 }
420
421 int XGUI_Displayer::getSelectionMode(int theShapeType)
422 {
423   return (theShapeType >= TopAbs_SHAPE)? theShapeType : 
424     AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
425 }
426
427 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
428 {
429   bool aVisible = false;
430   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
431   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
432   if (aPrs.get() || aResult.get()) {
433     aVisible = theDisplayer->isVisible(theObject);
434     // compsolid is not visualized in the viewer, but should have presentation when all sub solids are
435     // visible. It is useful for highlight presentation where compsolid shape is selectable
436     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
437       ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
438       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
439         bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
440         for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
441           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
442         }
443         aVisible = anAllSubsVisible;
444       }
445     }
446   }
447   // it is possible that feature is presentable and has results, so we should check visibility
448   // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
449   if (!aVisible) {
450     // check if all results of the feature are visible
451     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
452     std::list<ResultPtr> aResults = aFeature->results();
453     std::list<ResultPtr>::const_iterator aIt;
454     aVisible = !aResults.empty();
455     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
456       aVisible = aVisible && theDisplayer->isVisible(*aIt);
457     }
458   }
459   return aVisible;
460 }
461
462 #ifdef DEBUG_ACTIVATE_OBJECTS
463 QString getModeInfo(const int theMode)
464 {
465   QString anInfo = "Undefined";
466   switch(theMode) {
467     case 0: anInfo = "SHAPE(0)"; break;
468     case 1: anInfo = "VERTEX(1)"; break;
469     case 2: anInfo = "EDGE(2)"; break;
470     case 3: anInfo = "WIRE(3)"; break;
471     case 4: anInfo = "FACE(4)"; break;
472     case 5: anInfo = "SHELL(5)"; break;
473     case 6: anInfo = "SOLID(6)"; break;
474     case 7: anInfo = "COMPSOLID(7)"; break;
475     case 8: anInfo = "COMPOUND(8)"; break;
476     case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
477     case 101: anInfo = "Sel_Constraint(101)"; break;
478     case 102: anInfo = "Sel_Dimension_All(102)"; break;
479     case 103: anInfo = "Sel_Dimension_Line(103)"; break;
480     case 104: anInfo = "Sel_Dimension_Text(104)"; break;
481     default: break;
482   }
483   return anInfo;
484 }
485
486 QString getModesInfo(const QIntList& theModes)
487 {
488   QStringList aModesInfo;
489   for (int i = 0, aSize = theModes.size(); i < aSize; i++)
490     aModesInfo.append(getModeInfo(theModes[i]));
491   return QString("[%1] = %2").arg(aModesInfo.size()).arg(aModesInfo.join(", "));
492 }
493 #endif
494
495 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
496                                      const bool theUpdateViewer)
497 {
498   // Convert shape types to selection types
499   QIntList aModes;
500   foreach(int aType, theModes) {
501     aModes.append(getSelectionMode(aType));
502   }
503
504 #ifdef DEBUG_ACTIVATE_OBJECTS
505   QStringList anInfo;
506   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
507   for (; anIt != aLast; ++anIt) {
508     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
509   }
510   QString anInfoStr = anInfo.join(", ");
511
512   qDebug(QString("activateObjects: new modes%1, active modes%2, objects[%3] = %4").
513     arg(getModesInfo(aModes)).
514     arg(getModesInfo(myActiveSelectionModes)).
515     arg(theObjList.size()).
516     arg(anInfoStr).
517     toStdString().c_str());
518 #endif
519   // In order to avoid doblications of selection modes
520   QIntList aNewModes;
521   foreach (int aMode, aModes) {
522     if (!aNewModes.contains(aMode))
523       aNewModes.append(aMode);
524   }
525   myActiveSelectionModes = aNewModes;
526   Handle(AIS_InteractiveContext) aContext = AISContext();
527   // Open local context if there is no one
528   if (aContext.IsNull() || !aContext->HasOpenedContext()) 
529     return;
530
531   //aContext->UseDisplayedObjects();
532   //myUseExternalObjects = true;
533
534   Handle(AIS_InteractiveObject) anAISIO;
535   AIS_ListOfInteractive aPrsList;
536   //if (aObjList.isEmpty())
537   //  return;
538   //else {
539   foreach(ObjectPtr aObj, theObjList) {
540     if (myResult2AISObjectMap.contains(aObj))
541       aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
542   }
543   //}
544
545   // Add trihedron because it has to partisipate in selection
546   Handle(AIS_InteractiveObject) aTrihedron;
547   if (isTrihedronActive()) {
548     aTrihedron = getTrihedron();
549     if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
550       aPrsList.Append(aTrihedron);
551   }
552   if (aPrsList.Extent() == 0)
553     return;
554
555   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
556   bool isActivationChanged = false;
557   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
558     anAISIO = aLIt.Value();
559     if (activate(anAISIO, myActiveSelectionModes, false))
560       isActivationChanged = true;
561   }
562   if (!aTrihedron.IsNull()) {
563     foreach(int aMode, myActiveSelectionModes)
564       aContext->SetSelectionSensitivity(aTrihedron, aMode, 20);
565   }
566   // VSV It seems that there is no necessity to update viewer on activation
567   //if (theUpdateViewer && isActivationChanged)
568   //  updateViewer();
569 }
570
571 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
572 {
573   Handle(AIS_InteractiveContext) aContext = AISContext();
574   if (aContext.IsNull() || !isVisible(theObject))
575     return false;
576     
577   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
578   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
579
580   TColStd_ListOfInteger aModes;
581   aContext->ActivatedModes(anAIS, aModes);
582   return aModes.Extent() > 0;
583 }
584 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues, bool theUpdateViewer)
585 {
586   Handle(AIS_InteractiveContext) aContext = AISContext();
587   if (aContext.IsNull())
588     return;
589   if (aContext->HasOpenedContext()) {
590     aContext->UnhilightSelected(false);
591     aContext->ClearSelected(false);
592     NCollection_Map<TopoDS_Shape> aShapesToBeSelected;
593
594     foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
595       const GeomShapePtr& aGeomShape = aPrs->shape();
596       if (aGeomShape.get() && !aGeomShape->isNull()) {
597         const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
598 #ifdef DEBUG_OCCT_SHAPE_SELECTION
599         aContext->AddOrRemoveSelected(aShape, false);
600 #else
601         aShapesToBeSelected.Add(aShape);
602 #endif
603       } else {
604         ObjectPtr anObject = aPrs->object();
605         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
606         if (aResult.get() && isVisible(aResult)) {
607           AISObjectPtr anObj = myResult2AISObjectMap[aResult];
608           Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
609           if (!anAIS.IsNull()) {
610             // The methods are replaced in order to provide multi-selection, e.g. restore selection
611             // by activating multi selector widget. It also gives an advantage that the multi
612             // selection in OB gives multi-selection in the viewer
613             //aContext->SetSelected(anAIS, false);
614             // The selection in the context was cleared, so the method sets the objects are selected
615             aContext->AddOrRemoveSelected(anAIS, false);
616           }
617         }
618       }
619     }
620     if (!aShapesToBeSelected.IsEmpty())
621       XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
622   } else {
623     aContext->UnhilightCurrents(false);
624     aContext->ClearCurrents(false);
625     foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
626       ObjectPtr anObject = aPrs->object();
627       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
628       if (aResult.get() && isVisible(aResult)) {
629         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
630         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
631         if (!anAIS.IsNull())
632           aContext->SetCurrentObject(anAIS, false);
633       }
634     }
635   }
636   if (theUpdateViewer)
637     updateViewer();
638 }
639
640 void XGUI_Displayer::clearSelected()
641 {
642   Handle(AIS_InteractiveContext) aContext = AISContext();
643   if (!aContext.IsNull()) {
644     aContext->UnhilightCurrents(false);
645     aContext->ClearSelected();
646   }
647 }
648
649 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
650 {
651   bool aErased = false;
652   Handle(AIS_InteractiveContext) aContext = AISContext();
653   if (!aContext.IsNull()) {
654     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
655       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
656       // erase an object
657       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
658       if (!anIO.IsNull()) {
659         emit beforeObjectErase(aObj, aAISObj);
660         aContext->Remove(anIO, false/*update viewer*/);
661         aErased = true;
662       }
663     }
664     if (theUpdateViewer)
665       updateViewer();
666   }
667   myResult2AISObjectMap.clear();
668 #ifdef DEBUG_DISPLAY
669   qDebug("eraseAll");
670   qDebug(getResult2AISObjectMapInfo().c_str());
671 #endif
672   return aErased;
673 }
674
675 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
676                       Handle(AIS_InteractiveObject) theObject,
677                       const bool theClear = true)
678 {
679   if (!theObject.IsNull()) {
680     theContext->Deactivate(theObject);
681     //if (theClear) {
682       //theObject->ClearSelected();
683       //  theContext->LocalContext()->ClearOutdatedSelection(theObject, true);
684     //}
685   }
686 }
687
688 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
689 {
690   Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
691   Handle(AIS_InteractiveContext) aContext = AISContext();
692   if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
693     Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
694     deactivateObject(aContext, aTrie);
695
696     /// #1136 hidden axis are selected in sketch
697     /// workaround for Cascade: there is a crash in AIS_LocalContext::ClearOutdatedSelection
698     /// for Position AIS object in SelectionModes.
699     deactivateObject(aContext, aTrie->XAxis());
700     deactivateObject(aContext, aTrie->YAxis());
701     deactivateObject(aContext, aTrie->Axis());
702     deactivateObject(aContext, aTrie->Position());
703
704     deactivateObject(aContext, aTrie->XYPlane());
705     deactivateObject(aContext, aTrie->XZPlane());
706     deactivateObject(aContext, aTrie->YZPlane());
707
708     if (theUpdateViewer)
709       updateViewer();
710   }
711 }
712
713 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
714 {
715   return myWorkshop->viewer()->trihedron();
716 }
717
718 void XGUI_Displayer::openLocalContext()
719 {
720   Handle(AIS_InteractiveContext) aContext = AISContext();
721   // Open local context if there is no one
722   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
723     // Preserve selected objects
724     //AIS_ListOfInteractive aAisList;
725     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
726     //  aAisList.Append(aContext->Current());
727
728     // get the filters from the global context and append them to the local context
729     // a list of filters in the global context is not cleared and should be cleared here
730     SelectMgr_ListOfFilter aFilters;
731     aFilters.Assign(aContext->Filters());
732     // it is important to remove the filters in the global context, because there is a code
733     // in the closeLocalContex, which restore the global context filters
734     aContext->RemoveFilters();
735
736     //aContext->ClearCurrents();
737     aContext->OpenLocalContext();
738     //deactivateTrihedron();
739     //aContext->NotUseDisplayedObjects();
740
741     //myUseExternalObjects = false;
742
743     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
744     for (;aIt.More(); aIt.Next()) {
745       aContext->AddFilter(aIt.Value());
746     }
747     // Restore selection
748     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
749     //for(; aIt2.More(); aIt2.Next()) {
750     //  aContext->SetSelected(aIt2.Value(), false);
751     //}
752   }
753 }
754
755 void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
756 {
757   Handle(AIS_InteractiveContext) aContext = AISContext();
758   if (!aContext.IsNull() && aContext->HasOpenedContext()) {
759     // Preserve selected objects
760     //AIS_ListOfInteractive aAisList;
761     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
762     //  aAisList.Append(aContext->SelectedInteractive());
763
764     // get the filters from the local context and append them to the global context
765     // a list of filters in the local context is cleared
766     SelectMgr_ListOfFilter aFilters;
767     aFilters.Assign(aContext->Filters());
768
769     //aContext->ClearSelected();
770     aContext->CloseAllContexts(false);
771
772     // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
773     // this code is obsolete. It is temporaty commented and should be removed after
774     // the test campaign.
775     // Redisplay all object if they were displayed in localContext
776     /*Handle(AIS_InteractiveObject) aAISIO;
777     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
778       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
779       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
780         aContext->Display(aAISIO, false);
781         aContext->SetDisplayMode(aAISIO, Shading, false);
782       }
783     }*/
784
785     // Append the filters from the local selection in the global selection context
786     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
787     for (;aIt.More(); aIt.Next()) {
788       Handle(SelectMgr_Filter) aFilter = aIt.Value();
789       aContext->AddFilter(aFilter);
790     }
791
792     if (theUpdateViewer)
793       updateViewer();
794     //myUseExternalObjects = false;
795
796     // Restore selection
797     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
798     //for(; aIt2.More(); aIt2.Next()) {
799     //  if (aContext->IsDisplayed(aIt2.Value()))
800     //    aContext->SetCurrentObject(aIt2.Value(), false);
801     //}
802   }
803 }
804
805 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
806 {
807   AISObjectPtr anIO;
808   if (myResult2AISObjectMap.contains(theObject))
809     anIO = myResult2AISObjectMap[theObject];
810   return anIO;
811 }
812
813 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
814 {
815   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
816   return getObject(aRefAIS);
817 }
818
819 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
820 {
821   ObjectPtr anObject;
822   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
823     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
824     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
825     if (anAIS == theIO)
826       anObject = anObj;
827     if (anObject.get())
828       break;
829   }
830   if (!anObject.get()) {
831     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
832     if (!theIO.IsNull()) {
833       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
834     }
835     anObject = myWorkshop->module()->findPresentedObject(anAISObj);
836   }
837   return anObject;
838 }
839
840 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
841 {
842   bool aWasEnabled = isUpdateEnabled();
843   if (isEnabled)
844     myViewerBlockedRecursiveCount--;
845   else
846     myViewerBlockedRecursiveCount++;
847
848   if (myNeedUpdate && isUpdateEnabled()) {
849     updateViewer();
850     myNeedUpdate = false;
851   }
852   return aWasEnabled;
853 }
854
855 bool XGUI_Displayer::isUpdateEnabled() const
856 {
857   return myViewerBlockedRecursiveCount == 0;
858 }
859
860 void XGUI_Displayer::updateViewer() const
861 {
862   Handle(AIS_InteractiveContext) aContext = AISContext();
863   if (!aContext.IsNull() && isUpdateEnabled()) {
864     myWorkshop->viewer()->Zfitall();
865     aContext->UpdateCurrentViewer();
866   } else {
867     myNeedUpdate = true;
868   }
869 }
870
871 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
872                                  const int theMode, const bool theUpdateViewer) const
873 {
874   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
875   if (!aContext.IsNull()) {
876     aContext->Activate(theIO, theMode, false);
877
878 #ifdef DEBUG_ACTIVATE_AIS
879     ObjectPtr anObject = getObject(theIO);
880     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
881     qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
882 #endif
883     if (theUpdateViewer)
884       updateViewer();
885   }
886 }
887
888 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
889 {
890   Handle(AIS_InteractiveContext) aContext = AISContext();
891   if (!aContext.IsNull()) {
892     if (theMode == -1)
893       aContext->Deactivate(theIO);
894     else
895       aContext->Deactivate(theIO, theMode);
896
897 #ifdef DEBUG_DEACTIVATE_AIS
898     ObjectPtr anObject = getObject(theIO);
899     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
900     qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
901 #endif
902   }
903 }
904
905 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
906 {
907   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
908   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
909     aContext->OpenLocalContext();
910     if (!isTrihedronActive())
911       deactivateTrihedron(true);
912     aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
913     aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
914   }
915   return aContext;
916 }
917
918 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
919 {
920   Handle(AIS_InteractiveContext) aContext = AISContext();
921   if (!aContext.IsNull() && myAndFilter.IsNull()) {
922     myAndFilter = new SelectMgr_AndFilter();
923     aContext->AddFilter(myAndFilter);
924   }
925   return myAndFilter;
926 }
927
928 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
929                                 bool theUpdateViewer)
930 {
931   bool aDisplayed = false;
932   Handle(AIS_InteractiveContext) aContext = AISContext();
933   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
934   if (!aContext.IsNull() && !anAISIO.IsNull()) {
935     aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
936     aDisplayed = true;
937     aContext->Deactivate(anAISIO);
938     aContext->Load(anAISIO);
939     if (toActivateInSelectionModes) {
940       if (aContext->HasOpenedContext()) {
941         if (myActiveSelectionModes.size() == 0)
942           activateAIS(anAISIO, 0, theUpdateViewer);
943         else {
944           foreach(int aMode, myActiveSelectionModes) {
945             activateAIS(anAISIO, aMode, theUpdateViewer);
946           }
947         }
948       }
949     }
950     if (theUpdateViewer)
951       updateViewer();
952   }
953   return aDisplayed;
954 }
955
956 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
957 {
958   bool aErased = false;
959   Handle(AIS_InteractiveContext) aContext = AISContext();
960   if (!aContext.IsNull()) {
961     Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
962     if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
963       aContext->Remove(anAISIO, false/*update viewer*/);
964       aErased = true;
965     }
966   }
967   if (aErased && theUpdateViewer)
968     updateViewer();
969   return aErased;
970 }
971
972
973 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
974 {
975   if (theMode == NoMode)
976     return;
977
978   Handle(AIS_InteractiveContext) aContext = AISContext();
979   if (aContext.IsNull())
980     return;
981
982   AISObjectPtr aAISObj = getAISObject(theObject);
983   if (!aAISObj)
984     return;
985
986   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
987   aContext->SetDisplayMode(aAISIO, theMode, false);
988   // Redisplay in order to update new mode because it could be not computed before
989   if (theUpdateViewer)
990     updateViewer();
991 }
992
993 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
994 {
995   Handle(AIS_InteractiveContext) aContext = AISContext();
996   if (aContext.IsNull())
997     return NoMode;
998
999   AISObjectPtr aAISObj = getAISObject(theObject);
1000   if (!aAISObj)
1001     return NoMode;
1002
1003   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1004   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
1005 }
1006
1007 void XGUI_Displayer::deactivateSelectionFilters()
1008 {
1009   Handle(AIS_InteractiveContext) aContext = AISContext();
1010   if (!myAndFilter.IsNull()) {
1011     bool aFound = false;
1012     if (!aContext.IsNull()) {
1013       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1014       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
1015       for (; anIt.More() && !aFound; anIt.Next()) {
1016         Handle(SelectMgr_Filter) aFilter = anIt.Value();
1017         aFound = aFilter == myAndFilter;
1018       }
1019       if (aFound)
1020         aContext->RemoveFilter(myAndFilter);
1021     }
1022     myAndFilter.Nullify();
1023   }
1024 }
1025
1026 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1027 {
1028   Handle(AIS_InteractiveContext) aContext = AISContext();
1029   if (aContext.IsNull() || hasSelectionFilter(theFilter))
1030     return;
1031
1032   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1033   if (!aCompositeFilter.IsNull()) {
1034     aCompositeFilter->Add(theFilter);
1035 #ifdef DEBUG_SELECTION_FILTERS
1036     int aCount = aCompositeFilter->StoredFilters().Extent();
1037     qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1038 #endif
1039   }
1040 }
1041
1042 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1043 {
1044   Handle(AIS_InteractiveContext) aContext = AISContext();
1045   if (aContext.IsNull())
1046     return;
1047
1048   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1049   if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1050     aCompositeFilter->Remove(theFilter);
1051 #ifdef DEBUG_SELECTION_FILTERS
1052     int aCount = aCompositeFilter->StoredFilters().Extent();
1053     qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1054 #endif
1055   }
1056 }
1057
1058 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1059 {
1060   bool aFilterFound = false;
1061
1062   Handle(AIS_InteractiveContext) aContext = AISContext();
1063   if (aContext.IsNull())
1064     return aFilterFound;
1065   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1066   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1067   for (; aIt.More() && !aFilterFound; aIt.Next()) {
1068     if (theFilter.Access() == aIt.Value().Access())
1069       aFilterFound = true;
1070   }
1071   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1072   if (!aCompositeFilter.IsNull()) {
1073     const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1074     for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1075       if (theFilter.Access() == aIt.Value().Access())
1076         aFilterFound = true;
1077     }
1078   }
1079   return aFilterFound;
1080 }
1081
1082 void XGUI_Displayer::removeFilters()
1083 {
1084   Handle(AIS_InteractiveContext) aContext = AISContext();
1085   if (aContext.IsNull())
1086     return;
1087
1088   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1089   if (!aCompositeFilter.IsNull())
1090     aCompositeFilter->Clear();
1091 }
1092
1093 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1094 {
1095   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1096   foreach(ObjectPtr aObj, aDispList) {
1097     if (!theList.contains(aObj))
1098       erase(aObj, false);
1099   }
1100   foreach(ObjectPtr aObj, theList) {
1101     if (!isVisible(aObj))
1102       display(aObj, false);
1103   }
1104   updateViewer();
1105 }
1106
1107 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1108
1109   if (!isVisible(theObject))
1110     return false;
1111
1112   AISObjectPtr aAISObj = getAISObject(theObject);
1113   if (aAISObj.get() == NULL)
1114     return false;
1115
1116   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1117   return ::canBeShaded(anAIS, myWorkshop->module());
1118 }
1119
1120 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1121                               const QIntList& theModes,
1122                               const bool theUpdateViewer) const
1123 {
1124   Handle(AIS_InteractiveContext) aContext = AISContext();
1125   if (aContext.IsNull() || theIO.IsNull())
1126     return false;
1127   
1128   bool isActivationChanged = false;
1129   // deactivate object in all modes, which are not in the list of activation
1130   // It seems that after the IO deactivation the selected state of the IO's owners
1131   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1132   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1133   // only if there is a difference in the current modes and the parameters modes.
1134   // If the selection problem happens again, it is possible to write a test scenario and create
1135   // a bug. The bug steps are the following:
1136   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1137   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1138   TColStd_ListOfInteger aTColModes;
1139   aContext->ActivatedModes(theIO, aTColModes);
1140   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1141   QIntList aModesActivatedForIO;
1142   bool isDeactivated = false;
1143   for (; itr.More(); itr.Next() ) {
1144     Standard_Integer aMode = itr.Value();
1145     if (!theModes.contains(aMode)) {
1146       deactivateAIS(theIO, aMode);
1147       isDeactivated = true;
1148     }
1149     else {
1150       aModesActivatedForIO.append(aMode);
1151     }
1152   }
1153   if (isDeactivated) {
1154     // the selection from the previous activation modes should be cleared manually (#26172)
1155     theIO->ClearSelected();
1156     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1157     // For performance issues
1158     //if (theUpdateViewer)
1159     //  updateViewer();
1160     isActivationChanged = true;
1161   }
1162
1163   // loading the interactive object allowing the decomposition
1164   if (aTColModes.IsEmpty()) {
1165     aContext->Load(theIO, -1, true);
1166   }
1167
1168   // trihedron AIS check should be after the AIS loading.
1169   // If it is not loaded, it is steel selectable in the viewer.
1170   Handle(AIS_Trihedron) aTrihedron;
1171   if (!isTrihedronActive())
1172     aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1173   if (aTrihedron.IsNull()) {
1174       // In order to clear active modes list
1175     if (theModes.size() == 0) {
1176       activateAIS(theIO, 0, theUpdateViewer);
1177     } else {
1178       foreach(int aMode, theModes) {
1179         if (!aModesActivatedForIO.contains(aMode)) {
1180           activateAIS(theIO, aMode, theUpdateViewer);
1181           isActivationChanged = true;
1182         }
1183       }
1184     }
1185   }
1186   return isActivationChanged;
1187 }
1188
1189 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1190 {
1191   AISObjectPtr anAISObj = getAISObject(theObject);
1192   // correct the result's color it it has the attribute
1193   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1194
1195   // Customization of presentation
1196   GeomCustomPrsPtr aCustomPrs;
1197   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1198   if (aFeature.get() != NULL) {
1199     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1200     if (aCustPrs.get() != NULL)
1201       aCustomPrs = aCustPrs;
1202   }
1203   if (aCustomPrs.get() == NULL) {
1204     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1205     // we ignore presentable not customized objects
1206     if (aPrs.get() == NULL)
1207       aCustomPrs = myCustomPrs;
1208   }
1209   bool isCustomized = aCustomPrs.get() &&
1210                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1211   return isCustomized;
1212 }
1213
1214
1215 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1216 {
1217   if (!isVisible(theObject))
1218     return Qt::black;
1219
1220   AISObjectPtr anAISObj = getAISObject(theObject);
1221   int aR, aG, aB;
1222   anAISObj->getColor(aR, aG, aB);
1223   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1224   if (theUpdateViewer)
1225     updateViewer();
1226   return QColor(aR, aG, aB);
1227 }
1228
1229 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1230 {
1231   myResult2AISObjectMap[theObject] = theAIS;
1232
1233 #ifdef DEBUG_DISPLAY
1234   std::ostringstream aPtrStr;
1235   aPtrStr << theObject.get();
1236   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1237   qDebug(getResult2AISObjectMapInfo().c_str());
1238 #endif
1239 }
1240
1241 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1242 {
1243   QStringList aContent;
1244   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1245     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1246     std::ostringstream aPtrStr;
1247     aPtrStr << "aObj = " << aObj.get() << ":";
1248     aPtrStr << "anAIS = " << aAISObj.get() << ":";
1249     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1250     
1251     aContent.append(aPtrStr.str().c_str());
1252   }
1253   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1254                                             arg(aContent.join("\n")).toStdString().c_str();
1255 }
1256
1257 void XGUI_Displayer::activateTrihedron(bool theIsActive) 
1258 {  
1259   myIsTrihedronActive = theIsActive; 
1260   if (!myIsTrihedronActive) {
1261     deactivateTrihedron(true);
1262   }
1263 }
1264
1265 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1266 {
1267   Handle(AIS_InteractiveContext) aContext = AISContext();
1268   if (aContext.IsNull())
1269     return;
1270
1271   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1272
1273   if (theToDisplay) {
1274     if (!aContext->IsDisplayed(aTrihedron))
1275       aContext->Display(aTrihedron,
1276                         0 /*wireframe*/,
1277                         -1 /* selection mode */,
1278                         Standard_True /* update viewer*/,
1279                         Standard_False /* allow decomposition */,
1280                         AIS_DS_Displayed /* xdisplay status */);
1281
1282     if (!isTrihedronActive())
1283       deactivateTrihedron(false);
1284     else
1285       activate(aTrihedron, myActiveSelectionModes, false);
1286   } else {
1287     deactivateTrihedron(false);
1288     //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1289     // the selection from the previous activation modes should be cleared manually (#26172)
1290
1291     aContext->Erase(aTrihedron);
1292   }
1293
1294   updateViewer();
1295 }
1296
1297 QIntList XGUI_Displayer::activeSelectionModes() const 
1298
1299   QIntList aModes;
1300   foreach (int aMode, myActiveSelectionModes) {
1301     // aMode < 9 is a Shape Enum values
1302     aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
1303   }
1304   return aModes; 
1305 }
1306
1307 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1308                                       const NCollection_Map<TopoDS_Shape>& theShapesToBeSelected)
1309 {
1310   Handle(AIS_LocalContext) aLContext = theContext->LocalContext();
1311   TCollection_AsciiString aSelectionName = aLContext->SelectionName();
1312   aLContext->UnhilightPicked(Standard_False);
1313
1314   NCollection_Map<TopoDS_Shape> aShapesSelected;
1315
1316   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1317   aLContext->MainSelector()->ActiveOwners(anActiveOwners);
1318   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1319   Handle(SelectMgr_EntityOwner) anOwner;
1320   for (; anOwnersIt.More(); anOwnersIt.Next()) {
1321     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1322     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1323     if (!BROwnr.IsNull() && BROwnr->HasShape() && theShapesToBeSelected.Contains(BROwnr->Shape())) {
1324       if (aShapesSelected.Contains(BROwnr->Shape()))
1325         continue;
1326       AIS_Selection::Selection(aSelectionName.ToCString())->Select(anOwner);
1327       anOwner->SetSelected (Standard_True);
1328       aShapesSelected.Add(BROwnr->Shape());
1329     }
1330   }
1331   aLContext->HilightPicked(Standard_False);
1332 }