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