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