1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkColorLegend.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 /**
17  * @class   vtkColorLegend
18  * @brief   Legend item to display vtkScalarsToColors.
19  *
20  * vtkColorLegend is an item that will display the vtkScalarsToColors
21  * using a 1D texture, and a vtkAxis to show both the color and numerical range.
22  */
23 
24 #ifndef vtkColorLegend_h
25 #define vtkColorLegend_h
26 
27 #include "vtkChartLegend.h"
28 #include "vtkChartsCoreModule.h" // For export macro
29 #include "vtkSmartPointer.h"     // For SP ivars
30 #include "vtkVector.h"           // For vtkRectf
31 
32 class vtkAxis;
33 class vtkContextMouseEvent;
34 class vtkImageData;
35 class vtkScalarsToColors;
36 class vtkCallbackCommand;
37 
38 class VTKCHARTSCORE_EXPORT vtkColorLegend : public vtkChartLegend
39 {
40 public:
41   vtkTypeMacro(vtkColorLegend, vtkChartLegend);
42   void PrintSelf(ostream& os, vtkIndent indent) override;
43   static vtkColorLegend* New();
44 
45   /**
46    * Enum of legend orientation types
47    */
48   enum
49   {
50     VERTICAL = 0,
51     HORIZONTAL
52   };
53 
54   /**
55    * Bounds of the item, by default (0, 1, 0, 1) but it mainly depends on the
56    * range of the vtkScalarsToColors function.
57    */
58   virtual void GetBounds(double bounds[4]);
59 
60   /**
61    * Perform any updates to the item that may be necessary before rendering.
62    * The scene should take care of calling this on all items before their
63    * Paint function is invoked.
64    */
65   void Update() override;
66 
67   /**
68    * Paint the texture into a rectangle defined by the bounds. If
69    * MaskAboveCurve is true and a shape has been provided by a subclass, it
70    * draws the texture into the shape
71    */
72   bool Paint(vtkContext2D* painter) override;
73 
74   ///@{
75   /**
76    * Set/Get the transfer function that is used to draw the scalar bar
77    * within this legend.
78    */
79   virtual void SetTransferFunction(vtkScalarsToColors* transfer);
80   virtual vtkScalarsToColors* GetTransferFunction();
81   ///@}
82 
83   /**
84    * Set the point this legend is anchored to.
85    */
86   void SetPoint(float x, float y) override;
87 
88   /**
89    * Set the size of the scalar bar drawn by this legend.
90    */
91   virtual void SetTextureSize(float w, float h);
92 
93   /**
94    * Set the origin, width, and height of the scalar bar drawn by this legend.
95    * This method overrides the anchor point, as well as any horizontal and
96    * vertical alignment that has been set for this legend.  If this is a
97    * problem for you, use SetPoint() and SetTextureSize() instead.
98    */
99   virtual void SetPosition(const vtkRectf& pos);
100 
101   /**
102    * Returns the origin, width, and height of the scalar bar drawn by this
103    * legend.
104    */
105   virtual vtkRectf GetPosition();
106 
107   /**
108    * Request the space the legend requires to be drawn. This is returned as a
109    * vtkRect4f, with the corner being the offset from Point, and the width/
110    * height being the total width/height required by the axis. In order to
111    * ensure the numbers are correct, Update() should be called first.
112    */
113   vtkRectf GetBoundingRect(vtkContext2D* painter) override;
114 
115   ///@{
116   /**
117    * Set/get the orientation of the legend.
118    * Valid orientations are VERTICAL (default) and HORIZONTAL.
119    */
120   virtual void SetOrientation(int orientation);
121   vtkGetMacro(Orientation, int);
122   ///@}
123 
124   ///@{
125   /**
126    * Get/set the title text of the legend.
127    */
128   virtual void SetTitle(const vtkStdString& title);
129   virtual vtkStdString GetTitle();
130   ///@}
131 
132   ///@{
133   /**
134    * Toggle whether or not a border should be drawn around this legend.
135    * The default behavior is to not draw a border.
136    */
137   vtkSetMacro(DrawBorder, bool);
138   vtkGetMacro(DrawBorder, bool);
139   vtkBooleanMacro(DrawBorder, bool);
140   ///@}
141 
142   /**
143    * Mouse move event.
144    */
145   bool MouseMoveEvent(const vtkContextMouseEvent& mouse) override;
146 
147 protected:
148   vtkColorLegend();
149   ~vtkColorLegend() override;
150 
151   /**
152    * Need to be reimplemented by subclasses, ComputeTexture() is called at
153    * paint time if the texture is not up to date compared to vtkColorLegend
154    */
155   virtual void ComputeTexture();
156 
157   ///@{
158   /**
159    * Called whenever the ScalarsToColors function(s) is modified. It internally
160    * calls Modified(). Can be reimplemented by subclasses.
161    */
162   virtual void ScalarsToColorsModified(vtkObject* caller, unsigned long eid, void* calldata);
163   static void OnScalarsToColorsModified(
164     vtkObject* caller, unsigned long eid, void* clientdata, void* calldata);
165   ///@}
166 
167   /**
168    * Moves the axis whenever the position of this legend changes.
169    */
170   void UpdateAxisPosition();
171 
172   vtkScalarsToColors* TransferFunction;
173   vtkSmartPointer<vtkImageData> ImageData;
174   vtkSmartPointer<vtkAxis> Axis;
175   vtkSmartPointer<vtkCallbackCommand> Callback;
176   bool Interpolate;
177   bool CustomPositionSet;
178   bool DrawBorder;
179   vtkRectf Position;
180   int Orientation;
181 
182 private:
183   vtkColorLegend(const vtkColorLegend&) = delete;
184   void operator=(const vtkColorLegend&) = delete;
185 };
186 
187 #endif
188