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
|
#include <iostream>
#include <fstream>
#ifdef QWT_QTOPIA
#include <qpe/qpeapplication.h>
#endif
#include <qapplication.h>
#include <qvbox.h>
#include <qhbox.h>
#include <qfont.h>
#include <qlabel.h>
#include <qwt_thermo.h>
#include <qwt_math.h>
class Thermo: public QVBox
{
public:
Thermo(const QString &text, const QColor &color, QWidget *parent):
QVBox(parent)
{
d_thermo = new QwtThermo(this);
d_thermo->setOrientation(Qt::Vertical, QwtThermo::Right);
d_thermo->setRange(0.0,1.0);
d_thermo->setValue(1.0);
d_thermo->setBorderWidth(1);
d_thermo->setPipeWidth(4);
d_thermo->setFont(QFont("Helvetica",10));
d_thermo->setScaleMaxMajor(6);
d_thermo->setFillColor(color);
QLabel *label = new QLabel(text, this);
label->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
}
void setRange(double vmin, double vmax)
{ d_thermo->setRange(vmin, vmax); }
void setValue(double val) { d_thermo->setValue(val); }
double maxValue() { return d_thermo->maxValue(); }
private:
QwtThermo *d_thermo;
};
class MainWin : public QHBox
{
public:
MainWin();
protected:
virtual void timerEvent(QTimerEvent *e);
void updateThermo();
private:
Thermo *t1;
Thermo *t2;
Thermo *t3;
};
MainWin::MainWin()
{
t1 = new Thermo("1 min", Qt::darkRed, this);
t2 = new Thermo("10 min", Qt::darkGreen, this);
t3 = new Thermo("15 min", Qt::darkBlue, this);
(void)startTimer(2000);
}
void MainWin::timerEvent(QTimerEvent *)
{
updateThermo();
}
//---------------------------------------
// MainWin::update()
//
// read values from /proc/loadavg
// and display them. Adjust thermometer scales
// if necessary.
//---------------------------------------
void MainWin::updateThermo()
{
double v1, v2, v3;
static double vmin = 0.1;
double vmax, vnew;
std::ifstream inp("/proc/loadavg");
inp >> v1 >> v2 >> v3;
// adjust and synchronize ranges
vmax = qwtCeil125(qwtMax(qwtMax(qwtMax(v1,v2),v3),vmin));
if ( (vmax > t1->maxValue())
|| (vmax < 0.25 * t1->maxValue()) )
{
vnew = vmax;
t1->setRange(0.0, vnew);
t2->setRange(0.0, vnew);
t3->setRange(0.0, vnew);
}
// set values
t1->setValue(v1);
t2->setValue(v2);
t3->setValue(v3);
}
int main (int argc, char **argv)
{
#ifdef QWT_QTOPIA
QPEApplication a(argc, argv);
#else
QApplication a(argc, argv);
#endif
MainWin w;
a.setMainWidget(&w);
w.setCaption("load average");
w.show();
return a.exec();
}
|