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