Salome HOME
870c06af708ea5218520a3d6f03628754ff405ad
[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 <GeomAPI_ICustomPrs.h>
28
29 #include <ModelAPI_Result.h>
30
31 #include <ModuleBase_Definitions.h>
32
33 #include <AIS_InteractiveObject.hxx>
34 #include <AIS_InteractiveContext.hxx>
35 #include <NCollection_Map.hxx>
36 #include <NCollection_DataMap.hxx>
37 #include <SelectMgr_AndFilter.hxx>
38 #include <TopoDS_Shape.hxx>
39
40 #include <QColor>
41 #include <QMap>
42 #include <QObject>
43 #include <QString>
44
45 class ModuleBase_ViewerPrs;
46 class ModelAPI_Feature;
47 class XGUI_SelectionActivate;
48 class XGUI_Workshop;
49
50 #ifdef TINSPECTOR
51 class VInspectorAPI_CallBack;
52 #endif
53
54
55 class XGUI_TwoSidePresentationMap
56 {
57 public:
58   ~XGUI_TwoSidePresentationMap() { clear(); }
59
60   /// Add new values pair to the map
61   /// \param theObj an object
62   /// \param theAIS a corresponded presentation
63   bool add(const ObjectPtr& theObj, const AISObjectPtr& theAIS)
64   {
65     if (myResultToAISMap.contains(theObj))
66       return false;
67     Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
68     myResultToAISMap[theObj] = anAIS;
69     myAIStoResultMap[anAIS] = theObj;
70     return true;
71   }
72
73   /// Removes values by object
74   /// \param theObj an object
75   bool remove(const ObjectPtr& theObj)
76   {
77     if (!myResultToAISMap.contains(theObj))
78       return false;
79     Handle(AIS_InteractiveObject) aAIS = myResultToAISMap[theObj];
80     myResultToAISMap.remove(theObj);
81     myAIStoResultMap.remove(aAIS);
82     return true;
83   }
84
85   /// Removes values by presentation
86   /// \param theAIS a presentation
87   bool remove(const AISObjectPtr& theAIS)
88   {
89     Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
90     if (!myAIStoResultMap.contains(anAIS))
91       return false;
92     ObjectPtr aObj = myAIStoResultMap[anAIS];
93     myResultToAISMap.remove(aObj);
94     myAIStoResultMap.remove(anAIS);
95     return true;
96   }
97
98   /// Removes all values
99   void clear()
100   {
101     myResultToAISMap.clear();
102     myAIStoResultMap.clear();
103   }
104
105   /// Returns presentation by object
106   /// \param theObj an object
107   AISObjectPtr value(const ObjectPtr& theObj) const
108   {
109     if (myResultToAISMap.contains(theObj)) {
110       Handle(AIS_InteractiveObject) anAIS = myResultToAISMap[theObj];
111       AISObjectPtr anAISObj = AISObjectPtr(new GeomAPI_AISObject());
112       anAISObj->setImpl(new Handle(AIS_InteractiveObject)(anAIS));
113       return anAISObj;
114     }
115     return AISObjectPtr();
116   }
117
118   /// Returns object by presentation
119   /// \param theAIS a presentation
120   ObjectPtr value(const AISObjectPtr& theAIS) const
121   {
122     Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
123     if (myAIStoResultMap.contains(anAIS))
124       return myAIStoResultMap[anAIS];
125     return ObjectPtr();
126   }
127
128   /// Returns object by presentation
129   /// \param theAIS a presentation
130   ObjectPtr value(const Handle(AIS_InteractiveObject)& theAIS) const
131   {
132     if (myAIStoResultMap.contains(theAIS))
133       return myAIStoResultMap[theAIS];
134     return ObjectPtr();
135   }
136
137   /// Returns number of values
138   int size() const { return myResultToAISMap.size(); }
139
140   /// Returns list of objects
141   QObjectPtrList objects() const { return myResultToAISMap.keys(); }
142
143   /// returns list of presentations
144   QList<Handle(AIS_InteractiveObject)> presentations() const { return myAIStoResultMap.keys(); }
145
146   /// Returns true if the Map contains the object
147   /// \param theObj an object
148   bool contains(const ObjectPtr& theObj) const { return myResultToAISMap.contains(theObj); }
149
150   /// Returns true if the Map contains the presentation
151   /// \param theAIS a presentation
152   bool contains(const AISObjectPtr& theAIS) const
153   {
154     Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
155     return myAIStoResultMap.contains(anAIS);
156   }
157
158 private:
159   QMap<ObjectPtr, Handle(AIS_InteractiveObject)> myResultToAISMap;
160   QMap<Handle(AIS_InteractiveObject), ObjectPtr> myAIStoResultMap;
161 };
162
163
164 /**\class XGUI_Displayer
165  * \ingroup GUI
166  * \brief Displayer. Provides mechanizm of display/erase of objects in the viewer
167  */
168 class XGUI_EXPORT XGUI_Displayer: public QObject
169 {
170   Q_OBJECT
171  public:
172    /// \enum DisplayMode display mode
173    enum DisplayMode {
174      NoMode = -1, ///< Mode is not defined
175      Wireframe, ///< Wireframe display mode
176      Shading ///< Shading display mode
177    };
178
179   /// Constructor
180   /// \param theWorkshop a workshop instance
181   XGUI_Displayer(XGUI_Workshop* theWorkshop);
182
183   /// Destructor
184   virtual ~XGUI_Displayer();
185
186   /// Returns the feature visibility state.
187   /// \param theObject an object instance
188   bool isVisible(ObjectPtr theObject) const;
189
190   /// Display the feature. Obtain the visualized object from the feature.
191   /// \param theObject an object to display
192   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
193   /// \return true if the object visibility state is changed
194   bool display(ObjectPtr theObject, bool theUpdateViewer = true);
195
196   /// Display the given AIS object.
197   /// This object is not added to the displayer internal map of objects
198   /// So, it can not be obtained from displayer. This is just a wrap method of OCC display in
199   /// order to perform the display with correct flags.
200   /// \param theAIS AIOS object to display
201   /// \param toActivateInSelectionModes boolean value whether the presentation should be
202   /// activated in the current selection modes
203   /// \param theDisplayMode mode how the presentation should be displayed
204   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
205   /// \return true if the object visibility state is changed
206   bool displayAIS(AISObjectPtr theAIS, const bool toActivateInSelectionModes,
207                   const Standard_Integer theDisplayMode = 0, bool theUpdateViewer = true);
208
209   /// Redisplay the shape if it was displayed
210   /// \param theObject an object instance
211   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
212   /// \return true if the object visibility state is changed
213   bool redisplay(ObjectPtr theObject, bool theUpdateViewer = true);
214
215   /// Sends and flushes a signal to redisplay all visualized objects.
216   void redisplayObjects();
217
218   /// Add presentations to current selection. It unhighlight and deselect the current selection.
219   /// The shape and result components are processed in the values. If the presentation shape is not
220   /// empty, select it, otherwise select the result.
221   /// \param theValues a list of presentation to be selected
222   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
223   void setSelected(const  QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
224                    bool theUpdateViewer = true);
225
226   /// Unselect all objects
227   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
228   /// \param theUpdateViewer the parameter to update viewer
229   void clearSelected(const bool theUpdateViewer = true);
230
231   /// Erase the feature and a shape.
232   /// \param theObject an object instance
233   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
234   /// \return true if the object visibility state is changed
235   bool erase(ObjectPtr theObject, const bool theUpdateViewer = true);
236
237   /// Erase the given AIS object displayed by corresponded display method
238   /// \param theAIS instance of AIS object
239   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
240   /// \return true if the object visibility state is changed
241   bool eraseAIS(AISObjectPtr theAIS, const bool theUpdateViewer = true);
242
243   /// Erase all presentations
244   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
245   /// \return true if the object visibility state is changed
246   bool eraseAll(const bool theUpdateViewer = true);
247
248   /// Remove default selection filters of the module from the current viewer
249   /// \param theAddFilterOnly if is not 'true' it will deactivate all fiters in viewer
250   void deactivateSelectionFilters(const bool theAddFilterOnly = true);
251
252   /// \brief Add selection filter
253   /// \param theFilter a filter instance
254   void addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
255
256   /// \brief Remove selection filter
257   /// \param theFilter a filter instance
258   void removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
259
260   /// Returns true if the selection filter is set to the viewer
261   /// \param theFilter a selection filter
262   virtual bool hasSelectionFilter(const Handle(SelectMgr_Filter)& theFilter);
263
264   /// Remove all selection filters
265   void removeFilters();
266
267   /// Sets a flag to the displayer whether the internal viewer can be updated by
268   /// the updateViewer method call. If it is not enabled, this method do nothing.
269   /// This state maintain recurse, if the update is blocked twice or three times, the
270   /// viewer will not be updated until it is unblocked necessary times
271   /// (twice or three in the example).
272   /// \param isEnabled a boolean value
273   bool enableUpdateViewer(const bool isEnabled);
274
275   /// Returns true if the viewer update is not blocked
276   bool isUpdateEnabled() const;
277
278   /// Updates the viewer
279   void updateViewer() const;
280
281   /// Searches the interactive object by feature
282   /// \param theObject the object or presentable feature
283   /// \return theIO an interactive object
284   AISObjectPtr getAISObject(ObjectPtr theObject) const;
285
286   /// Searches the feature by interactive object
287   /// \param theIO an interactive object
288   /// \return feature the feature or NULL if it not visualized
289   ObjectPtr getObject(const AISObjectPtr& theIO) const;
290
291   /// Searches the feature by interactive object
292   /// \param theIO an interactive object
293   /// \return corresponded object or NULL if it not found
294   ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
295
296   /// Deactivates the given objects (not allow selection)
297   /// \param theObjList - list of objects which has to be deactivated.
298   /// \param theUpdateViewer update viewer flag
299   void deactivateObjects(const QObjectPtrList& theObjList,
300                          const bool theUpdateViewer = true);
301
302   /// Sets display mode for the given object if this object is displayed
303   void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
304
305   /// Returns current display mode for the given object.
306   /// If object is not dis played then returns NoMode.
307   /// \param theObject object to check
308   DisplayMode displayMode(ObjectPtr theObject) const;
309
310   /// Displays only objects listed in the list
311   /// \param theList list of objects
312   void showOnly(const QObjectPtrList& theList);
313
314   /// Returns number of displayed objects
315   int objectsCount() const { return myResult2AISObjectMap.size(); }
316
317   /// Returns list of displayed objects
318   QObjectPtrList displayedObjects() const { return myResult2AISObjectMap.objects(); }
319
320   /// Returns list of displayed objects
321   QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
322   {
323     return myResult2AISObjectMap.presentations();
324   }
325
326   /// Returns true if the given object can be shown in shaded mode
327   /// \param theObject object to check
328   bool canBeShaded(ObjectPtr theObject) const;
329
330   /// Set color on presentation of an object if it is displayed
331   /// \param theObject an object
332   /// \param theColor a color which has to be set
333   /// \param theUpdateViewer update viewer flag
334   /// \return previously defined color on the object
335   QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
336
337   /// Displays/erases thrihedron in current modes. It will be activated or deactivated
338   /// depending on the trihedron visible state and displayer active trihedron state
339   void displayTrihedron(bool theToDisplay) const;
340
341 #ifdef TINSPECTOR
342   void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
343     { myVCallBack = theCallBack; }
344   Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
345 #endif
346
347   /// Return true if the object is visible. If the object is feature, it returns true
348   /// if all results of the feature are shown
349   /// \param theDisplayer a displayer
350   /// \param theObject an object
351   /// \return a boolean value
352   static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
353
354 signals:
355   /// Signal on object display
356   /// \param theObject a data object
357   /// \param theAIS a presentation object
358   void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
359
360   /// Signal on before object erase
361   /// \param theObject a data object
362   /// \param theAIS a presentation object
363   void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
364
365  protected:
366   /// Returns currently installed AIS_InteractiveContext
367   Handle(AIS_InteractiveContext) AISContext() const;
368
369   /// Returns the viewer context top filter. If there is no a filter, it is created and set into
370   /// The context should have only this filter inside. Other filters should be add to the filter
371   Handle(SelectMgr_AndFilter) GetFilter();
372
373   /// Display the feature and a shape. This shape would be associated to the given feature
374   /// \param theObject an object instance
375   /// \param theAIS AIS presentation
376   /// \param isShading flag to show in shading mode
377   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
378   /// \return true if the object visibility state is changed
379   bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
380                bool theUpdateViewer = true);
381
382 private:
383   /// Update the object presentable properties such as color, lines width and other
384   /// If the object is result with the color attribute value set, it is used,
385   /// otherwise the customize is applyed to the object's feature if it is a custom prs
386   /// \param theObject an object instance
387   /// \return the true state if there is changes and the presentation is customized
388   bool customizeObject(ObjectPtr theObject);
389
390   /// Append the objects in the internal map. Checks whether the map already contains the object
391   /// \param theObject an object to display
392   /// \param theAIS AIOS object to display
393   void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
394
395 #ifdef _DEBUG
396   /// Returns an information about alredy displayed objects
397   /// \return a string representation
398   std::string getResult2AISObjectMapInfo() const;
399 #endif
400
401   /// Returns container of visible presentations for the object. For a feature object,
402   /// the feature results are processed also. The presentations map is not cleared inside.
403   /// \param theObject a feature or result
404   /// \param thePresentations result map of presentations
405   void getPresentations(const ObjectPtr& theObject,
406                         NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
407
408   /// Sets the shapes selected in the context. It contains logic of the similar method
409   /// in OCCT but improved for performance. The modification is to iterates by a list
410   /// of owners in the context only once.
411   /// \param theContext a viewer context. It has opened local context
412   /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
413   /// owner is selected if it is found there.
414   /// Only first owner is processed(according to OCCT logic)
415   static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
416     const NCollection_DataMap<TopoDS_Shape,
417       NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
418
419 protected:
420   XGUI_SelectionActivate* selectionActivate() const;
421
422 protected:
423   XGUI_Workshop* myWorkshop; ///< Reference to workshop
424 #ifdef TINSPECTOR
425   Handle(VInspectorAPI_CallBack) myVCallBack;
426 #endif
427   Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
428
429   /// A default custom presentation, which is used if the displayed feature is not
430   /// a custom presentation
431   GeomCustomPrsPtr myCustomPrs;
432
433   /// Definition of a type of map which defines correspondance between objects and presentations
434   XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
435
436   /// Number of blocking of the viewer update. The viewer is updated only if it is zero
437   int myViewerBlockedRecursiveCount;
438
439   bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
440   mutable bool myNeedUpdate; ///< A flag that update was requested but not done
441 };
442
443 #endif