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