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