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