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