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