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