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