1
#include <qapplication.h>
5
#include <qwt_plot_layout.h>
6
#include <qwt_plot_curve.h>
7
#include <qwt_scale_draw.h>
8
#include <qwt_scale_widget.h>
9
#include <qwt_legend.h>
10
#include <qwt_legend_item.h>
11
#include "cpupiemarker.h"
14
class TimeScaleDraw: public QwtScaleDraw
17
TimeScaleDraw(const QTime &base):
21
virtual QwtText label(double v) const
23
QTime upTime = baseTime.addSecs((int)v);
24
return upTime.toString();
30
class Background: public QwtPlotItem
38
virtual int rtti() const
40
return QwtPlotItem::Rtti_PlotUserItem;
43
virtual void draw(QPainter *painter,
44
const QwtScaleMap &, const QwtScaleMap &yMap,
45
const QRect &rect) const
50
for ( int i = 100; i > 0; i -= 10 )
52
r.setBottom(yMap.transform(i - 10));
53
r.setTop(yMap.transform(i));
54
painter->fillRect(r, c);
61
class CpuCurve: public QwtPlotCurve
64
CpuCurve(const QString &title):
67
#if QT_VERSION >= 0x040000
68
setRenderHint(QwtPlotItem::RenderAntialiased);
72
void setColor(const QColor &color)
74
#if QT_VERSION >= 0x040000
82
setBrush(QBrush(color, Qt::Dense4Pattern));
87
CpuPlot::CpuPlot(QWidget *parent):
93
plotLayout()->setAlignCanvasToScales(true);
95
QwtLegend *legend = new QwtLegend;
96
legend->setItemMode(QwtLegend::CheckableItem);
97
insertLegend(legend, QwtPlot::RightLegend);
99
setAxisTitle(QwtPlot::xBottom, " System Uptime [h:m:s]");
100
setAxisScaleDraw(QwtPlot::xBottom,
101
new TimeScaleDraw(cpuStat.upTime()));
102
setAxisScale(QwtPlot::xBottom, 0, HISTORY);
103
setAxisLabelRotation(QwtPlot::xBottom, -50.0);
104
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
107
In situations, when there is a label at the most right position of the
108
scale, additional space is needed to display the overlapping part
109
of the label would be taken by reducing the width of scale and canvas.
110
To avoid this "jumping canvas" effect, we add a permanent margin.
111
We don't need to do the same for the left border, because there
112
is enough space for the overlapping label below the left scale.
115
QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
116
const int fmh = QFontMetrics(scaleWidget->font()).height();
117
scaleWidget->setMinBorderDist(0, fmh / 2);
119
setAxisTitle(QwtPlot::yLeft, "Cpu Usage [%]");
120
setAxisScale(QwtPlot::yLeft, 0, 100);
122
Background *bg = new Background();
125
CpuPieMarker *pie = new CpuPieMarker();
130
curve = new CpuCurve("System");
131
curve->setColor(Qt::red);
133
data[System].curve = curve;
135
curve = new CpuCurve("User");
136
curve->setColor(Qt::blue);
137
curve->setZ(curve->z() - 1);
139
data[User].curve = curve;
141
curve = new CpuCurve("Total");
142
curve->setColor(Qt::black);
143
curve->setZ(curve->z() - 2);
145
data[Total].curve = curve;
147
curve = new CpuCurve("Idle");
148
curve->setColor(Qt::darkCyan);
149
curve->setZ(curve->z() - 3);
151
data[Idle].curve = curve;
153
showCurve(data[System].curve, true);
154
showCurve(data[User].curve, true);
155
showCurve(data[Total].curve, false);
156
showCurve(data[Idle].curve, false);
158
for ( int i = 0; i < HISTORY; i++ )
159
timeData[HISTORY - 1 - i] = i;
161
(void)startTimer(1000); // 1 second
163
connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)),
164
SLOT(showCurve(QwtPlotItem *, bool)));
167
void CpuPlot::timerEvent(QTimerEvent *)
169
for ( int i = dataCount; i > 0; i-- )
171
for ( int c = 0; c < NCpuData; c++ )
174
data[c].data[i] = data[c].data[i-1];
178
cpuStat.statistic(data[User].data[0], data[System].data[0]);
180
data[Total].data[0] = data[User].data[0] +
181
data[System].data[0];
182
data[Idle].data[0] = 100.0 - data[Total].data[0];
184
if ( dataCount < HISTORY )
187
for ( int j = 0; j < HISTORY; j++ )
190
setAxisScale(QwtPlot::xBottom,
191
timeData[HISTORY - 1], timeData[0]);
193
for ( int c = 0; c < NCpuData; c++ )
195
data[c].curve->setRawData(
196
timeData, data[c].data, dataCount);
202
void CpuPlot::showCurve(QwtPlotItem *item, bool on)
204
item->setVisible(on);
205
QWidget *w = legend()->find(item);
206
if ( w && w->inherits("QwtLegendItem") )
207
((QwtLegendItem *)w)->setChecked(on);
212
int main(int argc, char **argv)
214
QApplication a(argc, argv);
217
#if QT_VERSION >= 0x040000
218
vBox.setWindowTitle("Cpu Plot");
220
vBox.setCaption("Cpu Plot");
223
CpuPlot *plot = new CpuPlot(&vBox);
224
plot->setTitle("History");
227
QString info("Press the legend to en/disable a curve");
229
QLabel *label = new QLabel(info, &vBox);
231
QVBoxLayout *layout = new QVBoxLayout(&vBox);
232
layout->addWidget(plot);
233
layout->addWidget(label);
235
#if QT_VERSION < 0x040000
236
a.setMainWidget(&vBox);
239
vBox.resize(600,400);