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