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