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