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