1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef XGUI_Displayer_H
22 #define XGUI_Displayer_H
26 #include <GeomAPI_AISObject.h>
27 #include <TopoDS_Shape.hxx>
28 #include <AIS_InteractiveObject.hxx>
29 #include <AIS_InteractiveContext.hxx>
30 #include <NCollection_Map.hxx>
31 #include <NCollection_DataMap.hxx>
33 #include <ModelAPI_Result.h>
35 #include <ModuleBase_Definitions.h>
37 #include <GeomAPI_ICustomPrs.h>
39 #include <SelectMgr_AndFilter.hxx>
46 class ModuleBase_ViewerPrs;
47 class ModelAPI_Feature;
51 class VInspectorAPI_CallBack;
54 /**\class XGUI_Displayer
56 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
58 class XGUI_EXPORT XGUI_Displayer: public QObject
62 /// \enum DisplayMode display mode
64 /// Mode is not defined
66 /// Wireframe display mode
68 /// Shading display mode
73 /// \param theWorkshop a workshop instance
74 XGUI_Displayer(XGUI_Workshop* theWorkshop);
77 virtual ~XGUI_Displayer();
79 /// Returns the feature visibility state.
80 /// \param theObject an object instance
81 bool isVisible(ObjectPtr theObject) const;
83 /// Display the feature. Obtain the visualized object from the feature.
84 /// \param theObject an object to display
85 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
86 /// \return true if the object visibility state is changed
87 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
89 /// Display the given AIS object.
90 /// This object is not added to the displayer internal map of objects
91 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
92 /// order to perform the display with correct flags.
93 /// \param theAIS AIOS object to display
94 /// \param toActivateInSelectionModes boolean value whether the presentation should be
95 /// activated in the current selection modes
96 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
97 /// \return true if the object visibility state is changed
98 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
99 bool theUpdateViewer = true);
101 /// Redisplay the shape if it was displayed
102 /// \param theObject an object instance
103 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
104 /// \return true if the object visibility state is changed
105 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
107 /// Sends and flushes a signal to redisplay all visualized objects.
108 void redisplayObjects();
110 /// Add presentations to current selection. It unhighlight and deselect the current selection.
111 /// The shape and result components are processed in the values. If the presentation shape is not
112 /// empty, select it, otherwise select the result.
113 /// \param theValues a list of presentation to be selected
114 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
115 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
116 bool theUpdateViewer = true);
118 /// Unselect all objects
119 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
120 /// \param theUpdateViewer the parameter to update viewer
121 void clearSelected(const bool theUpdateViewer = true);
123 /// Erase the feature and a shape.
124 /// \param theObject an object instance
125 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
126 /// \return true if the object visibility state is changed
127 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
129 /// Erase the given AIS object displayed by corresponded display method
130 /// \param theAIS instance of AIS object
131 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
132 /// \return true if the object visibility state is changed
133 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
135 /// Erase all presentations
136 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
137 /// \return true if the object visibility state is changed
138 bool eraseAll(const bool theUpdateViewer = true);
140 /// Deactivates selection of sub-shapes
141 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
142 //void closeLocalContexts(const bool theUpdateViewer = true);
144 /// Remove default selection filters of the module from the current viewer
145 void deactivateSelectionFilters();
147 /// \brief Add selection filter
148 /// \param theFilter a filter instance
149 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
151 /// \brief Remove selection filter
152 /// \param theFilter a filter instance
153 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
155 /// Returns true if the selection filter is set to the viewer
156 /// \param theFilter a selection filter
157 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
159 /// Remove all selection filters
160 void removeFilters();
162 /// Sets a flag to the displayer whether the internal viewer can be updated by
163 /// the updateViewer method call. If it is not enabled, this method do nothing.
164 /// This state maintain recurse, if the update is blocked twice or three times, the
165 /// viewer will not be updated until it is unblocked necessary times
166 /// (twice or three in the example).
167 /// \param isEnabled a boolean value
168 bool enableUpdateViewer(const bool isEnabled);
170 /// Returns true if the viewer update is not blocked
171 bool isUpdateEnabled() const;
173 /// Updates the viewer
174 void updateViewer() const;
176 /// Activate interactive context
177 /// \param theIO an interactive object
178 /// \param theMode activation mode
179 /// \param theUpdateViewer update viewer flag
180 void activateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode,
181 const bool theUpdateViewer) const;
183 /// Activate interactive context. It is necessary to call ClearOutdatedSelection
184 /// after deactivation
185 /// \param theIO an interactive object
186 /// \param theMode a mode to deactivate. When theMode=-1 then all modes will be deactivated
187 void deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode = -1) const;
189 /// Searches the interactive object by feature
190 /// \param theObject the object or presentable feature
191 /// \return theIO an interactive object
192 AISObjectPtr getAISObject(ObjectPtr theObject) const;
194 /// Searches the feature by interactive object
195 /// \param theIO an interactive object
196 /// \return feature the feature or NULL if it not visualized
197 ObjectPtr getObject(const AISObjectPtr& theIO) const;
199 /// Searches the feature by interactive object
200 /// \param theIO an interactive object
201 /// \return corresponded object or NULL if it not found
202 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
204 /// Deactivates the given objects (not allow selection)
205 /// \param theObjList - list of objects which has to be deactivated.
206 /// \param theUpdateViewer update viewer flag
207 void deactivateObjects(const QObjectPtrList& theObjList,
208 const bool theUpdateViewer = true);
210 /// Returns the modes of activation
211 /// \param theObject the feature or NULL if it not visualized
212 /// \param theModes - modes on which it is activated (can be empty)
213 void getModesOfActivation(ObjectPtr theObject, QIntList& theModes);
215 /// Returns true if the given object can be selected
216 /// \param theObject object to check
217 bool isActive(ObjectPtr theObject) const;
219 /// Activates in local context displayed outside of the context.
220 /// \param theModes - modes on which it has to be activated (can be empty)
221 /// \param theObjList - list of objects which has to be activated.
222 /// \param theUpdateViewer an update viewer flag
223 void activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
224 const bool theUpdateViewer = true);
226 /// Sets display mode for the given object if this object is displayed
227 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
229 /// Returns current display mode for the given object.
230 /// If object is not dis played then returns NoMode.
231 /// \param theObject object to check
232 DisplayMode displayMode(ObjectPtr theObject) const;
234 /// Displays only objects listed in the list
235 /// \param theList list of objects
236 void showOnly(const QObjectPtrList& theList);
238 /// Returns number of displayed objects
239 int objectsCount() const { return myResult2AISObjectMap.size(); }
241 /// Returns list of displayed objects
242 QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.keys(); }
244 /// Returns list of displayed objects
245 QList<AISObjectPtr> displayedPresentations() const { return myResult2AISObjectMap.values(); }
247 /// Returns true if the given object can be shown in shaded mode
248 /// \param theObject object to check
249 bool canBeShaded(ObjectPtr theObject) const;
251 /// Set color on presentation of an object if it is displayed
252 /// \param theObject an object
253 /// \param theColor a color which has to be set
254 /// \param theUpdateViewer update viewer flag
255 /// \return previously defined color on the object
256 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
258 /// Returns Trihedron object if it is displayed
259 Handle(AIS_InteractiveObject) getTrihedron() const;
261 /// Set trihedron active (used in selection) or non active
262 void activateTrihedron(bool theIsActive);
264 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
265 /// depending on the trihedron visible state and displayer active trihedron state
266 void displayTrihedron(bool theToDisplay) const;
268 /// Returns true if the trihedron should be activated in current selection modes
269 bool isTrihedronActive() const { return myIsTrihedronActive; }
271 /// Returns list of currently active selection modes
272 /// Selection modes will be returned according to TopAbs_ShapeEnum
273 QIntList activeSelectionModes() const;
276 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
277 { myVCallBack = theCallBack; }
278 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
280 /// Converts shape type (TopAbs_ShapeEnum) to selection mode
281 /// \param theShapeType a shape type from TopAbs_ShapeEnum
282 static int getSelectionMode(int theShapeType);
284 /// Return true if the object is visible. If the object is feature, it returns true
285 /// if all results of the feature are shown
286 /// \param theDisplayer a displayer
287 /// \param theObject an object
288 /// \return a boolean value
289 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
292 /// Signal on object display
293 /// \param theObject a data object
294 /// \param theAIS a presentation object
295 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
297 /// Signal on before object erase
298 /// \param theObject a data object
299 /// \param theAIS a presentation object
300 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
303 /// Returns currently installed AIS_InteractiveContext
304 Handle(AIS_InteractiveContext) AISContext() const;
306 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
307 /// The context should have only this filter inside. Other filters should be add to the filter
308 Handle(SelectMgr_AndFilter) GetFilter();
310 /// Display the feature and a shape. This shape would be associated to the given feature
311 /// \param theObject an object instance
312 /// \param theAIS AIS presentation
313 /// \param isShading flag to show in shading mode
314 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
315 /// \return true if the object visibility state is changed
316 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
317 bool theUpdateViewer = true);
320 /// Activates the interactive object in the local context.
321 /// \param theIO an interactive object
322 /// \param theModes - modes on which it has to be activated (can be empty)
323 /// \return a flag is object activated or not
324 bool activate(const Handle(AIS_InteractiveObject)& theIO, const QIntList& theModes,
325 const bool theUpdateViewer) const;
327 /// Deactivates the given object (not allow selection)
328 /// \param theObject object to deactivate
329 void deactivate(ObjectPtr theObject, const bool theUpdateViewer);
331 /// Find a trihedron in a list of displayed presentations and deactivate it.
332 /// \param theUpdateViewer an update viewer flag
333 void deactivateTrihedron(const bool theUpdateViewer) const;
335 /// Update the object presentable properties such as color, lines width and other
336 /// If the object is result with the color attribute value set, it is used,
337 /// otherwise the customize is applyed to the object's feature if it is a custom prs
338 /// \param theObject an object instance
339 /// \return the true state if there is changes and the presentation is customized
340 bool customizeObject(ObjectPtr theObject);
342 /// Append the objects in the internal map. Checks whether the map already contains the object
343 /// \param theObject an object to display
344 /// \param theAIS AIOS object to display
345 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
348 /// Returns an information about alredy displayed objects
349 /// \return a string representation
350 std::string getResult2AISObjectMapInfo() const;
353 /// Returns container of visible presentations for the object. For a feature object,
354 /// the feature results are processed also. The presentations map is not cleared inside.
355 /// \param theObject a feature or result
356 /// \param thePresentations result map of presentations
357 void getPresentations(const ObjectPtr& theObject,
358 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
360 /// Sets the shapes selected in the context. It contains logic of the similar method
361 /// in OCCT but improved for performance. The modification is to iterates by a list
362 /// of owners in the context only once.
363 /// \param theContext a viewer context. It has opened local context
364 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
365 /// owner is selected if it is found there.
366 /// Only first owner is processed(according to OCCT logic)
367 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
368 const NCollection_DataMap<TopoDS_Shape,
369 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
372 /// Reference to workshop
373 XGUI_Workshop* myWorkshop;
375 Handle(VInspectorAPI_CallBack) myVCallBack;
377 /// A container for selection filters
378 Handle(SelectMgr_AndFilter) myAndFilter;
380 /// A default custom presentation, which is used if the displayed feature is not
381 /// a custom presentation
382 GeomCustomPrsPtr myCustomPrs;
384 /// Definition of a type of map which defines correspondance between objects and presentations
385 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
387 /// A map of displayed objects
388 ResultToAISMap myResult2AISObjectMap;
390 /// Selection modes installed for external objects in local context
391 QIntList myActiveSelectionModes;
393 /// Number of blocking of the viewer update. The viewer is updated only if it equals zero
394 int myViewerBlockedRecursiveCount;
396 /// Flag: first asking of AIS context: trihedron activation
397 bool myIsFirstAISContextUse;
399 /// Flag: use trihedgon for selection or not
400 bool myIsTrihedronActive;
402 /// A flag that update was requested but not done
403 mutable bool myNeedUpdate;