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>
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;
51 class VInspectorAPI_CallBack;
55 class XGUI_TwoSidePresentationMap
58 ~XGUI_TwoSidePresentationMap() { clear(); }
60 /// Add new values pair to the map
61 /// \param theObj an object
62 /// \param theAIS a corresponded presentation
63 bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
65 if (myResultToAISMap.contains(theObj))
67 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
68 myResultToAISMap[theObj] = anAIS;
69 myAIStoResultMap[anAIS] = theObj;
73 /// Removes values by object
74 /// \param theObj an object
75 bool remove(const ObjectPtr& theObj)
77 if (!myResultToAISMap.contains(theObj))
79 Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
80 myResultToAISMap.remove(theObj);
81 myAIStoResultMap.remove(aAIS);
85 /// Removes values by presentation
86 /// \param theAIS a presentation
87 bool remove(const AISObjectPtr& theAIS)
89 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
90 if (!myAIStoResultMap.contains(anAIS))
92 ObjectPtr aObj = myAIStoResultMap[anAIS];
93 myResultToAISMap.remove(aObj);
94 myAIStoResultMap.remove(anAIS);
98 /// Removes all values
101 myResultToAISMap.clear();
102 myAIStoResultMap.clear();
105 /// Returns presentation by object
106 /// \param theObj an object
107 AISObjectPtr value(const ObjectPtr& theObj) const
109 if (myResultToAISMap.contains(theObj)) {
110 Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
111 AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
112 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
115 return AISObjectPtr();
118 /// Returns object by presentation
119 /// \param theAIS a presentation
120 ObjectPtr value(const AISObjectPtr& theAIS) const
122 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
123 if (myAIStoResultMap.contains(anAIS))
124 return myAIStoResultMap[anAIS];
128 /// Returns object by presentation
129 /// \param theAIS a presentation
130 ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
132 if (myAIStoResultMap.contains(theAIS))
133 return myAIStoResultMap[theAIS];
137 /// Returns number of values
138 int size() const { return myResultToAISMap.size(); }
140 /// Returns list of objects
141 QObjectPtrList objects() const { return myResultToAISMap.keys(); }
143 /// returns list of presentations
144 QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
146 /// Returns true if the Map contains the object
147 /// \param theObj an object
148 bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
150 /// Returns true if the Map contains the presentation
151 /// \param theAIS a presentation
152 bool contains(const AISObjectPtr& theAIS) const
154 Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
155 return myAIStoResultMap.contains(anAIS);
159 QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
160 QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
164 /**\class XGUI_Displayer
166 * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
168 class XGUI_EXPORT XGUI_Displayer: public QObject
172 /// \enum DisplayMode display mode
174 NoMode = -1, ///< Mode is not defined
175 Wireframe, ///< Wireframe display mode
176 Shading ///< Shading display mode
180 /// \param theWorkshop a workshop instance
181 XGUI_Displayer(XGUI_Workshop* theWorkshop);
184 virtual ~XGUI_Displayer();
186 /// Returns the feature visibility state.
187 /// \param theObject an object instance
188 bool isVisible(ObjectPtr theObject) const;
190 /// Display the feature. Obtain the visualized object from the feature.
191 /// \param theObject an object to display
192 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
193 /// \return true if the object visibility state is changed
194 bool display(ObjectPtr theObject, bool theUpdateViewer = true);
196 /// Display the given AIS object.
197 /// This object is not added to the displayer internal map of objects
198 /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
199 /// order to perform the display with correct flags.
200 /// \param theAIS AIOS object to display
201 /// \param toActivateInSelectionModes boolean value whether the presentation should be
202 /// activated in the current selection modes
203 /// \param theDisplayMode mode how the presentation should be displayed
204 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
205 /// \return true if the object visibility state is changed
206 bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
207 const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
209 /// Redisplay the shape if it was displayed
210 /// \param theObject an object instance
211 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
212 /// \return true if the object visibility state is changed
213 bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
215 /// Sends and flushes a signal to redisplay all visualized objects.
216 void redisplayObjects();
218 /// Add presentations to current selection. It unhighlight and deselect the current selection.
219 /// The shape and result components are processed in the values. If the presentation shape is not
220 /// empty, select it, otherwise select the result.
221 /// \param theValues a list of presentation to be selected
222 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
223 void setSelected(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
224 bool theUpdateViewer = true);
226 /// Unselect all objects
227 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
228 /// \param theUpdateViewer the parameter to update viewer
229 void clearSelected(const bool theUpdateViewer = true);
231 /// Erase the feature and a shape.
232 /// \param theObject an object instance
233 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
234 /// \return true if the object visibility state is changed
235 bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
237 /// Erase the given AIS object displayed by corresponded display method
238 /// \param theAIS instance of AIS object
239 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
240 /// \return true if the object visibility state is changed
241 bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
243 /// Erase all presentations
244 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
245 /// \return true if the object visibility state is changed
246 bool eraseAll(const bool theUpdateViewer = true);
248 /// Remove default selection filters of the module from the current viewer
249 /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
250 void deactivateSelectionFilters(const bool theAddFilterOnly = true);
252 /// \brief Add selection filter
253 /// \param theFilter a filter instance
254 void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
256 /// \brief Remove selection filter
257 /// \param theFilter a filter instance
258 void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
260 /// Returns true if the selection filter is set to the viewer
261 /// \param theFilter a selection filter
262 virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
264 /// Remove all selection filters
265 void removeFilters();
267 /// Sets a flag to the displayer whether the internal viewer can be updated by
268 /// the updateViewer method call. If it is not enabled, this method do nothing.
269 /// This state maintain recurse, if the update is blocked twice or three times, the
270 /// viewer will not be updated until it is unblocked necessary times
271 /// (twice or three in the example).
272 /// \param isEnabled a boolean value
273 bool enableUpdateViewer(const bool isEnabled);
275 /// Returns true if the viewer update is not blocked
276 bool isUpdateEnabled() const;
278 /// Updates the viewer
279 void updateViewer() const;
281 /// Searches the interactive object by feature
282 /// \param theObject the object or presentable feature
283 /// \return theIO an interactive object
284 AISObjectPtr getAISObject(ObjectPtr theObject) const;
286 /// Searches the feature by interactive object
287 /// \param theIO an interactive object
288 /// \return feature the feature or NULL if it not visualized
289 ObjectPtr getObject(const AISObjectPtr& theIO) const;
291 /// Searches the feature by interactive object
292 /// \param theIO an interactive object
293 /// \return corresponded object or NULL if it not found
294 ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
296 /// Deactivates the given objects (not allow selection)
297 /// \param theObjList - list of objects which has to be deactivated.
298 /// \param theUpdateViewer update viewer flag
299 void deactivateObjects(const QObjectPtrList& theObjList,
300 const bool theUpdateViewer = true);
302 /// Sets display mode for the given object if this object is displayed
303 void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
305 /// Returns current display mode for the given object.
306 /// If object is not dis played then returns NoMode.
307 /// \param theObject object to check
308 DisplayMode displayMode(ObjectPtr theObject) const;
310 /// Displays only objects listed in the list
311 /// \param theList list of objects
312 void showOnly(const QObjectPtrList& theList);
314 /// Returns number of displayed objects
315 int objectsCount() const { return myResult2AISObjectMap.size(); }
317 /// Returns list of displayed objects
318 QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.objects(); }
320 /// Returns list of displayed objects
321 QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
323 return myResult2AISObjectMap.presentations();
326 /// Returns true if the given object can be shown in shaded mode
327 /// \param theObject object to check
328 bool canBeShaded(ObjectPtr theObject) const;
330 /// Set color on presentation of an object if it is displayed
331 /// \param theObject an object
332 /// \param theColor a color which has to be set
333 /// \param theUpdateViewer update viewer flag
334 /// \return previously defined color on the object
335 QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
337 /// Displays/erases thrihedron in current modes. It will be activated or deactivated
338 /// depending on the trihedron visible state and displayer active trihedron state
339 void displayTrihedron(bool theToDisplay) const;
342 void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
343 { myVCallBack = theCallBack; }
344 Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
347 /// Return true if the object is visible. If the object is feature, it returns true
348 /// if all results of the feature are shown
349 /// \param theDisplayer a displayer
350 /// \param theObject an object
351 /// \return a boolean value
352 static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
355 /// Signal on object display
356 /// \param theObject a data object
357 /// \param theAIS a presentation object
358 void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
360 /// Signal on before object erase
361 /// \param theObject a data object
362 /// \param theAIS a presentation object
363 void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
366 /// Returns currently installed AIS_InteractiveContext
367 Handle(AIS_InteractiveContext) AISContext() const;
369 /// Returns the viewer context top filter. If there is no a filter, it is created and set into
370 /// The context should have only this filter inside. Other filters should be add to the filter
371 Handle(SelectMgr_AndFilter) GetFilter();
373 /// Display the feature and a shape. This shape would be associated to the given feature
374 /// \param theObject an object instance
375 /// \param theAIS AIS presentation
376 /// \param isShading flag to show in shading mode
377 /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
378 /// \return true if the object visibility state is changed
379 bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
380 bool theUpdateViewer = true);
383 /// Update the object presentable properties such as color, lines width and other
384 /// If the object is result with the color attribute value set, it is used,
385 /// otherwise the customize is applyed to the object's feature if it is a custom prs
386 /// \param theObject an object instance
387 /// \return the true state if there is changes and the presentation is customized
388 bool customizeObject(ObjectPtr theObject);
390 /// Append the objects in the internal map. Checks whether the map already contains the object
391 /// \param theObject an object to display
392 /// \param theAIS AIOS object to display
393 void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
396 /// Returns an information about alredy displayed objects
397 /// \return a string representation
398 std::string getResult2AISObjectMapInfo() const;
401 /// Returns container of visible presentations for the object. For a feature object,
402 /// the feature results are processed also. The presentations map is not cleared inside.
403 /// \param theObject a feature or result
404 /// \param thePresentations result map of presentations
405 void getPresentations(const ObjectPtr& theObject,
406 NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
408 /// Sets the shapes selected in the context. It contains logic of the similar method
409 /// in OCCT but improved for performance. The modification is to iterates by a list
410 /// of owners in the context only once.
411 /// \param theContext a viewer context. It has opened local context
412 /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
413 /// owner is selected if it is found there.
414 /// Only first owner is processed(according to OCCT logic)
415 static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
416 const NCollection_DataMap<TopoDS_Shape,
417 NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
420 XGUI_SelectionActivate* selectionActivate() const;
423 XGUI_Workshop* myWorkshop; ///< Reference to workshop
425 Handle(VInspectorAPI_CallBack) myVCallBack;
427 Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
429 /// A default custom presentation, which is used if the displayed feature is not
430 /// a custom presentation
431 GeomCustomPrsPtr myCustomPrs;
433 /// Definition of a type of map which defines correspondance between objects and presentations
434 XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
436 /// Number of blocking of the viewer update. The viewer is updated only if it is zero
437 int myViewerBlockedRecursiveCount;
439 bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
440 mutable bool myNeedUpdate; ///< A flag that update was requested but not done