vtkChartPie.cxx 8.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkChartPie.cxx

  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.

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

#include "vtkChartPie.h"

#include "vtkObjectFactory.h"

#include "vtkContext2D.h"
#include "vtkTransform2D.h"
#include "vtkContextScene.h"
23
#include "vtkContextMouseEvent.h"
24 25 26 27 28 29 30
#include "vtkPoints2D.h"

#include "vtkPlotPie.h"

#include "vtkChartLegend.h"
#include "vtkTooltipItem.h"

31
#include <sstream>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

class vtkChartPiePrivate
{
  public:
    vtkChartPiePrivate()
      {
      }

  vtkSmartPointer<vtkPlotPie> Plot;
};


//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkChartPie);

//-----------------------------------------------------------------------------
vtkChartPie::vtkChartPie()
{
  this->Legend = vtkChartLegend::New();
  this->Legend->SetChart(this);
  this->Legend->SetVisible(false);
  this->AddItem(this->Legend);
54
  this->Legend->Delete();
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

  this->Tooltip = vtkTooltipItem::New();
  this->Tooltip->SetVisible(false);

  this->Private = new vtkChartPiePrivate();
}

//-----------------------------------------------------------------------------
vtkChartPie::~vtkChartPie()
{
  this->Tooltip->Delete();
  delete this->Private;
}

//-----------------------------------------------------------------------------
void vtkChartPie::Update()
{
  if (this->Private->Plot && this->Private->Plot->GetVisible())
    {
    this->Private->Plot->Update();
    }

  this->Legend->Update();
  this->Legend->SetVisible(this->ShowLegend);
}

//-----------------------------------------------------------------------------
bool vtkChartPie::Paint(vtkContext2D *painter)
{
  // This is where everything should be drawn, or dispatched to other methods.
  vtkDebugMacro(<< "Paint event called.");

  int geometry[] = { this->GetScene()->GetSceneWidth(),
                     this->GetScene()->GetSceneHeight() };
  if (geometry[0] == 0 || geometry[1] == 0 || !this->Visible)
    {
    // The geometry of the chart must be valid before anything can be drawn
    return false;
    }

  this->Update();

  if ( geometry[0] != this->Geometry[0] || geometry[1] != this->Geometry[1] )
    {
    // Take up the entire window right now, this could be made configurable
    this->SetGeometry(geometry);
101 102 103 104 105

    vtkVector2i tileScale = this->Scene->GetLogicalTileScale();
    this->SetBorders(20 * tileScale.GetX(), 20 * tileScale.GetY(),
                     20 * tileScale.GetX(), 20 * tileScale.GetY());

106
    // Put the legend in the top corner of the chart
107
    vtkRectf rect = this->Legend->GetBoundingRect(painter);
108 109
    this->Legend->SetPoint(this->Point2[0] - rect.GetWidth(),
                           this->Point2[1] - rect.GetHeight());
110 111 112 113

    // Set the dimensions of the Plot
    if (this->Private->Plot)
      {
114 115
      this->Private->Plot->SetDimensions(20, 20, this->Geometry[0]-40,
                                         this->Geometry[1]-40);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      }
    }

  this->PaintChildren(painter);

  if (this->Title)
    {
    vtkPoints2D *rect = vtkPoints2D::New();
    rect->InsertNextPoint(this->Point1[0], this->Point2[1]);
    rect->InsertNextPoint(this->Point2[0]-this->Point1[0], 10);
    painter->ApplyTextProp(this->TitleProperties);
    painter->DrawStringRect(rect, this->Title);
    rect->Delete();
    }

  this->Tooltip->Paint(painter);

  return true;
}

136 137 138 139 140 141 142
//-----------------------------------------------------------------------------
void vtkChartPie::SetScene(vtkContextScene *scene)
{
  this->vtkAbstractContextItem::SetScene(scene);
  this->Tooltip->SetScene(scene);
}

143 144 145
//-----------------------------------------------------------------------------
vtkPlot * vtkChartPie::AddPlot(int /* type */)
{
146 147 148 149 150
  if (!this->Private->Plot)
    {
    this->Private->Plot = vtkSmartPointer<vtkPlotPie>::New();
    this->AddItem(this->Private->Plot);
    }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  return this->Private->Plot;
}

//-----------------------------------------------------------------------------
vtkPlot* vtkChartPie::GetPlot(vtkIdType index)
{
  if (index == 0)
    {
    return this->Private->Plot;
    }

  return 0;
}

//-----------------------------------------------------------------------------
vtkIdType vtkChartPie::GetNumberOfPlots()
{
  if (this->Private->Plot)
    {
    return 1;
    }
  else
    {
    return 0;
    }
}
177 178 179 180 181 182 183 184 185 186 187 188 189 190

//-----------------------------------------------------------------------------
void vtkChartPie::SetShowLegend(bool visible)
{
  this->vtkChart::SetShowLegend(visible);
  this->Legend->SetVisible(visible);
}

//-----------------------------------------------------------------------------
vtkChartLegend * vtkChartPie::GetLegend()
{
  return this->Legend;
}

191 192 193
//-----------------------------------------------------------------------------
bool vtkChartPie::Hit(const vtkContextMouseEvent &mouse)
{
194 195 196 197 198
  vtkVector2i pos(mouse.GetScreenPos());
  if (pos[0] > this->Point1[0] &&
      pos[0] < this->Point2[0] &&
      pos[1] > this->Point1[1] &&
      pos[1] < this->Point2[1])
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    {
    return true;
    }
  else
    {
    return false;
    }
}

//-----------------------------------------------------------------------------
bool vtkChartPie::MouseEnterEvent(const vtkContextMouseEvent &)
{
  return true;
}

//-----------------------------------------------------------------------------
bool vtkChartPie::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
217
  if (mouse.GetButton() == vtkContextMouseEvent::NO_BUTTON)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    {
    this->Scene->SetDirty(true);
    this->Tooltip->SetVisible(this->LocatePointInPlots(mouse));
    }

  return true;
}

//-----------------------------------------------------------------------------
bool vtkChartPie::MouseLeaveEvent(const vtkContextMouseEvent &)
{
  return true;
}

//-----------------------------------------------------------------------------
bool vtkChartPie::MouseButtonPressEvent(const vtkContextMouseEvent &/*mouse*/)
{
  return true;
}

//-----------------------------------------------------------------------------
bool vtkChartPie::MouseButtonReleaseEvent(const vtkContextMouseEvent &/*mouse*/)
{
  return true;
}

//-----------------------------------------------------------------------------
bool vtkChartPie::MouseWheelEvent(const vtkContextMouseEvent &, int /*delta*/)
{
  return true;
}

bool vtkChartPie::LocatePointInPlots(const vtkContextMouseEvent &mouse)
{
  if (!this->Private->Plot || !this->Private->Plot->GetVisible())
    {
    return false;
    }
  else
    {
    int dimensions[4];
259 260
    vtkVector2f position(mouse.GetScreenPos().Cast<float>().GetData());
    vtkVector2f tolerance(5, 5);
261
    vtkVector2f plotPos(0, 0);
262
    this->Private->Plot->GetDimensions(dimensions);
263 264 265 266 267 268

    vtkVector2i pos(mouse.GetScreenPos());
    if (pos[0] >= dimensions[0] &&
        pos[0] <= dimensions[0] + dimensions[2] &&
        pos[1] >= dimensions[1] &&
        pos[1] <= dimensions[1] + dimensions[3])
269
      {
270 271
      int labelIndex = this->Private->Plot->GetNearestPoint(position, tolerance,
                                                            &plotPos);
272 273 274
      if (labelIndex >= 0)
        {
        const char *label = this->Private->Plot->GetLabel(labelIndex);
275
        std::ostringstream ostr;
276
        ostr << label << ": " << plotPos.GetY();
277
        this->Tooltip->SetText(ostr.str().c_str());
278 279
        this->Tooltip->SetPosition(mouse.GetScreenPos()[0] + 2,
                                   mouse.GetScreenPos()[1] + 2);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        return true;
        }
      }
    }
  return false;
}

//-----------------------------------------------------------------------------
void vtkChartPie::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  if (this->Private->Plot)
    {
    os << indent << "Plot: " << endl;
    this->Private->Plot->PrintSelf(os,indent.GetNextIndent());
    }
}