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