1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkLegendBoxActor.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkLegendBoxActor
17  * @brief   draw symbols with text
18  *
19  * vtkLegendBoxActor is used to associate a symbol with a text string.
20  * The user specifies a vtkPolyData to use as the symbol, and a string
21  * associated with the symbol. The actor can then be placed in the scene
22  * in the same way that any other vtkActor2D can be used.
23  *
24  * To use this class, you must define the position of the legend box by using
25  * the superclasses' vtkActor2D::Position coordinate and
26  * Position2 coordinate. Then define the set of symbols and text strings that
27  * make up the menu box. The font attributes of the entries can be set through
28  * the vtkTextProperty associated to this actor. The class will
29  * scale the symbols and text to fit in the legend box defined by
30  * (Position,Position2). Optional features like turning on a border line and
31  * setting the spacing between the border and the symbols/text can also be
32  * set.
33  *
34  * @sa
35  * vtkXYPlotActor vtkActor2D vtkGlyphSource2D
36 */
37 
38 #ifndef vtkLegendBoxActor_h
39 #define vtkLegendBoxActor_h
40 
41 #include "vtkRenderingAnnotationModule.h" // For export macro
42 #include "vtkActor2D.h"
43 
44 class vtkActor;
45 class vtkDoubleArray;
46 class vtkImageData;
47 class vtkPolyData;
48 class vtkPolyDataMapper2D;
49 class vtkPolyDataMapper;
50 class vtkPlaneSource;
51 class vtkTextMapper;
52 class vtkTextProperty;
53 class vtkTexturedActor2D;
54 class vtkTransform;
55 class vtkTransformPolyDataFilter;
56 class vtkProperty2D;
57 
58 class VTKRENDERINGANNOTATION_EXPORT vtkLegendBoxActor : public vtkActor2D
59 {
60 public:
61   vtkTypeMacro(vtkLegendBoxActor,vtkActor2D);
62   void PrintSelf(ostream& os, vtkIndent indent) override;
63 
64   /**
65    * Instantiate object with a rectangle in normaled view coordinates
66    * of (0.2,0.85, 0.8, 0.95).
67    */
68   static vtkLegendBoxActor *New();
69 
70   /**
71    * Specify the number of entries in the legend box.
72    */
73   void SetNumberOfEntries(int num);
GetNumberOfEntries()74   int GetNumberOfEntries()
75     {return this->NumberOfEntries;}
76 
77   //@{
78   /**
79    * Add an entry to the legend box. You must supply a vtkPolyData to be
80    * used as a symbol (it can be NULL) and a text string (which also can
81    * be NULL). The vtkPolyData is assumed to be defined in the x-y plane,
82    * and the text is assumed to be a single line in height. Note that when
83    * this method is invoked previous entries are deleted. Also supply a text
84    * string and optionally a color. (If a color is not specified, then the
85    * entry color is the same as this actor's color.) (Note: use the set
86    * methods when you use SetNumberOfEntries().)
87    */
88   void SetEntry(int i, vtkPolyData *symbol, const char* string, double color[3]);
89   void SetEntry(int i, vtkImageData *symbol, const char* string, double color[3]);
90   void SetEntry(int i, vtkPolyData *symbol, vtkImageData *icon,
91                 const char* string, double color[3]);
92   //@}
93 
94   void SetEntrySymbol (int i, vtkPolyData *symbol);
95   void SetEntryIcon   (int i, vtkImageData *icon);
96   void SetEntryString (int i, const char* string);
97   void SetEntryColor  (int i, double color[3]);
98   void SetEntryColor  (int i, double r, double g, double b);
99 
100   vtkPolyData*  GetEntrySymbol(int i);
101   vtkImageData* GetEntryIcon(int i);
102   const char*   GetEntryString(int i);
103   double*       GetEntryColor(int i) VTK_SIZEHINT(3);
104 
105   //@{
106   /**
107    * Set/Get the text property.
108    */
109   virtual void SetEntryTextProperty(vtkTextProperty *p);
110   vtkGetObjectMacro(EntryTextProperty,vtkTextProperty);
111   //@}
112 
113   //@{
114   /**
115    * Set/Get the flag that controls whether a border will be drawn
116    * around the legend box.
117    */
118   vtkSetMacro(Border, vtkTypeBool);
119   vtkGetMacro(Border, vtkTypeBool);
120   vtkBooleanMacro(Border, vtkTypeBool);
121   //@}
122 
123   //@{
124   /**
125    * Set/Get the flag that controls whether the border and legend
126    * placement is locked into the rectangle defined by (Position,Position2).
127    * If off, then the legend box will adjust its size so that the border
128    * fits nicely around the text and symbols. (The ivar is off by default.)
129    * Note: the legend box is guaranteed to lie within the original border
130    * definition.
131    */
132   vtkSetMacro(LockBorder, vtkTypeBool);
133   vtkGetMacro(LockBorder, vtkTypeBool);
134   vtkBooleanMacro(LockBorder, vtkTypeBool);
135   //@}
136 
137   //@{
138   /**
139    * Set/Get the flag that controls whether a box will be drawn/filled
140    * corresponding to the legend box.
141    */
142   vtkSetMacro(Box, vtkTypeBool);
143   vtkGetMacro(Box, vtkTypeBool);
144   vtkBooleanMacro(Box, vtkTypeBool);
145   //@}
146 
147   /**
148    * Get the box vtkProperty2D.
149    */
GetBoxProperty()150   vtkProperty2D* GetBoxProperty() { return this->BoxActor->GetProperty(); };
151 
152   //@{
153   /**
154    * Set/Get the padding between the legend entries and the border. The value
155    * is specified in pixels.
156    */
157   vtkSetClampMacro(Padding, int, 0, 50);
158   vtkGetMacro(Padding, int);
159   //@}
160 
161   //@{
162   /**
163    * Turn on/off flag to control whether the symbol's scalar data
164    * is used to color the symbol. If off, the color of the
165    * vtkLegendBoxActor is used.
166    */
167   vtkSetMacro(ScalarVisibility,vtkTypeBool);
168   vtkGetMacro(ScalarVisibility,vtkTypeBool);
169   vtkBooleanMacro(ScalarVisibility,vtkTypeBool);
170   //@}
171 
172   //@{
173   /**
174    * Turn on/off background.
175    */
176   vtkSetMacro(UseBackground, vtkTypeBool);
177   vtkGetMacro(UseBackground, vtkTypeBool);
178   vtkBooleanMacro(UseBackground, vtkTypeBool);
179   //@}
180 
181   //@{
182   /**
183    * Set/Get background color.
184    * Default is: (0.3, 0.3, 0.3).
185    */
186   vtkSetVector3Macro(BackgroundColor, double);
187   vtkGetVector3Macro(BackgroundColor, double);
188   //@}
189 
190   //@{
191   /**
192    * Set/Get background opacity.
193    * Default is: 1.0
194    */
195   vtkSetClampMacro(BackgroundOpacity, double, 0.0, 1.0);
196   vtkGetMacro(BackgroundOpacity, double);
197   //@}
198 
199   /**
200    * Shallow copy of this scaled text actor. Overloads the virtual
201    * vtkProp method.
202    */
203   void ShallowCopy(vtkProp *prop) override;
204 
205   /**
206    * WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE
207    * DO NOT USE THIS METHOD OUTSIDE OF THE RENDERING PROCESS.
208    * Release any graphics resources that are being consumed by this actor.
209    * The parameter window could be used to determine which graphic
210    * resources to release.
211    */
212   void ReleaseGraphicsResources(vtkWindow *) override;
213 
214   //@{
215   /**
216    * WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE
217    * DO NOT USE THIS METHOD OUTSIDE OF THE RENDERING PROCESS.
218    * Draw the legend box to the screen.
219    */
220   int RenderOpaqueGeometry(vtkViewport* viewport) override;
RenderTranslucentPolygonalGeometry(vtkViewport *)221   int RenderTranslucentPolygonalGeometry(vtkViewport* ) override {return 0;};
222   int RenderOverlay(vtkViewport* viewport) override;
223   //@}
224 
225   /**
226    * Does this prop have some translucent polygonal geometry?
227    */
228   vtkTypeBool HasTranslucentPolygonalGeometry() override;
229 
230 protected:
231   vtkLegendBoxActor();
232   ~vtkLegendBoxActor() override;
233 
234   void InitializeEntries();
235 
236   vtkPolyData createTexturedPlane();
237 
238   vtkTypeBool   Border;
239   vtkTypeBool   Box;
240   int   Padding;
241   vtkTypeBool   LockBorder;
242   vtkTypeBool   ScalarVisibility;
243   double BoxOpacity;
244 
245   // Internal actors, mappers, data to represent the legend
246   int                        NumberOfEntries;
247   int                        Size; //allocation size
248   vtkDoubleArray              *Colors;
249   vtkTextMapper              **TextMapper;
250   vtkActor2D                 **TextActor;
251 
252   vtkPolyData                **Symbol;
253   vtkTransform               **Transform;
254   vtkTransformPolyDataFilter **SymbolTransform;
255   vtkPolyDataMapper2D        **SymbolMapper;
256   vtkActor2D                 **SymbolActor;
257 
258   vtkPlaneSource             **Icon;
259   vtkTransform               **IconTransform;
260   vtkTransformPolyDataFilter **IconTransformFilter;
261   vtkPolyDataMapper2D        **IconMapper;
262   vtkTexturedActor2D         **IconActor;
263   vtkImageData               **IconImage;
264 
265   vtkPolyData                *BorderPolyData;
266   vtkPolyDataMapper2D        *BorderMapper;
267   vtkActor2D                 *BorderActor;
268   vtkPolyData                *BoxPolyData;
269   vtkPolyDataMapper2D        *BoxMapper;
270   vtkActor2D                 *BoxActor;
271   vtkTextProperty            *EntryTextProperty;
272 
273   // Background plane.
274   vtkTypeBool                         UseBackground;
275   double                      BackgroundOpacity;
276   double                      BackgroundColor[3];
277   vtkPlaneSource             *Background;
278 
279   // May use texture.
280   vtkTexturedActor2D         *BackgroundActor;
281   vtkPolyDataMapper2D        *BackgroundMapper;
282 
283   // Used to control whether the stuff is recomputed
284   int           LegendEntriesVisible;
285   int           CachedSize[2];
286   vtkTimeStamp  BuildTime;
287 
288 private:
289   vtkLegendBoxActor(const vtkLegendBoxActor&) = delete;
290   void operator=(const vtkLegendBoxActor&) = delete;
291 };
292 
293 
294 #endif
295 
296