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