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