1 // Copyright (C) 2014-2024 CEA, EDF
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 email : webmaster.salome@opencascade.com
20 #ifndef XGUI_Displayer_H
21 #define XGUI_Displayer_H
25 #include <GeomAPI_AISObject.h>
26 #include <GeomAPI_Pln.h>
28 #include <ModelAPI_Result.h>
30 #include <ModuleBase_Definitions.h>
32 #include <AIS_InteractiveObject.hxx>
33 #include <AIS_InteractiveContext.hxx>
34 #include <NCollection_Map.hxx>
35 #include <NCollection_DataMap.hxx>
36 #include <SelectMgr_AndFilter.hxx>
37 #include <TopoDS_Shape.hxx>
44 class ModuleBase_ViewerPrs;
45 class ModelAPI_Feature;
46 class XGUI_SelectionActivate;
52 class VInspectorAPI_CallBack;
57 class XGUI_TwoSidePresentationMap
60 ~XGUI_TwoSidePresentationMap() { clear(); }
62 /// Add new values pair to the map
63 /// \param theObj an object
64 /// \param theAIS a corresponded presentation
65 bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
67 if (myResultToAISMap.contains(theObj))
69 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
70 myResultToAISMap[theObj] = anAIS;
71 myAIStoResultMap[anAIS] = theObj;
75 /// Removes values by object
76 /// \param theObj an object
77 bool remove(const ObjectPtr& theObj)
79 if (!myResultToAISMap.contains(theObj))
81 Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
82 myResultToAISMap.remove(theObj);
83 myAIStoResultMap.remove(aAIS);
87 /// Removes values by presentation
88 /// \param theAIS a presentation
89 bool remove(const AISObjectPtr& theAIS)
91 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
92 if (!myAIStoResultMap.contains(anAIS))
94 ObjectPtr aObj = myAIStoResultMap[anAIS];
95 myResultToAISMap.remove(aObj);
96 myAIStoResultMap.remove(anAIS);
100 /// Removes all values
103 myResultToAISMap.clear();
104 myAIStoResultMap.clear();
107 /// Returns presentation by object
108 /// \param theObj an object
109 AISObjectPtr value(const ObjectPtr& theObj) const
111 if (myResultToAISMap.contains(theObj)) {
112 Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
113 AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
114 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
117 return AISObjectPtr();
120 /// Returns object by presentation
121 /// \param theAIS a presentation
122 ObjectPtr value(const AISObjectPtr& theAIS) const
124 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
125 if (myAIStoResultMap.contains(anAIS))
126 return myAIStoResultMap[anAIS];
130 /// Returns object by presentation
131 /// \param theAIS a presentation
132 ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
134 if (myAIStoResultMap.contains(theAIS))
135 return myAIStoResultMap[theAIS];
139 /// Returns number of values
140 int size() const { return myResultToAISMap.size(); }
142 /// Returns list of objects
143 QObjectPtrList objects() const { return myResultToAISMap.keys(); }
145 /// returns list of presentations
146 QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
148 /// Returns true if the Map contains the object
149 /// \param theObj an object
150 bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
152 /// Returns true if the Map contains the presentation
153 /// \param theAIS a presentation
154 bool contains(const AISObjectPtr& theAIS) const
156 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
157 return myAIStoResultMap.contains(anAIS);
161 QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
162 QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
167 /**\class XGUI_Displayer
169 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
171 class XGUI_EXPORT XGUI_Displayer : public QObject
175 /// \enum DisplayMode display mode
177 NoMode = -1, ///< Mode is not defined
178 Wireframe, ///< Wireframe display mode
179 Shading ///< Shading display mode
183 /// \param theWorkshop a workshop instance
184 XGUI_Displayer(XGUI_Workshop* theWorkshop);
187 virtual ~XGUI_Displayer();
189 /// Returns the feature visibility state.
190 /// \param theObject an object instance
191 bool isVisible(ObjectPtr theObject) const;
193 /// Display the feature. Obtain the visualized object from the feature.
194 /// \param theObject an object to display
195 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
196 /// \return true if the object visibility state is changed
197 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
199 /// Display the given AIS object.
200 /// This object is not added to the displayer internal map of objects
201 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
202 /// order to perform the display with correct flags.
203 /// \param theAIS AIOS object to display
204 /// \param toActivateInSelectionModes boolean value whether the presentation should be
205 /// activated in the current selection modes
206 /// \param theDisplayMode mode how the presentation should be displayed
207 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
208 /// \return true if the object visibility state is changed
209 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
210 const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
212 /// Redisplay the shape if it was displayed
213 /// \param theObject an object instance
214 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
215 /// \return true if the object visibility state is changed
216 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
218 /// Sends and flushes a signal to redisplay all visualized objects.
219 void redisplayObjects();
221 /// Add presentations to current selection. It unhighlight and deselect the current selection.
222 /// The shape and result components are processed in the values. If the presentation shape is not
223 /// empty, select it, otherwise select the result.
224 /// \param theValues a list of presentation to be selected
225 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
226 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
227 bool theUpdateViewer = true);
229 /// Unselect all objects
230 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
231 /// \param theUpdateViewer the parameter to update viewer
232 void clearSelected(const bool theUpdateViewer = true);
234 /// Erase the feature and a shape.
235 /// \param theObject an object instance
236 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
237 /// \return true if the object visibility state is changed
238 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
240 /// Erase the given AIS object displayed by corresponded display method
241 /// \param theAIS instance of AIS object
242 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
243 /// \return true if the object visibility state is changed
244 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
246 /// Erase all presentations
247 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
248 /// \return true if the object visibility state is changed
249 bool eraseAll(const bool theUpdateViewer = true);
251 /// Remove default selection filters of the module from the current viewer
252 /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
253 void deactivateSelectionFilters(const bool theAddFilterOnly = true);
255 /// \brief Add selection filter
256 /// \param theFilter a filter instance
257 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
259 /// \brief Remove selection filter
260 /// \param theFilter a filter instance
261 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
263 /// Returns true if the selection filter is set to the viewer
264 /// \param theFilter a selection filter
265 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
267 /// Remove all selection filters
268 void removeFilters();
270 /// Sets a flag to the displayer whether the internal viewer can be updated by
271 /// the updateViewer method call. If it is not enabled, this method do nothing.
272 /// This state maintain recurse, if the update is blocked twice or three times, the
273 /// viewer will not be updated until it is unblocked necessary times
274 /// (twice or three in the example).
275 /// \param isEnabled a boolean value
276 bool enableUpdateViewer(const bool isEnabled);
278 /// Returns true if the viewer update is not blocked
279 bool isUpdateEnabled() const
281 return myViewerBlockedRecursiveCount == 0;
284 /// Updates the viewer
285 void updateViewer() const;
287 /// Searches the interactive object by feature
288 /// \param theObject the object or presentable feature
289 /// \return theIO an interactive object
290 AISObjectPtr getAISObject(ObjectPtr theObject) const;
292 /// Searches the feature by interactive object
293 /// \param theIO an interactive object
294 /// \return feature the feature or NULL if it not visualized
295 ObjectPtr getObject(const AISObjectPtr& theIO) const;
297 /// Searches the feature by interactive object
298 /// \param theIO an interactive object
299 /// \return corresponded object or NULL if it not found
300 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
302 /// Deactivates the given objects (not allow selection)
303 /// \param theObjList - list of objects which has to be deactivated.
304 /// \param theUpdateViewer update viewer flag
305 void deactivateObjects(const QObjectPtrList& theObjList,
306 const bool theUpdateViewer = true);
308 /// Sets display mode for the given object if this object is displayed
309 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
311 /// Returns current display mode for the given object.
312 /// If object is not dis played then returns NoMode.
313 /// \param theObject object to check
314 DisplayMode displayMode(ObjectPtr theObject) const;
316 /// Displays only objects listed in the list
317 /// \param theList list of objects
318 void showOnly(const QObjectPtrList& theList);
320 /// Returns number of displayed objects
321 int objectsCount() const { return myResult2AISObjectMap.size(); }
323 /// Returns list of displayed objects
324 QObjectPtrList displayedObjects() const {
326 return myResult2AISObjectMap.objects();
328 return myResult2AISObjectMap.keys();
332 /// Returns list of displayed objects
334 QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
336 return myResult2AISObjectMap.presentations();
339 QList<AISObjectPtr> displayedPresentations() const
341 return myResult2AISObjectMap.values();
345 /// Returns true if the given object can be shown in shaded mode
346 /// \param theObject object to check
347 bool canBeShaded(ObjectPtr theObject) const;
349 /// Set color on presentation of an object if it is displayed
350 /// \param theObject an object
351 /// \param theColor a color which has to be set
352 /// \param theUpdateViewer update viewer flag
353 /// \return previously defined color on the object
354 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
356 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
357 /// depending on the trihedron visible state and displayer active trihedron state
358 void displayTrihedron(bool theToDisplay) const;
361 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
362 { myVCallBack = theCallBack; }
363 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
366 /// Return true if the object is visible. If the object is feature, it returns true
367 /// if all results of the feature are shown
368 /// \param theDisplayer a displayer
369 /// \param theObject an object
370 /// \return a boolean value
371 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
374 /// Returns screen plane of active view
375 GeomPlanePtr getScreenPlane() const;
377 /// Returns scale of active view
378 double getViewScale() const;
380 /// Set color of selection
381 /// \param theColor R,G,B values of color
382 void setSelectionColor(const std::vector<int>& theColor);
384 /// Returns current selection color
385 std::vector<int> selectionColor() const;
388 /// Signal on object display
389 /// \param theObject a data object
390 /// \param theAIS a presentation object
391 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
393 /// Signal on before object erase
394 /// \param theObject a data object
395 /// \param theAIS a presentation object
396 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
399 /// Returns currently installed AIS_InteractiveContext
400 Handle(AIS_InteractiveContext) AISContext() const;
402 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
403 /// The context should have only this filter inside. Other filters should be add to the filter
404 Handle(SelectMgr_AndFilter) GetFilter();
406 /// Display the feature and a shape. This shape would be associated to the given feature
407 /// \param theObject an object instance
408 /// \param theAIS AIS presentation
409 /// \param isShading flag to show in shading mode
410 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
411 /// \return true if the object visibility state is changed
412 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
413 bool theUpdateViewer = true);
416 /// Update the object presentable properties such as color, lines width and other
417 /// If the object is result with the color attribute value set, it is used,
418 /// otherwise the customize is applyed to the object's feature if it is a custom prs
419 /// \param theObject an object instance
420 /// \return the true state if there is changes and the presentation is customized
421 bool customizeObject(ObjectPtr theObject);
423 /// Append the objects in the internal map. Checks whether the map already contains the object
424 /// \param theObject an object to display
425 /// \param theAIS AIOS object to display
426 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
429 /// Returns an information about alredy displayed objects
430 /// \return a string representation
431 std::string getResult2AISObjectMapInfo() const;
434 /// Returns container of visible presentations for the object. For a feature object,
435 /// the feature results are processed also. The presentations map is not cleared inside.
436 /// \param theObject a feature or result
437 /// \param thePresentations result map of presentations
438 void getPresentations(const ObjectPtr& theObject,
439 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
441 /// Sets the shapes selected in the context. It contains logic of the similar method
442 /// in OCCT but improved for performance. The modification is to iterates by a list
443 /// of owners in the context only once.
444 /// \param theContext a viewer context. It has opened local context
445 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
446 /// owner is selected if it is found there.
447 /// Only first owner is processed(according to OCCT logic)
448 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
449 const NCollection_DataMap<TopoDS_Shape,
450 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
453 XGUI_SelectionActivate* selectionActivate() const;
456 XGUI_Workshop* myWorkshop; ///< Reference to workshop
458 Handle(VInspectorAPI_CallBack) myVCallBack;
460 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
462 /// Definition of a type of map which defines correspondance between objects and presentations
464 XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
466 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
467 ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
470 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
471 int myViewerBlockedRecursiveCount;
473 mutable void* myContextId;
474 mutable bool myNeedUpdate; ///< A flag that update was requested but not done