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