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