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 /// Updates the viewer
283 void updateViewer() const;
285 /// Searches the interactive object by feature
286 /// \param theObject the object or presentable feature
287 /// \return theIO an interactive object
288 AISObjectPtr getAISObject(ObjectPtr theObject) const;
290 /// Searches the feature by interactive object
291 /// \param theIO an interactive object
292 /// \return feature the feature or NULL if it not visualized
293 ObjectPtr getObject(const AISObjectPtr& theIO) const;
295 /// Searches the feature by interactive object
296 /// \param theIO an interactive object
297 /// \return corresponded object or NULL if it not found
298 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
300 /// Deactivates the given objects (not allow selection)
301 /// \param theObjList - list of objects which has to be deactivated.
302 /// \param theUpdateViewer update viewer flag
303 void deactivateObjects(const QObjectPtrList& theObjList,
304 const bool theUpdateViewer = true);
306 /// Sets display mode for the given object if this object is displayed
307 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
309 /// Returns current display mode for the given object.
310 /// If object is not dis played then returns NoMode.
311 /// \param theObject object to check
312 DisplayMode displayMode(ObjectPtr theObject) const;
314 /// Displays only objects listed in the list
315 /// \param theList list of objects
316 void showOnly(const QObjectPtrList& theList);
318 /// Returns number of displayed objects
319 int objectsCount() const { return myResult2AISObjectMap.size(); }
321 /// Returns list of displayed objects
322 QObjectPtrList displayedObjects() const {
324 return myResult2AISObjectMap.objects();
326 return myResult2AISObjectMap.keys();
330 /// Returns list of displayed objects
332 QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
334 return myResult2AISObjectMap.presentations();
337 QList<AISObjectPtr> displayedPresentations() const
339 return myResult2AISObjectMap.values();
343 /// Returns true if the given object can be shown in shaded mode
344 /// \param theObject object to check
345 bool canBeShaded(ObjectPtr theObject) const;
347 /// Set color on presentation of an object if it is displayed
348 /// \param theObject an object
349 /// \param theColor a color which has to be set
350 /// \param theUpdateViewer update viewer flag
351 /// \return previously defined color on the object
352 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
354 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
355 /// depending on the trihedron visible state and displayer active trihedron state
356 void displayTrihedron(bool theToDisplay) const;
359 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
360 { myVCallBack = theCallBack; }
361 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
364 /// Return true if the object is visible. If the object is feature, it returns true
365 /// if all results of the feature are shown
366 /// \param theDisplayer a displayer
367 /// \param theObject an object
368 /// \return a boolean value
369 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
372 /// Returns screen plane of active view
373 GeomPlanePtr getScreenPlane() const;
375 /// Returns scale of active view
376 double getViewScale() const;
379 /// Signal on object display
380 /// \param theObject a data object
381 /// \param theAIS a presentation object
382 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
384 /// Signal on before object erase
385 /// \param theObject a data object
386 /// \param theAIS a presentation object
387 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
390 /// Returns currently installed AIS_InteractiveContext
391 Handle(AIS_InteractiveContext) AISContext() const;
393 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
394 /// The context should have only this filter inside. Other filters should be add to the filter
395 Handle(SelectMgr_AndFilter) GetFilter();
397 /// Display the feature and a shape. This shape would be associated to the given feature
398 /// \param theObject an object instance
399 /// \param theAIS AIS presentation
400 /// \param isShading flag to show in shading mode
401 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
402 /// \return true if the object visibility state is changed
403 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
404 bool theUpdateViewer = true);
407 /// Update the object presentable properties such as color, lines width and other
408 /// If the object is result with the color attribute value set, it is used,
409 /// otherwise the customize is applyed to the object's feature if it is a custom prs
410 /// \param theObject an object instance
411 /// \return the true state if there is changes and the presentation is customized
412 bool customizeObject(ObjectPtr theObject);
414 /// Append the objects in the internal map. Checks whether the map already contains the object
415 /// \param theObject an object to display
416 /// \param theAIS AIOS object to display
417 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
420 /// Returns an information about alredy displayed objects
421 /// \return a string representation
422 std::string getResult2AISObjectMapInfo() const;
425 /// Returns container of visible presentations for the object. For a feature object,
426 /// the feature results are processed also. The presentations map is not cleared inside.
427 /// \param theObject a feature or result
428 /// \param thePresentations result map of presentations
429 void getPresentations(const ObjectPtr& theObject,
430 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
432 /// Sets the shapes selected in the context. It contains logic of the similar method
433 /// in OCCT but improved for performance. The modification is to iterates by a list
434 /// of owners in the context only once.
435 /// \param theContext a viewer context. It has opened local context
436 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
437 /// owner is selected if it is found there.
438 /// Only first owner is processed(according to OCCT logic)
439 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
440 const NCollection_DataMap<TopoDS_Shape,
441 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
444 XGUI_SelectionActivate* selectionActivate() const;
447 XGUI_Workshop* myWorkshop; ///< Reference to workshop
449 Handle(VInspectorAPI_CallBack) myVCallBack;
451 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
453 /// A default custom presentation, which is used if the displayed feature is not
454 /// a custom presentation
455 GeomCustomPrsPtr myCustomPrs;
457 /// Definition of a type of map which defines correspondance between objects and presentations
459 XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
461 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
462 ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
465 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
466 int myViewerBlockedRecursiveCount;
468 bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
469 mutable bool myNeedUpdate; ///< A flag that update was requested but not done