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