Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

memorymeter.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
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                 // read memory status
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     // To simulate a 3-d memory, we use 4 bands of colour.  From left
00162     // to right, these are: medium, light, medium, dark.  To avoid
00163     // hardcoding values for band "width", figure everything out on the run.
00164     //
00165     int batt_width;                 // width of each band
00166     int batt_height;                // memory height (not including terminal)
00167     int used_height;                // used amount of memory (scanlines)
00168 
00169     int batt_yoffset;               // top of terminal
00170     int batt_xoffset;               // left edge of core
00171 
00172     int band_width;                 // width of colour band
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;    // +2 for 1 pixel border on both sides
00180     batt_height = height()-2;
00181     batt_xoffset = (width() - batt_width) / 2;
00182     batt_yoffset = (height() - batt_height) / 2;
00183 
00184     //
00185     // Memory border.  +1 to make space for the terminal at row 0.
00186     //
00187     p.setPen(QColor(80, 80, 80));
00188     p.drawRect(batt_xoffset, batt_yoffset + 1, batt_width, batt_height);
00189 
00190     //
00191     // Draw terminal.  +1 to take into account the left border.
00192     //
00193     //p.drawLine(batt_xoffset + band_width + 1, batt_yoffset, batt_xoffset + 3 * band_width, batt_yoffset);
00194 
00195     batt_height -= 2;   // -2 because we don't want to include border
00196     batt_yoffset += 2;  // +2 to account for border and terminal
00197     batt_xoffset++;
00198 
00199     //
00200     // 100 - percent, since percent is amount remaining, and we draw
00201     // reverse to this.
00202     //
00203     used_height = percent * batt_height / 100;
00204     if (used_height < 0)
00205                 used_height = 0;
00206 
00207     //
00208     // Drained section.
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/*.light(130)*/);
00218                 p.drawRect(batt_xoffset + band_width, batt_yoffset, band_width, used_height);
00219 
00220                 p.setBrush(gray/*.dark(120)*/);
00221                 p.drawRect(batt_xoffset + 3 * band_width, batt_yoffset, band_width, used_height);
00222     }
00223 
00224     //
00225     // Unused section.
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 

Generated on Sat Nov 5 16:16:44 2005 for OPIE by  doxygen 1.4.2