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