vtkContextArea.h 9.2 KB
Newer Older
David C. Lonie's avatar
David C. Lonie committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkContextArea.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * @class   vtkContextArea
 * @brief   Clipped, transformed area with axes for context items.
 *
 *
 * vtkContextArea provides an clipped drawing area surrounded by four axes.
 * The drawing area is transformed to map the 2D area described by
 * DrawAreaBounds into pixel coordinates. DrawAreaBounds is also used to
 * configure the axes. Item to be rendered in the draw area should be added
 * to the context item returned by GetDrawAreaItem().
 *
 * The size and shape of the draw area is configured by the following member
 * variables:
 * - Geometry: The rect (pixel coordinates) defining the location of the context
 *   area in the scene. This includes the draw area and axis ticks/labels.
 * - FillViewport: If true (default), Geometry is set to span the size returned
 *   by vtkContextDevice2D::GetViewportSize().
 * - DrawAreaResizeBehavior: Controls how the draw area should be shaped.
 *   Available options: Expand (default), FixedAspect, FixedRect, FixedMargins.
 * - FixedAspect: Aspect ratio to enforce for FixedAspect resize behavior.
 * - FixedRect: Rect used to enforce for FixedRect resize behavior.
 * - FixedMargins: Margins to enforce for FixedMargins resize behavior.
*/
David C. Lonie's avatar
David C. Lonie committed
39

40 41
#ifndef vtkContextArea_h
#define vtkContextArea_h
David C. Lonie's avatar
David C. Lonie committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

#include "vtkAbstractContextItem.h"

#include "vtkAxis.h" // For enums
#include "vtkChartsCoreModule.h" // For export macro
#include "vtkRect.h" // For vtkRect/vtkVector/vtkTuple
#include "vtkNew.h" // For vtkNew

class vtkContextClip;
class vtkContextTransform;
class vtkPlotGrid;

class VTKCHARTSCORE_EXPORT vtkContextArea: public vtkAbstractContextItem
{
public:
  typedef vtkTuple<int, 4> Margins;
  vtkTypeMacro(vtkContextArea, vtkAbstractContextItem)
59
  void PrintSelf(ostream &os, vtkIndent indent) override;
David C. Lonie's avatar
David C. Lonie committed
60 61 62

  static vtkContextArea *New();

63 64 65
  /**
   * Get the vtkAxis associated with the specified location.
   */
David C. Lonie's avatar
David C. Lonie committed
66 67
  vtkAxis* GetAxis(vtkAxis::Location location);

68 69 70 71
  /**
   * Returns the vtkAbstractContextItem that will draw in the clipped,
   * transformed space. This is the item to add children for.
   */
David C. Lonie's avatar
David C. Lonie committed
72 73
  vtkAbstractContextItem* GetDrawAreaItem();

74 75 76
  /**
   * Paint event for the item, called whenever the item needs to be drawn.
   */
77
  bool Paint(vtkContext2D *painter) override;
David C. Lonie's avatar
David C. Lonie committed
78

79 80 81 82 83 84
  //@{
  /**
   * The rect defining the pixel location and size of the entire vtkContextArea,
   * including axis labels, title, etc. Note that this will be updated to the
   * window geometry if FillWindow is true.
   */
David C. Lonie's avatar
David C. Lonie committed
85 86
  vtkGetMacro(Geometry, vtkRecti)
  vtkSetMacro(Geometry, vtkRecti)
87
  //@}
David C. Lonie's avatar
David C. Lonie committed
88

89 90 91 92 93
  //@{
  /**
   * The data bounds of the clipped and transformed area inside of the axes.
   * This is used to configure the axes labels and setup the transform.
   */
David C. Lonie's avatar
David C. Lonie committed
94 95
  vtkGetMacro(DrawAreaBounds, vtkRectd)
  vtkSetMacro(DrawAreaBounds, vtkRectd)
96
  //@}
David C. Lonie's avatar
David C. Lonie committed
97 98 99 100 101 102 103 104

  enum DrawAreaResizeBehaviorType {
    DARB_Expand,
    DARB_FixedAspect,
    DARB_FixedRect,
    DARB_FixedMargins
  };

105 106 107 108 109 110
  //@{
  /**
   * Set the resize behavior for the draw area:
   * - @a Expand: The default behavior. The draw area will automatically resize
   * to take up as much of @a Geometry as possible. Margin sizes are
   * minimized based on the space required for axis labels/tick marks.
luz.paz's avatar
luz.paz committed
111
   * - FixedAspect: Same as Expand, but a fixed aspect ratio is enforced.
112 113 114 115 116 117 118
   * See SetFixedAspect.
   * - FixedRect: Draw area is always constrained to a fixed rectangle.
   * See SetFixedRect.
   * - FixMargins: The draw area expands to fill @a Geometry, but margins
   * (axis labels, etc) are fixed, rather than dynamically sized.
   * See SetFixedMargins.
   */
David C. Lonie's avatar
David C. Lonie committed
119 120
  vtkGetMacro(DrawAreaResizeBehavior, DrawAreaResizeBehaviorType)
  vtkSetMacro(DrawAreaResizeBehavior, DrawAreaResizeBehaviorType)
121 122 123 124 125 126 127 128 129
  //@}

  //@{
  /**
   * The fixed aspect ratio, if DrawAreaResizeBehavior is FixedAspect.
   * Defined as width/height. Default is 1.
   * Setting the aspect ratio will also set DrawAreaResizeBehavior to
   * FixedAspect.
   */
David C. Lonie's avatar
David C. Lonie committed
130 131
  vtkGetMacro(FixedAspect, float)
  virtual void SetFixedAspect(float aspect);
132 133 134 135 136 137 138 139 140
  //@}

  //@{
  /**
   * The fixed rect to use for the draw area, if DrawAreaResizeBehavior is
   * FixedRect. Units are in pixels, default is 300x300+0+0.
   * Setting the fixed rect will also set DrawAreaResizeBehavior to
   * FixedRect.
   */
David C. Lonie's avatar
David C. Lonie committed
141 142 143
  vtkGetMacro(FixedRect, vtkRecti)
  virtual void SetFixedRect(vtkRecti rect);
  virtual void SetFixedRect(int x, int y, int width, int height);
144 145 146 147 148 149 150 151 152 153
  //@}

  //@{
  /**
   * The left, right, bottom, and top margins for the draw area, if
   * DrawAreaResizeBehavior is FixedMargins. Units are in pixels, default is
   * { 0, 0, 0, 0 }.
   * Setting the fixed margins will also set DrawAreaResizeBehavior to
   * FixedMargins.
   */
David C. Lonie's avatar
David C. Lonie committed
154 155 156 157 158 159
  virtual const Margins& GetFixedMargins() { return this->FixedMargins; }
  virtual void GetFixedMarginsArray(int margins[4]);
  virtual const int* GetFixedMarginsArray();
  virtual void SetFixedMargins(Margins margins);
  virtual void SetFixedMargins(int margins[4]);
  virtual void SetFixedMargins(int left, int right, int bottom, int top);
160 161 162 163 164 165 166 167
  //@}

  //@{
  /**
   * If true, Geometry is set to (0, 0, vpSize[0], vpSize[1]) at the start
   * of each Paint call. vpSize is vtkContextDevice2D::GetViewportSize. Default
   * is true.
   */
David C. Lonie's avatar
David C. Lonie committed
168 169 170
  vtkGetMacro(FillViewport, bool)
  vtkSetMacro(FillViewport, bool)
  vtkBooleanMacro(FillViewport, bool)
171
  //@}
David C. Lonie's avatar
David C. Lonie committed
172

173 174 175 176
  //@{
  /**
   * Turn on/off grid visibility.
   */
David C. Lonie's avatar
David C. Lonie committed
177 178 179 180
  virtual void SetShowGrid(bool show);
  virtual bool GetShowGrid();
  virtual void ShowGridOn() { this->SetShowGrid(true); }
  virtual void ShowGridOff() { this->SetShowGrid(false); }
181
  //@}
David C. Lonie's avatar
David C. Lonie committed
182 183 184

protected:
  vtkContextArea();
185
  ~vtkContextArea() override;
David C. Lonie's avatar
David C. Lonie committed
186

187 188 189 190 191
  /**
   * Sync the Axes locations with Geometry, and update the DrawAreaGeometry
   * to account for Axes size (margins). Must be called while the painter
   * is active.
   */
David C. Lonie's avatar
David C. Lonie committed
192
  void LayoutAxes(vtkContext2D *painter);
193 194
  virtual void SetAxisRange(vtkRectd const& data);
  virtual void ComputeViewTransform();
David C. Lonie's avatar
David C. Lonie committed
195

196 197 198
  /**
   * Return the draw area's geometry.
   */
David C. Lonie's avatar
David C. Lonie committed
199 200
  vtkRecti ComputeDrawAreaGeometry(vtkContext2D *painter);

201 202 203 204
  //@{
  /**
   * Working implementations for ComputeDrawAreaGeometry.
   */
David C. Lonie's avatar
David C. Lonie committed
205 206 207 208
  vtkRecti ComputeExpandedDrawAreaGeometry(vtkContext2D *painter);
  vtkRecti ComputeFixedAspectDrawAreaGeometry(vtkContext2D *painter);
  vtkRecti ComputeFixedRectDrawAreaGeometry(vtkContext2D *painter);
  vtkRecti ComputeFixedMarginsDrawAreaGeometry(vtkContext2D *painter);
209
  //@}
David C. Lonie's avatar
David C. Lonie committed
210

211 212 213 214
  /**
   * Set the transform to map DrawAreaBounds to DrawAreaGeometry. Should be
   * called after LayoutAxes to ensure that DrawAreaGeometry is up to date.
   */
David C. Lonie's avatar
David C. Lonie committed
215 216
  void UpdateDrawArea();

217 218 219
  /**
   * vtkAxis objects that surround the draw area, indexed by vtkAxis::Location.
   */
David C. Lonie's avatar
David C. Lonie committed
220 221
  vtkTuple<vtkAxis*, 4> Axes;

222 223 224
  /**
   * The vtkPlotGrid that renders a grid atop the data in the draw area.
   */
David C. Lonie's avatar
David C. Lonie committed
225 226
  vtkNew<vtkPlotGrid> Grid;

227 228 229
  /**
   * The context item that clips rendered data.
   */
David C. Lonie's avatar
David C. Lonie committed
230 231
  vtkNew<vtkContextClip> Clip;

232 233 234
  /**
   * The context item that clips rendered data.
   */
David C. Lonie's avatar
David C. Lonie committed
235 236
  vtkNew<vtkContextTransform> Transform;

237 238 239 240
  /**
   * The rect defining the pixel location and size of the entire vtkContextArea,
   * including axis label, title, etc.
   */
David C. Lonie's avatar
David C. Lonie committed
241 242
  vtkRecti Geometry;

243 244 245 246
  /**
   * The data bounds of the clipped and transformed area inside of the axes.
   * This is used to configure the axes labels and setup the transform.
   */
David C. Lonie's avatar
David C. Lonie committed
247 248
  vtkRectd DrawAreaBounds;

249 250 251 252
  /**
   * The rect defining the pixel location and size of the clipped and
   * transformed area inside the axes. Relative to Geometry.
   */
David C. Lonie's avatar
David C. Lonie committed
253 254
  vtkRecti DrawAreaGeometry;

255 256 257
  /**
   * Controls how the draw area size is determined.
   */
David C. Lonie's avatar
David C. Lonie committed
258 259
  DrawAreaResizeBehaviorType DrawAreaResizeBehavior;

260 261 262 263
  /**
   * The fixed aspect ratio, if DrawAreaResizeBehavior is FixedAspect.
   * Defined as width/height. Default is 1.
   */
David C. Lonie's avatar
David C. Lonie committed
264 265
  float FixedAspect;

266 267 268 269
  /**
   * The fixed rect to use for the draw area, if DrawAreaResizeBehavior is
   * FixedRect. Units are in pixels, default is 300x300+0+0.
   */
David C. Lonie's avatar
David C. Lonie committed
270 271
  vtkRecti FixedRect;

272 273 274 275 276
  /**
   * The left, right, bottom, and top margins for the draw area, if
   * DrawAreaResizeBehavior is FixedMargins. Units are in pixels, default is
   * { 0, 0, 0, 0 }
   */
David C. Lonie's avatar
David C. Lonie committed
277 278
  Margins FixedMargins;

279 280 281 282 283
  /**
   * If true, Geometry is set to (0, 0, vpSize[0], vpSize[1]) at the start
   * of each Paint call. vpSize is vtkContextDevice2D::GetViewportSize. Default
   * is true.
   */
David C. Lonie's avatar
David C. Lonie committed
284 285
  bool FillViewport;

286 287 288 289
  /**
   * Initialize the drawing area's item hierarchy
   */
  virtual void InitializeDrawArea();
David C. Lonie's avatar
David C. Lonie committed
290 291 292 293 294 295

  // Smart pointers for axis lifetime management. See this->Axes.
  vtkNew<vtkAxis> TopAxis;
  vtkNew<vtkAxis> BottomAxis;
  vtkNew<vtkAxis> LeftAxis;
  vtkNew<vtkAxis> RightAxis;
296 297

private:
298 299
  vtkContextArea(const vtkContextArea &) = delete;
  void operator=(const vtkContextArea &) = delete;
David C. Lonie's avatar
David C. Lonie committed
300 301
};

302
#endif //vtkContextArea_h