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