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