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