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