Salome HOME
Make highlight of points as a circle instead of +
[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     double aPrecision = theIO->Width() + 2;
907     if (theMode == getSelectionMode(TopAbs_VERTEX)) 
908       aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer", "point-selection-sensitivity", 20);
909     else if ((theMode == getSelectionMode(TopAbs_EDGE)) || (theMode == getSelectionMode(TopAbs_WIRE)))
910       aPrecision = theIO->Width() + 
911                    ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer", "edge-selection-sensitivity", 2);
912     aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
913
914     ModuleBase_Tools::selectionInfo(aContext, "XGUI_Displayer::activateAIS -- Activate");
915
916 #ifdef DEBUG_ACTIVATE_AIS
917     ObjectPtr anObject = getObject(theIO);
918     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
919     qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
920 #endif
921     if (theUpdateViewer)
922       updateViewer();
923   }
924 }
925
926 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode) const
927 {
928   Handle(AIS_InteractiveContext) aContext = AISContext();
929   if (!aContext.IsNull()) {
930     if (theMode == -1)
931       aContext->Deactivate(theIO);
932     else 
933       aContext->Deactivate(theIO, theMode);
934     ModuleBase_Tools::selectionInfo(aContext, "XGUI_Displayer::deactivateAIS -- Deactivate");
935    
936 #ifdef DEBUG_DEACTIVATE_AIS
937     ObjectPtr anObject = getObject(theIO);
938     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
939     qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode).arg(anInfo).toStdString().c_str());
940 #endif
941   }
942 }
943
944 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
945 {
946   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
947   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
948     aContext->OpenLocalContext();
949     if (!isTrihedronActive())
950       deactivateTrihedron(true);
951     aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
952     aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
953   }
954   return aContext;
955 }
956
957 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
958 {
959   Handle(AIS_InteractiveContext) aContext = AISContext();
960   if (!aContext.IsNull() && myAndFilter.IsNull()) {
961     myAndFilter = new SelectMgr_AndFilter();
962     aContext->AddFilter(myAndFilter);
963   }
964   return myAndFilter;
965 }
966
967 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
968                                 bool theUpdateViewer)
969 {
970   bool aDisplayed = false;
971   Handle(AIS_InteractiveContext) aContext = AISContext();
972   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
973   if (!aContext.IsNull() && !anAISIO.IsNull()) {
974     aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
975     aDisplayed = true;
976     aContext->Deactivate(anAISIO);
977     aContext->Load(anAISIO);
978     if (toActivateInSelectionModes) {
979       if (aContext->HasOpenedContext()) {
980         if (myActiveSelectionModes.size() == 0)
981           activateAIS(anAISIO, 0, theUpdateViewer);
982         else {
983           foreach(int aMode, myActiveSelectionModes) {
984             activateAIS(anAISIO, aMode, theUpdateViewer);
985           }
986         }
987       }
988     }
989     if (theUpdateViewer)
990       updateViewer();
991   }
992   return aDisplayed;
993 }
994
995 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
996 {
997   bool aErased = false;
998   Handle(AIS_InteractiveContext) aContext = AISContext();
999   if (!aContext.IsNull()) {
1000     Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
1001     if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
1002       aContext->Remove(anAISIO, false/*update viewer*/);
1003       ModuleBase_Tools::selectionInfo(aContext, "XGUI_Displayer::eraseAIS -- Remove");
1004       aErased = true;
1005     }
1006   }
1007   if (aErased && theUpdateViewer)
1008     updateViewer();
1009   return aErased;
1010 }
1011
1012
1013 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
1014 {
1015   if (theMode == NoMode)
1016     return;
1017
1018   Handle(AIS_InteractiveContext) aContext = AISContext();
1019   if (aContext.IsNull())
1020     return;
1021
1022   AISObjectPtr aAISObj = getAISObject(theObject);
1023   if (!aAISObj)
1024     return;
1025
1026   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1027   aContext->SetDisplayMode(aAISIO, theMode, false);
1028   // Redisplay in order to update new mode because it could be not computed before
1029   if (theUpdateViewer)
1030     updateViewer();
1031 }
1032
1033 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
1034 {
1035   Handle(AIS_InteractiveContext) aContext = AISContext();
1036   if (aContext.IsNull())
1037     return NoMode;
1038
1039   AISObjectPtr aAISObj = getAISObject(theObject);
1040   if (!aAISObj)
1041     return NoMode;
1042
1043   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1044   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
1045 }
1046
1047 void XGUI_Displayer::deactivateSelectionFilters()
1048 {
1049   Handle(AIS_InteractiveContext) aContext = AISContext();
1050   if (!myAndFilter.IsNull()) {
1051     bool aFound = false;
1052     if (!aContext.IsNull()) {
1053       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1054       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
1055       for (; anIt.More() && !aFound; anIt.Next()) {
1056         Handle(SelectMgr_Filter) aFilter = anIt.Value();
1057         aFound = aFilter == myAndFilter;
1058       }
1059       if (aFound)
1060         aContext->RemoveFilter(myAndFilter);
1061     }
1062     myAndFilter.Nullify();
1063   }
1064 }
1065
1066 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1067 {
1068   Handle(AIS_InteractiveContext) aContext = AISContext();
1069   if (aContext.IsNull() || hasSelectionFilter(theFilter))
1070     return;
1071
1072   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1073   if (!aCompositeFilter.IsNull()) {
1074     aCompositeFilter->Add(theFilter);
1075 #ifdef DEBUG_SELECTION_FILTERS
1076     int aCount = aCompositeFilter->StoredFilters().Extent();
1077     qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1078 #endif
1079   }
1080 }
1081
1082 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1083 {
1084   Handle(AIS_InteractiveContext) aContext = AISContext();
1085   if (aContext.IsNull())
1086     return;
1087
1088   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
1089   if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
1090     aCompositeFilter->Remove(theFilter);
1091 #ifdef DEBUG_SELECTION_FILTERS
1092     int aCount = aCompositeFilter->StoredFilters().Extent();
1093     qDebug(QString("removeSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
1094 #endif
1095   }
1096 }
1097
1098 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
1099 {
1100   bool aFilterFound = false;
1101
1102   Handle(AIS_InteractiveContext) aContext = AISContext();
1103   if (aContext.IsNull())
1104     return aFilterFound;
1105   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
1106   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
1107   for (; aIt.More() && !aFilterFound; aIt.Next()) {
1108     if (theFilter.Access() == aIt.Value().Access())
1109       aFilterFound = true;
1110   }
1111   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1112   if (!aCompositeFilter.IsNull()) {
1113     const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
1114     for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
1115       if (theFilter.Access() == aIt.Value().Access())
1116         aFilterFound = true;
1117     }
1118   }
1119   return aFilterFound;
1120 }
1121
1122 void XGUI_Displayer::removeFilters()
1123 {
1124   Handle(AIS_InteractiveContext) aContext = AISContext();
1125   if (aContext.IsNull())
1126     return;
1127
1128   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
1129   if (!aCompositeFilter.IsNull())
1130     aCompositeFilter->Clear();
1131 }
1132
1133 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
1134 {
1135   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
1136   foreach(ObjectPtr aObj, aDispList) {
1137     if (!theList.contains(aObj))
1138       erase(aObj, false);
1139   }
1140   foreach(ObjectPtr aObj, theList) {
1141     if (!isVisible(aObj))
1142       display(aObj, false);
1143   }
1144   updateViewer();
1145 }
1146
1147 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
1148
1149   if (!isVisible(theObject))
1150     return false;
1151
1152   AISObjectPtr aAISObj = getAISObject(theObject);
1153   if (aAISObj.get() == NULL)
1154     return false;
1155
1156   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1157   return ::canBeShaded(anAIS, myWorkshop->module());
1158 }
1159
1160 bool XGUI_Displayer::activate(const Handle(AIS_InteractiveObject)& theIO,
1161                               const QIntList& theModes,
1162                               const bool theUpdateViewer) const
1163 {
1164   Handle(AIS_InteractiveContext) aContext = AISContext();
1165   if (aContext.IsNull() || theIO.IsNull())
1166     return false;
1167   
1168   bool isActivationChanged = false;
1169   // deactivate object in all modes, which are not in the list of activation
1170   // It seems that after the IO deactivation the selected state of the IO's owners
1171   // is modified in OCC(version: 6.8.0) and the selection of the object later is lost.
1172   // By this reason, the number of the IO deactivate is decreased and the object is deactivated
1173   // only if there is a difference in the current modes and the parameters modes.
1174   // If the selection problem happens again, it is possible to write a test scenario and create
1175   // a bug. The bug steps are the following:
1176   // Create two IO, activate them in 5 modes, select the first IO, deactivate 3 modes for both,
1177   // with clicked SHIFT select the second object. The result is the selection of the first IO is lost.
1178   TColStd_ListOfInteger aTColModes;
1179   aContext->ActivatedModes(theIO, aTColModes);
1180   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1181   QIntList aModesActivatedForIO;
1182   bool isDeactivated = false;
1183   for (; itr.More(); itr.Next() ) {
1184     Standard_Integer aMode = itr.Value();
1185     int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
1186     if (!theModes.contains(aMode)) {
1187       deactivateAIS(theIO, aMode);
1188       isDeactivated = true;
1189     }
1190     else {
1191       aModesActivatedForIO.append(aMode);
1192     }
1193   }
1194   if (isDeactivated) {
1195     // the selection from the previous activation modes should be cleared manually (#26172)
1196     theIO->ClearSelected();
1197     aContext->LocalContext()->ClearOutdatedSelection(theIO, true);
1198     ModuleBase_Tools::selectionInfo(aContext, "XGUI_Displayer::activate -- ClearSelected/ClearOutdatedSelection");
1199     // For performance issues
1200     //if (theUpdateViewer)
1201     //  updateViewer();
1202     isActivationChanged = true;
1203   }
1204
1205   // loading the interactive object allowing the decomposition
1206   if (aTColModes.IsEmpty()) {
1207     aContext->Load(theIO, -1, true);
1208   }
1209
1210   // trihedron AIS check should be after the AIS loading.
1211   // If it is not loaded, it is steel selectable in the viewer.
1212   Handle(AIS_Trihedron) aTrihedron;
1213   if (!isTrihedronActive())
1214     aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1215   if (aTrihedron.IsNull()) {
1216       // In order to clear active modes list
1217     if (theModes.size() == 0) {
1218       activateAIS(theIO, 0, theUpdateViewer);
1219     } else {
1220       foreach(int aMode, theModes) {
1221         if (!aModesActivatedForIO.contains(aMode)) {
1222           activateAIS(theIO, aMode, theUpdateViewer);
1223           isActivationChanged = true;
1224         }
1225       }
1226     }
1227   }
1228   return isActivationChanged;
1229 }
1230
1231 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1232 {
1233   AISObjectPtr anAISObj = getAISObject(theObject);
1234   // correct the result's color it it has the attribute
1235   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1236
1237   // Customization of presentation
1238   GeomCustomPrsPtr aCustomPrs;
1239   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1240   if (aFeature.get() != NULL) {
1241     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1242     if (aCustPrs.get() != NULL)
1243       aCustomPrs = aCustPrs;
1244   }
1245   if (aCustomPrs.get() == NULL) {
1246     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1247     // we ignore presentable not customized objects
1248     if (aPrs.get() == NULL)
1249       aCustomPrs = myCustomPrs;
1250   }
1251   bool isCustomized = aCustomPrs.get() &&
1252                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1253   return isCustomized;
1254 }
1255
1256
1257 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer)
1258 {
1259   if (!isVisible(theObject))
1260     return Qt::black;
1261
1262   AISObjectPtr anAISObj = getAISObject(theObject);
1263   int aR, aG, aB;
1264   anAISObj->getColor(aR, aG, aB);
1265   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1266   if (theUpdateViewer)
1267     updateViewer();
1268   return QColor(aR, aG, aB);
1269 }
1270
1271 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1272 {
1273   myResult2AISObjectMap[theObject] = theAIS;
1274
1275 #ifdef DEBUG_DISPLAY
1276   std::ostringstream aPtrStr;
1277   aPtrStr << theObject.get();
1278   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1279   qDebug(getResult2AISObjectMapInfo().c_str());
1280 #endif
1281 }
1282
1283 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1284 {
1285   QStringList aContent;
1286   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1287     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1288     std::ostringstream aPtrStr;
1289     aPtrStr << "aObj = " << aObj.get() << ":";
1290     aPtrStr << "anAIS = " << aAISObj.get() << ":";
1291     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1292     
1293     aContent.append(aPtrStr.str().c_str());
1294   }
1295   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1296                                             arg(aContent.join("\n")).toStdString().c_str();
1297 }
1298
1299 void XGUI_Displayer::activateTrihedron(bool theIsActive) 
1300 {  
1301   myIsTrihedronActive = theIsActive; 
1302   if (!myIsTrihedronActive) {
1303     deactivateTrihedron(true);
1304   }
1305 }
1306
1307 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1308 {
1309   Handle(AIS_InteractiveContext) aContext = AISContext();
1310   if (aContext.IsNull())
1311     return;
1312
1313   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1314
1315   if (theToDisplay) {
1316     if (!aContext->IsDisplayed(aTrihedron))
1317       aContext->Display(aTrihedron,
1318                         0 /*wireframe*/,
1319                         -1 /* selection mode */,
1320                         Standard_True /* update viewer*/,
1321                         Standard_False /* allow decomposition */,
1322                         AIS_DS_Displayed /* xdisplay status */);
1323
1324     if (!isTrihedronActive())
1325       deactivateTrihedron(false);
1326     else
1327       activate(aTrihedron, myActiveSelectionModes, false);
1328   } else {
1329     deactivateTrihedron(false);
1330     //aContext->LocalContext()->ClearOutdatedSelection(aTrihedron, true);
1331     // the selection from the previous activation modes should be cleared manually (#26172)
1332
1333     aContext->Erase(aTrihedron);
1334   }
1335
1336   updateViewer();
1337 }
1338
1339 QIntList XGUI_Displayer::activeSelectionModes() const 
1340
1341   QIntList aModes;
1342   foreach (int aMode, myActiveSelectionModes) {
1343     // aMode < 9 is a Shape Enum values
1344     aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
1345   }
1346   return aModes; 
1347 }
1348
1349 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1350                                       const NCollection_Map<TopoDS_Shape>& theShapesToBeSelected)
1351 {
1352   Handle(AIS_LocalContext) aLContext = theContext->LocalContext();
1353   TCollection_AsciiString aSelectionName = aLContext->SelectionName();
1354   aLContext->UnhilightPicked(Standard_False);
1355
1356   NCollection_Map<TopoDS_Shape> aShapesSelected;
1357
1358   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1359   aLContext->MainSelector()->ActiveOwners(anActiveOwners);
1360   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1361   Handle(SelectMgr_EntityOwner) anOwner;
1362   for (; anOwnersIt.More(); anOwnersIt.Next()) {
1363     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1364     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1365     if (!BROwnr.IsNull() && BROwnr->HasShape() && theShapesToBeSelected.Contains(BROwnr->Shape())) {
1366       if (aShapesSelected.Contains(BROwnr->Shape()))
1367         continue;
1368       AIS_Selection::Selection(aSelectionName.ToCString())->Select(anOwner);
1369       anOwner->SetSelected (Standard_True);
1370       aShapesSelected.Add(BROwnr->Shape());
1371     }
1372   }
1373   aLContext->HilightPicked(Standard_False);
1374 }