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