Salome HOME
5ad16e6bcc00bcbca8be10123dc301481f67ff87
[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 #include <ModuleBase_ViewerPrs.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 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<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   /// \param isEnabled a boolean value
142   bool enableUpdateViewer(const bool isEnabled);
143
144   /// Returns myEnableUpdateViewer flag
145   bool isUpdateEnabled() const { return myEnableUpdateViewer; }
146
147   /// Updates the viewer
148   void updateViewer() const;
149
150   /// Activate interactive context
151   /// \param theIO an interactive object
152   /// \param theMode activation mode
153   /// \param theUpdateViewer update viewer flag
154   void activateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode,
155                    const bool theUpdateViewer) const;
156
157   /// Activate interactive context. It is necessary to call ClearOutdatedSelection after deactivation
158   /// \param theIO an interactive object
159   /// \param theMode a mode to deactivate. When theMode=-1 then all modes will be deactivated
160   void deactivateAIS(const Handle(AIS_InteractiveObject)& theIO, const int theMode = -1) const;
161
162   /// Searches the interactive object by feature
163   /// \param theObject the object or presentable feature
164   /// \return theIO an interactive object
165   AISObjectPtr getAISObject(ObjectPtr theObject) const;
166
167   /// Searches the feature by interactive object
168   /// \param theIO an interactive object
169   /// \return feature the feature or NULL if it not visualized
170   ObjectPtr getObject(const AISObjectPtr& theIO) const;
171
172   /// Searches the feature by interactive object
173   /// \param theIO an interactive object
174   /// \return corresponded object or NULL if it not found
175   ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
176
177   /// Deactivates the given objects (not allow selection)
178   /// \param theObjList - list of objects which has to be deactivated.
179   /// \param theUpdateViewer update viewer flag
180   void deactivateObjects(const QObjectPtrList& theObjList,
181                          const bool theUpdateViewer = true);
182
183   /// Returns the modes of activation
184   /// \param theObject the feature or NULL if it not visualized
185   /// \param theModes - modes on which it is activated (can be empty)
186   void getModesOfActivation(ObjectPtr theObject, QIntList& theModes);
187
188   /// Returns true if the given object can be selected
189   /// \param theObject object to check
190   bool isActive(ObjectPtr theObject) const;
191
192   /// Activates in local context displayed outside of the context.
193   /// \param theModes - modes on which it has to be activated (can be empty)
194   /// \param theObjList - list of objects which has to be activated.
195   /// \param theUpdateViewer an update viewer flag
196   void activateObjects(const QIntList& theModes, const QObjectPtrList& theObjList,
197                        const bool theUpdateViewer = true);
198
199   /// Sets display mode for the given object if this object is displayed
200   void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
201
202   /// Returns current display mode for the given object.
203   /// If object is not dis played then returns NoMode.
204   /// \param theObject object to check
205   DisplayMode displayMode(ObjectPtr theObject) const;
206
207   /// Displays only objects listed in the list
208   /// \param theList list of objects
209   void showOnly(const QObjectPtrList& theList);
210
211   /// Returns number of displayed objects
212   int objectsCount() const { return myResult2AISObjectMap.size(); }
213
214   /// Returns list of displayed objects
215   QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.keys(); }
216
217   /// Returns list of displayed objects
218   QList<AISObjectPtr> displayedPresentations() const { return myResult2AISObjectMap.values(); }
219
220   /// Returns true if the given object can be shown in shaded mode
221   /// \param theObject object to check
222   bool canBeShaded(ObjectPtr theObject) const;
223
224   /// Set color on presentation of an object if it is displayed
225   /// \param theObject an object 
226   /// \param theColor a color which has to be set
227   /// \param theUpdateViewer update viewer flag
228   /// \return previously defined color on the object
229   QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
230
231   /// Returns Trihedron object if it is displayed
232   Handle(AIS_InteractiveObject) getTrihedron() const;
233   
234   /// Set trihedron active (used in selection) or non active
235   void activateTrihedron(bool theIsActive);
236
237   /// Displays/erases thrihedron in current modes. It will be activated or deactivated
238   /// depending on the trihedron visible state and displayer active trihedron state
239   void displayTrihedron(bool theToDisplay) const;
240
241   /// Returns true if the trihedron should be activated in current selection modes
242   bool isTrihedronActive() const { return myIsTrihedronActive; }
243
244   /// Converts shape type (TopAbs_ShapeEnum) to selection mode
245   /// \param theShapeType a shape type from TopAbs_ShapeEnum
246   static int getSelectionMode(int theShapeType);
247
248 signals:
249   /// Signal on object display
250   /// \param theObject a data object
251   /// \param theAIS a presentation object
252   void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
253
254   /// Signal on before object erase
255   /// \param theObject a data object
256   /// \param theAIS a presentation object
257   void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
258
259  protected:
260   /// Returns currently installed AIS_InteractiveContext
261   Handle(AIS_InteractiveContext) AISContext() const;
262
263   /// Returns the viewer context top filter. If there is no a filter, it is created and set into
264   /// The context should have only this filter inside. Other filters should be add to the filter
265   Handle(SelectMgr_AndFilter) GetFilter();
266
267   /// Display the feature and a shape. This shape would be associated to the given feature
268   /// \param theObject an object instance
269   /// \param theAIS AIS presentation
270   /// \param isShading flag to show in shading mode
271   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
272   /// \return true if the object visibility state is changed
273   bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
274                bool theUpdateViewer = true);
275
276 private:
277   /// Activates the interactive object in the local context.
278   /// \param theIO an interactive object
279   /// \param theModes - modes on which it has to be activated (can be empty)
280   /// \return a flag is object activated or not
281   bool activate(const Handle(AIS_InteractiveObject)& theIO, const QIntList& theModes,
282                 const bool theUpdateViewer) const;
283
284   /// Deactivates the given object (not allow selection)
285   /// \param theObject object to deactivate
286   void deactivate(ObjectPtr theObject, const bool theUpdateViewer);
287
288   /// Find a trihedron in a list of displayed presentations and deactivate it.
289   /// \param theUpdateViewer an update viewer flag
290   void deactivateTrihedron(const bool theUpdateViewer) const;
291
292   /// Opens local context. Does nothing if it is already opened.
293   void openLocalContext();
294
295   /// Update the object presentable properties such as color, lines width and other
296   /// If the object is result with the color attribute value set, it is used,
297   /// otherwise the customize is applyed to the object's feature if it is a custom prs
298   /// \param theObject an object instance
299   /// \return the true state if there is changes and the presentation is customized
300   bool customizeObject(ObjectPtr theObject);
301
302   /// Append the objects in the internal map. Checks whether the map already contains the object
303   /// \param theObject an object to display
304   /// \param theAIS AIOS object to display
305   void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
306
307   /// Returns an information about alredy displayed objects
308   /// \return a string representation
309   std::string getResult2AISObjectMapInfo() const;
310
311  protected:
312    /// Reference to workshop
313   XGUI_Workshop* myWorkshop;
314
315   /// A container for selection filters
316   Handle(SelectMgr_AndFilter) myAndFilter;
317
318   /// A default custom presentation, which is used if the displayed feature is not a custom presentation
319   GeomCustomPrsPtr myCustomPrs;
320
321   /// Definition of a type of map which defines correspondance between objects and presentations
322   typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
323
324   /// A map of displayed objects
325   ResultToAISMap myResult2AISObjectMap;
326
327   /// Selection modes installed for external objects in local context
328   QIntList myActiveSelectionModes;
329
330   /// the enable update viewer flag
331   bool myEnableUpdateViewer; 
332
333   // Flag: use trihedgon for selection or not
334   bool myIsTrihedronActive;
335
336   /// A flag that update was requested but not done
337   mutable bool myNeedUpdate;
338 };
339
340 #endif