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