00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "memorymeter.h"
00021 #include "memorystatus.h"
00022
00023 #include <opie2/otaskbarapplet.h>
00024 #include <qtopia/power.h>
00025 #include <qtopia/config.h>
00026 #include <qtopia/qcopenvelope_qws.h>
00027
00028 #include <qpainter.h>
00029 #include <qtimer.h>
00030 #include <qapplication.h>
00031
00032 #include <qtopia/applnk.h>
00033
00034 using namespace Opie::Ui;
00035 MemoryMeter::MemoryMeter( QWidget *parent )
00036 : QWidget( parent ), memoryView(0)
00037 {
00038 bvsz = QSize();
00039 if ( qApp->desktop()->height() >= 300 )
00040 {
00041 memoryView = new MemoryStatus( 0, WStyle_StaysOnTop | WType_Popup );
00042 memoryView->setFrameStyle( QFrame::Panel | QFrame::Raised );
00043 }
00044 else
00045 {
00046 memoryView = new MemoryStatus( 0 );
00047 memoryView->showMaximized();
00048 }
00049
00050 Config config("MemoryPlugin");
00051 config.setGroup("Warning levels");
00052 low = config.readNumEntry("low", 40);
00053 critical = config.readNumEntry("critical", 20);
00054
00055 startTimer( 10000 );
00056 setFixedWidth(10);
00057 setFixedHeight(AppLnk::smallIconSize());
00058 usageTimer = new QTimer( this );
00059 connect( usageTimer, SIGNAL(timeout()), this, SLOT(usageTimeout()) );
00060 timerEvent(0);
00061 }
00062
00063 MemoryMeter::~MemoryMeter()
00064 {
00065 delete (QWidget *) memoryView;
00066 }
00067
00068 int MemoryMeter::position()
00069 {
00070 return 7;
00071 }
00072
00073 QSize MemoryMeter::sizeHint() const
00074 {
00075 return QSize(10, AppLnk::smallIconSize());
00076 }
00077
00078 bool MemoryMeter::updateMemoryViewGeometry()
00079 {
00080 if (memoryView != 0)
00081 {
00082 QSize sz = memoryView->sizeHint();
00083 if ( sz != bvsz )
00084 {
00085 bvsz = sz;
00086 QRect r(memoryView->pos(), memoryView->sizeHint());
00087 if ( qApp->desktop()->height() >= 300 )
00088 {
00089 QPoint curPos = mapToGlobal( rect().topLeft() );
00090 int lp = qApp->desktop()->width() - memoryView->sizeHint().width();
00091 r.moveTopLeft( QPoint(lp, curPos.y() - memoryView->sizeHint().height()-1) );
00092 }
00093 memoryView->setGeometry(r);
00094 return TRUE;
00095 }
00096 return FALSE;
00097 }
00098
00099 return FALSE;
00100 }
00101
00102 void MemoryMeter::mousePressEvent( QMouseEvent *)
00103 {
00104 if ( memoryView->isVisible() )
00105 {
00106 memoryView->hide();
00107 }
00108 else
00109 {
00110 bvsz = QSize();
00111 updateMemoryViewGeometry();
00112 memoryView->raise();
00113 memoryView->show();
00114 }
00115 }
00116
00117 void MemoryMeter::timerEvent( QTimerEvent * )
00118 {
00119 if (memoryView != 0)
00120 {
00121
00122 percent = (memoryView->percent());
00123 usageTimer->start( 1000 );
00124 }
00125 }
00126
00127 void MemoryMeter::usageTimeout()
00128 {
00129 if (memoryView != 0)
00130 {
00131 percent = (memoryView->percent());
00132 if (updateMemoryViewGeometry() && memoryView->isVisible())
00133 {
00134 memoryView->hide();
00135 memoryView->show();
00136 }
00137
00138 repaint(FALSE);
00139 }
00140 }
00141
00142 void MemoryMeter::paintEvent( QPaintEvent* )
00143 {
00144 QPainter p(this);
00145
00146 QColor c;
00147 QColor darkc;
00148 QColor lightc;
00149
00150 if (percent > low)
00151 c = green;
00152 else if (percent > critical)
00153 c = yellow.dark(110);
00154 else
00155 c = red;
00156
00157 darkc = c.dark(120);
00158 lightc = c.light(160);
00159
00160
00161
00162
00163
00164
00165 int batt_width;
00166 int batt_height;
00167 int used_height;
00168
00169 int batt_yoffset;
00170 int batt_xoffset;
00171
00172 int band_width;
00173
00174 int w = QMIN(height(), width());
00175 band_width = (w-2) / 4;
00176 if ( band_width < 1 )
00177 band_width = 1;
00178
00179 batt_width = 4 * band_width + 2;
00180 batt_height = height()-2;
00181 batt_xoffset = (width() - batt_width) / 2;
00182 batt_yoffset = (height() - batt_height) / 2;
00183
00184
00185
00186
00187 p.setPen(QColor(80, 80, 80));
00188 p.drawRect(batt_xoffset, batt_yoffset + 1, batt_width, batt_height);
00189
00190
00191
00192
00193
00194
00195 batt_height -= 2;
00196 batt_yoffset += 2;
00197 batt_xoffset++;
00198
00199
00200
00201
00202
00203 used_height = percent * batt_height / 100;
00204 if (used_height < 0)
00205 used_height = 0;
00206
00207
00208
00209
00210 if (used_height != 0)
00211 {
00212 p.setPen(NoPen);
00213 p.setBrush(gray);
00214 p.drawRect(batt_xoffset, batt_yoffset, band_width, used_height);
00215 p.drawRect(batt_xoffset + 2 * band_width, batt_yoffset, band_width, used_height);
00216
00217 p.setBrush(gray);
00218 p.drawRect(batt_xoffset + band_width, batt_yoffset, band_width, used_height);
00219
00220 p.setBrush(gray);
00221 p.drawRect(batt_xoffset + 3 * band_width, batt_yoffset, band_width, used_height);
00222 }
00223
00224
00225
00226
00227 if ( batt_height - used_height > 0 )
00228 {
00229 int unused_offset = used_height + batt_yoffset;
00230 int unused_height = batt_height - used_height;
00231 p.setPen(NoPen);
00232 p.setBrush(c);
00233 p.drawRect(batt_xoffset, unused_offset, band_width, unused_height);
00234 p.drawRect(batt_xoffset + 2 * band_width, unused_offset, band_width, unused_height);
00235
00236 p.setBrush(lightc);
00237 p.drawRect(batt_xoffset + band_width, unused_offset, band_width, unused_height);
00238
00239 p.setBrush(darkc);
00240 p.drawRect(batt_xoffset + 3 * band_width, unused_offset, band_width, unused_height);
00241 }
00242 }
00243
00244 EXPORT_OPIE_APPLET_v1( MemoryMeter )
00245