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