Isis 3 Programmer Reference
HistogramItem.cpp
1 #include "HistogramItem.h"
2 
3 #include <iostream>
4 
5 #include <QPainter>
6 #include <QScopedPointer>
7 #include <QString>
8 
9 #include <qwt_plot.h>
10 #include <qwt_painter.h>
11 #include <qwt_scale_map.h>
12 #include <qwt_series_data.h>
13 
14 namespace Isis {
15 
22  public:
23  int attributes;
24  QScopedPointer<QwtIntervalSeriesData> data;
25  QColor color;
26  double reference;
27  };
28 
35  HistogramItem::HistogramItem(const QwtText &title): QwtPlotItem() {
36  init();
37  }
38 
39 
46  HistogramItem::HistogramItem(const QString &title): QwtPlotItem(QwtText(title)) {
47  init();
48  }
49 
50 
56  delete d_data;
57  }
58 
59 
65  d_data = new PrivateData();
66  d_data->data.reset(new QwtIntervalSeriesData());
67  d_data->reference = 0.0;
68  d_data->attributes = HistogramItem::Auto;
69 
70  setItemAttribute(QwtPlotItem::AutoScale, true);
71  setItemAttribute(QwtPlotItem::Legend, true);
72 
73  setZ(20.0);
74  }
75 
76 
83  void HistogramItem::setBaseline(double reference) {
84  if(d_data->reference != reference) {
85  d_data->reference = reference;
86  itemChanged();
87  }
88  }
89 
90 
97  double HistogramItem::baseline() const {
98  return d_data->reference;
99  }
100 
101 
108  void HistogramItem::setData(const QwtIntervalSeriesData &data) {
109  d_data->data.reset(new QwtIntervalSeriesData(data));
110  itemChanged();
111  }
112 
113 
120  const QwtIntervalSeriesData &HistogramItem::data() const {
121  return *d_data->data;
122  }
123 
124 
131  void HistogramItem::setColor(const QColor &color) {
132  if(d_data->color != color) {
133  d_data->color = color;
134  itemChanged();
135  }
136  }
137 
138 
145  QColor HistogramItem::color() const {
146  return d_data->color;
147  }
148 
149 
157  QRectF rect = d_data->data->boundingRect();
158  if(!rect.isValid())
159  return rect;
160 
161  if(d_data->attributes & Xfy) {
162  rect = QRectF(rect.y(), rect.x(),
163  rect.height(), rect.width());
164 
165  if(rect.left() > d_data->reference)
166  rect.setLeft(d_data->reference);
167  else if(rect.right() < d_data->reference)
168  rect.setRight(d_data->reference);
169  }
170  else {
171  if(rect.bottom() < d_data->reference)
172  rect.setBottom(d_data->reference);
173  else if(rect.top() > d_data->reference)
174  rect.setTop(d_data->reference);
175  }
176 
177  return rect;
178  }
179 
180 
187  int HistogramItem::rtti() const {
188  return QwtPlotItem::Rtti_PlotHistogram;
189  }
190 
191 
199  void HistogramItem::setHistogramAttribute(HistogramAttribute attribute, bool on) {
200  if(bool(d_data->attributes & attribute) == on)
201  return;
202 
203  if(on)
204  d_data->attributes |= attribute;
205  else
206  d_data->attributes &= ~attribute;
207 
208  itemChanged();
209  }
210 
211 
220  bool HistogramItem::testHistogramAttribute(HistogramAttribute attribute) const {
221  return d_data->attributes & attribute;
222  }
223 
224 
233  void HistogramItem::draw(QPainter *painter, const QwtScaleMap &xMap,
234  const QwtScaleMap &yMap, const QRectF &) const {
235  const QwtIntervalSeriesData &data = *d_data->data;
236 
237  painter->setPen(QPen(d_data->color));
238 
239  const int x0 = xMap.transform(baseline());
240  const int y0 = yMap.transform(baseline());
241 
242  for(int i = 0; i < (int)data.size(); i++) {
243  if(d_data->attributes & HistogramItem::Xfy) {
244  const int x2 = xMap.transform(data.sample(i).value);
245  if(x2 == x0)
246  continue;
247 
248  int y1 = yMap.transform(data.sample(i).interval.minValue());
249  int y2 = yMap.transform(data.sample(i).interval.maxValue());
250  if(y1 > y2)
251  qSwap(y1, y2);
252 
253  if(i < (int)data.size() - 2) {
254  const int yy1 = yMap.transform(data.sample(i + 1).interval.minValue());
255  const int yy2 = yMap.transform(data.sample(i + 1).interval.maxValue());
256 
257  if(y2 == qMin(yy1, yy2)) {
258  const int xx2 = xMap.transform(
259  data.sample(i + 1).interval.minValue());
260  if(xx2 != x0 && ((xx2 < x0 && x2 < x0) ||
261  (xx2 > x0 && x2 > x0))) {
262  // One pixel distance between neighboured bars
263  y2++;
264  }
265  }
266  }
267 
268  drawBar(painter, Qt::Horizontal,
269  QRect(x0, y1, x2 - x0, y2 - y1));
270  }
271  else {
272  const int y2 = yMap.transform(data.sample(i).value);
273  if(y2 == y0)
274  continue;
275 
276  int x1 = xMap.transform(data.sample(i).interval.minValue());
277  int x2 = xMap.transform(data.sample(i).interval.maxValue());
278  if(x1 > x2)
279  qSwap(x1, x2);
280 
281  if(i < (int)data.size() - 2) {
282  const int xx1 = xMap.transform(data.sample(i + 1).interval.minValue());
283  const int xx2 = xMap.transform(data.sample(i + 1).interval.maxValue());
284 
285  if(x2 == qMin(xx1, xx2)) {
286  const int yy2 = yMap.transform(data.sample(i + 1).value);
287  if(yy2 != y0 && ((yy2 < y0 && y2 < y0) ||
288  (yy2 > y0 && y2 > y0))) {
289  // One pixel distance between neighboured bars
290  x2--;
291  }
292  }
293  }
294  drawBar(painter, Qt::Vertical,
295  QRect(x1, y0, x2 - x1, y2 - y0));
296  }
297  }
298  }
299 
300 
308  void HistogramItem::drawBar(QPainter *painter,
309  Qt::Orientation, const QRect &rect) const {
310  painter->save();
311 
312  const QColor color(painter->pen().color());
313  const QRect r = rect.normalized();
314 
315  const int factor = 125;
316  const QColor light(color.light(factor));
317  const QColor dark(color.dark(factor));
318 
319  painter->setBrush(color);
320  painter->setPen(Qt::NoPen);
321  QwtPainter::drawRect(painter, r.x() + 1, r.y() + 1,
322  r.width() - 2, r.height() - 2);
323  painter->setBrush(Qt::NoBrush);
324 
325  painter->setPen(QPen(light, 2));
326  QwtPainter::drawLine(painter,
327  r.left() + 1, r.top() + 2, r.right() + 1, r.top() + 2);
328 
329  painter->setPen(QPen(dark, 2));
330  QwtPainter::drawLine(painter,
331  r.left() + 1, r.bottom(), r.right() + 1, r.bottom());
332 
333  painter->setPen(QPen(light, 1));
334 
335  QwtPainter::drawLine(painter,
336  r.left(), r.top() + 1, r.left(), r.bottom());
337  QwtPainter::drawLine(painter,
338  r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() - 1);
339 
340  painter->setPen(QPen(dark, 1));
341 
342  QwtPainter::drawLine(painter,
343  r.right() + 1, r.top() + 1, r.right() + 1, r.bottom());
344  QwtPainter::drawLine(painter,
345  r.right(), r.top() + 2, r.right(), r.bottom() - 1);
346 
347  painter->restore();
348  }
349 
350 
358  return p_pointList;
359  }
360 
361 
368  double sample, line;
369  p_pointList.clear();
370  for(int i = 0; getViewPort() && i < points.size(); i++) {
371  getViewPort()->viewportToCube(points[i].x(), points[i].y(), sample, line);
372  p_pointList.push_back(QPointF((sample), (line)));
373  }
374  }
375 
376 
384  return p_cvp;
385  }
386 
387 
393  p_cvp = cvp;
394  }
395 }
Isis::HistogramItem::baseline
double baseline() const
Returns the baseline.
Definition: HistogramItem.cpp:97
Isis::HistogramItem::boundingRect
virtual QRectF boundingRect() const
Returns the bounding rectangle of the item.
Definition: HistogramItem.cpp:156
Isis::HistogramItem::p_cvp
CubeViewport * p_cvp
Viewport the data is from.
Definition: HistogramItem.h:70
Isis::HistogramItem::setVertices
void setVertices(const QList< QPoint > &points)
This method sets the vertices of the selected area on the cvp.
Definition: HistogramItem.cpp:367
QList< QPointF >
Isis::HistogramItem::color
QColor color() const
Return the color of the item.
Definition: HistogramItem.cpp:145
Isis::HistogramItem::setBaseline
void setBaseline(double reference)
Definition: HistogramItem.cpp:83
Isis::HistogramItem::PrivateData
Definition: HistogramItem.cpp:21
QwtPlotItem
Isis::HistogramItem::setColor
void setColor(const QColor &)
Set the color of the hist.
Definition: HistogramItem.cpp:131
Isis::HistogramItem::setData
void setData(const QwtIntervalSeriesData &data)
Overridden method to set the data in the histogram.
Definition: HistogramItem.cpp:108
Isis::HistogramItem::~HistogramItem
virtual ~HistogramItem()
Destructor.
Definition: HistogramItem.cpp:55
Isis::HistogramItem::testHistogramAttribute
bool testHistogramAttribute(HistogramAttribute) const
Definition: HistogramItem.cpp:220
Isis::HistogramItem::HistogramItem
HistogramItem(const QString &title=QString::null)
Constructor 2.
Definition: HistogramItem.cpp:46
Isis::HistogramItem::setViewPort
void setViewPort(CubeViewport *cvp)
This method sets the view port.
Definition: HistogramItem.cpp:392
Isis::CubeViewport
Widget to display Isis cubes for qt apps.
Definition: CubeViewport.h:122
Isis::HistogramItem::p_pointList
QList< QPointF > p_pointList
List of data points.
Definition: HistogramItem.h:69
Isis::HistogramItem::data
const QwtIntervalSeriesData & data() const
Returns this item's data.
Definition: HistogramItem.cpp:120
Isis::HistogramItem::getViewPort
CubeViewport * getViewPort() const
This method returns the cube view port associated with the curve.
Definition: HistogramItem.cpp:383
Isis::HistogramItem::drawBar
virtual void drawBar(QPainter *, Qt::Orientation o, const QRect &) const
This method draws the bars of the bar graph.
Definition: HistogramItem.cpp:308
Isis::CubeViewport::viewportToCube
void viewportToCube(int x, int y, double &sample, double &line) const
Turns a viewport into a cube.
Definition: CubeViewport.cpp:815
Isis::HistogramItem::setHistogramAttribute
void setHistogramAttribute(HistogramAttribute, bool on=true)
Definition: HistogramItem.cpp:199
Isis::HistogramItem::rtti
virtual int rtti() const
Definition: HistogramItem.cpp:187
Isis::HistogramItem::init
void init()
Initialization method.
Definition: HistogramItem.cpp:64
Isis::HistogramItem::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) const
Definition: HistogramItem.cpp:233
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::HistogramItem::getVertices
QList< QPointF > getVertices() const
This method returns a list of points which are the vertices of the selected area (by the rubberband) ...
Definition: HistogramItem.cpp:357