~showard314/ubuntu/natty/qtiplot/Python2.7_fix

« back to all changes in this revision

Viewing changes to 3rdparty/qwt/examples/cpuplot/cpuplot.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-04-04 15:11:55 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080404151155-rjp12ziov4tryj0o
Tags: 0.9.4-1
* New upstream release.
* Refresh patches.
* Remove 04_homepage_url patch. Merged upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <qapplication.h>
 
2
#include <qlayout.h>
 
3
#include <qlabel.h>
 
4
#include <qpainter.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"
 
12
#include "cpuplot.h"
 
13
 
 
14
class TimeScaleDraw: public QwtScaleDraw
 
15
{
 
16
public:
 
17
    TimeScaleDraw(const QTime &base):
 
18
        baseTime(base)
 
19
    {
 
20
    }
 
21
    virtual QwtText label(double v) const
 
22
    {
 
23
        QTime upTime = baseTime.addSecs((int)v);
 
24
        return upTime.toString();
 
25
    }
 
26
private:
 
27
    QTime baseTime;
 
28
};
 
29
 
 
30
class Background: public QwtPlotItem
 
31
{
 
32
public:
 
33
    Background()
 
34
    {
 
35
        setZ(0.0);
 
36
    }
 
37
 
 
38
    virtual int rtti() const
 
39
    {
 
40
        return QwtPlotItem::Rtti_PlotUserItem;
 
41
    }
 
42
 
 
43
    virtual void draw(QPainter *painter,
 
44
        const QwtScaleMap &, const QwtScaleMap &yMap,
 
45
        const QRect &rect) const
 
46
    {
 
47
        QColor c(Qt::white);
 
48
        QRect r = rect;
 
49
 
 
50
        for ( int i = 100; i > 0; i -= 10 )
 
51
        {
 
52
            r.setBottom(yMap.transform(i - 10));
 
53
            r.setTop(yMap.transform(i));
 
54
            painter->fillRect(r, c);
 
55
 
 
56
            c = c.dark(110);
 
57
        }
 
58
    }
 
59
};
 
60
 
 
61
class CpuCurve: public QwtPlotCurve
 
62
{
 
63
public:
 
64
    CpuCurve(const QString &title):
 
65
        QwtPlotCurve(title)
 
66
    {
 
67
#if QT_VERSION >= 0x040000
 
68
        setRenderHint(QwtPlotItem::RenderAntialiased);
 
69
#endif
 
70
    }
 
71
 
 
72
    void setColor(const QColor &color)
 
73
    {
 
74
#if QT_VERSION >= 0x040000
 
75
        QColor c = color;
 
76
        c.setAlpha(150);
 
77
 
 
78
        setPen(c);
 
79
        setBrush(c);
 
80
#else
 
81
        setPen(color);
 
82
        setBrush(QBrush(color, Qt::Dense4Pattern));
 
83
#endif
 
84
    }
 
85
};
 
86
 
 
87
CpuPlot::CpuPlot(QWidget *parent):
 
88
    QwtPlot(parent),
 
89
    dataCount(0)
 
90
{
 
91
    setAutoReplot(false);
 
92
 
 
93
    plotLayout()->setAlignCanvasToScales(true);
 
94
 
 
95
    QwtLegend *legend = new QwtLegend;
 
96
    legend->setItemMode(QwtLegend::CheckableItem);
 
97
    insertLegend(legend, QwtPlot::RightLegend);
 
98
 
 
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);
 
105
 
 
106
    /*
 
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.
 
113
     */
 
114
 
 
115
    QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
 
116
    const int fmh = QFontMetrics(scaleWidget->font()).height();
 
117
    scaleWidget->setMinBorderDist(0, fmh / 2);
 
118
 
 
119
    setAxisTitle(QwtPlot::yLeft, "Cpu Usage [%]");
 
120
    setAxisScale(QwtPlot::yLeft, 0, 100);
 
121
 
 
122
    Background *bg = new Background();
 
123
    bg->attach(this);
 
124
 
 
125
    CpuPieMarker *pie = new CpuPieMarker();
 
126
    pie->attach(this);
 
127
    
 
128
    CpuCurve *curve;
 
129
 
 
130
    curve = new CpuCurve("System");
 
131
    curve->setColor(Qt::red);
 
132
    curve->attach(this);
 
133
    data[System].curve = curve;
 
134
 
 
135
    curve = new CpuCurve("User");
 
136
    curve->setColor(Qt::blue);
 
137
    curve->setZ(curve->z() - 1);
 
138
    curve->attach(this);
 
139
    data[User].curve = curve;
 
140
 
 
141
    curve = new CpuCurve("Total");
 
142
    curve->setColor(Qt::black);
 
143
    curve->setZ(curve->z() - 2);
 
144
    curve->attach(this);
 
145
    data[Total].curve = curve;
 
146
 
 
147
    curve = new CpuCurve("Idle");
 
148
    curve->setColor(Qt::darkCyan);
 
149
    curve->setZ(curve->z() - 3);
 
150
    curve->attach(this);
 
151
    data[Idle].curve = curve;
 
152
 
 
153
    showCurve(data[System].curve, true);
 
154
    showCurve(data[User].curve, true);
 
155
    showCurve(data[Total].curve, false);
 
156
    showCurve(data[Idle].curve, false);
 
157
 
 
158
    for ( int i = 0; i < HISTORY; i++ )
 
159
        timeData[HISTORY - 1 - i] = i;
 
160
 
 
161
    (void)startTimer(1000); // 1 second
 
162
 
 
163
    connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)),
 
164
        SLOT(showCurve(QwtPlotItem *, bool)));
 
165
}
 
166
 
 
167
void CpuPlot::timerEvent(QTimerEvent *)
 
168
{
 
169
    for ( int i = dataCount; i > 0; i-- )
 
170
    {
 
171
        for ( int c = 0; c < NCpuData; c++ )
 
172
        {
 
173
            if ( i < HISTORY )
 
174
                data[c].data[i] = data[c].data[i-1];
 
175
        }
 
176
    }
 
177
 
 
178
    cpuStat.statistic(data[User].data[0], data[System].data[0]);
 
179
 
 
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];
 
183
 
 
184
    if ( dataCount < HISTORY )
 
185
        dataCount++;
 
186
 
 
187
    for ( int j = 0; j < HISTORY; j++ )
 
188
        timeData[j]++;
 
189
 
 
190
    setAxisScale(QwtPlot::xBottom, 
 
191
        timeData[HISTORY - 1], timeData[0]);
 
192
 
 
193
    for ( int c = 0; c < NCpuData; c++ )
 
194
    {
 
195
        data[c].curve->setRawData(
 
196
            timeData, data[c].data, dataCount);
 
197
    }
 
198
 
 
199
    replot();
 
200
}
 
201
 
 
202
void CpuPlot::showCurve(QwtPlotItem *item, bool on)
 
203
{
 
204
    item->setVisible(on);
 
205
    QWidget *w = legend()->find(item);
 
206
    if ( w && w->inherits("QwtLegendItem") )
 
207
        ((QwtLegendItem *)w)->setChecked(on);
 
208
    
 
209
    replot();
 
210
}
 
211
 
 
212
int main(int argc, char **argv)
 
213
{
 
214
    QApplication a(argc, argv); 
 
215
    
 
216
    QWidget vBox;
 
217
#if QT_VERSION >= 0x040000
 
218
    vBox.setWindowTitle("Cpu Plot");
 
219
#else
 
220
    vBox.setCaption("Cpu Plot");
 
221
#endif
 
222
 
 
223
    CpuPlot *plot = new CpuPlot(&vBox);
 
224
    plot->setTitle("History");
 
225
    plot->setMargin(5);
 
226
 
 
227
    QString info("Press the legend to en/disable a curve");
 
228
 
 
229
    QLabel *label = new QLabel(info, &vBox);
 
230
 
 
231
    QVBoxLayout *layout = new QVBoxLayout(&vBox);
 
232
    layout->addWidget(plot);
 
233
    layout->addWidget(label);
 
234
 
 
235
#if QT_VERSION < 0x040000
 
236
    a.setMainWidget(&vBox);
 
237
#endif
 
238
 
 
239
    vBox.resize(600,400);
 
240
    vBox.show();
 
241
 
 
242
    return a.exec();  
 
243
}   
 
244