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 <GeomAPI_ICustomPrs.h>
28 #include <GeomAPI_Pln.h>
30 #include <ModelAPI_Result.h>
32 #include <ModuleBase_Definitions.h>
34 #include <AIS_InteractiveObject.hxx>
35 #include <AIS_InteractiveContext.hxx>
36 #include <NCollection_Map.hxx>
37 #include <NCollection_DataMap.hxx>
38 #include <SelectMgr_AndFilter.hxx>
39 #include <TopoDS_Shape.hxx>
46 class ModuleBase_ViewerPrs;
47 class ModelAPI_Feature;
48 class XGUI_SelectionActivate;
52 class VInspectorAPI_CallBack;
55 /**\class XGUI_Displayer
57 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
59 class XGUI_EXPORT XGUI_Displayer: public QObject
63 /// \enum DisplayMode display mode
65 NoMode = -1, ///< Mode is not defined
66 Wireframe, ///< Wireframe display mode
67 Shading ///< Shading display mode
71 /// \param theWorkshop a workshop instance
72 XGUI_Displayer(XGUI_Workshop* theWorkshop);
75 virtual ~XGUI_Displayer();
77 /// Returns the feature visibility state.
78 /// \param theObject an object instance
79 bool isVisible(ObjectPtr theObject) const;
81 /// Display the feature. Obtain the visualized object from the feature.
82 /// \param theObject an object to display
83 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
84 /// \return true if the object visibility state is changed
85 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
87 /// Display the given AIS object.
88 /// This object is not added to the displayer internal map of objects
89 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
90 /// order to perform the display with correct flags.
91 /// \param theAIS AIOS object to display
92 /// \param toActivateInSelectionModes boolean value whether the presentation should be
93 /// activated in the current selection modes
94 /// \param theDisplayMode mode how the presentation should be displayed
95 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
96 /// \return true if the object visibility state is changed
97 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
98 const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
100 /// Redisplay the shape if it was displayed
101 /// \param theObject an object instance
102 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
103 /// \return true if the object visibility state is changed
104 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
106 /// Sends and flushes a signal to redisplay all visualized objects.
107 void redisplayObjects();
109 /// Add presentations to current selection. It unhighlight and deselect the current selection.
110 /// The shape and result components are processed in the values. If the presentation shape is not
111 /// empty, select it, otherwise select the result.
112 /// \param theValues a list of presentation to be selected
113 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
114 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
115 bool theUpdateViewer = true);
117 /// Unselect all objects
118 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
119 /// \param theUpdateViewer the parameter to update viewer
120 void clearSelected(const bool theUpdateViewer = true);
122 /// Erase the feature and a shape.
123 /// \param theObject an object instance
124 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
125 /// \return true if the object visibility state is changed
126 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
128 /// Erase the given AIS object displayed by corresponded display method
129 /// \param theAIS instance of AIS object
130 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
131 /// \return true if the object visibility state is changed
132 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
134 /// Erase all presentations
135 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
136 /// \return true if the object visibility state is changed
137 bool eraseAll(const bool theUpdateViewer = true);
139 /// Remove default selection filters of the module from the current viewer
140 /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
141 void deactivateSelectionFilters(const bool theAddFilterOnly = true);
143 /// \brief Add selection filter
144 /// \param theFilter a filter instance
145 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
147 /// \brief Remove selection filter
148 /// \param theFilter a filter instance
149 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
151 /// Returns true if the selection filter is set to the viewer
152 /// \param theFilter a selection filter
153 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
155 /// Remove all selection filters
156 void removeFilters();
158 /// Sets a flag to the displayer whether the internal viewer can be updated by
159 /// the updateViewer method call. If it is not enabled, this method do nothing.
160 /// This state maintain recurse, if the update is blocked twice or three times, the
161 /// viewer will not be updated until it is unblocked necessary times
162 /// (twice or three in the example).
163 /// \param isEnabled a boolean value
164 bool enableUpdateViewer(const bool isEnabled);
166 /// Returns true if the viewer update is not blocked
167 bool isUpdateEnabled() const;
169 /// Updates the viewer
170 void updateViewer() const;
172 /// Searches the interactive object by feature
173 /// \param theObject the object or presentable feature
174 /// \return theIO an interactive object
175 AISObjectPtr getAISObject(ObjectPtr theObject) const;
177 /// Searches the feature by interactive object
178 /// \param theIO an interactive object
179 /// \return feature the feature or NULL if it not visualized
180 ObjectPtr getObject(const AISObjectPtr& theIO) const;
182 /// Searches the feature by interactive object
183 /// \param theIO an interactive object
184 /// \return corresponded object or NULL if it not found
185 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
187 /// Deactivates the given objects (not allow selection)
188 /// \param theObjList - list of objects which has to be deactivated.
189 /// \param theUpdateViewer update viewer flag
190 void deactivateObjects(const QObjectPtrList& theObjList,
191 const bool theUpdateViewer = true);
193 /// Sets display mode for the given object if this object is displayed
194 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
196 /// Returns current display mode for the given object.
197 /// If object is not dis played then returns NoMode.
198 /// \param theObject object to check
199 DisplayMode displayMode(ObjectPtr theObject) const;
201 /// Displays only objects listed in the list
202 /// \param theList list of objects
203 void showOnly(const QObjectPtrList& theList);
205 /// Returns number of displayed objects
206 int objectsCount() const { return myResult2AISObjectMap.size(); }
208 /// Returns list of displayed objects
209 QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.keys(); }
211 /// Returns list of displayed objects
212 QList<AISObjectPtr> displayedPresentations() const { return myResult2AISObjectMap.values(); }
214 /// Returns true if the given object can be shown in shaded mode
215 /// \param theObject object to check
216 bool canBeShaded(ObjectPtr theObject) const;
218 /// Set color on presentation of an object if it is displayed
219 /// \param theObject an object
220 /// \param theColor a color which has to be set
221 /// \param theUpdateViewer update viewer flag
222 /// \return previously defined color on the object
223 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
225 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
226 /// depending on the trihedron visible state and displayer active trihedron state
227 void displayTrihedron(bool theToDisplay) const;
230 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
231 { myVCallBack = theCallBack; }
232 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
235 /// Return true if the object is visible. If the object is feature, it returns true
236 /// if all results of the feature are shown
237 /// \param theDisplayer a displayer
238 /// \param theObject an object
239 /// \return a boolean value
240 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
243 /// Returns screen plane of active view
244 GeomPlanePtr getScreenPlane() const;
246 /// Returns scale of active view
247 double getViewScale() const;
250 /// Signal on object display
251 /// \param theObject a data object
252 /// \param theAIS a presentation object
253 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
255 /// Signal on before object erase
256 /// \param theObject a data object
257 /// \param theAIS a presentation object
258 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
261 /// Returns currently installed AIS_InteractiveContext
262 Handle(AIS_InteractiveContext) AISContext() const;
264 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
265 /// The context should have only this filter inside. Other filters should be add to the filter
266 Handle(SelectMgr_AndFilter) GetFilter();
268 /// Display the feature and a shape. This shape would be associated to the given feature
269 /// \param theObject an object instance
270 /// \param theAIS AIS presentation
271 /// \param isShading flag to show in shading mode
272 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
273 /// \return true if the object visibility state is changed
274 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
275 bool theUpdateViewer = true);
278 /// Update the object presentable properties such as color, lines width and other
279 /// If the object is result with the color attribute value set, it is used,
280 /// otherwise the customize is applyed to the object's feature if it is a custom prs
281 /// \param theObject an object instance
282 /// \return the true state if there is changes and the presentation is customized
283 bool customizeObject(ObjectPtr theObject);
285 /// Append the objects in the internal map. Checks whether the map already contains the object
286 /// \param theObject an object to display
287 /// \param theAIS AIOS object to display
288 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
291 /// Returns an information about alredy displayed objects
292 /// \return a string representation
293 std::string getResult2AISObjectMapInfo() const;
296 /// Returns container of visible presentations for the object. For a feature object,
297 /// the feature results are processed also. The presentations map is not cleared inside.
298 /// \param theObject a feature or result
299 /// \param thePresentations result map of presentations
300 void getPresentations(const ObjectPtr& theObject,
301 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
303 /// Sets the shapes selected in the context. It contains logic of the similar method
304 /// in OCCT but improved for performance. The modification is to iterates by a list
305 /// of owners in the context only once.
306 /// \param theContext a viewer context. It has opened local context
307 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
308 /// owner is selected if it is found there.
309 /// Only first owner is processed(according to OCCT logic)
310 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
311 const NCollection_DataMap<TopoDS_Shape,
312 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
315 XGUI_SelectionActivate* selectionActivate() const;
318 XGUI_Workshop* myWorkshop; ///< Reference to workshop
320 Handle(VInspectorAPI_CallBack) myVCallBack;
322 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
324 /// A default custom presentation, which is used if the displayed feature is not
325 /// a custom presentation
326 GeomCustomPrsPtr myCustomPrs;
328 /// Definition of a type of map which defines correspondance between objects and presentations
329 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
330 ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
332 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
333 int myViewerBlockedRecursiveCount;
335 bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
336 mutable bool myNeedUpdate; ///< A flag that update was requested but not done