File: meminfo.cpp

package info (click to toggle)
libqwt 4.2.0-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,856 kB
  • ctags: 5,512
  • sloc: cpp: 22,973; ansic: 244; makefile: 59
file content (188 lines) | stat: -rw-r--r-- 3,840 bytes parent folder | download | duplicates (5)
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
183
184
185
186
187
188
#include <iostream>
#include <fstream>
#ifdef QWT_QTOPIA
#include <qpe/qpeapplication.h>
#endif
#include <qapplication.h>
#include <qfont.h>
#include <qlabel.h>
#include <qwt_thermo.h>
#include <qwt_math.h>

class MainWin : public QWidget 
{
public:
    MainWin();
    ~MainWin();

    void start();
    void update();
    
protected:
    void timerEvent(QTimerEvent *e);
    
private:
    enum {MemUsed, MemShared, MemBuffers, MemCached, SwapUsed, ThermoCnt };
    QwtThermo *th[ThermoCnt];
    QLabel  *lb[ThermoCnt];
    int tmrID;
};

void MainWin::start()
{
    update();
    tmrID = startTimer(2000);
}

void MainWin::update()
{
    double mtotal, mused, mcached, mshared, mbuffers, mfree;
    double stotal, sused, sfree;
    static char buffer[81];
    QString text;   
    std::ifstream inp("/proc/meminfo");

    inp.getline(buffer, 80);
    inp >> buffer >> mtotal >> mused >> mfree >> mshared >> mbuffers
       >> mcached;
    inp >> buffer >> stotal >> sused >> sfree;

    mtotal *= 0.01;
    stotal *= 0.01;

    mused /= mtotal;
    mshared /= mtotal;
    mbuffers /= mtotal;
    mcached /= mtotal;
    sused /= stotal;
    
    // set values
    th[MemUsed]->setValue(mused);
    th[MemShared]->setValue(mshared);
    th[MemBuffers]->setValue(mbuffers);
    th[MemCached]->setValue(mcached);
    th[SwapUsed]->setValue(sused);

    text.setNum(mused, 'g', 3);
    text = QString("Used: ") + text + QString(" %"); 
    lb[MemUsed]->setText(text);
    
    text.setNum(mshared, 'g', 3);
    text = QString("Shared: ") + text + QString(" %"); 
    lb[MemShared]->setText(text);

    text.setNum(mbuffers, 'g', 3);
    text = QString("Buffers: ") + text + QString(" %"); 
    lb[MemBuffers]->setText(text);

    text.setNum(mcached, 'g', 3);
    text = QString("Cache: ") + text + QString(" %"); 
    lb[MemCached]->setText(text);

    text.setNum(sused, 'g', 3);
    text = QString("Swap Used: ") + text + QString(" %"); 
    lb[SwapUsed]->setText(text);
}


void MainWin::timerEvent(QTimerEvent *)
{
    update();
}



MainWin::MainWin(): 
    QWidget()
{
    int i;

    QFont fnThermo("Helvetica", 8);
    QFont fnLabel("Helvetica", 10);
    QColor cFill("DarkMagenta");

    //
    // initialize members
    //
    tmrID = 0;

    //
    // create widgets
    //
    for(i=0;i<ThermoCnt;i++)        
       th[i] = new QwtThermo(this,"");
    
    lb[MemUsed] = new QLabel("Used",this);
    lb[MemShared] = new QLabel("Shared",this);
    lb[MemCached] = new QLabel("Cache",this);
    lb[MemBuffers] = new QLabel("Buffers",this);
    lb[SwapUsed] = new QLabel("Swap Used",this);

    //
    // place widgets
    //
    for (i=0; i<ThermoCnt; i++)
    {
        lb[i]->setGeometry(0,i*65,130,20);
        th[i]->setGeometry(0,20 + i * 65 ,130,45);
    }

    //
    // configure thermometers
    //
    for (i=0;i<ThermoCnt;i++)
    {
        th[i]->setOrientation(Qt::Horizontal, QwtThermo::Bottom);
        th[i]->setRange(0.0,100.0);
        th[i]->setValue(0.0);
        th[i]->setFont(fnThermo);
        th[i]->setPipeWidth(6);
        th[i]->setScaleMaxMajor(6);
        th[i]->setScaleMaxMinor(5);
        th[i]->setMargin(10);
        th[i]->setFillColor(cFill);
    }

    //
    // configure labels
    //
    for (i=0;i<ThermoCnt;i++)
    {
        lb[i]->setFont(fnLabel);
        lb[i]->setAlignment(Qt::AlignCenter);
    }

    setCaption("Memory Usage");
    setFixedSize(130,325);
}


MainWin::~MainWin()
{
    for(int i=0;i<ThermoCnt;i++)
    {   
        delete th[i];
        delete lb[i];
    }   
    if (tmrID != 0) 
        killTimer(tmrID);
}


int main (int argc, char **argv)
{
#ifdef QWT_QTOPIA
    QPEApplication a(argc, argv);
#else
    QApplication a(argc, argv);
#endif

    MainWin w;
    w.resize(180,140);

    a.setMainWidget(&w);
    w.start();
    w.show();
    
    return a.exec();
}