]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Issue #1959 multiply selection with Shift button pressed works wrong
[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 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
95
96 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
97 {
98   // Get from null point
99   theAIS->DisplayedObjects(theList, true);
100 }
101
102 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
103 {
104   QStringList anInfo;
105   QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
106   for (; anIt != aLast; anIt++) {
107     anInfo.append(QString::number(*anIt));
108   }
109   return anInfo.join(theSeparator);
110 }
111
112 void deselectPresentation(const Handle(AIS_InteractiveObject) theObject,
113                           const Handle(AIS_InteractiveContext)& theContext)
114 {
115   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
116
117   for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected()) {
118     Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
119     if (anOwner.IsNull()) // TODO: check why it is possible
120       continue;
121     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
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 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
381       deselectPresentation(aAISIO, aContext);
382 #endif
383       aContext->Redisplay(aAISIO, false);
384
385       #ifdef VINSPECTOR
386       if (getCallBack()) getCallBack()->Redisplay(aAISIO);
387       #endif
388
389       if (aNeedToRestoreSelection)
390         myWorkshop->module()->restoreSelection();
391
392       aRedisplayed = true;
393       #ifdef DEBUG_FEATURE_REDISPLAY
394         qDebug("  Redisplay happens");
395       #endif
396       if (theUpdateViewer)
397         updateViewer();
398     }
399   }
400   return aRedisplayed;
401 }
402
403 void XGUI_Displayer::redisplayObjects()
404 {
405   // redisplay objects visualized in the viewer
406   static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
407   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
408   QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
409   QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
410   for (; anIt != aLast; anIt++) {
411     aECreator->sendUpdated(*anIt, EVENT_DISP);
412   }
413   Events_Loop::loop()->flush(EVENT_DISP);
414 }
415
416 void XGUI_Displayer::deactivate(ObjectPtr theObject, const bool theUpdateViewer)
417 {
418 #ifdef DEBUG_DEACTIVATE
419   QString anInfoStr = ModuleBase_Tools::objectInfo(theObject);
420   qDebug(QString("deactivate: myActiveSelectionModes[%1]: %2, objects = ").
421     arg(myActiveSelectionModes.size()).arg(qIntListInfo(myActiveSelectionModes)).
422     arg(anInfoStr).
423     toStdString().c_str());
424 #endif
425   Handle(AIS_InteractiveContext) aContext = AISContext();
426   if (!aContext.IsNull() && isVisible(theObject)) {
427     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
428     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
429
430     deactivateAIS(anAIS);
431     // the selection from the previous activation modes should be cleared manually (#26172)
432 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
433     deselectPresentation(anAIS, aContext);
434 #endif
435     if (theUpdateViewer)
436       updateViewer();
437   }
438 }
439
440 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
441                                        const bool theUpdateViewer)
442 {
443   //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
444   //if (!aTrihedron.IsNull())
445   //  deactivateAIS(aTrihedron);
446
447   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
448   for (; anIt != aLast; anIt++) {
449     deactivate(*anIt, false);
450   }
451   //VSV It seems that there is no necessity to update viewer on deactivation
452   //if (theUpdateViewer)
453   //  updateViewer();
454 }
455
456 void XGUI_Displayer::getModesOfActivation(ObjectPtr theObject, QIntList& theModes)
457 {
458   Handle(AIS_InteractiveContext) aContext = AISContext();
459   if (aContext.IsNull() || !isVisible(theObject))
460     return;
461
462   AISObjectPtr aAISObj = getAISObject(theObject);
463
464   if (aAISObj.get() != NULL) {
465     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
466     TColStd_ListOfInteger aTColModes;
467     aContext->ActivatedModes(anAISIO, aTColModes);
468     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
469     for (; itr.More(); itr.Next() ) {
470       theModes.append(itr.Value());
471     }
472   }
473 }
474
475 int XGUI_Displayer::getSelectionMode(int theShapeType)
476 {
477   return (theShapeType > TopAbs_SHAPE) ? theShapeType :
478                                          AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theShapeType);
479 }
480
481 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
482 {
483   bool aVisible = false;
484   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
485   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
486   if (aPrs.get() || aResult.get()) {
487     aVisible = theDisplayer->isVisible(theObject);
488     // compsolid is not visualized in the viewer,
489     // but should have presentation when all sub solids are
490     // visible. It is useful for highlight presentation where compsolid shape is selectable
491     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
492       ResultCompSolidPtr aCompsolidResult =
493         std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
494       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
495         bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
496         for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
497           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
498         }
499         aVisible = anAllSubsVisible;
500       }
501     }
502   }
503   // it is possible that feature is presentable and has results, so we should check visibility
504   // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
505   if (!aVisible) {
506     // check if all results of the feature are visible
507     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
508     std::list<ResultPtr> aResults;
509     ModelAPI_Tools::allResults(aFeature, aResults);
510     std::list<ResultPtr>::const_iterator aIt;
511     aVisible = !aResults.empty();
512     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
513       aVisible = aVisible && theDisplayer->isVisible(*aIt);
514     }
515   }
516   return aVisible;
517 }
518
519 #ifdef DEBUG_ACTIVATE_OBJECTS
520 QString getModeInfo(const int theMode)
521 {
522   QString anInfo = "Undefined";
523   switch(theMode) {
524     case 0: anInfo = "SHAPE(0)"; break;
525     case 1: anInfo = "VERTEX(1)"; break;
526     case 2: anInfo = "EDGE(2)"; break;
527     case 3: anInfo = "WIRE(3)"; break;
528     case 4: anInfo = "FACE(4)"; break;
529     case 5: anInfo = "SHELL(5)"; break;
530     case 6: anInfo = "SOLID(6)"; break;
531     case 7: anInfo = "COMPSOLID(7)"; break;
532     case 8: anInfo = "COMPOUND(8)"; break;
533     case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
534     case 101: anInfo = "Sel_Constraint(101)"; break;
535     case 102: anInfo = "Sel_Dimension_All(102)"; break;
536     case 103: anInfo = "Sel_Dimension_Line(103)"; break;
537     case 104: anInfo = "Sel_Dimension_Text(104)"; break;
538     default: break;
539   }
540   return anInfo;
541 }
542
543 QString getModesInfo(const QIntList& theModes)
544 {
545   QStringList aModesInfo;
546   for (int i = 0, aSize = theModes.size(); i < aSize; i++)
547     aModesInfo.append(getModeInfo(theModes[i]));
548   return QString("[%1] = %2").arg(aModesInfo.size()).arg(aModesInfo.join(", "));
549 }
550 #endif
551
552 void XGUI_Displayer::activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
553                                      const bool theUpdateViewer)
554 {
555   // Convert shape types to selection types
556   QIntList aModes;
557   foreach(int aType, theModes) {
558     aModes.append(getSelectionMode(aType));
559   }
560
561 #ifdef DEBUG_ACTIVATE_OBJECTS
562   QStringList anInfo;
563   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
564   for (; anIt != aLast; ++anIt) {
565     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
566   }
567   QString anInfoStr = anInfo.join(", ");
568
569   qDebug(QString("activateObjects: new modes%1, active modes%2, objects[%3] = %4").
570     arg(getModesInfo(aModes)).
571     arg(getModesInfo(myActiveSelectionModes)).
572     arg(theObjList.size()).
573     arg(anInfoStr).
574     toStdString().c_str());
575 #endif
576   // In order to avoid doblications of selection modes
577   QIntList aNewModes;
578   foreach (int aMode, aModes) {
579     if (!aNewModes.contains(aMode))
580       aNewModes.append(aMode);
581   }
582   myActiveSelectionModes = aNewModes;
583   Handle(AIS_InteractiveContext) aContext = AISContext();
584   // Open local context if there is no one
585   if (aContext.IsNull())
586     return;
587
588   //aContext->UseDisplayedObjects();
589   //myUseExternalObjects = true;
590
591   Handle(AIS_InteractiveObject) anAISIO;
592   AIS_ListOfInteractive aPrsList;
593   //if (aObjList.isEmpty())
594   //  return;
595   //else {
596   foreach(ObjectPtr aObj, theObjList) {
597     if (myResult2AISObjectMap.contains(aObj))
598       aPrsList.Append(myResult2AISObjectMap[aObj]->impl<Handle(AIS_InteractiveObject)>());
599   }
600   //}
601
602   // Add trihedron because it has to partisipate in selection
603   Handle(AIS_InteractiveObject) aTrihedron;
604   if (isTrihedronActive()) {
605     aTrihedron = getTrihedron();
606     if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron))
607       aPrsList.Append(aTrihedron);
608   }
609   if (aPrsList.Extent() == 0)
610     return;
611
612   AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
613   bool isActivationChanged = false;
614   for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
615     anAISIO = aLIt.Value();
616     if (activate(anAISIO, myActiveSelectionModes, false))
617       isActivationChanged = true;
618   }
619 }
620
621 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
622 {
623   Handle(AIS_InteractiveContext) aContext = AISContext();
624   if (aContext.IsNull() || !isVisible(theObject))
625     return false;
626
627   AISObjectPtr anObj = myResult2AISObjectMap[theObject];
628   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
629
630   TColStd_ListOfInteger aModes;
631   aContext->ActivatedModes(anAIS, aModes);
632   #ifdef VINSPECTOR
633   if (getCallBack()) getCallBack()->ActivatedModes(anAIS, aModes);
634   #endif
635
636   return aModes.Extent() > 0;
637 }
638
639
640 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues,
641                                  bool theUpdateViewer)
642 {
643   Handle(AIS_InteractiveContext) aContext = AISContext();
644   if (aContext.IsNull())
645     return;
646   aContext->UnhilightSelected(false);
647   aContext->ClearSelected(false);
648   #ifdef VINSPECTOR
649   if (getCallBack()) getCallBack()->ClearSelected();
650   #endif
651   NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
652     aShapesToBeSelected;
653
654   foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
655     const GeomShapePtr& aGeomShape = aPrs->shape();
656     if (aGeomShape.get() && !aGeomShape->isNull()) {
657       const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
658 #ifdef DEBUG_OCCT_SHAPE_SELECTION
659       // problem 1: performance
660       // problem 2: IO is not specified, so the first found owner is selected, as a result
661       // it might belong to another result
662       aContext->AddOrRemoveSelected(aShape, false);
663       #ifdef VINSPECTOR
664       if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
665       #endif
666 #else
667       NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
668       if (aShapesToBeSelected.IsBound(aShape))
669         aPresentations = aShapesToBeSelected.Find(aShape);
670       ObjectPtr anObject = aPrs->object();
671       getPresentations(anObject, aPresentations);
672
673       aShapesToBeSelected.Bind(aShape, aPresentations);
674 #endif
675     } else {
676       ObjectPtr anObject = aPrs->object();
677       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
678       if (aResult.get() && isVisible(aResult)) {
679         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
680         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
681         if (!anAIS.IsNull()) {
682           // The methods are replaced in order to provide multi-selection, e.g. restore selection
683           // by activating multi selector widget. It also gives an advantage that the multi
684           // selection in OB gives multi-selection in the viewer
685           //aContext->SetSelected(anAIS, false);
686           // The selection in the context was cleared, so the method sets the objects are selected
687           aContext->AddOrRemoveSelected(anAIS, false);
688           #ifdef VINSPECTOR
689           if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
690           #endif
691         }
692       }
693     }
694   }
695   if (!aShapesToBeSelected.IsEmpty())
696     XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
697
698   if (theUpdateViewer)
699     updateViewer();
700 }
701
702 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
703 {
704   Handle(AIS_InteractiveContext) aContext = AISContext();
705   if (!aContext.IsNull()) {
706     aContext->UnhilightCurrents(false);
707     aContext->ClearSelected(theUpdateViewer);
708     #ifdef VINSPECTOR
709     if (getCallBack()) getCallBack()->ClearSelected();
710     #endif
711   }
712 }
713
714 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
715 {
716   bool aErased = false;
717   Handle(AIS_InteractiveContext) aContext = AISContext();
718   if (!aContext.IsNull()) {
719     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
720       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
721       // erase an object
722       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
723       if (!anIO.IsNull()) {
724         emit beforeObjectErase(aObj, aAISObj);
725         aContext->Remove(anIO, false/*update viewer*/);
726         #ifdef VINSPECTOR
727         if (getCallBack()) getCallBack()->Remove(anIO);
728         #endif
729         aErased = true;
730       }
731     }
732     if (theUpdateViewer)
733       updateViewer();
734   }
735   myResult2AISObjectMap.clear();
736 #ifdef DEBUG_DISPLAY
737   qDebug("eraseAll");
738   qDebug(getResult2AISObjectMapInfo().c_str());
739 #endif
740   return aErased;
741 }
742
743 void deactivateObject(Handle(AIS_InteractiveContext) theContext,
744                       Handle(AIS_InteractiveObject) theObject
745 #ifdef VINSPECTOR
746                       , VInspectorAPI_CallBack* theCallBack
747 #endif
748                       )
749 {
750   if (!theObject.IsNull()) {
751     theContext->Deactivate(theObject);
752     #ifdef VINSPECTOR
753     if (theCallBack) theCallBack->Deactivate(theObject);
754     #endif
755   }
756 }
757
758 void XGUI_Displayer::deactivateTrihedron(const bool theUpdateViewer) const
759 {
760   Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
761   Handle(AIS_InteractiveContext) aContext = AISContext();
762   if (!aTrihedron.IsNull() && aContext->IsDisplayed(aTrihedron)) {
763     Handle(AIS_Trihedron) aTrie = Handle(AIS_Trihedron)::DownCast(aTrihedron);
764     deactivateObject(aContext, aTrie
765     #ifdef VINSPECTOR
766       , getCallBack()
767     #endif
768       );
769
770     /// #1136 hidden axis are selected in sketch
771     deactivateObject(aContext, aTrie->XAxis()
772     #ifdef VINSPECTOR
773       , getCallBack()
774     #endif
775     );
776     deactivateObject(aContext, aTrie->YAxis()
777     #ifdef VINSPECTOR
778       , getCallBack()
779     #endif
780     );
781     deactivateObject(aContext, aTrie->Axis()
782     #ifdef VINSPECTOR
783       , getCallBack()
784     #endif
785     );
786     deactivateObject(aContext, aTrie->Position()
787     #ifdef VINSPECTOR
788       , getCallBack()
789     #endif
790     );
791
792     deactivateObject(aContext, aTrie->XYPlane()
793     #ifdef VINSPECTOR
794       , getCallBack()
795     #endif
796     );
797     deactivateObject(aContext, aTrie->XZPlane()
798     #ifdef VINSPECTOR
799       , getCallBack()
800     #endif
801     );
802     deactivateObject(aContext, aTrie->YZPlane()
803     #ifdef VINSPECTOR
804       , getCallBack()
805     #endif
806     );
807
808     if (theUpdateViewer)
809       updateViewer();
810   }
811 }
812
813 Handle(AIS_InteractiveObject) XGUI_Displayer::getTrihedron() const
814 {
815   return myWorkshop->viewer()->trihedron();
816 }
817
818 /*void XGUI_Displayer::openLocalContext()
819 {
820   Handle(AIS_InteractiveContext) aContext = AISContext();
821   // Open local context if there is no one
822   if (!aContext.IsNull() && !aContext->HasOpenedContext()) {
823     // Preserve selected objects
824     //AIS_ListOfInteractive aAisList;
825     //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
826     //  aAisList.Append(aContext->Current());
827
828     // get the filters from the global context and append them to the local context
829     // a list of filters in the global context is not cleared and should be cleared here
830     SelectMgr_ListOfFilter aFilters;
831     aFilters.Assign(aContext->Filters());
832     // it is important to remove the filters in the global context, because there is a code
833     // in the closeLocalContex, which restore the global context filters
834     aContext->RemoveFilters();
835
836     //aContext->ClearCurrents();
837     aContext->OpenLocalContext();
838     //deactivateTrihedron();
839     //aContext->NotUseDisplayedObjects();
840
841     //myUseExternalObjects = false;
842
843     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
844     for (;aIt.More(); aIt.Next()) {
845       aContext->AddFilter(aIt.Value());
846     }
847     // Restore selection
848     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
849     //for(; aIt2.More(); aIt2.Next()) {
850     //  aContext->SetSelected(aIt2.Value(), false);
851     //}
852   }
853 }*/
854
855 /*void XGUI_Displayer::closeLocalContexts(const bool theUpdateViewer)
856 {
857   Handle(AIS_InteractiveContext) aContext = AISContext();
858   if (!aContext.IsNull() && aContext->HasOpenedContext()) {
859     // Preserve selected objects
860     //AIS_ListOfInteractive aAisList;
861     //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
862     //  aAisList.Append(aContext->SelectedInteractive());
863
864     // get the filters from the local context and append them to the global context
865     // a list of filters in the local context is cleared
866     SelectMgr_ListOfFilter aFilters;
867     aFilters.Assign(aContext->Filters());
868
869     //aContext->ClearSelected();
870     aContext->CloseAllContexts(false);
871
872     // From the moment when the AIS_DS_Displayed flag is used in the Display of AIS object,
873     // this code is obsolete. It is temporaty commented and should be removed after
874     // the test campaign.
875     // Redisplay all object if they were displayed in localContext
876     /*Handle(AIS_InteractiveObject) aAISIO;
877     foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
878       aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
879       if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
880         aContext->Display(aAISIO, false);
881         aContext->SetDisplayMode(aAISIO, Shading, false);
882       }
883     }*+/
884
885     // Append the filters from the local selection in the global selection context
886     SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
887     for (;aIt.More(); aIt.Next()) {
888       Handle(SelectMgr_Filter) aFilter = aIt.Value();
889       aContext->AddFilter(aFilter);
890     }
891
892     if (theUpdateViewer)
893       updateViewer();
894     //myUseExternalObjects = false;
895
896     // Restore selection
897     //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
898     //for(; aIt2.More(); aIt2.Next()) {
899     //  if (aContext->IsDisplayed(aIt2.Value()))
900     //    aContext->SetCurrentObject(aIt2.Value(), false);
901     //}
902   }
903 }*/
904
905 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
906 {
907   AISObjectPtr anIO;
908   if (myResult2AISObjectMap.contains(theObject))
909     anIO = myResult2AISObjectMap[theObject];
910   return anIO;
911 }
912
913 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
914 {
915   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
916   return getObject(aRefAIS);
917 }
918
919 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
920 {
921   ObjectPtr anObject;
922   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
923     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
924     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
925     if (anAIS == theIO)
926       anObject = anObj;
927     if (anObject.get())
928       break;
929   }
930   if (!anObject.get()) {
931     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
932     if (!theIO.IsNull()) {
933       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
934     }
935     anObject = myWorkshop->module()->findPresentedObject(anAISObj);
936   }
937   return anObject;
938 }
939
940 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
941 {
942   bool aWasEnabled = isUpdateEnabled();
943   if (isEnabled)
944     myViewerBlockedRecursiveCount--;
945   else
946     myViewerBlockedRecursiveCount++;
947
948   if (myNeedUpdate && isUpdateEnabled()) {
949     updateViewer();
950     myNeedUpdate = false;
951   }
952   return aWasEnabled;
953 }
954
955 bool XGUI_Displayer::isUpdateEnabled() const
956 {
957   return myViewerBlockedRecursiveCount == 0;
958 }
959
960 void XGUI_Displayer::updateViewer() const
961 {
962   Handle(AIS_InteractiveContext) aContext = AISContext();
963   if (!aContext.IsNull() && isUpdateEnabled()) {
964     myWorkshop->viewer()->Zfitall();
965     aContext->UpdateCurrentViewer();
966   } else {
967     myNeedUpdate = true;
968   }
969 }
970
971 void XGUI_Displayer::activateAIS(const Handle(AIS_InteractiveObject)& theIO,
972                                  const int theMode, const bool theUpdateViewer) const
973 {
974   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
975   if (!theIO.IsNull() && theIO == getTrihedron()) {
976     if (theMode != AIS_Shape::SelectionType(TopAbs_EDGE) &&
977         theMode != AIS_Shape::SelectionType(TopAbs_VERTEX))
978       return;
979   }
980   if (!aContext.IsNull()) {
981     if (myWorkshop->module()) {
982       int aMode = (theMode > 8)? theMode : AIS_Shape::SelectionType(theMode);
983       aContext->Activate(theIO, theMode, false);
984     } else
985       aContext->Activate(theIO, theMode, false);
986     #ifdef VINSPECTOR
987     if (getCallBack()) getCallBack()->Activate(theIO, theMode);
988     #endif
989
990     // the fix from VPA for more suitable selection of sketcher lines
991     if (theIO->Width() > 1) {
992       double aPrecision = theIO->Width() + 2;
993       if (theMode == getSelectionMode(TopAbs_VERTEX))
994         aPrecision = ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
995                                                                     "point-selection-sensitivity",
996                                                                         12);
997       else if ((theMode == getSelectionMode(TopAbs_EDGE)) ||
998                (theMode == getSelectionMode(TopAbs_WIRE)))
999         aPrecision = theIO->Width() +
1000            ModuleBase_Preferences::resourceMgr()->doubleValue("Viewer",
1001                                                               "edge-selection-sensitivity", 2);
1002       aContext->SetSelectionSensitivity(theIO, theMode, aPrecision);
1003     }
1004
1005 #ifdef DEBUG_ACTIVATE_AIS
1006     ObjectPtr anObject = getObject(theIO);
1007     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
1008     qDebug(QString("activateAIS: theMode = %1, object = %2").arg(theMode)
1009       .arg(anInfo).toStdString().c_str());
1010 #endif
1011     if (theUpdateViewer)
1012       updateViewer();
1013   }
1014 }
1015
1016 void XGUI_Displayer::deactivateAIS(const Handle(AIS_InteractiveObject)& theIO,
1017                                    const int theMode) const
1018 {
1019   Handle(AIS_InteractiveContext) aContext = AISContext();
1020   if (!aContext.IsNull()) {
1021     if (theMode == -1) {
1022       aContext->Deactivate(theIO);
1023       #ifdef VINSPECTOR
1024       if (getCallBack()) getCallBack()->Deactivate(theIO);
1025       #endif
1026     }
1027     else {
1028       aContext->Deactivate(theIO, theMode);
1029       #ifdef VINSPECTOR
1030       if (getCallBack()) getCallBack()->Deactivate(theIO, theMode);
1031       #endif
1032     }
1033
1034 #ifdef DEBUG_DEACTIVATE_AIS
1035     ObjectPtr anObject = getObject(theIO);
1036     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
1037     qDebug(QString("deactivateAIS: theMode = %1, object = %2").arg(theMode)
1038       .arg(anInfo).toStdString().c_str());
1039 #endif
1040   }
1041 }
1042
1043 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
1044 {
1045   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1046   if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
1047 #ifdef VINSPECTOR
1048     if (VInspector_FirstCall) {
1049       XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
1050       VInspectorAPI_Communicator* aCommunicator = VInspectorAPI_PluginMgr::activateVInspector(
1051                                         "VInspector.dll", aContext);
1052       aDisplayer->setCommunicator(aCommunicator);
1053       #ifndef HAVE_SALOME
1054       AppElements_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
1055       if (aViewer)
1056         aViewer->setCallBack(aCommunicator->getCallBack());
1057       #endif
1058       VInspector_FirstCall = false;
1059     }
1060 #endif
1061     XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
1062     aDisplayer->myIsFirstAISContextUse = false;
1063     //aContext->OpenLocalContext();
1064     if (!isTrihedronActive())
1065       deactivateTrihedron(true);
1066     aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
1067     aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
1068   }
1069   return aContext;
1070 }
1071
1072 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
1073 {
1074   Handle(AIS_InteractiveContext) aContext = AISContext();
1075   if (!aContext.IsNull() && myAndFilter.IsNull()) {
1076     myAndFilter = new SelectMgr_AndFilter();
1077     aContext->AddFilter(myAndFilter);
1078   }
1079   return myAndFilter;
1080 }
1081
1082 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
1083                                 bool theUpdateViewer)
1084 {
1085   bool aDisplayed = false;
1086   Handle(AIS_InteractiveContext) aContext = AISContext();
1087   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
1088   if (!aContext.IsNull() && !anAISIO.IsNull()) {
1089     aContext->Display(anAISIO, 0/*wireframe*/, 0, false/*update viewer*/, true, AIS_DS_Displayed);
1090     #ifdef VINSPECTOR
1091     if (getCallBack()) getCallBack()->Display(anAISIO);
1092     #endif
1093     aDisplayed = true;
1094     aContext->Deactivate(anAISIO);
1095     #ifdef VINSPECTOR
1096     if (getCallBack()) getCallBack()->Deactivate(anAISIO);
1097     #endif
1098     aContext->Load(anAISIO);
1099     #ifdef VINSPECTOR
1100     if (getCallBack()) getCallBack()->Load(anAISIO);
1101     #endif
1102     if (toActivateInSelectionModes) {
1103       if (myActiveSelectionModes.size() == 0)
1104         activateAIS(anAISIO, 0, theUpdateViewer);
1105       else {
1106         foreach(int aMode, myActiveSelectionModes) {
1107           activateAIS(anAISIO, aMode, theUpdateViewer);
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 (getCallBack()) getCallBack()->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 (getCallBack()) getCallBack()->ActivatedModes(theIO, aTColModes);
1308   #endif
1309   TColStd_ListIteratorOfListOfInteger itr( aTColModes );
1310   QIntList aModesActivatedForIO;
1311   bool isDeactivated = false;
1312   bool aHasValidMode = false;
1313   for (; itr.More(); itr.Next() ) {
1314     Standard_Integer aMode = itr.Value();
1315     aHasValidMode = aHasValidMode || aMode != -1;
1316     int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
1317     if (!theModes.contains(aMode)) {
1318       deactivateAIS(theIO, aMode);
1319       isDeactivated = true;
1320     }
1321     else {
1322       aModesActivatedForIO.append(aMode);
1323     }
1324   }
1325   if (isDeactivated) {
1326     // the selection from the previous activation modes should be cleared manually (#26172)
1327     //theIO->ClearSelected();
1328     //#ifdef VINSPECTOR
1329     //if (getCallBack()) getCallBack()->ClearSelected(theIO);
1330     //#endif
1331 #ifndef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
1332     deselectPresentation(theIO, aContext);
1333 #endif
1334     // For performance issues
1335     //if (theUpdateViewer)
1336     //  updateViewer();
1337     isActivationChanged = true;
1338   }
1339
1340   // loading the interactive object allowing the decomposition
1341   if (aTColModes.IsEmpty() || !aHasValidMode) {
1342     aContext->Load(theIO, -1, true);
1343     Handle(AIS_Trihedron) aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1344     if (!aTrihedron.IsNull()) {
1345       // Workaround for Trihedron. It should be loaded using the next Load method to
1346       // add this object to myGlobal map of selection manager
1347       // it is important to activate trihedron in two selection modes: edges and vertices
1348       aContext->SelectionManager()->Load(theIO);
1349     }
1350
1351     #ifdef VINSPECTOR
1352     if (getCallBack()) getCallBack()->Load(theIO);
1353     #endif
1354   }
1355
1356   // trihedron AIS check should be after the AIS loading.
1357   // If it is not loaded, it is steel selectable in the viewer.
1358   Handle(AIS_Trihedron) aTrihedron;
1359   if (!isTrihedronActive())
1360     aTrihedron = Handle(AIS_Trihedron)::DownCast(theIO);
1361   if (aTrihedron.IsNull()) {
1362       // In order to clear active modes list
1363     if (theModes.size() == 0) {
1364       activateAIS(theIO, 0, theUpdateViewer);
1365     } else {
1366       foreach(int aMode, theModes) {
1367         if (!aModesActivatedForIO.contains(aMode)) {
1368           activateAIS(theIO, aMode, theUpdateViewer);
1369           isActivationChanged = true;
1370         }
1371       }
1372     }
1373   }
1374   return isActivationChanged;
1375 }
1376
1377 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
1378 {
1379   AISObjectPtr anAISObj = getAISObject(theObject);
1380   // correct the result's color it it has the attribute
1381   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1382
1383   // Customization of presentation
1384   GeomCustomPrsPtr aCustomPrs;
1385   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1386   if (aFeature.get() != NULL) {
1387     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
1388     if (aCustPrs.get() != NULL)
1389       aCustomPrs = aCustPrs;
1390   }
1391   if (aCustomPrs.get() == NULL) {
1392     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
1393     // we ignore presentable not customized objects
1394     if (aPrs.get() == NULL)
1395       aCustomPrs = myCustomPrs;
1396   }
1397   bool isCustomized = aCustomPrs.get() &&
1398                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
1399   return isCustomized;
1400 }
1401
1402
1403 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
1404                                       const QColor& theColor,
1405                                       bool theUpdateViewer)
1406 {
1407   if (!isVisible(theObject))
1408     return Qt::black;
1409
1410   AISObjectPtr anAISObj = getAISObject(theObject);
1411   int aR, aG, aB;
1412   anAISObj->getColor(aR, aG, aB);
1413   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
1414   if (theUpdateViewer)
1415     updateViewer();
1416   return QColor(aR, aG, aB);
1417 }
1418
1419 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
1420 {
1421   myResult2AISObjectMap[theObject] = theAIS;
1422
1423 #ifdef DEBUG_DISPLAY
1424   std::ostringstream aPtrStr;
1425   aPtrStr << theObject.get();
1426   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
1427   qDebug(getResult2AISObjectMapInfo().c_str());
1428 #endif
1429 }
1430
1431 #ifdef _DEBUG
1432 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
1433 {
1434   QStringList aContent;
1435   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
1436     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
1437     std::ostringstream aPtrStr;
1438     aPtrStr << "aObj = " << aObj.get() << ":";
1439     aPtrStr << "anAIS = " << aAISObj.get() << ":";
1440     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
1441
1442     aContent.append(aPtrStr.str().c_str());
1443   }
1444   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
1445                                             arg(aContent.join("\n")).toStdString().c_str();
1446 }
1447 #endif
1448
1449 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
1450                                   NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
1451 {
1452   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1453   if (aResult.get()) {
1454     AISObjectPtr aAISObj = getAISObject(aResult);
1455     if (aAISObj.get() != NULL) {
1456       Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1457       if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1458         thePresentations.Add(anAIS);
1459     }
1460   }
1461   else {
1462     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1463     // find presentation of the feature
1464     AISObjectPtr aAISObj = getAISObject(aFeature);
1465     if (aAISObj.get() != NULL) {
1466       Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1467       if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1468         thePresentations.Add(anAIS);
1469     }
1470     // find presentations of the feature results
1471     std::list<ResultPtr> aResults;
1472     ModelAPI_Tools::allResults(aFeature, aResults);
1473     std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1474     for (; anIt != aLast; ++anIt) {
1475       AISObjectPtr aAISObj = getAISObject(*anIt);
1476       if (aAISObj.get() != NULL) {
1477         Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1478         if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
1479           thePresentations.Add(anAIS);
1480       }
1481     }
1482   }
1483 }
1484
1485 void XGUI_Displayer::activateTrihedron(bool theIsActive)
1486 {
1487   myIsTrihedronActive = theIsActive;
1488   if (!myIsTrihedronActive) {
1489     deactivateTrihedron(true);
1490   }
1491 }
1492
1493 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1494 {
1495   Handle(AIS_InteractiveContext) aContext = AISContext();
1496   if (aContext.IsNull())
1497     return;
1498
1499   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1500
1501   if (theToDisplay) {
1502     if (!aContext->IsDisplayed(aTrihedron))
1503       aContext->Display(aTrihedron,
1504                         0 /*wireframe*/,
1505                         -1 /* selection mode */,
1506                         Standard_True /* update viewer*/,
1507                         Standard_False /* allow decomposition */,
1508                         AIS_DS_Displayed /* xdisplay status */);
1509     #ifdef VINSPECTOR
1510     if (getCallBack()) getCallBack()->Display(aTrihedron);
1511     #endif
1512
1513     if (!isTrihedronActive())
1514       deactivateTrihedron(false);
1515     else
1516       activate(aTrihedron, myActiveSelectionModes, false);
1517   } else {
1518     deactivateTrihedron(false);
1519
1520     aContext->Erase(aTrihedron);
1521     #ifdef VINSPECTOR
1522     if (getCallBack()) getCallBack()->Remove(aTrihedron);
1523     #endif
1524   }
1525
1526   updateViewer();
1527 }
1528
1529 QIntList XGUI_Displayer::activeSelectionModes() const
1530 {
1531   QIntList aModes;
1532   foreach (int aMode, myActiveSelectionModes) {
1533     // aMode < 9 is a Shape Enum values
1534     aModes << ((aMode < 9)? AIS_Shape::SelectionType(aMode) : aMode);
1535   }
1536   return aModes;
1537 }
1538
1539 #ifdef VINSPECTOR
1540 void XGUI_Displayer::setCommunicator(VInspectorAPI_Communicator* theCommunicator)
1541 {
1542   myCommunicator = theCommunicator;
1543 }
1544
1545 void XGUI_Displayer::setVInspectorVisible(const bool theVisible)
1546 {
1547   if (myCommunicator)
1548     myCommunicator->setVisible(true);
1549 }
1550
1551 VInspectorAPI_CallBack* XGUI_Displayer::getCallBack() const
1552 {
1553   return myCommunicator ? myCommunicator->getCallBack() : NULL;
1554 }
1555 #endif
1556
1557 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1558                            const NCollection_DataMap<TopoDS_Shape,
1559                            NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1560 {
1561   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1562   theContext->MainSelector()->ActiveOwners(anActiveOwners);
1563   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1564   Handle(SelectMgr_EntityOwner) anOwner;
1565
1566   /// It is very important to check that the owner is processed only once and has a map of
1567   /// processed owners because SetSelected works as a switch.
1568   /// If count of calls setSelectec is even, the object stays in the previous state
1569   /// (selected, deselected)
1570   /// OCCT: to write about the problem that active owners method returns one owner several times
1571   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
1572   for (; anOwnersIt.More(); anOwnersIt.Next()) {
1573     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1574     if (aSelectedIds.contains((long)anOwner.get()))
1575       continue;
1576     aSelectedIds.append((long)anOwner.get());
1577
1578     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1579     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1580       const TopoDS_Shape& aShape = BROwnr->Shape();
1581       if (!aShape.IsNull() && theShapesToBeSelected.IsBound(aShape)) {
1582         Handle(AIS_InteractiveObject) anOwnerPresentation =
1583                                     Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1584         NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1585                                     theShapesToBeSelected.Find(aShape);
1586         if (aPresentations.Contains(anOwnerPresentation)) {
1587           theContext->AddOrRemoveSelected(anOwner);
1588           anOwner->SetSelected (Standard_True);
1589         }
1590       }
1591     }
1592   }
1593 }