Salome HOME
Activation objects redesign.
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_Displayer.h"
22
23 #include "XGUI_CustomPrs.h"
24 #include "XGUI_FacesPanel.h"
25 #include "XGUI_Selection.h"
26 #include "XGUI_SelectionActivate.h"
27 #include "XGUI_SelectionMgr.h"
28 #include "XGUI_ViewerProxy.h"
29 #include "XGUI_Workshop.h"
30
31 #ifndef HAVE_SALOME
32 #include <AppElements_Viewer.h>
33 #endif
34
35 #include <ModelAPI_Document.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Object.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_AttributeIntArray.h>
40 #include <ModelAPI_ResultCompSolid.h>
41
42 #include <ModuleBase_BRepOwner.h>
43 #include <ModuleBase_IModule.h>
44 #include <ModuleBase_Preferences.h>
45 #include <ModuleBase_ResultPrs.h>
46 #include <ModuleBase_Tools.h>
47 #include <ModuleBase_ViewerPrs.h>
48
49 #include <GeomAPI_Shape.h>
50 #include <GeomAPI_IPresentable.h>
51 #include <GeomAPI_ICustomPrs.h>
52
53 #include <SUIT_ResourceMgr.h>
54
55 #include <AIS_InteractiveContext.hxx>
56 #include <AIS_ListOfInteractive.hxx>
57 #include <AIS_ListIteratorOfListOfInteractive.hxx>
58 #include <AIS_DimensionSelectionMode.hxx>
59 #include <AIS_Shape.hxx>
60 #include <AIS_Dimension.hxx>
61 #include <AIS_Trihedron.hxx>
62 #ifdef BEFORE_TRIHEDRON_PATCH
63 #include <AIS_Axis.hxx>
64 #include <AIS_Plane.hxx>
65 #include <AIS_Point.hxx>
66 #endif
67 #include <AIS_Selection.hxx>
68 #include <Prs3d_Drawer.hxx>
69 #include <Prs3d_IsoAspect.hxx>
70 #include <SelectMgr_ListOfFilter.hxx>
71 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
72 #include <SelectMgr_SelectionManager.hxx>
73 #include <TColStd_ListIteratorOfListOfInteger.hxx>
74
75 #include <StdSelect_ViewerSelector3d.hxx>
76
77 #include <TColStd_MapOfTransient.hxx>
78 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
79
80 #ifdef TINSPECTOR
81 #include <inspector/VInspectorAPI_CallBack.hxx>
82 #endif
83
84 #include <Events_Loop.h>
85 #include <ModelAPI_Events.h>
86
87 #include <set>
88
89 /// defines the local context mouse selection sensitivity
90 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;
91
92 //#define DEBUG_DISPLAY
93 //#define DEBUG_FEATURE_REDISPLAY
94 //#define DEBUG_SELECTION_FILTERS
95
96 //#define DEBUG_COMPOSILID_DISPLAY
97
98 //#define DEBUG_OCCT_SHAPE_SELECTION
99
100 #define CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
101
102 //**************************************************************
103 void displayedObjects(const Handle(AIS_InteractiveContext)& theAIS, AIS_ListOfInteractive& theList)
104 {
105   // Get from null point
106   theAIS->DisplayedObjects(theList, true);
107 }
108
109 QString qIntListInfo(const QIntList& theValues, const QString& theSeparator = QString(", "))
110 {
111   QStringList anInfo;
112   QIntList::const_iterator anIt = theValues.begin(), aLast = theValues.end();
113   for (; anIt != aLast; anIt++) {
114     anInfo.append(QString::number(*anIt));
115   }
116   return anInfo.join(theSeparator);
117 }
118
119 //**************************************************************
120 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
121 : myWorkshop(theWorkshop), myNeedUpdate(false),
122   myViewerBlockedRecursiveCount(0), myIsFirstAISContextUse(true)
123 {
124   myCustomPrs = std::shared_ptr<GeomAPI_ICustomPrs>(new XGUI_CustomPrs(theWorkshop));
125 }
126
127 //**************************************************************
128 XGUI_Displayer::~XGUI_Displayer()
129 {
130 }
131
132 //**************************************************************
133 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
134 {
135   return myResult2AISObjectMap.contains(theObject);
136 }
137
138 //**************************************************************
139 bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
140 {
141   bool aDisplayed = false;
142   if (isVisible(theObject)) {
143 #ifdef DEBUG_COMPOSILID_DISPLAY
144     ResultCompSolidPtr aCompsolidResult =
145       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           redisplay(aSubResult, false);
151       }
152       if (theUpdateViewer)
153         updateViewer();
154     }
155     else
156 #endif
157     aDisplayed = redisplay(theObject, theUpdateViewer);
158   } else {
159     AISObjectPtr anAIS;
160     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
161     bool isShading = false;
162     if (aPrs.get() != NULL) {
163       anAIS = aPrs->getAISObject(anAIS);
164       if (anAIS.get()) {
165         // correct deviation coefficient for
166         /*Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
167         if (!anAISPrs.IsNull()) {
168           Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
169           if (!aShapePrs.IsNull()) {
170             TopoDS_Shape aShape = aShapePrs->Shape();
171             if (!aShape.IsNull())
172               //ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, anAISPrs->Attributes());
173           }
174         }*/
175       }
176     } else {
177       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
178       if (aResult.get() != NULL) {
179 #ifdef DEBUG_COMPOSILID_DISPLAY
180         ResultCompSolidPtr aCompsolidResult =
181           std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObject);
182         if (aCompsolidResult.get()) {
183           for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
184             ResultPtr aSubResult = aCompsolidResult->subResult(i);
185             if (aSubResult.get())
186               display(aSubResult, false);
187           }
188           if (theUpdateViewer)
189             updateViewer();
190         }
191         else {
192 #endif
193         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
194         if (aShapePtr.get() != NULL) {
195           anAIS = AISObjectPtr(new GeomAPI_AISObject());
196           Handle(AIS_InteractiveObject) anAISPrs =
197             myWorkshop->module()->createPresentation(aResult);
198           if (anAISPrs.IsNull())
199             anAISPrs = new ModuleBase_ResultPrs(aResult);
200           else {
201             Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISPrs);
202             if (!aShapePrs.IsNull())
203               ModuleBase_Tools::setPointBallHighlighting((AIS_Shape*) aShapePrs.get());
204           }
205           anAIS->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
206           //anAIS->createShape(aShapePtr);
207           isShading = true;
208         }
209 #ifdef DEBUG_COMPOSILID_DISPLAY
210         } // close else
211 #endif
212       }
213     }
214     if (anAIS)
215       aDisplayed = display(theObject, anAIS, isShading, theUpdateViewer);
216   }
217   return aDisplayed;
218 }
219
220 //**************************************************************
221 bool canBeShaded(Handle(AIS_InteractiveObject) theAIS, ModuleBase_IModule* theModule)
222 {
223   Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(theAIS);
224   if (!aShapePrs.IsNull()) {
225     TopoDS_Shape aShape = aShapePrs->Shape();
226     if (aShape.IsNull())
227       return false;
228     TopAbs_ShapeEnum aType = aShape.ShapeType();
229     if ((aType == TopAbs_VERTEX) || (aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
230       return false;
231     else {
232       // Check that the presentation is not a sketch
233       return theModule->canBeShaded(theAIS);
234     }
235   }
236   return false;
237 }
238
239 //**************************************************************
240 bool XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
241                              bool isShading, bool theUpdateViewer)
242 {
243   bool aDisplayed = false;
244
245   Handle(AIS_InteractiveContext) aContext = AISContext();
246   if (aContext.IsNull())
247     return aDisplayed;
248
249   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
250   if (!anAISIO.IsNull()) {
251     appendResultObject(theObject, theAIS);
252
253     bool isCustomized = customizeObject(theObject);
254
255     int aDispMode = isShading? Shading : Wireframe;
256     if (isShading)
257       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
258     anAISIO->SetDisplayMode(aDispMode);
259     aContext->Display(anAISIO, aDispMode, 0, false, true, AIS_DS_Displayed);
260     #ifdef TINSPECTOR
261     if (getCallBack()) getCallBack()->Display(anAISIO);
262     #endif
263     aDisplayed = true;
264
265     emit objectDisplayed(theObject, theAIS);
266     selectionActivate()->activate(anAISIO, theUpdateViewer);
267   }
268   if (theUpdateViewer)
269     updateViewer();
270
271   return aDisplayed;
272 }
273
274 //**************************************************************
275 bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
276 {
277   bool aErased = false;
278   if (!isVisible(theObject))
279     return aErased;
280
281   Handle(AIS_InteractiveContext) aContext = AISContext();
282   if (aContext.IsNull())
283     return aErased;
284
285   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
286   if (anObject) {
287     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
288     if (!anAIS.IsNull()) {
289       emit beforeObjectErase(theObject, anObject);
290       aContext->Remove(anAIS, false/*update viewer*/);
291       #ifdef TINSPECTOR
292       if (getCallBack()) getCallBack()->Remove(anAIS);
293       #endif
294       aErased = true;
295     }
296   }
297   myResult2AISObjectMap.remove(theObject);
298
299 #ifdef DEBUG_DISPLAY
300   std::ostringstream aPtrStr;
301   aPtrStr << theObject.get();
302   qDebug(QString("erase object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
303   qDebug(getResult2AISObjectMapInfo().c_str());
304 #endif
305
306   if (theUpdateViewer)
307     updateViewer();
308
309   return aErased;
310 }
311
312 //**************************************************************
313 bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
314 {
315   bool aRedisplayed = false;
316   if (!isVisible(theObject))
317     return aRedisplayed;
318
319   AISObjectPtr aAISObj = getAISObject(theObject);
320   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
321
322   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
323   if (aPrs) {
324     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
325     if (!aAIS_Obj) {
326       aRedisplayed = erase(theObject, theUpdateViewer);
327       return aRedisplayed;
328     }
329     if (aAIS_Obj != aAISObj) {
330       appendResultObject(theObject, aAIS_Obj);
331     }
332     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
333   }
334
335   Handle(AIS_InteractiveContext) aContext = AISContext();
336   if (!aContext.IsNull() && !aAISIO.IsNull()) {
337     // Check that the visualized shape is the same and the redisplay is not necessary
338     // Redisplay of AIS object leads to this object selection compute and the selection
339     // in the browser is lost
340     // this check is not necessary anymore because the selection store/restore is realized
341     // before and after the values modification.
342     // Moreother, this check avoids customize and redisplay presentation if the presentable
343     // parameter is changed.
344     bool isEqualShapes = false;
345     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
346     if (aResult.get() != NULL) {
347       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aAISIO);
348       if (!aShapePrs.IsNull()) {
349         std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
350         if (aShapePtr.get()) {
351           const TopoDS_Shape& aOldShape = aShapePrs->Shape();
352           if (!aOldShape.IsNull())
353             isEqualShapes = aOldShape.IsEqual(aShapePtr->impl<TopoDS_Shape>());
354         }
355       }
356     }
357     // Customization of presentation
358     bool isCustomized = customizeObject(theObject);
359     #ifdef DEBUG_FEATURE_REDISPLAY
360       qDebug(QString("Redisplay: %1, isEqualShapes=%2, isCustomized=%3").
361         arg(!isEqualShapes || isCustomized).arg(isEqualShapes)
362         .arg(isCustomized).toStdString().c_str());
363     #endif
364     if (!isEqualShapes || isCustomized) {
365       /// if shapes are equal and presentation are customized, selection should be restored
366       bool aNeedToRestoreSelection = isEqualShapes && isCustomized;
367       if (aNeedToRestoreSelection)
368         myWorkshop->module()->storeSelection();
369
370 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
371       myWorkshop->selector()->deselectPresentation(aAISIO);
372 #endif
373       aContext->Redisplay(aAISIO, false);
374
375       #ifdef TINSPECTOR
376       if (getCallBack()) getCallBack()->Redisplay(aAISIO);
377       #endif
378
379       if (aNeedToRestoreSelection)
380         myWorkshop->module()->restoreSelection();
381
382       aRedisplayed = true;
383       #ifdef DEBUG_FEATURE_REDISPLAY
384         qDebug("  Redisplay happens");
385       #endif
386       if (theUpdateViewer)
387         updateViewer();
388     }
389   }
390   return aRedisplayed;
391 }
392
393 //**************************************************************
394 void XGUI_Displayer::redisplayObjects()
395 {
396   // redisplay objects visualized in the viewer
397   static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
398   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
399   QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
400   QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
401   for (; anIt != aLast; anIt++) {
402     aECreator->sendUpdated(*anIt, EVENT_DISP);
403   }
404   Events_Loop::loop()->flush(EVENT_DISP);
405 }
406
407 //**************************************************************
408 void XGUI_Displayer::deactivateObjects(const QObjectPtrList& theObjList,
409                                        const bool theUpdateViewer)
410 {
411   //Handle(AIS_InteractiveObject) aTrihedron = getTrihedron();
412   //if (!aTrihedron.IsNull())
413   //  deactivateAIS(aTrihedron);
414
415   QObjectPtrList::const_iterator anIt = theObjList.begin(), aLast = theObjList.end();
416   for (; anIt != aLast; anIt++) {
417     selectionActivate()->deactivate(*anIt, false);
418   }
419   //VSV It seems that there is no necessity to update viewer on deactivation
420   //if (theUpdateViewer)
421   //  updateViewer();
422 }
423
424 //**************************************************************
425 bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject)
426 {
427   bool aVisible = false;
428   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
429   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
430   if (aPrs.get() || aResult.get()) {
431     aVisible = theDisplayer->isVisible(theObject);
432     // compsolid is not visualized in the viewer,
433     // but should have presentation when all sub solids are
434     // visible. It is useful for highlight presentation where compsolid shape is selectable
435     if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
436       ResultCompSolidPtr aCompsolidResult =
437         std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
438       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
439         bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
440         for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
441           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
442         }
443         aVisible = anAllSubsVisible;
444       }
445     }
446   }
447   // it is possible that feature is presentable and has results, so we should check visibility
448   // of results if presentation is not shown (e.g. Sketch Circle/Arc features)
449   if (!aVisible) {
450     // check if all results of the feature are visible
451     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
452     std::list<ResultPtr> aResults;
453     ModelAPI_Tools::allResults(aFeature, aResults);
454     std::list<ResultPtr>::const_iterator aIt;
455     aVisible = !aResults.empty();
456     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
457       aVisible = aVisible && theDisplayer->isVisible(*aIt);
458     }
459   }
460   return aVisible;
461 }
462
463 //**************************************************************
464 void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValues,
465                                  bool theUpdateViewer)
466 {
467   Handle(AIS_InteractiveContext) aContext = AISContext();
468   if (aContext.IsNull())
469     return;
470   aContext->UnhilightSelected(false);
471   aContext->ClearSelected(false);
472   #ifdef TINSPECTOR
473   if (getCallBack()) getCallBack()->ClearSelected();
474   #endif
475   NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)>>
476     aShapesToBeSelected;
477
478   foreach (ModuleBase_ViewerPrsPtr aPrs, theValues) {
479     const GeomShapePtr& aGeomShape = aPrs->shape();
480     if (aGeomShape.get() && !aGeomShape->isNull()) {
481       const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
482 #ifdef DEBUG_OCCT_SHAPE_SELECTION
483       // problem 1: performance
484       // problem 2: IO is not specified, so the first found owner is selected, as a result
485       // it might belong to another result
486       aContext->AddOrRemoveSelected(aShape, false);
487       #ifdef TINSPECTOR
488       if (getCallBack()) getCallBack()->AddOrRemoveSelected(aShape);
489       #endif
490 #else
491       NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations;
492       if (aShapesToBeSelected.IsBound(aShape))
493         aPresentations = aShapesToBeSelected.Find(aShape);
494       ObjectPtr anObject = aPrs->object();
495       getPresentations(anObject, aPresentations);
496
497       aShapesToBeSelected.Bind(aShape, aPresentations);
498 #endif
499     } else {
500       ObjectPtr anObject = aPrs->object();
501       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
502       if (aResult.get() && isVisible(aResult)) {
503         AISObjectPtr anObj = myResult2AISObjectMap[aResult];
504         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
505         if (!anAIS.IsNull()) {
506           // The methods are replaced in order to provide multi-selection, e.g. restore selection
507           // by activating multi selector widget. It also gives an advantage that the multi
508           // selection in OB gives multi-selection in the viewer
509           //aContext->SetSelected(anAIS, false);
510           // The selection in the context was cleared, so the method sets the objects are selected
511           aContext->AddOrRemoveSelected(anAIS, false);
512           #ifdef TINSPECTOR
513           if (getCallBack()) getCallBack()->AddOrRemoveSelected(anAIS);
514           #endif
515         }
516       }
517     }
518   }
519   if (!aShapesToBeSelected.IsEmpty())
520     XGUI_Displayer::AddOrRemoveSelectedShapes(aContext, aShapesToBeSelected);
521
522   if (theUpdateViewer)
523     updateViewer();
524 }
525
526 //**************************************************************
527 void XGUI_Displayer::clearSelected(const bool theUpdateViewer)
528 {
529   Handle(AIS_InteractiveContext) aContext = AISContext();
530   if (!aContext.IsNull()) {
531     aContext->UnhilightSelected(false);//UnhilightCurrents(false);
532     aContext->ClearSelected(theUpdateViewer);
533     #ifdef TINSPECTOR
534     if (getCallBack()) getCallBack()->ClearSelected();
535     #endif
536   }
537 }
538
539 //**************************************************************
540 bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
541 {
542   bool aErased = false;
543   Handle(AIS_InteractiveContext) aContext = AISContext();
544   if (!aContext.IsNull()) {
545     foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
546       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
547       // erase an object
548       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
549       if (!anIO.IsNull()) {
550         emit beforeObjectErase(aObj, aAISObj);
551         aContext->Remove(anIO, false/*update viewer*/);
552         #ifdef TINSPECTOR
553         if (getCallBack()) getCallBack()->Remove(anIO);
554         #endif
555         aErased = true;
556       }
557     }
558     if (theUpdateViewer)
559       updateViewer();
560   }
561   myResult2AISObjectMap.clear();
562 #ifdef DEBUG_DISPLAY
563   qDebug("eraseAll");
564   qDebug(getResult2AISObjectMapInfo().c_str());
565 #endif
566   return aErased;
567 }
568
569
570 //**************************************************************
571 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
572 {
573   AISObjectPtr anIO;
574   if (myResult2AISObjectMap.contains(theObject))
575     anIO = myResult2AISObjectMap[theObject];
576   return anIO;
577 }
578
579 //**************************************************************
580 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
581 {
582   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
583   return getObject(aRefAIS);
584 }
585
586 //**************************************************************
587 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
588 {
589   ObjectPtr anObject;
590   foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
591     AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
592     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
593     if (anAIS == theIO)
594       anObject = anObj;
595     if (anObject.get())
596       break;
597   }
598   if (!anObject.get()) {
599     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
600     if (!theIO.IsNull()) {
601       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(theIO));
602     }
603     anObject = myWorkshop->module()->findPresentedObject(anAISObj);
604   }
605   return anObject;
606 }
607
608 //**************************************************************
609 bool XGUI_Displayer::enableUpdateViewer(const bool isEnabled)
610 {
611   bool aWasEnabled = isUpdateEnabled();
612   if (isEnabled)
613     myViewerBlockedRecursiveCount--;
614   else
615     myViewerBlockedRecursiveCount++;
616
617   if (myNeedUpdate && isUpdateEnabled()) {
618     updateViewer();
619     myNeedUpdate = false;
620   }
621   return aWasEnabled;
622 }
623
624 //**************************************************************
625 bool XGUI_Displayer::isUpdateEnabled() const
626 {
627   return myViewerBlockedRecursiveCount == 0;
628 }
629
630 //**************************************************************
631 void XGUI_Displayer::updateViewer() const
632 {
633   Handle(AIS_InteractiveContext) aContext = AISContext();
634   if (!aContext.IsNull() && isUpdateEnabled()) {
635     //myWorkshop->viewer()->Zfitall();
636     aContext->UpdateCurrentViewer();
637   } else {
638     myNeedUpdate = true;
639   }
640 }
641
642 //**************************************************************
643 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
644 {
645   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
646   if (!aContext.IsNull() && myIsFirstAISContextUse/*&& !aContext->HasOpenedContext()*/) {
647     XGUI_Displayer* aDisplayer = (XGUI_Displayer*)this;
648     aDisplayer->myIsFirstAISContextUse = false;
649     if (!myWorkshop->selectionActivate()->isTrihedronActive())
650       selectionActivate()->deactivateTrihedron(true);
651     aContext->DefaultDrawer()->VIsoAspect()->SetNumber(0);
652     aContext->DefaultDrawer()->UIsoAspect()->SetNumber(0);
653   }
654   return aContext;
655 }
656
657 //**************************************************************
658 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
659 {
660   Handle(AIS_InteractiveContext) aContext = AISContext();
661   if (!aContext.IsNull() && myAndFilter.IsNull()) {
662     myAndFilter = new SelectMgr_AndFilter();
663     aContext->AddFilter(myAndFilter);
664   }
665   return myAndFilter;
666 }
667
668 //**************************************************************
669 bool XGUI_Displayer::displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
670                                 const Standard_Integer theDisplayMode, bool theUpdateViewer)
671 {
672   bool aDisplayed = false;
673   Handle(AIS_InteractiveContext) aContext = AISContext();
674   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
675   if (!aContext.IsNull() && !anAISIO.IsNull()) {
676     aContext->Display(anAISIO, theDisplayMode, 0, false/*update viewer*/, true, AIS_DS_Displayed);
677     #ifdef TINSPECTOR
678     if (getCallBack()) getCallBack()->Display(anAISIO);
679     #endif
680     aDisplayed = true;
681     aContext->Deactivate(anAISIO);
682     #ifdef TINSPECTOR
683     if (getCallBack()) getCallBack()->Deactivate(anAISIO);
684     #endif
685     aContext->Load(anAISIO);
686     #ifdef TINSPECTOR
687     if (getCallBack()) getCallBack()->Load(anAISIO);
688     #endif
689     if (toActivateInSelectionModes)
690       myWorkshop->selectionActivate()->activateOnDisplay(anAISIO, theUpdateViewer);
691
692     if (theUpdateViewer)
693       updateViewer();
694   }
695   return aDisplayed;
696 }
697
698 //**************************************************************
699 bool XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer)
700 {
701   bool aErased = false;
702   Handle(AIS_InteractiveContext) aContext = AISContext();
703   if (!aContext.IsNull()) {
704     Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
705     if (!anAISIO.IsNull() && aContext->IsDisplayed(anAISIO)) {
706       aContext->Remove(anAISIO, false/*update viewer*/);
707       #ifdef TINSPECTOR
708       if (getCallBack()) getCallBack()->Remove(anAISIO);
709       #endif
710       aErased = true;
711     }
712   }
713   if (aErased && theUpdateViewer)
714     updateViewer();
715   return aErased;
716 }
717
718 //**************************************************************
719 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer)
720 {
721   if (theMode == NoMode)
722     return;
723
724   Handle(AIS_InteractiveContext) aContext = AISContext();
725   if (aContext.IsNull())
726     return;
727
728   AISObjectPtr aAISObj = getAISObject(theObject);
729   if (!aAISObj)
730     return;
731
732   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
733   aContext->SetDisplayMode(aAISIO, theMode, false);
734   // Redisplay in order to update new mode because it could be not computed before
735   if (theUpdateViewer)
736     updateViewer();
737 }
738
739 //**************************************************************
740 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
741 {
742   Handle(AIS_InteractiveContext) aContext = AISContext();
743   if (aContext.IsNull())
744     return NoMode;
745
746   AISObjectPtr aAISObj = getAISObject(theObject);
747   if (!aAISObj)
748     return NoMode;
749
750   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
751   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
752 }
753
754 //**************************************************************
755 void XGUI_Displayer::deactivateSelectionFilters(const bool theAddFilterOnly)
756 {
757   Handle(AIS_InteractiveContext) aContext = AISContext();
758   if (!myAndFilter.IsNull()) {
759     bool aFound = false;
760     if (!aContext.IsNull()) {
761       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
762       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
763       for (; anIt.More() && !aFound; anIt.Next()) {
764         Handle(SelectMgr_Filter) aFilter = anIt.Value();
765         aFound = aFilter == myAndFilter;
766       }
767       if (aFound)
768         aContext->RemoveFilter(myAndFilter);
769     }
770     myAndFilter.Nullify();
771   }
772 }
773
774 //**************************************************************
775 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
776 {
777   Handle(AIS_InteractiveContext) aContext = AISContext();
778   if (aContext.IsNull() || hasSelectionFilter(theFilter))
779     return;
780
781   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
782   if (!aCompositeFilter.IsNull()) {
783     aCompositeFilter->Add(theFilter);
784 #ifdef DEBUG_SELECTION_FILTERS
785     int aCount = aCompositeFilter->StoredFilters().Extent();
786     qDebug(QString("addSelectionFilter: filters.count() = %1").arg(aCount).toStdString().c_str());
787 #endif
788   }
789 }
790
791 //**************************************************************
792 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
793 {
794   Handle(AIS_InteractiveContext) aContext = AISContext();
795   if (aContext.IsNull())
796     return;
797
798   Handle(SelectMgr_AndFilter) aCompositeFilter = GetFilter();
799   if (!aCompositeFilter.IsNull() && aCompositeFilter->IsIn(theFilter)) {
800     aCompositeFilter->Remove(theFilter);
801 #ifdef DEBUG_SELECTION_FILTERS
802     int aCount = aCompositeFilter->StoredFilters().Extent();
803     qDebug(QString("removeSelectionFilter: filters.count() = %1")
804       .arg(aCount).toStdString().c_str());
805 #endif
806   }
807 }
808
809 //**************************************************************
810 bool XGUI_Displayer::hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
811 {
812   bool aFilterFound = false;
813
814   Handle(AIS_InteractiveContext) aContext = AISContext();
815   if (aContext.IsNull())
816     return aFilterFound;
817   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
818   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
819   for (; aIt.More() && !aFilterFound; aIt.Next()) {
820     if (theFilter.get() == aIt.Value().get())
821       aFilterFound = true;
822   }
823   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
824   if (!aCompositeFilter.IsNull()) {
825     const SelectMgr_ListOfFilter& aStoredFilters = aCompositeFilter->StoredFilters();
826     for (aIt.Initialize(aStoredFilters); aIt.More() && !aFilterFound; aIt.Next()) {
827       if (theFilter.get() == aIt.Value().get())
828         aFilterFound = true;
829     }
830   }
831   return aFilterFound;
832 }
833
834 //**************************************************************
835 void XGUI_Displayer::removeFilters()
836 {
837   Handle(AIS_InteractiveContext) aContext = AISContext();
838   if (aContext.IsNull())
839     return;
840
841   Handle(SelectMgr_CompositionFilter) aCompositeFilter = GetFilter();
842   if (!aCompositeFilter.IsNull())
843     aCompositeFilter->Clear();
844 }
845
846 //**************************************************************
847 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
848 {
849   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
850   foreach(ObjectPtr aObj, aDispList) {
851     if (!theList.contains(aObj))
852       erase(aObj, false);
853   }
854   foreach(ObjectPtr aObj, theList) {
855     if (!isVisible(aObj))
856       display(aObj, false);
857   }
858   updateViewer();
859 }
860
861 //**************************************************************
862 bool XGUI_Displayer::canBeShaded(ObjectPtr theObject) const
863 {
864   if (!isVisible(theObject))
865     return false;
866
867   AISObjectPtr aAISObj = getAISObject(theObject);
868   if (aAISObj.get() == NULL)
869     return false;
870
871   Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
872   return ::canBeShaded(anAIS, myWorkshop->module());
873 }
874
875 //**************************************************************
876 bool XGUI_Displayer::customizeObject(ObjectPtr theObject)
877 {
878   AISObjectPtr anAISObj = getAISObject(theObject);
879   // correct the result's color it it has the attribute
880   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
881
882   // Customization of presentation
883   GeomCustomPrsPtr aCustomPrs;
884   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
885   if (aFeature.get() != NULL) {
886     GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
887     if (aCustPrs.get() != NULL)
888       aCustomPrs = aCustPrs;
889   }
890   if (aCustomPrs.get() == NULL) {
891     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
892     // we ignore presentable not customized objects
893     if (aPrs.get() == NULL)
894       aCustomPrs = myCustomPrs;
895   }
896   bool isCustomized = aCustomPrs.get() &&
897                       aCustomPrs->customisePresentation(aResult, anAISObj, myCustomPrs);
898   isCustomized = myWorkshop->module()->afterCustomisePresentation(aResult, anAISObj, myCustomPrs)
899                  || isCustomized;
900
901   // update presentation state if faces panel is active
902   if (anAISObj.get() && myWorkshop->facesPanel())
903     isCustomized = myWorkshop->facesPanel()->customizeObject(theObject, anAISObj) || isCustomized;
904
905   return isCustomized;
906 }
907
908 //**************************************************************
909 QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
910                                       const QColor& theColor,
911                                       bool theUpdateViewer)
912 {
913   if (!isVisible(theObject))
914     return Qt::black;
915
916   AISObjectPtr anAISObj = getAISObject(theObject);
917   int aR, aG, aB;
918   anAISObj->getColor(aR, aG, aB);
919   anAISObj->setColor(theColor.red(), theColor.green(), theColor.blue());
920   if (theUpdateViewer)
921     updateViewer();
922   return QColor(aR, aG, aB);
923 }
924
925 //**************************************************************
926 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
927 {
928   myResult2AISObjectMap[theObject] = theAIS;
929
930 #ifdef DEBUG_DISPLAY
931   std::ostringstream aPtrStr;
932   aPtrStr << theObject.get();
933   qDebug(QString("display object: %1").arg(aPtrStr.str().c_str()).toStdString().c_str());
934   qDebug(getResult2AISObjectMapInfo().c_str());
935 #endif
936 }
937
938 #ifdef _DEBUG
939 //**************************************************************
940 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
941 {
942   QStringList aContent;
943   foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
944     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
945     std::ostringstream aPtrStr;
946     aPtrStr << "aObj = " << aObj.get() << ":";
947     aPtrStr << "anAIS = " << aAISObj.get() << ":";
948     aPtrStr << "[" << ModuleBase_Tools::objectInfo(aObj).toStdString().c_str() << "]";
949
950     aContent.append(aPtrStr.str().c_str());
951   }
952   return QString("myResult2AISObjectMap: size = %1\n%2\n").arg(myResult2AISObjectMap.size()).
953                                             arg(aContent.join("\n")).toStdString().c_str();
954 }
955 #endif
956
957 //**************************************************************
958 void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
959                                   NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations)
960 {
961   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
962   if (aResult.get()) {
963     AISObjectPtr aAISObj = getAISObject(aResult);
964     if (aAISObj.get() == NULL) {
965       // if result is a result of a composite feature, it is visualized by visualization of
966       // composite children, so we should get one of this presentations
967       ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
968       if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
969         aAISObj = getAISObject(aCompSolid->subResult(0));
970       }
971     }
972     if (aAISObj.get() != NULL) {
973       Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
974       if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
975         thePresentations.Add(anAIS);
976     }
977   }
978   else {
979     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
980     // find presentation of the feature
981     AISObjectPtr aAISObj = getAISObject(aFeature);
982     if (aAISObj.get() != NULL) {
983       Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
984       if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
985         thePresentations.Add(anAIS);
986     }
987     // find presentations of the feature results
988     std::list<ResultPtr> aResults;
989     ModelAPI_Tools::allResults(aFeature, aResults);
990     std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
991     for (; anIt != aLast; ++anIt) {
992       AISObjectPtr aAISObj = getAISObject(*anIt);
993       if (aAISObj.get() != NULL) {
994         Handle(AIS_InteractiveObject) anAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
995         if (!anAIS.IsNull() && !thePresentations.Contains(anAIS))
996           thePresentations.Add(anAIS);
997       }
998     }
999   }
1000 }
1001
1002 //**************************************************************
1003 void XGUI_Displayer::displayTrihedron(bool theToDisplay) const
1004 {
1005   Handle(AIS_InteractiveContext) aContext = AISContext();
1006   if (aContext.IsNull())
1007     return;
1008
1009   Handle(AIS_Trihedron) aTrihedron = myWorkshop->viewer()->trihedron();
1010
1011   XGUI_SelectionActivate* aSelectionActive = selectionActivate();
1012   if (theToDisplay) {
1013     if (!aContext->IsDisplayed(aTrihedron))
1014       aContext->Display(aTrihedron,
1015                         0 /*wireframe*/,
1016                         -1 /* selection mode */,
1017                         Standard_True /* update viewer*/,
1018                         Standard_False /* allow decomposition */,
1019                         AIS_DS_Displayed /* xdisplay status */);
1020     #ifdef TINSPECTOR
1021     if (getCallBack()) getCallBack()->Display(aTrihedron);
1022     #endif
1023
1024     if (!aSelectionActive->isTrihedronActive())
1025       aSelectionActive->deactivateTrihedron(false);
1026     else
1027       aSelectionActive->activate(aTrihedron, false);
1028   } else {
1029     aSelectionActive->deactivateTrihedron(false);
1030
1031     aContext->Erase(aTrihedron, Standard_True);
1032     #ifdef TINSPECTOR
1033     if (getCallBack()) getCallBack()->Remove(aTrihedron);
1034     #endif
1035   }
1036
1037   updateViewer();
1038 }
1039
1040 //**************************************************************
1041 void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
1042                            const NCollection_DataMap<TopoDS_Shape,
1043                            NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected)
1044 {
1045   NCollection_Map<Handle(AIS_InteractiveObject)> aCompsolidPresentations;
1046   NCollection_Map<Handle(AIS_InteractiveObject)> aSelectedPresentations;
1047
1048   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
1049   theContext->MainSelector()->ActiveOwners(anActiveOwners);
1050   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
1051   Handle(SelectMgr_EntityOwner) anOwner;
1052
1053   /// It is very important to check that the owner is processed only once and has a map of
1054   /// processed owners because SetSelected works as a switch.
1055   /// If count of calls setSelectec is even, the object stays in the previous state
1056   /// (selected, deselected)
1057   /// OCCT: to write about the problem that active owners method returns one owner several times
1058   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
1059   for (; anOwnersIt.More(); anOwnersIt.Next()) {
1060     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
1061     if (aSelectedIds.contains((long)anOwner.get()))
1062       continue;
1063     aSelectedIds.append((long)anOwner.get());
1064
1065     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
1066     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
1067       const TopoDS_Shape& aShape = BROwnr->Shape();
1068       if (aShape.IsNull())
1069         continue;
1070
1071       Handle(ModuleBase_BRepOwner) aCustomOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
1072
1073       NCollection_DataMap<TopoDS_Shape, NCollection_Map<Handle(AIS_InteractiveObject)> >
1074                                              ::Iterator aShapeIt(theShapesToBeSelected);
1075       for (; aShapeIt.More(); aShapeIt.Next()) {
1076         const TopoDS_Shape& aParameterShape = aShapeIt.Key();
1077         // isSame should be used here as it does not check orientation of shapes
1078         // despite on isEqual of shapes or IsBound for shape in QMap. Orientation is
1079         // different for Edges shapes in model shape and owner even if this is the same shape
1080         if (aParameterShape.IsSame(aShape)) {
1081           Handle(AIS_InteractiveObject) anOwnerPresentation =
1082                             Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
1083           NCollection_Map<Handle(AIS_InteractiveObject)> aPresentations =
1084                                       theShapesToBeSelected.Find(aParameterShape);
1085           if (aPresentations.Contains(anOwnerPresentation)) {
1086             theContext->AddOrRemoveSelected(anOwner, Standard_False);
1087             anOwner->SetSelected (Standard_True);
1088             // collect selected presentations to do not select them if compsolid is selected
1089             if (!aSelectedPresentations.Contains(anOwnerPresentation))
1090               aSelectedPresentations.Add(anOwnerPresentation);
1091           }
1092         }
1093         else if (!aCustomOwner.IsNull()) { // CompSolid processing #2219
1094           // shape of owner is compound, but shape to be selected is compsolid, so
1095           // we need to compare shape to AIS presentation of owner(rule of the owner creation)
1096           Handle(AIS_Shape) anOwnerPresentation =
1097                             Handle(AIS_Shape)::DownCast(anOwner->Selectable());
1098           const TopoDS_Shape& aPresentationShape = anOwnerPresentation->Shape();
1099           if (aParameterShape.IsSame(anOwnerPresentation->Shape()) &&
1100               !aCompsolidPresentations.Contains(anOwnerPresentation))
1101             aCompsolidPresentations.Add(anOwnerPresentation);
1102         }
1103       }
1104     }
1105   }
1106   // select CompSolid presentations if their owners was not selected yet
1107   NCollection_Map<Handle(AIS_InteractiveObject)>::Iterator anIt (aCompsolidPresentations);
1108   for (; anIt.More(); anIt.Next()) {
1109     if (aSelectedPresentations.Contains(anIt.Value()))
1110       continue;
1111     theContext->AddOrRemoveSelected(anIt.Value(), Standard_False);
1112   }
1113 }
1114
1115 //**************************************************************
1116 XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
1117 {
1118   return myWorkshop->selectionActivate();
1119 }