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