1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_Displayer.h
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #ifndef XGUI_Displayer_H
8 #define XGUI_Displayer_H
12 #include <GeomAPI_AISObject.h>
13 #include <TopoDS_Shape.hxx>
14 #include <AIS_InteractiveObject.hxx>
15 #include <AIS_InteractiveContext.hxx>
16 #include <NCollection_Map.hxx>
17 #include <NCollection_DataMap.hxx>
19 #include <ModelAPI_Result.h>
21 #include <ModuleBase_Definitions.h>
23 #include <GeomAPI_ICustomPrs.h>
25 #include <SelectMgr_AndFilter.hxx>
32 class ModuleBase_ViewerPrs;
33 class ModelAPI_Feature;
36 /**\class XGUI_Displayer
38 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
40 class XGUI_EXPORT XGUI_Displayer: public QObject
44 /// \enum DisplayMode display mode
46 /// Mode is not defined
48 /// Wireframe display mode
50 /// Shading display mode
55 /// \param theWorkshop a workshop instance
56 XGUI_Displayer(XGUI_Workshop* theWorkshop);
59 virtual ~XGUI_Displayer();
61 /// Returns the feature visibility state.
62 /// \param theObject an object instance
63 bool isVisible(ObjectPtr theObject) const;
65 /// Display the feature. Obtain the visualized object from the feature.
66 /// \param theObject an object to display
67 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
68 /// \return true if the object visibility state is changed
69 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
71 /// Display the given AIS object. This object is not added to the displayer internal map of objects
72 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in order
73 /// to perform the display with correct flags.
74 /// \param theAIS AIOS object to display
75 /// \param toActivateInSelectionModes boolean value whether the presentation should be
76 /// activated in the current selection modes
77 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
78 /// \return true if the object visibility state is changed
79 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
80 bool theUpdateViewer = true);
82 /// Redisplay the shape if it was displayed
83 /// \param theObject an object instance
84 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
85 /// \return true if the object visibility state is changed
86 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
88 /// Sends and flushes a signal to redisplay all visualized objects.
89 void redisplayObjects();
91 /// Add presentations to current selection. It unhighlight and deselect the current selection.
92 /// The shape and result components are processed in the values. If the presentation shape is not
93 /// empty, select it, otherwise select the result.
94 /// \param theValues a list of presentation to be selected
95 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
96 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues, bool theUpdateViewer = true);
98 /// Unselect all objects
99 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
100 /// \param theUpdateViewer the parameter to update viewer
101 void clearSelected(const bool theUpdateViewer = true);
103 /// Erase the feature and a shape.
104 /// \param theObject an object instance
105 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
106 /// \return true if the object visibility state is changed
107 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
109 /// Erase the given AIS object displayed by corresponded display method
110 /// \param theAIS instance of AIS object
111 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
112 /// \return true if the object visibility state is changed
113 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
115 /// Erase all presentations
116 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
117 /// \return true if the object visibility state is changed
118 bool eraseAll(const bool theUpdateViewer = true);
120 /// Deactivates selection of sub-shapes
121 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
122 void closeLocalContexts(const bool theUpdateViewer = true);
124 /// Remove default selection filters of the module from the current viewer
125 void deactivateSelectionFilters();
127 /// \brief Add selection filter
128 /// \param theFilter a filter instance
129 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
131 /// \brief Remove selection filter
132 /// \param theFilter a filter instance
133 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
135 /// Returns true if the selection filter is set to the viewer
136 /// \param theFilter a selection filter
137 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
139 /// Remove all selection filters
140 void removeFilters();
142 /// Sets a flag to the displayer whether the internal viewer can be updated by
143 /// the updateViewer method call. If it is not enabled, this method do nothing.
144 /// This state maintain recurse, if the update is blocked twice or three times, the
145 /// viewer will not be updated until it is unblocked necessary times(twice or three in the example).
146 /// \param isEnabled a boolean value
147 bool enableUpdateViewer(const bool isEnabled);
149 /// Returns true if the viewer update is not blocked
150 bool isUpdateEnabled() const;
152 /// Updates the viewer
153 void updateViewer() const;
155 /// Activate interactive context
156 /// \param theIO an interactive object
157 /// \param theMode activation mode
158 /// \param theUpdateViewer update viewer flag
159 void activateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode,
160 const bool theUpdateViewer) const;
162 /// Activate interactive context. It is necessary to call ClearOutdatedSelection after deactivation
163 /// \param theIO an interactive object
164 /// \param theMode a mode to deactivate. When theMode=-1 then all modes will be deactivated
165 void deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode = -1) const;
167 /// Searches the interactive object by feature
168 /// \param theObject the object or presentable feature
169 /// \return theIO an interactive object
170 AISObjectPtr getAISObject(ObjectPtr theObject) const;
172 /// Searches the feature by interactive object
173 /// \param theIO an interactive object
174 /// \return feature the feature or NULL if it not visualized
175 ObjectPtr getObject(const AISObjectPtr& theIO) const;
177 /// Searches the feature by interactive object
178 /// \param theIO an interactive object
179 /// \return corresponded object or NULL if it not found
180 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
182 /// Deactivates the given objects (not allow selection)
183 /// \param theObjList - list of objects which has to be deactivated.
184 /// \param theUpdateViewer update viewer flag
185 void deactivateObjects(const QObjectPtrList& theObjList,
186 const bool theUpdateViewer = true);
188 /// Returns the modes of activation
189 /// \param theObject the feature or NULL if it not visualized
190 /// \param theModes - modes on which it is activated (can be empty)
191 void getModesOfActivation(ObjectPtr theObject, QIntList& theModes);
193 /// Returns true if the given object can be selected
194 /// \param theObject object to check
195 bool isActive(ObjectPtr theObject) const;
197 /// Activates in local context displayed outside of the context.
198 /// \param theModes - modes on which it has to be activated (can be empty)
199 /// \param theObjList - list of objects which has to be activated.
200 /// \param theUpdateViewer an update viewer flag
201 void activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
202 const bool theUpdateViewer = true);
204 /// Sets display mode for the given object if this object is displayed
205 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
207 /// Returns current display mode for the given object.
208 /// If object is not dis played then returns NoMode.
209 /// \param theObject object to check
210 DisplayMode displayMode(ObjectPtr theObject) const;
212 /// Displays only objects listed in the list
213 /// \param theList list of objects
214 void showOnly(const QObjectPtrList& theList);
216 /// Returns number of displayed objects
217 int objectsCount() const { return myResult2AISObjectMap.size(); }
219 /// Returns list of displayed objects
220 QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.keys(); }
222 /// Returns list of displayed objects
223 QList<AISObjectPtr> displayedPresentations() const { return myResult2AISObjectMap.values(); }
225 /// Returns true if the given object can be shown in shaded mode
226 /// \param theObject object to check
227 bool canBeShaded(ObjectPtr theObject) const;
229 /// Set color on presentation of an object if it is displayed
230 /// \param theObject an object
231 /// \param theColor a color which has to be set
232 /// \param theUpdateViewer update viewer flag
233 /// \return previously defined color on the object
234 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
236 /// Returns Trihedron object if it is displayed
237 Handle(AIS_InteractiveObject) getTrihedron() const;
239 /// Set trihedron active (used in selection) or non active
240 void activateTrihedron(bool theIsActive);
242 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
243 /// depending on the trihedron visible state and displayer active trihedron state
244 void displayTrihedron(bool theToDisplay) const;
246 /// Returns true if the trihedron should be activated in current selection modes
247 bool isTrihedronActive() const { return myIsTrihedronActive; }
249 /// Returns list of currently active selection modes
250 /// Selection modes will be returned according to TopAbs_ShapeEnum
251 QIntList activeSelectionModes() const;
254 /// Converts shape type (TopAbs_ShapeEnum) to selection mode
255 /// \param theShapeType a shape type from TopAbs_ShapeEnum
256 static int getSelectionMode(int theShapeType);
258 /// Return true if the object is visible. If the object is feature, it returns true
259 /// if all results of the feature are shown
260 /// \param theDisplayer a displayer
261 /// \param theObject an object
262 /// \return a boolean value
263 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
266 /// Signal on object display
267 /// \param theObject a data object
268 /// \param theAIS a presentation object
269 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
271 /// Signal on before object erase
272 /// \param theObject a data object
273 /// \param theAIS a presentation object
274 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
277 /// Returns currently installed AIS_InteractiveContext
278 Handle(AIS_InteractiveContext) AISContext() const;
280 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
281 /// The context should have only this filter inside. Other filters should be add to the filter
282 Handle(SelectMgr_AndFilter) GetFilter();
284 /// Display the feature and a shape. This shape would be associated to the given feature
285 /// \param theObject an object instance
286 /// \param theAIS AIS presentation
287 /// \param isShading flag to show in shading mode
288 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
289 /// \return true if the object visibility state is changed
290 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
291 bool theUpdateViewer = true);
294 /// Activates the interactive object in the local context.
295 /// \param theIO an interactive object
296 /// \param theModes - modes on which it has to be activated (can be empty)
297 /// \return a flag is object activated or not
298 bool activate(const Handle(AIS_InteractiveObject)& theIO, const QIntList& theModes,
299 const bool theUpdateViewer) const;
301 /// Deactivates the given object (not allow selection)
302 /// \param theObject object to deactivate
303 void deactivate(ObjectPtr theObject, const bool theUpdateViewer);
305 /// Find a trihedron in a list of displayed presentations and deactivate it.
306 /// \param theUpdateViewer an update viewer flag
307 void deactivateTrihedron(const bool theUpdateViewer) const;
309 /// Opens local context. Does nothing if it is already opened.
310 void openLocalContext();
312 /// Update the object presentable properties such as color, lines width and other
313 /// If the object is result with the color attribute value set, it is used,
314 /// otherwise the customize is applyed to the object's feature if it is a custom prs
315 /// \param theObject an object instance
316 /// \return the true state if there is changes and the presentation is customized
317 bool customizeObject(ObjectPtr theObject);
319 /// Append the objects in the internal map. Checks whether the map already contains the object
320 /// \param theObject an object to display
321 /// \param theAIS AIOS object to display
322 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
324 /// Returns an information about alredy displayed objects
325 /// \return a string representation
326 std::string getResult2AISObjectMapInfo() const;
328 /// Returns container of visible presentations for the object. For a feature object,
329 /// the feature results are processed also. The presentations map is not cleared inside.
330 /// \param theObject a feature or result
331 /// \param thePresentations result map of presentations
332 void getPresentations(const ObjectPtr& theObject,
333 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
335 /// Sets the shapes selected in the context. It contains logic of the similar method
336 /// in OCCT but improved for performance. The modification is to iterates by a list
337 /// of owners in the context only once.
338 /// \param theContext a viewer context. It has opened local context
339 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the owner
340 /// is selected if it is found there. Only first owner is processed(according to OCCT logic)
341 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
342 const NCollection_DataMap<TopoDS_Shape,
343 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
345 //const NCollection_Map<TopoDS_Shape>& theShapesToBeSelected);
348 /// Reference to workshop
349 XGUI_Workshop* myWorkshop;
351 /// A container for selection filters
352 Handle(SelectMgr_AndFilter) myAndFilter;
354 /// A default custom presentation, which is used if the displayed feature is not a custom presentation
355 GeomCustomPrsPtr myCustomPrs;
357 /// Definition of a type of map which defines correspondance between objects and presentations
358 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
360 /// A map of displayed objects
361 ResultToAISMap myResult2AISObjectMap;
363 /// Selection modes installed for external objects in local context
364 QIntList myActiveSelectionModes;
366 /// Number of blocking of the viewer update. The viewer is updated only if it equals zero
367 int myViewerBlockedRecursiveCount;
369 /// Flag: use trihedgon for selection or not
370 bool myIsTrihedronActive;
372 /// A flag that update was requested but not done
373 mutable bool myNeedUpdate;