1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #ifndef XGUI_Displayer_H
21 #define XGUI_Displayer_H
25 #include <GeomAPI_AISObject.h>
26 #include <GeomAPI_ICustomPrs.h>
27 #include <GeomAPI_Pln.h>
29 #include <ModelAPI_Result.h>
31 #include <ModuleBase_Definitions.h>
33 #include <AIS_InteractiveObject.hxx>
34 #include <AIS_InteractiveContext.hxx>
35 #include <NCollection_Map.hxx>
36 #include <NCollection_DataMap.hxx>
37 #include <SelectMgr_AndFilter.hxx>
38 #include <TopoDS_Shape.hxx>
45 class ModuleBase_ViewerPrs;
46 class ModelAPI_Feature;
47 class XGUI_SelectionActivate;
53 class VInspectorAPI_CallBack;
58 class XGUI_TwoSidePresentationMap
61 ~XGUI_TwoSidePresentationMap() { clear(); }
63 /// Add new values pair to the map
64 /// \param theObj an object
65 /// \param theAIS a corresponded presentation
66 bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
68 if (myResultToAISMap.contains(theObj))
70 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
71 myResultToAISMap[theObj] = anAIS;
72 myAIStoResultMap[anAIS] = theObj;
76 /// Removes values by object
77 /// \param theObj an object
78 bool remove(const ObjectPtr& theObj)
80 if (!myResultToAISMap.contains(theObj))
82 Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
83 myResultToAISMap.remove(theObj);
84 myAIStoResultMap.remove(aAIS);
88 /// Removes values by presentation
89 /// \param theAIS a presentation
90 bool remove(const AISObjectPtr& theAIS)
92 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
93 if (!myAIStoResultMap.contains(anAIS))
95 ObjectPtr aObj = myAIStoResultMap[anAIS];
96 myResultToAISMap.remove(aObj);
97 myAIStoResultMap.remove(anAIS);
101 /// Removes all values
104 myResultToAISMap.clear();
105 myAIStoResultMap.clear();
108 /// Returns presentation by object
109 /// \param theObj an object
110 AISObjectPtr value(const ObjectPtr& theObj) const
112 if (myResultToAISMap.contains(theObj)) {
113 Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
114 AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
115 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
118 return AISObjectPtr();
121 /// Returns object by presentation
122 /// \param theAIS a presentation
123 ObjectPtr value(const AISObjectPtr& theAIS) const
125 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
126 if (myAIStoResultMap.contains(anAIS))
127 return myAIStoResultMap[anAIS];
131 /// Returns object by presentation
132 /// \param theAIS a presentation
133 ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
135 if (myAIStoResultMap.contains(theAIS))
136 return myAIStoResultMap[theAIS];
140 /// Returns number of values
141 int size() const { return myResultToAISMap.size(); }
143 /// Returns list of objects
144 QObjectPtrList objects() const { return myResultToAISMap.keys(); }
146 /// returns list of presentations
147 QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
149 /// Returns true if the Map contains the object
150 /// \param theObj an object
151 bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
153 /// Returns true if the Map contains the presentation
154 /// \param theAIS a presentation
155 bool contains(const AISObjectPtr& theAIS) const
157 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
158 return myAIStoResultMap.contains(anAIS);
162 QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
163 QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
168 /**\class XGUI_Displayer
170 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
172 class XGUI_EXPORT XGUI_Displayer : public QObject
176 /// \enum DisplayMode display mode
178 NoMode = -1, ///< Mode is not defined
179 Wireframe, ///< Wireframe display mode
180 Shading ///< Shading display mode
184 /// \param theWorkshop a workshop instance
185 XGUI_Displayer(XGUI_Workshop* theWorkshop);
188 virtual ~XGUI_Displayer();
190 /// Returns the feature visibility state.
191 /// \param theObject an object instance
192 bool isVisible(ObjectPtr theObject) const;
194 /// Display the feature. Obtain the visualized object from the feature.
195 /// \param theObject an object to display
196 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
197 /// \return true if the object visibility state is changed
198 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
200 /// Display the given AIS object.
201 /// This object is not added to the displayer internal map of objects
202 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
203 /// order to perform the display with correct flags.
204 /// \param theAIS AIOS object to display
205 /// \param toActivateInSelectionModes boolean value whether the presentation should be
206 /// activated in the current selection modes
207 /// \param theDisplayMode mode how the presentation should be displayed
208 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
209 /// \return true if the object visibility state is changed
210 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
211 const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
213 /// Redisplay the shape if it was displayed
214 /// \param theObject an object instance
215 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
216 /// \return true if the object visibility state is changed
217 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
219 /// Sends and flushes a signal to redisplay all visualized objects.
220 void redisplayObjects();
222 /// Add presentations to current selection. It unhighlight and deselect the current selection.
223 /// The shape and result components are processed in the values. If the presentation shape is not
224 /// empty, select it, otherwise select the result.
225 /// \param theValues a list of presentation to be selected
226 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
227 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
228 bool theUpdateViewer = true);
230 /// Unselect all objects
231 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
232 /// \param theUpdateViewer the parameter to update viewer
233 void clearSelected(const bool theUpdateViewer = true);
235 /// Erase the feature and a shape.
236 /// \param theObject an object instance
237 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
238 /// \return true if the object visibility state is changed
239 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
241 /// Erase the given AIS object displayed by corresponded display method
242 /// \param theAIS instance of AIS object
243 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
244 /// \return true if the object visibility state is changed
245 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
247 /// Erase all presentations
248 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
249 /// \return true if the object visibility state is changed
250 bool eraseAll(const bool theUpdateViewer = true);
252 /// Remove default selection filters of the module from the current viewer
253 /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
254 void deactivateSelectionFilters(const bool theAddFilterOnly = true);
256 /// \brief Add selection filter
257 /// \param theFilter a filter instance
258 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
260 /// \brief Remove selection filter
261 /// \param theFilter a filter instance
262 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
264 /// Returns true if the selection filter is set to the viewer
265 /// \param theFilter a selection filter
266 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
268 /// Remove all selection filters
269 void removeFilters();
271 /// Sets a flag to the displayer whether the internal viewer can be updated by
272 /// the updateViewer method call. If it is not enabled, this method do nothing.
273 /// This state maintain recurse, if the update is blocked twice or three times, the
274 /// viewer will not be updated until it is unblocked necessary times
275 /// (twice or three in the example).
276 /// \param isEnabled a boolean value
277 bool enableUpdateViewer(const bool isEnabled);
279 /// Returns true if the viewer update is not blocked
280 bool isUpdateEnabled() const
282 return myViewerBlockedRecursiveCount == 0;
285 /// Updates the viewer
286 void updateViewer() const;
288 /// Searches the interactive object by feature
289 /// \param theObject the object or presentable feature
290 /// \return theIO an interactive object
291 AISObjectPtr getAISObject(ObjectPtr theObject) const;
293 /// Searches the feature by interactive object
294 /// \param theIO an interactive object
295 /// \return feature the feature or NULL if it not visualized
296 ObjectPtr getObject(const AISObjectPtr& theIO) const;
298 /// Searches the feature by interactive object
299 /// \param theIO an interactive object
300 /// \return corresponded object or NULL if it not found
301 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
303 /// Deactivates the given objects (not allow selection)
304 /// \param theObjList - list of objects which has to be deactivated.
305 /// \param theUpdateViewer update viewer flag
306 void deactivateObjects(const QObjectPtrList& theObjList,
307 const bool theUpdateViewer = true);
309 /// Sets display mode for the given object if this object is displayed
310 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
312 /// Returns current display mode for the given object.
313 /// If object is not dis played then returns NoMode.
314 /// \param theObject object to check
315 DisplayMode displayMode(ObjectPtr theObject) const;
317 /// Displays only objects listed in the list
318 /// \param theList list of objects
319 void showOnly(const QObjectPtrList& theList);
321 /// Returns number of displayed objects
322 int objectsCount() const { return myResult2AISObjectMap.size(); }
324 /// Returns list of displayed objects
325 QObjectPtrList displayedObjects() const {
327 return myResult2AISObjectMap.objects();
329 return myResult2AISObjectMap.keys();
333 /// Returns list of displayed objects
335 QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
337 return myResult2AISObjectMap.presentations();
340 QList<AISObjectPtr> displayedPresentations() const
342 return myResult2AISObjectMap.values();
346 /// Returns true if the given object can be shown in shaded mode
347 /// \param theObject object to check
348 bool canBeShaded(ObjectPtr theObject) const;
350 /// Set color on presentation of an object if it is displayed
351 /// \param theObject an object
352 /// \param theColor a color which has to be set
353 /// \param theUpdateViewer update viewer flag
354 /// \return previously defined color on the object
355 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
357 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
358 /// depending on the trihedron visible state and displayer active trihedron state
359 void displayTrihedron(bool theToDisplay) const;
362 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
363 { myVCallBack = theCallBack; }
364 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
367 /// Return true if the object is visible. If the object is feature, it returns true
368 /// if all results of the feature are shown
369 /// \param theDisplayer a displayer
370 /// \param theObject an object
371 /// \return a boolean value
372 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
375 /// Returns screen plane of active view
376 GeomPlanePtr getScreenPlane() const;
378 /// Returns scale of active view
379 double getViewScale() const;
381 /// Set color of selection
382 /// \param theColor R,G,B values of color
383 void setSelectionColor(const std::vector<int>& theColor);
385 /// Returns current selection color
386 std::vector<int> selectionColor() const;
389 /// Signal on object display
390 /// \param theObject a data object
391 /// \param theAIS a presentation object
392 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
394 /// Signal on before object erase
395 /// \param theObject a data object
396 /// \param theAIS a presentation object
397 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
400 /// Returns currently installed AIS_InteractiveContext
401 Handle(AIS_InteractiveContext) AISContext() const;
403 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
404 /// The context should have only this filter inside. Other filters should be add to the filter
405 Handle(SelectMgr_AndFilter) GetFilter();
407 /// Display the feature and a shape. This shape would be associated to the given feature
408 /// \param theObject an object instance
409 /// \param theAIS AIS presentation
410 /// \param isShading flag to show in shading mode
411 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
412 /// \return true if the object visibility state is changed
413 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
414 bool theUpdateViewer = true);
417 /// Update the object presentable properties such as color, lines width and other
418 /// If the object is result with the color attribute value set, it is used,
419 /// otherwise the customize is applyed to the object's feature if it is a custom prs
420 /// \param theObject an object instance
421 /// \return the true state if there is changes and the presentation is customized
422 bool customizeObject(ObjectPtr theObject);
424 /// Append the objects in the internal map. Checks whether the map already contains the object
425 /// \param theObject an object to display
426 /// \param theAIS AIOS object to display
427 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
430 /// Returns an information about alredy displayed objects
431 /// \return a string representation
432 std::string getResult2AISObjectMapInfo() const;
435 /// Returns container of visible presentations for the object. For a feature object,
436 /// the feature results are processed also. The presentations map is not cleared inside.
437 /// \param theObject a feature or result
438 /// \param thePresentations result map of presentations
439 void getPresentations(const ObjectPtr& theObject,
440 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
442 /// Sets the shapes selected in the context. It contains logic of the similar method
443 /// in OCCT but improved for performance. The modification is to iterates by a list
444 /// of owners in the context only once.
445 /// \param theContext a viewer context. It has opened local context
446 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
447 /// owner is selected if it is found there.
448 /// Only first owner is processed(according to OCCT logic)
449 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
450 const NCollection_DataMap<TopoDS_Shape,
451 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
454 XGUI_SelectionActivate* selectionActivate() const;
457 XGUI_Workshop* myWorkshop; ///< Reference to workshop
459 Handle(VInspectorAPI_CallBack) myVCallBack;
461 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
463 /// A default custom presentation, which is used if the displayed feature is not
464 /// a custom presentation
465 GeomCustomPrsPtr myCustomPrs;
467 /// Definition of a type of map which defines correspondance between objects and presentations
469 XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
471 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
472 ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
475 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
476 int myViewerBlockedRecursiveCount;
478 mutable void* myContextId;
479 mutable bool myNeedUpdate; ///< A flag that update was requested but not done