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