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