]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.h
Salome HOME
Fix for formula definition with '=' in an integer widget
[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 #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   /// Updates the viewer
282   void updateViewer() const;
283
284   /// Searches the interactive object by feature
285   /// \param theObject the object or presentable feature
286   /// \return theIO an interactive object
287   AISObjectPtr getAISObject(ObjectPtr theObject) const;
288
289   /// Searches the feature by interactive object
290   /// \param theIO an interactive object
291   /// \return feature the feature or NULL if it not visualized
292   ObjectPtr getObject(const AISObjectPtr& theIO) const;
293
294   /// Searches the feature by interactive object
295   /// \param theIO an interactive object
296   /// \return corresponded object or NULL if it not found
297   ObjectPtr getObject(const Handle(AIS_InteractiveObject)& theIO) const;
298
299   /// Deactivates the given objects (not allow selection)
300   /// \param theObjList - list of objects which has to be deactivated.
301   /// \param theUpdateViewer update viewer flag
302   void deactivateObjects(const QObjectPtrList& theObjList,
303     const bool theUpdateViewer = true);
304
305   /// Sets display mode for the given object if this object is displayed
306   void setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool theUpdateViewer = true);
307
308   /// Returns current display mode for the given object.
309   /// If object is not dis played then returns NoMode.
310   /// \param theObject object to check
311   DisplayMode displayMode(ObjectPtr theObject) const;
312
313   /// Displays only objects listed in the list
314   /// \param theList list of objects
315   void showOnly(const QObjectPtrList& theList);
316
317   /// Returns number of displayed objects
318   int objectsCount() const { return myResult2AISObjectMap.size(); }
319
320   /// Returns list of displayed objects
321   QObjectPtrList displayedObjects() const {
322 #ifdef OPTIMIZE_PRS
323     return myResult2AISObjectMap.objects();
324 #else
325     return myResult2AISObjectMap.keys();
326 #endif
327   }
328
329   /// Returns list of displayed objects
330 #ifdef OPTIMIZE_PRS
331   QList<Handle(AIS_InteractiveObject)> displayedPresentations() const
332   {
333     return myResult2AISObjectMap.presentations();
334   }
335 #else
336   QList<AISObjectPtr> displayedPresentations() const
337   {
338     return myResult2AISObjectMap.values();
339   }
340 #endif
341
342   /// Returns true if the given object can be shown in shaded mode
343   /// \param theObject object to check
344   bool canBeShaded(ObjectPtr theObject) const;
345
346   /// Set color on presentation of an object if it is displayed
347   /// \param theObject an object
348   /// \param theColor a color which has to be set
349   /// \param theUpdateViewer update viewer flag
350   /// \return previously defined color on the object
351   QColor setObjectColor(ObjectPtr theObject, const QColor& theColor, bool theUpdateViewer = true);
352
353   /// Displays/erases thrihedron in current modes. It will be activated or deactivated
354   /// depending on the trihedron visible state and displayer active trihedron state
355   void displayTrihedron(bool theToDisplay) const;
356
357 #ifdef TINSPECTOR
358   void setCallBack(const Handle(VInspectorAPI_CallBack)& theCallBack)
359     { myVCallBack = theCallBack; }
360   Handle(VInspectorAPI_CallBack) getCallBack() const { return myVCallBack; }
361 #endif
362
363   /// Return true if the object is visible. If the object is feature, it returns true
364   /// if all results of the feature are shown
365   /// \param theDisplayer a displayer
366   /// \param theObject an object
367   /// \return a boolean value
368   static bool isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& theObject);
369
370
371   /// Returns screen plane of active view
372   GeomPlanePtr getScreenPlane() const;
373
374   /// Returns scale of active view
375   double getViewScale() const;
376
377 signals:
378   /// Signal on object display
379   /// \param theObject a data object
380   /// \param theAIS a presentation object
381   void objectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS);
382
383   /// Signal on before object erase
384   /// \param theObject a data object
385   /// \param theAIS a presentation object
386   void beforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS);
387
388  protected:
389   /// Returns currently installed AIS_InteractiveContext
390   Handle(AIS_InteractiveContext) AISContext() const;
391
392   /// Returns the viewer context top filter. If there is no a filter, it is created and set into
393   /// The context should have only this filter inside. Other filters should be add to the filter
394   Handle(SelectMgr_AndFilter) GetFilter();
395
396   /// Display the feature and a shape. This shape would be associated to the given feature
397   /// \param theObject an object instance
398   /// \param theAIS AIS presentation
399   /// \param isShading flag to show in shading mode
400   /// \param theUpdateViewer the parameter whether the viewer should be update immediatelly
401   /// \return true if the object visibility state is changed
402   bool display(ObjectPtr theObject, AISObjectPtr theAIS, bool isShading,
403                bool theUpdateViewer = true);
404
405 private:
406   /// Update the object presentable properties such as color, lines width and other
407   /// If the object is result with the color attribute value set, it is used,
408   /// otherwise the customize is applyed to the object's feature if it is a custom prs
409   /// \param theObject an object instance
410   /// \return the true state if there is changes and the presentation is customized
411   bool customizeObject(ObjectPtr theObject);
412
413   /// Append the objects in the internal map. Checks whether the map already contains the object
414   /// \param theObject an object to display
415   /// \param theAIS AIOS object to display
416   void appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS);
417
418 #ifdef _DEBUG
419   /// Returns an information about alredy displayed objects
420   /// \return a string representation
421   std::string getResult2AISObjectMapInfo() const;
422 #endif
423
424   /// Returns container of visible presentations for the object. For a feature object,
425   /// the feature results are processed also. The presentations map is not cleared inside.
426   /// \param theObject a feature or result
427   /// \param thePresentations result map of presentations
428   void getPresentations(const ObjectPtr& theObject,
429                         NCollection_Map<Handle(AIS_InteractiveObject)>& thePresentations);
430
431   /// Sets the shapes selected in the context. It contains logic of the similar method
432   /// in OCCT but improved for performance. The modification is to iterates by a list
433   /// of owners in the context only once.
434   /// \param theContext a viewer context. It has opened local context
435   /// \param theShapesToBeSelected a map of shapes. Owner's shape is searched in the map and the
436   /// owner is selected if it is found there.
437   /// Only first owner is processed(according to OCCT logic)
438   static void AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) theContext,
439     const NCollection_DataMap<TopoDS_Shape,
440       NCollection_Map<Handle(AIS_InteractiveObject)>>& theShapesToBeSelected);
441
442 protected:
443   XGUI_SelectionActivate* selectionActivate() const;
444
445 protected:
446   XGUI_Workshop* myWorkshop; ///< Reference to workshop
447 #ifdef TINSPECTOR
448   Handle(VInspectorAPI_CallBack) myVCallBack;
449 #endif
450   Handle(SelectMgr_AndFilter) myAndFilter; ///< A container for selection filters
451
452   /// A default custom presentation, which is used if the displayed feature is not
453   /// a custom presentation
454   GeomCustomPrsPtr myCustomPrs;
455
456   /// Definition of a type of map which defines correspondance between objects and presentations
457 #ifdef OPTIMIZE_PRS
458   XGUI_TwoSidePresentationMap myResult2AISObjectMap; ///< A map of displayed objects
459 #else
460   typedef QMap<ObjectPtr, AISObjectPtr> ResultToAISMap;
461   ResultToAISMap myResult2AISObjectMap; ///< A map of displayed objects
462 #endif
463
464   /// Number of blocking of the viewer update. The viewer is updated only if it is zero
465   int myViewerBlockedRecursiveCount;
466
467   bool myIsFirstAISContextUse; ///< Flag: first asking of AIS context: trihedron activation
468   mutable bool myNeedUpdate; ///< A flag that update was requested but not done
469 };
470
471 #endif