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