summaryrefslogtreecommitdiff
path: root/noncore/applets/memoryapplet/memorymeter.cpp
Unidiff
Diffstat (limited to 'noncore/applets/memoryapplet/memorymeter.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/applets/memoryapplet/memorymeter.cpp239
1 files changed, 239 insertions, 0 deletions
diff --git a/noncore/applets/memoryapplet/memorymeter.cpp b/noncore/applets/memoryapplet/memorymeter.cpp
new file mode 100644
index 0000000..54b5c52
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memorymeter.cpp
@@ -0,0 +1,239 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include "memorymeter.h"
21#include "memorystatus.h"
22
23#include <qtopia/power.h>
24#include <qtopia/config.h>
25
26#if defined(Q_WS_QWS) && !defined(QT_NO_COP)
27#include <qtopia/qcopenvelope_qws.h>
28#endif
29
30#include <qpainter.h>
31#include <qtimer.h>
32#include <qapplication.h>
33
34#include <qtopia/applnk.h>
35
36MemoryMeter::MemoryMeter( QWidget *parent )
37 : QWidget( parent ), memoryView(0)
38{
39 bvsz = QSize();
40 if ( qApp->desktop()->height() >= 300 )
41 {
42 memoryView = new MemoryStatus( 0, WStyle_StaysOnTop | WType_Popup );
43 memoryView->setFrameStyle( QFrame::Panel | QFrame::Raised );
44 }
45 else
46 {
47 memoryView = new MemoryStatus( 0 );
48 memoryView->showMaximized();
49 }
50
51 Config config("MemoryPlugin");
52 config.setGroup("Warning levels");
53 low = config.readNumEntry("low", 40);
54 critical = config.readNumEntry("critical", 20);
55
56 startTimer( 10000 );
57 setFixedWidth(10);
58 setFixedHeight(AppLnk::smallIconSize());
59 usageTimer = new QTimer( this );
60 connect( usageTimer, SIGNAL(timeout()), this, SLOT(usageTimeout()) );
61 timerEvent(0);
62}
63
64MemoryMeter::~MemoryMeter()
65{
66 delete (QWidget *) memoryView;
67}
68
69QSize MemoryMeter::sizeHint() const
70{
71 return QSize(10, AppLnk::smallIconSize());
72}
73
74bool MemoryMeter::updateMemoryViewGeometry()
75{
76 if (memoryView != 0)
77 {
78 QSize sz = memoryView->sizeHint();
79 if ( sz != bvsz )
80 {
81 bvsz = sz;
82 QRect r(memoryView->pos(), memoryView->sizeHint());
83 if ( qApp->desktop()->height() >= 300 )
84 {
85 QPoint curPos = mapToGlobal( rect().topLeft() );
86 int lp = qApp->desktop()->width() - memoryView->sizeHint().width();
87 r.moveTopLeft( QPoint(lp, curPos.y() - memoryView->sizeHint().height()-1) );
88 }
89 memoryView->setGeometry(r);
90 return TRUE;
91 }
92 return FALSE;
93 }
94
95 return FALSE;
96}
97
98void MemoryMeter::mousePressEvent( QMouseEvent *)
99{
100 if ( memoryView->isVisible() )
101 {
102 memoryView->hide();
103 }
104 else
105 {
106 bvsz = QSize();
107 updateMemoryViewGeometry();
108 memoryView->raise();
109 memoryView->show();
110 }
111}
112
113void MemoryMeter::timerEvent( QTimerEvent * )
114{
115 if (memoryView != 0)
116 {
117 // read memory status
118 percent = (memoryView->percent());
119 usageTimer->start( 1000 );
120 }
121}
122
123void MemoryMeter::usageTimeout()
124{
125 if (memoryView != 0)
126 {
127 percent = (memoryView->percent());
128 if (updateMemoryViewGeometry() && memoryView->isVisible())
129 {
130 memoryView->hide();
131 memoryView->show();
132 }
133
134 repaint(FALSE);
135 }
136}
137
138void MemoryMeter::paintEvent( QPaintEvent* )
139{
140 QPainter p(this);
141
142 QColor c;
143 QColor darkc;
144 QColor lightc;
145
146 if (percent > low)
147 c = green;
148 else if (percent > critical)
149 c = yellow.dark(110);
150 else
151 c = red;
152
153 darkc = c.dark(120);
154 lightc = c.light(160);
155
156 //
157 // To simulate a 3-d memory, we use 4 bands of colour. From left
158 // to right, these are: medium, light, medium, dark. To avoid
159 // hardcoding values for band "width", figure everything out on the run.
160 //
161 int batt_width; // width of each band
162 int batt_height; // memory height (not including terminal)
163 int used_height; // used amount of memory (scanlines)
164
165 int batt_yoffset; // top of terminal
166 int batt_xoffset; // left edge of core
167
168 int band_width; // width of colour band
169
170 int w = QMIN(height(), width());
171 band_width = (w-2) / 4;
172 if ( band_width < 1 )
173 band_width = 1;
174
175 batt_width = 4 * band_width + 2;// +2 for 1 pixel border on both sides
176 batt_height = height()-2;
177 batt_xoffset = (width() - batt_width) / 2;
178 batt_yoffset = (height() - batt_height) / 2;
179
180 //
181 // Memory border. +1 to make space for the terminal at row 0.
182 //
183 p.setPen(QColor(80, 80, 80));
184 p.drawRect(batt_xoffset, batt_yoffset + 1, batt_width, batt_height);
185
186 //
187 // Draw terminal. +1 to take into account the left border.
188 //
189 //p.drawLine(batt_xoffset + band_width + 1, batt_yoffset, batt_xoffset + 3 * band_width, batt_yoffset);
190
191 batt_height -= 2;// -2 because we don't want to include border
192 batt_yoffset += 2; // +2 to account for border and terminal
193 batt_xoffset++;
194
195 //
196 // 100 - percent, since percent is amount remaining, and we draw
197 // reverse to this.
198 //
199 used_height = percent * batt_height / 100;
200 if (used_height < 0)
201 used_height = 0;
202
203 //
204 // Drained section.
205 //
206 if (used_height != 0)
207 {
208 p.setPen(NoPen);
209 p.setBrush(gray);
210 p.drawRect(batt_xoffset, batt_yoffset, band_width, used_height);
211 p.drawRect(batt_xoffset + 2 * band_width, batt_yoffset, band_width, used_height);
212
213 p.setBrush(gray/*.light(130)*/);
214 p.drawRect(batt_xoffset + band_width, batt_yoffset, band_width, used_height);
215
216 p.setBrush(gray/*.dark(120)*/);
217 p.drawRect(batt_xoffset + 3 * band_width, batt_yoffset, band_width, used_height);
218 }
219
220 //
221 // Unused section.
222 //
223 if ( batt_height - used_height > 0 )
224 {
225 int unused_offset = used_height + batt_yoffset;
226 int unused_height = batt_height - used_height;
227 p.setPen(NoPen);
228 p.setBrush(c);
229 p.drawRect(batt_xoffset, unused_offset, band_width, unused_height);
230 p.drawRect(batt_xoffset + 2 * band_width, unused_offset, band_width, unused_height);
231
232 p.setBrush(lightc);
233 p.drawRect(batt_xoffset + band_width, unused_offset, band_width, unused_height);
234
235 p.setBrush(darkc);
236 p.drawRect(batt_xoffset + 3 * band_width, unused_offset, band_width, unused_height);
237 }
238}
239