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