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;
54 class VInspectorAPI_CallBack;
59 class XGUI_TwoSidePresentationMap
62 ~XGUI_TwoSidePresentationMap() { clear(); }
64 /// Add new values pair to the map
65 /// \param theObj an object
66 /// \param theAIS a corresponded presentation
67 bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
69 if (myResultToAISMap.contains(theObj))
71 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
72 myResultToAISMap[theObj] = anAIS;
73 myAIStoResultMap[anAIS] = theObj;
77 /// Removes values by object
78 /// \param theObj an object
79 bool remove(const ObjectPtr& theObj)
81 if (!myResultToAISMap.contains(theObj))
83 Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
84 myResultToAISMap.remove(theObj);
85 myAIStoResultMap.remove(aAIS);
89 /// Removes values by presentation
90 /// \param theAIS a presentation
91 bool remove(const AISObjectPtr& theAIS)
93 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
94 if (!myAIStoResultMap.contains(anAIS))
96 ObjectPtr aObj = myAIStoResultMap[anAIS];
97 myResultToAISMap.remove(aObj);
98 myAIStoResultMap.remove(anAIS);
102 /// Removes all values
105 myResultToAISMap.clear();
106 myAIStoResultMap.clear();
109 /// Returns presentation by object
110 /// \param theObj an object
111 AISObjectPtr value(const ObjectPtr& theObj) const
113 if (myResultToAISMap.contains(theObj)) {
114 Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
115 AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
116 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
119 return AISObjectPtr();
122 /// Returns object by presentation
123 /// \param theAIS a presentation
124 ObjectPtr value(const AISObjectPtr& theAIS) const
126 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
127 if (myAIStoResultMap.contains(anAIS))
128 return myAIStoResultMap[anAIS];
132 /// Returns object by presentation
133 /// \param theAIS a presentation
134 ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
136 if (myAIStoResultMap.contains(theAIS))
137 return myAIStoResultMap[theAIS];
141 /// Returns number of values
142 int size() const { return myResultToAISMap.size(); }
144 /// Returns list of objects
145 QObjectPtrList objects() const { return myResultToAISMap.keys(); }
147 /// returns list of presentations
148 QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
150 /// Returns true if the Map contains the object
151 /// \param theObj an object
152 bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
154 /// Returns true if the Map contains the presentation
155 /// \param theAIS a presentation
156 bool contains(const AISObjectPtr& theAIS) const
158 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
159 return myAIStoResultMap.contains(anAIS);
163 QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
164 QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
169 /**\class XGUI_Displayer
171 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
173 class XGUI_EXPORT XGUI_Displayer : public QObject
177 /// \enum DisplayMode display mode
179 NoMode = -1, ///< Mode is not defined
180 Wireframe, ///< Wireframe display mode
181 Shading ///< Shading display mode
185 /// \param theWorkshop a workshop instance
186 XGUI_Displayer(XGUI_Workshop* theWorkshop);
189 virtual ~XGUI_Displayer();
191 /// Returns the feature visibility state.
192 /// \param theObject an object instance
193 bool isVisible(ObjectPtr theObject) const;
195 /// Display the feature. Obtain the visualized object from the feature.
196 /// \param theObject an object to display
197 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
198 /// \return true if the object visibility state is changed
199 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
201 /// Display the given AIS object.
202 /// This object is not added to the displayer internal map of objects
203 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
204 /// order to perform the display with correct flags.
205 /// \param theAIS AIOS object to display
206 /// \param toActivateInSelectionModes boolean value whether the presentation should be
207 /// activated in the current selection modes
208 /// \param theDisplayMode mode how the presentation should be displayed
209 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
210 /// \return true if the object visibility state is changed
211 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
212 const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
214 /// Redisplay the shape if it was displayed
215 /// \param theObject an object instance
216 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
217 /// \return true if the object visibility state is changed
218 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
220 /// Sends and flushes a signal to redisplay all visualized objects.
221 void redisplayObjects();
223 /// Add presentations to current selection. It unhighlight and deselect the current selection.
224 /// The shape and result components are processed in the values. If the presentation shape is not
225 /// empty, select it, otherwise select the result.
226 /// \param theValues a list of presentation to be selected
227 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
228 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
229 bool theUpdateViewer = true);
231 /// Unselect all objects
232 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
233 /// \param theUpdateViewer the parameter to update viewer
234 void clearSelected(const bool theUpdateViewer = true);
236 /// Erase the feature and a shape.
237 /// \param theObject an object instance
238 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
239 /// \return true if the object visibility state is changed
240 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
242 /// Erase the given AIS object displayed by corresponded display method
243 /// \param theAIS instance of AIS object
244 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
245 /// \return true if the object visibility state is changed
246 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
248 /// Erase all presentations
249 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
250 /// \return true if the object visibility state is changed
251 bool eraseAll(const bool theUpdateViewer = true);
253 /// Remove default selection filters of the module from the current viewer
254 /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
255 void deactivateSelectionFilters(const bool theAddFilterOnly = true);
257 /// \brief Add selection filter
258 /// \param theFilter a filter instance
259 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
261 /// \brief Remove selection filter
262 /// \param theFilter a filter instance
263 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
265 /// Returns true if the selection filter is set to the viewer
266 /// \param theFilter a selection filter
267 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
269 /// Remove all selection filters
270 void removeFilters();
272 /// Sets a flag to the displayer whether the internal viewer can be updated by
273 /// the updateViewer method call. If it is not enabled, this method do nothing.
274 /// This state maintain recurse, if the update is blocked twice or three times, the
275 /// viewer will not be updated until it is unblocked necessary times
276 /// (twice or three in the example).
277 /// \param isEnabled a boolean value
278 bool enableUpdateViewer(const bool isEnabled);
280 /// Returns true if the viewer update is not blocked
281 bool isUpdateEnabled() const;
283 /// Updates the viewer
284 void updateViewer() const;
286 /// Searches the interactive object by feature
287 /// \param theObject the object or presentable feature
288 /// \return theIO an interactive object
289 AISObjectPtr getAISObject(ObjectPtr theObject) const;
291 /// Searches the feature by interactive object
292 /// \param theIO an interactive object
293 /// \return feature the feature or NULL if it not visualized
294 ObjectPtr getObject(const AISObjectPtr& theIO) const;
296 /// Searches the feature by interactive object
297 /// \param theIO an interactive object
298 /// \return corresponded object or NULL if it not found
299 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
301 /// Deactivates the given objects (not allow selection)
302 /// \param theObjList - list of objects which has to be deactivated.
303 /// \param theUpdateViewer update viewer flag
304 void deactivateObjects(const QObjectPtrList& theObjList,
305 const bool theUpdateViewer = true);
307 /// Sets display mode for the given object if this object is displayed
308 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
310 /// Returns current display mode for the given object.
311 /// If object is not dis played then returns NoMode.
312 /// \param theObject object to check
313 DisplayMode displayMode(ObjectPtr theObject) const;
315 /// Displays only objects listed in the list
316 /// \param theList list of objects
317 void showOnly(const QObjectPtrList& theList);
319 /// Returns number of displayed objects
320 int objectsCount() const { return myResult2AISObjectMap.size(); }
322 /// Returns list of displayed objects
323 QObjectPtrList displayedObjects() const {
325 return myResult2AISObjectMap.objects();
327 return myResult2AISObjectMap.keys();
331 /// Returns list of displayed objects
333 QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
335 return myResult2AISObjectMap.presentations();
338 QList<AISObjectPtr> displayedPresentations() const
340 return myResult2AISObjectMap.values();
344 /// Returns true if the given object can be shown in shaded mode
345 /// \param theObject object to check
346 bool canBeShaded(ObjectPtr theObject) const;
348 /// Set color on presentation of an object if it is displayed
349 /// \param theObject an object
350 /// \param theColor a color which has to be set
351 /// \param theUpdateViewer update viewer flag
352 /// \return previously defined color on the object
353 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
355 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
356 /// depending on the trihedron visible state and displayer active trihedron state
357 void displayTrihedron(bool theToDisplay) const;
360 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
361 { myVCallBack = theCallBack; }
362 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
365 /// Return true if the object is visible. If the object is feature, it returns true
366 /// if all results of the feature are shown
367 /// \param theDisplayer a displayer
368 /// \param theObject an object
369 /// \return a boolean value
370 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
373 /// Returns screen plane of active view
374 GeomPlanePtr getScreenPlane() const;
376 /// Returns scale of active view
377 double getViewScale() const;
380 /// Signal on object display
381 /// \param theObject a data object
382 /// \param theAIS a presentation object
383 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
385 /// Signal on before object erase
386 /// \param theObject a data object
387 /// \param theAIS a presentation object
388 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
391 /// Returns currently installed AIS_InteractiveContext
392 Handle(AIS_InteractiveContext) AISContext() const;
394 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
395 /// The context should have only this filter inside. Other filters should be add to the filter
396 Handle(SelectMgr_AndFilter) GetFilter();
398 /// Display the feature and a shape. This shape would be associated to the given feature
399 /// \param theObject an object instance
400 /// \param theAIS AIS presentation
401 /// \param isShading flag to show in shading mode
402 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
403 /// \return true if the object visibility state is changed
404 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
405 bool theUpdateViewer = true);
408 /// Update the object presentable properties such as color, lines width and other
409 /// If the object is result with the color attribute value set, it is used,
410 /// otherwise the customize is applyed to the object's feature if it is a custom prs
411 /// \param theObject an object instance
412 /// \return the true state if there is changes and the presentation is customized
413 bool customizeObject(ObjectPtr theObject);
415 /// Append the objects in the internal map. Checks whether the map already contains the object
416 /// \param theObject an object to display
417 /// \param theAIS AIOS object to display
418 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
421 /// Returns an information about alredy displayed objects
422 /// \return a string representation
423 std::string getResult2AISObjectMapInfo() const;
426 /// Returns container of visible presentations for the object. For a feature object,
427 /// the feature results are processed also. The presentations map is not cleared inside.
428 /// \param theObject a feature or result
429 /// \param thePresentations result map of presentations
430 void getPresentations(const ObjectPtr& theObject,
431 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
433 /// Sets the shapes selected in the context. It contains logic of the similar method
434 /// in OCCT but improved for performance. The modification is to iterates by a list
435 /// of owners in the context only once.
436 /// \param theContext a viewer context. It has opened local context
437 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
438 /// owner is selected if it is found there.
439 /// Only first owner is processed(according to OCCT logic)
440 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
441 const NCollection_DataMap<TopoDS_Shape,
442 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
445 XGUI_SelectionActivate* selectionActivate() const;
448 XGUI_Workshop* myWorkshop; ///< Reference to workshop
450 Handle(VInspectorAPI_CallBack) myVCallBack;
452 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
454 /// A default custom presentation, which is used if the displayed feature is not
455 /// a custom presentation
456 GeomCustomPrsPtr myCustomPrs;
458 /// Definition of a type of map which defines correspondance between objects and presentations
460 XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
462 typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
463 ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
466 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
467 int myViewerBlockedRecursiveCount;
469 bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
470 mutable bool myNeedUpdate; ///< A flag that update was requested but not done