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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#ifdef QWT_QTOPIA
#include <qpe/qpeapplication.h>
#endif
#include <qapplication.h>
#include <qvbox.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_canvas.h>
#include "cpupiemarker.h"
#include "cpuaveragecurve.h"
#include "cpuplot.h"
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(const QTime &base):
baseTime(base)
{
}
virtual QString label(double v) const
{
QTime upTime = baseTime.addSecs((int)v);
return upTime.toString();
}
private:
QTime baseTime;
};
CpuPlot::CpuPlot(QWidget *parent):
QwtPlot(parent),
dataCount(0)
{
plotLayout()->setAlignCanvasToScales(TRUE);
setCanvasBackground(Qt::darkGray);
setAutoLegend(TRUE);
setLegendPosition(QwtPlot::Right);
setAxisTitle(QwtPlot::xBottom, " System Uptime [h:m:s]");
setAxisScaleDraw(QwtPlot::xBottom,
new TimeScaleDraw(cpuStat.upTime()));
setAxisScale(QwtPlot::xBottom, 0, HISTORY);
setAxisLabelRotation(QwtPlot::xBottom, -50.0);
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
setAxisTitle(QwtPlot::yLeft, "Cpu Usage [%]");
setAxisScale(QwtPlot::yLeft, 0, 100);
data[Total].curve = insertCurve("Total");
setCurvePen(data[Total].curve, QPen(Qt::black));
setCurveBrush(data[Total].curve,
QBrush(QColor(), Qt::SolidPattern));
data[Idle].curve = insertCurve("Idle");
setCurvePen(data[Idle].curve, QPen(Qt::lightGray));
setCurveBrush(data[Idle].curve,
QBrush(QColor(), Qt::Dense4Pattern));
data[User].curve = insertCurve("User");
setCurvePen(data[User].curve, QPen(Qt::blue));
setCurveBrush(data[User].curve,
QBrush(QColor(), Qt::Dense2Pattern));
data[System].curve = insertCurve("System");
setCurvePen(data[System].curve, QPen(Qt::red));
setCurveBrush(data[System].curve,
QBrush(QColor(), Qt::Dense2Pattern));
CpuAverageCurve *curve = new CpuAverageCurve(this);
data[Average].curve = insertCurve(curve);
setCurveTitle(data[Average].curve, "Average");
setCurvePen(data[Average].curve, QPen(Qt::NoPen));
setCurveBrush(data[Average].curve,
QBrush(QColor(Qt::black), Qt::BDiagPattern));
CpuPieMarker *pie = new CpuPieMarker(this);
(void)insertMarker(pie);
toggleCurve(data[Total].curve);
toggleCurve(data[Idle].curve);
toggleCurve(data[Average].curve);
for ( int i = 0; i < HISTORY; i++ )
timeData[HISTORY - 1 - i] = i;
(void)startTimer(1000); // 1 second
replot();
connect(this, SIGNAL(legendClicked(long)),
SLOT(toggleCurve(long)));
}
void CpuPlot::drawCanvasItems(QPainter *painter, const QRect &rect,
const QwtArray<QwtDiMap> &map, const QwtPlotPrintFilter &pfilter) const
{
const QwtDiMap &yMap = map[QwtPlot::yLeft];
QRect r = canvas()->rect();
r.setHeight(yMap.transform(80.0) - r.top());
painter->fillRect(r, QColor(Qt::white));
r.setTop(yMap.transform(80.0));
r.setHeight(yMap.transform(40.0) - yMap.transform(80.0));
painter->fillRect(r, QColor(Qt::gray));
QwtPlot::drawCanvasItems(painter, rect, map, pfilter);
}
void CpuPlot::timerEvent(QTimerEvent *)
{
for ( int i = dataCount; i > 0; i-- )
{
for ( int c = 0; c < NCpuData; c++ )
{
if ( i < HISTORY )
data[c].data[i] = data[c].data[i-1];
}
}
cpuStat.statistic(data[User].data[0], data[System].data[0]);
data[Total].data[0] = data[User].data[0] +
data[System].data[0];
data[Idle].data[0] = 100.0 - data[Total].data[0];
data[Average].data[0] = cpuStat.average();
if ( dataCount < HISTORY )
dataCount++;
for ( int j = 0; j < HISTORY; j++ )
timeData[j]++;
setAxisScale(QwtPlot::xBottom,
timeData[HISTORY - 1], timeData[0]);
for ( int c = 0; c < NCpuData; c++ )
{
setCurveRawData(data[c].curve,
timeData, data[c].data, dataCount);
}
replot();
}
void CpuPlot::toggleCurve(long curveId)
{
QwtPlotCurve *c = curve(curveId);
if ( c )
{
c->setEnabled(!c->enabled());
replot();
}
}
int main(int argc, char **argv)
{
#ifdef QWT_QTOPIA
QPEApplication a(argc, argv);
#else
QApplication a(argc, argv);
#endif
QVBox vBox;
vBox.setCaption("Cpu Plot");
CpuPlot *plot = new CpuPlot(&vBox);
plot->setTitle("History");
plot->setMargin(5);
QString info("Press the legend to en/disable a curve");
(void)new QLabel(info, &vBox);
a.setMainWidget(&vBox);
vBox.resize(500,300);
vBox.show();
return a.exec();
}
|