-
Notifications
You must be signed in to change notification settings - Fork 1
/
lw_histogram.cpp
153 lines (125 loc) · 3.67 KB
/
lw_histogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// *****************************************************************************
// NICOS, the Networked Instrument Control System of the FRM-II
// Copyright (c) 2009-2014 by the NICOS contributors (see AUTHORS)
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Module authors:
// Josef Wilgen, Uwe Rathmann (authors of Qwt example code)
// Tobias Weber <[email protected]>
// Georg Brandl <[email protected]>
//
// *****************************************************************************
#include "lw_common.h"
#include <QPainter>
#include <qwt_plot.h>
#include <qwt_interval_data.h>
#include <qwt_painter.h>
#include <qwt_scale_map.h>
#include "lw_histogram.h"
class LWHistogramItem::PrivateData
{
public:
QwtCPointerData *data;
QColor color;
double reference;
};
LWHistogramItem::LWHistogramItem(const QwtText &title) :
QwtPlotItem(title)
{
init();
}
LWHistogramItem::LWHistogramItem(const QString &title) :
QwtPlotItem(QwtText(title))
{
init();
}
LWHistogramItem::~LWHistogramItem()
{
delete d_data;
}
void LWHistogramItem::init()
{
d_data = new PrivateData();
d_data->reference = 0.0;
setItemAttribute(QwtPlotItem::AutoScale, true);
setItemAttribute(QwtPlotItem::Legend, true);
setZ(20.0);
}
void LWHistogramItem::setBaseline(double reference)
{
if (d_data->reference != reference) {
d_data->reference = reference;
itemChanged();
}
}
double LWHistogramItem::baseline() const
{
return d_data->reference;
}
void LWHistogramItem::setData(const QwtCPointerData &data)
{
d_data->data = new QwtCPointerData(data);
itemChanged();
}
const QwtCPointerData &LWHistogramItem::data() const
{
return *d_data->data;
}
void LWHistogramItem::setColor(const QColor &color)
{
if (d_data->color != color) {
d_data->color = color;
itemChanged();
}
}
QColor LWHistogramItem::color() const
{
return d_data->color;
}
QwtDoubleRect LWHistogramItem::boundingRect() const
{
QwtDoubleRect rect = d_data->data->boundingRect();
if (!rect.isValid())
return rect;
if (rect.bottom() < d_data->reference)
rect.setBottom(d_data->reference);
else if (rect.top() > d_data->reference)
rect.setTop(d_data->reference);
return rect;
}
int LWHistogramItem::rtti() const
{
return QwtPlotItem::Rtti_PlotHistogram;
}
void LWHistogramItem::draw(QPainter *painter, const QwtScaleMap &xMap,
const QwtScaleMap &yMap, const QRect &) const
{
const QwtCPointerData *data = d_data->data;
painter->save();
painter->setBrush(d_data->color);
const int y0 = yMap.transform(baseline());
for (int i = 0; i < (int)data->size() - 1; i++) {
const int y2 = yMap.transform(data->y(i));
if (y2 == y0)
continue;
int x1 = xMap.transform(data->x(i));
int x2 = xMap.transform(data->x(i+1));
if (x1 > x2)
qSwap(x1, x2);
QwtPainter::drawRect(painter, x1, y0, x2 - x1, y2 - y0);
}
painter->restore();
}