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