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