Salome HOME
6374f157bb1decc6f0ba01eba7c8572d5defedef
[modules/shaper.git] / src / XGUI / XGUI_Displayer.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_Displayer.h
4 // Created:     20 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #ifndef XGUI_Displayer_H
8 #define XGUI_Displayer_H
9
10 #include "XGUI.h"
11
12 #include <GeomAPI_AISObject.h>
13 #include <TopoDS_Shape.hxx>
14 #include <AIS_InteractiveObject.hxx>
15 #include <AIS_InteractiveContext.hxx>
16 #include <NCollection_Map.hxx>
17 #include <NCollection_DataMap.hxx>
18
19 #include <ModelAPI_Result.h>
20
21 #include <ModuleBase_Definitions.h>
22
23 #include <GeomAPI_ICustomPrs.h>
24
25 #include <SelectMgr_AndFilter.hxx>
26
27 #include <QString>
28 #include <QMap>
29 #include <QObject>
30 #include <QColor>
31
32 class ModuleBase_ViewerPrs;
33 class ModelAPI_Feature;
34 class XGUI_Workshop;
35
36 #ifdef VINSPECTOR
37 class VInspectorAPI_Communicator;
38 class VInspectorAPI_CallBack;
39 #endif
40
41 /**\class XGUI_Displayer
42  * \ingroup GUI
43  * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
44  */
45 class XGUI_EXPORT XGUI_Displayer: public QObject
46 {
47   Q_OBJECT
48  public:
49    /// \enum DisplayMode display mode
50    enum DisplayMode {
51      /// Mode is not defined
52      NoMode = -1,
53      /// Wireframe display mode
54      Wireframe,
55      /// Shading display mode
56      Shading
57    };
58
59   /// Constructor
60   /// \param theWorkshop a workshop instance
61   XGUI_Displayer(XGUI_Workshop* theWorkshop);
62
63   /// Destructor
64   virtual ~XGUI_Displayer();
65
66   /// Returns the feature visibility state.
67   /// \param theObject an object instance
68   bool isVisible(ObjectPtr theObject) const;
69
70   /// Display the feature. Obtain the visualized object from the feature.
71   /// \param theObject an object to display
72   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
73   /// \return true if the object visibility state is changed
74   bool display(ObjectPtr theObject, bool theUpdateViewer = true);
75
76   /// Display the given AIS object.
77   /// This object is not added to the displayer internal map of objects
78   /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
79   /// order to perform the display with correct flags.
80   /// \param theAIS AIOS object to display
81   /// \param toActivateInSelectionModes boolean value whether the presentation should be
82   /// activated in the current selection modes
83   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
84   /// \return true if the object visibility state is changed
85   bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
86                   bool theUpdateViewer = true);
87
88   /// Redisplay the shape if it was displayed
89   /// \param theObject an object instance
90   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
91   /// \return true if the object visibility state is changed
92   bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
93
94   /// Sends and flushes a signal to redisplay all visualized objects.
95   void redisplayObjects();
96
97   /// Add presentations to current selection. It unhighlight and deselect the current selection.
98   /// The shape and result components are processed in the values. If the presentation shape is not
99   /// empty, select it, otherwise select the result.
100   /// \param theValues a list of presentation to be selected
101   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
102   void setSelected(const  QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
103                    bool theUpdateViewer = true);
104
105   /// Unselect all objects
106   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
107   /// \param theUpdateViewer the parameter to update viewer
108   void clearSelected(const bool theUpdateViewer = true);
109
110   /// Erase the feature and a shape.
111   /// \param theObject an object instance
112   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
113   /// \return true if the object visibility state is changed
114   bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
115
116   /// Erase the given AIS object displayed by corresponded display method
117   /// \param theAIS instance of AIS object
118   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
119   /// \return true if the object visibility state is changed
120   bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
121
122   /// Erase all presentations
123   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
124   /// \return true if the object visibility state is changed
125   bool eraseAll(const bool theUpdateViewer = true);
126
127   /// Deactivates selection of sub-shapes
128   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
129   //void closeLocalContexts(const bool theUpdateViewer = true);
130
131   /// Remove default selection filters of the module from the current viewer
132   void deactivateSelectionFilters();
133
134   /// \brief Add selection filter
135   /// \param theFilter a filter instance
136   void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
137
138   /// \brief Remove selection filter
139   /// \param theFilter a filter instance
140   void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
141
142   /// Returns true if the selection filter is set to the viewer
143   /// \param theFilter a selection filter
144   virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
145
146   /// Remove all selection filters
147   void removeFilters();
148
149   /// Sets a flag to the displayer whether the internal viewer can be updated by
150   /// the updateViewer method call. If it is not enabled, this method do nothing.
151   /// This state maintain recurse, if the update is blocked twice or three times, the
152   /// viewer will not be updated until it is unblocked necessary times
153   /// (twice or three in the example).
154   /// \param isEnabled a boolean value
155   bool enableUpdateViewer(const bool isEnabled);
156
157   /// Returns true if the viewer update is not blocked
158   bool isUpdateEnabled() const;
159
160   /// Updates the viewer
161   void updateViewer() const;
162
163   /// Activate interactive context
164   /// \param theIO an interactive object
165   /// \param theMode activation mode
166   /// \param theUpdateViewer update viewer flag
167   void activateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode,
168                    const bool theUpdateViewer) const;
169
170   /// Activate interactive context. It is necessary to call ClearOutdatedSelection
171   /// after deactivation
172   /// \param theIO an interactive object
173   /// \param theMode a mode to deactivate. When theMode=-1 then all modes will be deactivated
174   void deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode = -1) const;
175
176   /// Searches the interactive object by feature
177   /// \param theObject the object or presentable feature
178   /// \return theIO an interactive object
179   AISObjectPtr getAISObject(ObjectPtr theObject) const;
180
181   /// Searches the feature by interactive object
182   /// \param theIO an interactive object
183   /// \return feature the feature or NULL if it not visualized
184   ObjectPtr getObject(const AISObjectPtr& theIO) const;
185
186   /// Searches the feature by interactive object
187   /// \param theIO an interactive object
188   /// \return corresponded object or NULL if it not found
189   ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
190
191   /// Deactivates the given objects (not allow selection)
192   /// \param theObjList - list of objects which has to be deactivated.
193   /// \param theUpdateViewer update viewer flag
194   void deactivateObjects(const QObjectPtrList& theObjList,
195                          const bool theUpdateViewer = true);
196
197   /// Returns the modes of activation
198   /// \param theObject the feature or NULL if it not visualized
199   /// \param theModes - modes on which it is activated (can be empty)
200   void getModesOfActivation(ObjectPtr theObject, QIntList& theModes);
201
202   /// Returns true if the given object can be selected
203   /// \param theObject object to check
204   bool isActive(ObjectPtr theObject) const;
205
206   /// Activates in local context displayed outside of the context.
207   /// \param theModes - modes on which it has to be activated (can be empty)
208   /// \param theObjList - list of objects which has to be activated.
209   /// \param theUpdateViewer an update viewer flag
210   void activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
211                        const bool theUpdateViewer = true);
212
213   /// Sets display mode for the given object if this object is displayed
214   void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
215
216   /// Returns current display mode for the given object.
217   /// If object is not dis played then returns NoMode.
218   /// \param theObject object to check
219   DisplayMode displayMode(ObjectPtr theObject) const;
220
221   /// Displays only objects listed in the list
222   /// \param theList list of objects
223   void showOnly(const QObjectPtrList& theList);
224
225   /// Returns number of displayed objects
226   int objectsCount() const { return myResult2AISObjectMap.size(); }
227
228   /// Returns list of displayed objects
229   QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.keys(); }
230
231   /// Returns list of displayed objects
232   QList<AISObjectPtr> displayedPresentations() const { return myResult2AISObjectMap.values(); }
233
234   /// Returns true if the given object can be shown in shaded mode
235   /// \param theObject object to check
236   bool canBeShaded(ObjectPtr theObject) const;
237
238   /// Set color on presentation of an object if it is displayed
239   /// \param theObject an object
240   /// \param theColor a color which has to be set
241   /// \param theUpdateViewer update viewer flag
242   /// \return previously defined color on the object
243   QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
244
245   /// Returns Trihedron object if it is displayed
246   Handle(AIS_InteractiveObject) getTrihedron() const;
247
248   /// Set trihedron active (used in selection) or non active
249   void activateTrihedron(bool theIsActive);
250
251   /// Displays/erases thrihedron in current modes. It will be activated or deactivated
252   /// depending on the trihedron visible state and displayer active trihedron state
253   void displayTrihedron(bool theToDisplay) const;
254
255   /// Returns true if the trihedron should be activated in current selection modes
256   bool isTrihedronActive() const { return myIsTrihedronActive; }
257
258   /// Returns list of currently active selection modes
259   /// Selection modes will be returned according to TopAbs_ShapeEnum
260   QIntList activeSelectionModes() const;
261
262 #ifdef VINSPECTOR
263   void setVInspectorVisible(const bool theVisible);
264
265   void setCommunicator(VInspectorAPI_Communicator* theCommunicator);
266
267   VInspectorAPI_CallBack* getCallBack() const;
268 #endif
269   /// Converts shape type (TopAbs_ShapeEnum) to selection mode
270   /// \param theShapeType a shape type from TopAbs_ShapeEnum
271   static int getSelectionMode(int theShapeType);
272
273   /// Return true if the object is visible. If the object is feature, it returns true
274   /// if all results of the feature are shown
275   /// \param theDisplayer a displayer
276   /// \param theObject an object
277   /// \return a boolean value
278   static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
279
280 signals:
281   /// Signal on object display
282   /// \param theObject a data object
283   /// \param theAIS a presentation object
284   void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
285
286   /// Signal on before object erase
287   /// \param theObject a data object
288   /// \param theAIS a presentation object
289   void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
290
291  protected:
292   /// Returns currently installed AIS_InteractiveContext
293   Handle(AIS_InteractiveContext) AISContext() const;
294
295   /// Returns the viewer context top filter. If there is no a filter, it is created and set into
296   /// The context should have only this filter inside. Other filters should be add to the filter
297   Handle(SelectMgr_AndFilter) GetFilter();
298
299   /// Display the feature and a shape. This shape would be associated to the given feature
300   /// \param theObject an object instance
301   /// \param theAIS AIS presentation
302   /// \param isShading flag to show in shading mode
303   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
304   /// \return true if the object visibility state is changed
305   bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
306                bool theUpdateViewer = true);
307
308 private:
309   /// Activates the interactive object in the local context.
310   /// \param theIO an interactive object
311   /// \param theModes - modes on which it has to be activated (can be empty)
312   /// \return a flag is object activated or not
313   bool activate(const Handle(AIS_InteractiveObject)& theIO, const QIntList& theModes,
314                 const bool theUpdateViewer) const;
315
316   /// Deactivates the given object (not allow selection)
317   /// \param theObject object to deactivate
318   void deactivate(ObjectPtr theObject, const bool theUpdateViewer);
319
320   /// Find a trihedron in a list of displayed presentations and deactivate it.
321   /// \param theUpdateViewer an update viewer flag
322   void deactivateTrihedron(const bool theUpdateViewer) const;
323
324   /// Update the object presentable properties such as color, lines width and other
325   /// If the object is result with the color attribute value set, it is used,
326   /// otherwise the customize is applyed to the object's feature if it is a custom prs
327   /// \param theObject an object instance
328   /// \return the true state if there is changes and the presentation is customized
329   bool customizeObject(ObjectPtr theObject);
330
331   /// Append the objects in the internal map. Checks whether the map already contains the object
332   /// \param theObject an object to display
333   /// \param theAIS AIOS object to display
334   void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
335
336 #ifdef _DEBUG
337   /// Returns an information about alredy displayed objects
338   /// \return a string representation
339   std::string getResult2AISObjectMapInfo() const;
340 #endif
341
342   /// Returns container of visible presentations for the object. For a feature object,
343   /// the feature results are processed also. The presentations map is not cleared inside.
344   /// \param theObject a feature or result
345   /// \param thePresentations result map of presentations
346   void getPresentations(const ObjectPtr& theObject,
347                         NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
348
349   /// Sets the shapes selected in the context. It contains logic of the similar method
350   /// in OCCT but improved for performance. The modification is to iterates by a list
351   /// of owners in the context only once.
352   /// \param theContext a viewer context. It has opened local context
353   /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
354   /// owner is selected if it is found there.
355   /// Only first owner is processed(according to OCCT logic)
356   static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
357           const NCollection_DataMap<TopoDS_Shape,
358                           NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
359
360  protected:
361    /// Reference to workshop
362   XGUI_Workshop* myWorkshop;
363 #ifdef VINSPECTOR
364   VInspectorAPI_Communicator* myCommunicator; ///< callback to debug display, show/hide it
365 #endif
366   /// A container for selection filters
367   Handle(SelectMgr_AndFilter) myAndFilter;
368
369   /// A default custom presentation, which is used if the displayed feature is not
370   /// a custom presentation
371   GeomCustomPrsPtr myCustomPrs;
372
373   /// Definition of a type of map which defines correspondance between objects and presentations
374   typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
375
376   /// A map of displayed objects
377   ResultToAISMap myResult2AISObjectMap;
378
379   /// Selection modes installed for external objects in local context
380   QIntList myActiveSelectionModes;
381
382   /// Number of blocking of the viewer update. The viewer is updated only if it equals zero
383   int myViewerBlockedRecursiveCount;
384
385   /// Flag: first asking of AIS context: trihedron activation
386   bool myIsFirstAISContextUse;
387
388   /// Flag: use trihedgon for selection or not
389   bool myIsTrihedronActive;
390
391   /// A flag that update was requested but not done
392   mutable bool myNeedUpdate;
393 };
394
395 #endif