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;
56 class XGUI_TwoSidePresentationMap
59 ~XGUI_TwoSidePresentationMap() { clear(); }
61 /// Add new values pair to the map
62 /// \param theObj an object
63 /// \param theAIS a corresponded presentation
64 bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
66 if (myResultToAISMap.contains(theObj))
68 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
69 myResultToAISMap[theObj] = anAIS;
70 myAIStoResultMap[anAIS] = theObj;
74 /// Removes values by object
75 /// \param theObj an object
76 bool remove(const ObjectPtr& theObj)
78 if (!myResultToAISMap.contains(theObj))
80 Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
81 myResultToAISMap.remove(theObj);
82 myAIStoResultMap.remove(aAIS);
86 /// Removes values by presentation
87 /// \param theAIS a presentation
88 bool remove(const AISObjectPtr& theAIS)
90 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
91 if (!myAIStoResultMap.contains(anAIS))
93 ObjectPtr aObj = myAIStoResultMap[anAIS];
94 myResultToAISMap.remove(aObj);
95 myAIStoResultMap.remove(anAIS);
99 /// Removes all values
102 myResultToAISMap.clear();
103 myAIStoResultMap.clear();
106 /// Returns presentation by object
107 /// \param theObj an object
108 AISObjectPtr value(const ObjectPtr& theObj) const
110 if (myResultToAISMap.contains(theObj)) {
111 Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
112 AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
113 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
116 return AISObjectPtr();
119 /// Returns object by presentation
120 /// \param theAIS a presentation
121 ObjectPtr value(const AISObjectPtr& theAIS) const
123 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
124 if (myAIStoResultMap.contains(anAIS))
125 return myAIStoResultMap[anAIS];
129 /// Returns object by presentation
130 /// \param theAIS a presentation
131 ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
133 if (myAIStoResultMap.contains(theAIS))
134 return myAIStoResultMap[theAIS];
138 /// Returns number of values
139 int size() const { return myResultToAISMap.size(); }
141 /// Returns list of objects
142 QObjectPtrList objects() const { return myResultToAISMap.keys(); }
144 /// returns list of presentations
145 QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
147 /// Returns true if the Map contains the object
148 /// \param theObj an object
149 bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
151 /// Returns true if the Map contains the presentation
152 /// \param theAIS a presentation
153 bool contains(const AISObjectPtr& theAIS) const
155 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
156 return myAIStoResultMap.contains(anAIS);
160 QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
161 QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
165 /**\class XGUI_Displayer
167 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
169 class XGUI_EXPORT XGUI_Displayer: public QObject
173 /// \enum DisplayMode display mode
175 NoMode = -1, ///< Mode is not defined
176 Wireframe, ///< Wireframe display mode
177 Shading ///< Shading display mode
181 /// \param theWorkshop a workshop instance
182 XGUI_Displayer(XGUI_Workshop* theWorkshop);
185 virtual ~XGUI_Displayer();
187 /// Returns the feature visibility state.
188 /// \param theObject an object instance
189 bool isVisible(ObjectPtr theObject) const;
191 /// Display the feature. Obtain the visualized object from the feature.
192 /// \param theObject an object to display
193 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
194 /// \return true if the object visibility state is changed
195 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
197 /// Display the given AIS object.
198 /// This object is not added to the displayer internal map of objects
199 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
200 /// order to perform the display with correct flags.
201 /// \param theAIS AIOS object to display
202 /// \param toActivateInSelectionModes boolean value whether the presentation should be
203 /// activated in the current selection modes
204 /// \param theDisplayMode mode how the presentation should be displayed
205 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
206 /// \return true if the object visibility state is changed
207 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
208 const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
210 /// Redisplay the shape if it was displayed
211 /// \param theObject an object instance
212 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
213 /// \return true if the object visibility state is changed
214 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
216 /// Sends and flushes a signal to redisplay all visualized objects.
217 void redisplayObjects();
219 /// Add presentations to current selection. It unhighlight and deselect the current selection.
220 /// The shape and result components are processed in the values. If the presentation shape is not
221 /// empty, select it, otherwise select the result.
222 /// \param theValues a list of presentation to be selected
223 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
224 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
225 bool theUpdateViewer = true);
227 /// Unselect all objects
228 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
229 /// \param theUpdateViewer the parameter to update viewer
230 void clearSelected(const bool theUpdateViewer = true);
232 /// Erase the feature and a shape.
233 /// \param theObject an object instance
234 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
235 /// \return true if the object visibility state is changed
236 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
238 /// Erase the given AIS object displayed by corresponded display method
239 /// \param theAIS instance of AIS object
240 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
241 /// \return true if the object visibility state is changed
242 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
244 /// Erase all presentations
245 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
246 /// \return true if the object visibility state is changed
247 bool eraseAll(const bool theUpdateViewer = true);
249 /// Remove default selection filters of the module from the current viewer
250 /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
251 void deactivateSelectionFilters(const bool theAddFilterOnly = true);
253 /// \brief Add selection filter
254 /// \param theFilter a filter instance
255 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
257 /// \brief Remove selection filter
258 /// \param theFilter a filter instance
259 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
261 /// Returns true if the selection filter is set to the viewer
262 /// \param theFilter a selection filter
263 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
265 /// Remove all selection filters
266 void removeFilters();
268 /// Sets a flag to the displayer whether the internal viewer can be updated by
269 /// the updateViewer method call. If it is not enabled, this method do nothing.
270 /// This state maintain recurse, if the update is blocked twice or three times, the
271 /// viewer will not be updated until it is unblocked necessary times
272 /// (twice or three in the example).
273 /// \param isEnabled a boolean value
274 bool enableUpdateViewer(const bool isEnabled);
276 /// Returns true if the viewer update is not blocked
277 bool isUpdateEnabled() const;
279 /// Updates the viewer
280 void updateViewer() const;
282 /// Searches the interactive object by feature
283 /// \param theObject the object or presentable feature
284 /// \return theIO an interactive object
285 AISObjectPtr getAISObject(ObjectPtr theObject) const;
287 /// Searches the feature by interactive object
288 /// \param theIO an interactive object
289 /// \return feature the feature or NULL if it not visualized
290 ObjectPtr getObject(const AISObjectPtr& theIO) const;
292 /// Searches the feature by interactive object
293 /// \param theIO an interactive object
294 /// \return corresponded object or NULL if it not found
295 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
297 /// Deactivates the given objects (not allow selection)
298 /// \param theObjList - list of objects which has to be deactivated.
299 /// \param theUpdateViewer update viewer flag
300 void deactivateObjects(const QObjectPtrList& theObjList,
301 const bool theUpdateViewer = true);
303 /// Sets display mode for the given object if this object is displayed
304 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
306 /// Returns current display mode for the given object.
307 /// If object is not dis played then returns NoMode.
308 /// \param theObject object to check
309 DisplayMode displayMode(ObjectPtr theObject) const;
311 /// Displays only objects listed in the list
312 /// \param theList list of objects
313 void showOnly(const QObjectPtrList& theList);
315 /// Returns number of displayed objects
316 int objectsCount() const { return myResult2AISObjectMap.size(); }
318 /// Returns list of displayed objects
319 QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.objects(); }
321 /// Returns list of displayed objects
322 QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
324 return myResult2AISObjectMap.presentations();
327 /// Returns true if the given object can be shown in shaded mode
328 /// \param theObject object to check
329 bool canBeShaded(ObjectPtr theObject) const;
331 /// Set color on presentation of an object if it is displayed
332 /// \param theObject an object
333 /// \param theColor a color which has to be set
334 /// \param theUpdateViewer update viewer flag
335 /// \return previously defined color on the object
336 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
338 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
339 /// depending on the trihedron visible state and displayer active trihedron state
340 void displayTrihedron(bool theToDisplay) const;
343 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
344 { myVCallBack = theCallBack; }
345 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
348 /// Return true if the object is visible. If the object is feature, it returns true
349 /// if all results of the feature are shown
350 /// \param theDisplayer a displayer
351 /// \param theObject an object
352 /// \return a boolean value
353 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
356 /// Returns screen plane of active view
357 GeomPlanePtr getScreenPlane() const;
359 /// Returns scale of active view
360 double getViewScale() const;
363 /// Signal on object display
364 /// \param theObject a data object
365 /// \param theAIS a presentation object
366 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
368 /// Signal on before object erase
369 /// \param theObject a data object
370 /// \param theAIS a presentation object
371 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
374 /// Returns currently installed AIS_InteractiveContext
375 Handle(AIS_InteractiveContext) AISContext() const;
377 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
378 /// The context should have only this filter inside. Other filters should be add to the filter
379 Handle(SelectMgr_AndFilter) GetFilter();
381 /// Display the feature and a shape. This shape would be associated to the given feature
382 /// \param theObject an object instance
383 /// \param theAIS AIS presentation
384 /// \param isShading flag to show in shading mode
385 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
386 /// \return true if the object visibility state is changed
387 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
388 bool theUpdateViewer = true);
391 /// Update the object presentable properties such as color, lines width and other
392 /// If the object is result with the color attribute value set, it is used,
393 /// otherwise the customize is applyed to the object's feature if it is a custom prs
394 /// \param theObject an object instance
395 /// \return the true state if there is changes and the presentation is customized
396 bool customizeObject(ObjectPtr theObject);
398 /// Append the objects in the internal map. Checks whether the map already contains the object
399 /// \param theObject an object to display
400 /// \param theAIS AIOS object to display
401 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
404 /// Returns an information about alredy displayed objects
405 /// \return a string representation
406 std::string getResult2AISObjectMapInfo() const;
409 /// Returns container of visible presentations for the object. For a feature object,
410 /// the feature results are processed also. The presentations map is not cleared inside.
411 /// \param theObject a feature or result
412 /// \param thePresentations result map of presentations
413 void getPresentations(const ObjectPtr& theObject,
414 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
416 /// Sets the shapes selected in the context. It contains logic of the similar method
417 /// in OCCT but improved for performance. The modification is to iterates by a list
418 /// of owners in the context only once.
419 /// \param theContext a viewer context. It has opened local context
420 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
421 /// owner is selected if it is found there.
422 /// Only first owner is processed(according to OCCT logic)
423 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
424 const NCollection_DataMap<TopoDS_Shape,
425 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
428 XGUI_SelectionActivate* selectionActivate() const;
431 XGUI_Workshop* myWorkshop; ///< Reference to workshop
433 Handle(VInspectorAPI_CallBack) myVCallBack;
435 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
437 /// A default custom presentation, which is used if the displayed feature is not
438 /// a custom presentation
439 GeomCustomPrsPtr myCustomPrs;
441 /// Definition of a type of map which defines correspondance between objects and presentations
442 XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
444 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
445 int myViewerBlockedRecursiveCount;
447 bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
448 mutable bool myNeedUpdate; ///< A flag that update was requested but not done