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