summaryrefslogtreecommitdiff
path: root/noncore/applets/memoryapplet/memorymeter.cpp
blob: 9cdeaf45211f5fed1d32cf95bad4579f264e0d00 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "memorymeter.h"
#include "memorystatus.h"

#include <opie2/otaskbarapplet.h>
#include <qtopia/power.h>
#include <qtopia/config.h>
#include <qtopia/qcopenvelope_qws.h>

#include <qpainter.h>
#include <qtimer.h>
#include <qapplication.h>

#include <qtopia/applnk.h>

using namespace Opie::Ui;
MemoryMeter::MemoryMeter( QWidget *parent )
    : QWidget( parent ), memoryView(0)
{
	bvsz = QSize();
	if ( qApp->desktop()->height() >= 300 )
	{
		memoryView = new MemoryStatus( 0, WStyle_StaysOnTop | WType_Popup );
		memoryView->setFrameStyle( QFrame::Panel | QFrame::Raised );
	}
	else
	{
		memoryView = new MemoryStatus( 0 );
		memoryView->showMaximized();
	}

    Config config("MemoryPlugin");
    config.setGroup("Warning levels");
    low = config.readNumEntry("low", 40);
    critical = config.readNumEntry("critical", 20);

    startTimer( 10000 );
    setFixedWidth(10);
    setFixedHeight(AppLnk::smallIconSize());
    usageTimer = new QTimer( this );
    connect( usageTimer, SIGNAL(timeout()), this, SLOT(usageTimeout()) );
    timerEvent(0);
}

MemoryMeter::~MemoryMeter()
{
    delete (QWidget *) memoryView;
}

int MemoryMeter::position()
{
    return 7;
}

QSize MemoryMeter::sizeHint() const
{
    return QSize(10, AppLnk::smallIconSize());
}

bool MemoryMeter::updateMemoryViewGeometry()
{
	if (memoryView != 0)
	{
		QSize sz = memoryView->sizeHint();
		if ( sz != bvsz )
		{
			bvsz = sz;
			QRect r(memoryView->pos(), memoryView->sizeHint());
			if ( qApp->desktop()->height() >= 300 )
			{
				QPoint curPos = mapToGlobal( rect().topLeft() );
				int lp = qApp->desktop()->width() - memoryView->sizeHint().width();
				r.moveTopLeft( QPoint(lp, curPos.y() - memoryView->sizeHint().height()-1) );
			}
			memoryView->setGeometry(r);
			return TRUE;
		}
		return FALSE;
	}

	return FALSE;
}

void MemoryMeter::mousePressEvent( QMouseEvent *)
{
    if ( memoryView->isVisible() )
	{
		memoryView->hide();
	}
	else
	{
		bvsz = QSize();
		updateMemoryViewGeometry();
		memoryView->raise();
		memoryView->show();
    }
}

void MemoryMeter::timerEvent( QTimerEvent * )
{
	if (memoryView != 0)
	{
		// read memory status
		percent = (memoryView->percent());
		usageTimer->start( 1000 );
	}
}

void MemoryMeter::usageTimeout()
{
	if (memoryView != 0)
	{
		percent = (memoryView->percent());
		if (updateMemoryViewGeometry() && memoryView->isVisible())
		{
			memoryView->hide();
			memoryView->show();
		}

		repaint(FALSE);
	}
}

void MemoryMeter::paintEvent( QPaintEvent* )
{
    QPainter p(this);

    QColor c;
    QColor darkc;
    QColor lightc;

	if (percent > low)
		c = green;
	else if (percent > critical)
		c = yellow.dark(110);
	else
		c = red;

	darkc = c.dark(120);
	lightc = c.light(160);

    //
    // To simulate a 3-d memory, we use 4 bands of colour.  From left
    // to right, these are: medium, light, medium, dark.  To avoid
    // hardcoding values for band "width", figure everything out on the run.
    //
    int	batt_width;		    // width of each band
    int	batt_height;		    // memory height (not including terminal)
    int	used_height;		    // used amount of memory (scanlines)

    int	batt_yoffset;		    // top of terminal
    int batt_xoffset;		    // left edge of core

    int	band_width;		    // width of colour band

    int w = QMIN(height(), width());
    band_width = (w-2) / 4;
    if ( band_width < 1 )
		band_width = 1;

    batt_width = 4 * band_width + 2;	// +2 for 1 pixel border on both sides
    batt_height = height()-2;
    batt_xoffset = (width() - batt_width) / 2;
    batt_yoffset = (height() - batt_height) / 2;

    //
    // Memory border.  +1 to make space for the terminal at row 0.
    //
    p.setPen(QColor(80, 80, 80));
    p.drawRect(batt_xoffset, batt_yoffset + 1, batt_width, batt_height);

    //
    // Draw terminal.  +1 to take into account the left border.
    //
    //p.drawLine(batt_xoffset + band_width + 1, batt_yoffset, batt_xoffset + 3 * band_width, batt_yoffset);

    batt_height -= 2;	// -2 because we don't want to include border
    batt_yoffset += 2;  // +2 to account for border and terminal
    batt_xoffset++;

    //
    // 100 - percent, since percent is amount remaining, and we draw
    // reverse to this.
    //
    used_height = percent * batt_height / 100;
    if (used_height < 0)
		used_height = 0;

    //
    // Drained section.
    //
    if (used_height != 0)
	{
		p.setPen(NoPen);
		p.setBrush(gray);
		p.drawRect(batt_xoffset, batt_yoffset, band_width, used_height);
		p.drawRect(batt_xoffset + 2 * band_width, batt_yoffset, band_width, used_height);

		p.setBrush(gray/*.light(130)*/);
		p.drawRect(batt_xoffset + band_width, batt_yoffset, band_width, used_height);

		p.setBrush(gray/*.dark(120)*/);
		p.drawRect(batt_xoffset + 3 * band_width, batt_yoffset, band_width, used_height);
    }

    //
    // Unused section.
    //
    if ( batt_height - used_height > 0 )
	{
		int unused_offset = used_height + batt_yoffset;
		int unused_height = batt_height - used_height;
		p.setPen(NoPen);
		p.setBrush(c);
		p.drawRect(batt_xoffset, unused_offset, band_width, unused_height);
		p.drawRect(batt_xoffset + 2 * band_width, unused_offset, band_width, unused_height);

		p.setBrush(lightc);
		p.drawRect(batt_xoffset + band_width, unused_offset, band_width, unused_height);

		p.setBrush(darkc);
		p.drawRect(batt_xoffset + 3 * band_width, unused_offset, band_width, unused_height);
    }
}

EXPORT_OPIE_APPLET_v1( MemoryMeter )