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

load.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of 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 
00021 #include <stdio.h>
00022 
00023 #include <qfile.h>
00024 #include <qlayout.h>
00025 #include <qlabel.h>
00026 #include <qpainter.h>
00027 #include <qpixmap.h>
00028 #include <qtextstream.h>
00029 #include <qtimer.h>
00030 #include <qwhatsthis.h>
00031 
00032 #include "load.h"
00033 
00034 LoadInfo::LoadInfo( QWidget *parent, const char *name, WFlags f )
00035     : QWidget( parent, name, f )
00036 {
00037     QVBoxLayout *vb = new QVBoxLayout( this, 6 );
00038 
00039     QString cpuInfo = getCpuInfo();
00040     if ( !cpuInfo.isNull() )
00041         vb->addWidget( new QLabel( cpuInfo, this ) );
00042     vb->addWidget( new Load( this ), 100 );
00043     QLabel *l = new QLabel( this );
00044     l->setPixmap( makeLabel( red, tr("Application CPU usage (%)") ) );
00045     vb->addWidget( l, 1 );
00046     l = new QLabel( this );
00047     l->setPixmap( makeLabel( green, tr("System CPU usage (%)") ) );
00048     vb->addWidget( l, 1 );
00049     vb->addStretch(50);
00050 
00051     QWhatsThis::add( this, tr( "This page shows how much this device's processor is being used." ) );
00052 }
00053 
00054 QPixmap LoadInfo::makeLabel( const QColor &col, const QString &text )
00055 {
00056     int h = fontMetrics().height();
00057     QPixmap pm( 20 + fontMetrics().width( text ), h );
00058     QPainter p( &pm );
00059     p.fillRect( pm.rect(), colorGroup().background() );
00060     p.fillRect( 0, h/2-4, 18, h/2+3, black );
00061     p.setPen( col );
00062     p.drawLine( 2, h/2, 15, h/2 );
00063     p.setPen( colorGroup().text() );
00064     p.drawText( 20, fontMetrics().ascent(), text );
00065 
00066     return pm;
00067 }
00068 
00069 QString LoadInfo::getCpuInfo()
00070 {
00071     bool haveInfo = FALSE;
00072     QString info = tr("Type: ");
00073     QFile f( "/proc/cpuinfo" );
00074     if ( f.open( IO_ReadOnly ) ) {
00075         QTextStream ts( &f );
00076 
00077         while ( !ts.atEnd() ) {
00078             QString s = ts.readLine();
00079             if ( s.find( "model name" ) == 0 ) {
00080                 info += s.mid( s.find( ':' ) + 2 );
00081                 haveInfo = TRUE;
00082             } else if ( s.find( "cpu MHz" ) == 0 ) {
00083                 double mhz = s.mid( s.find( ':' ) + 2 ).toDouble();
00084                 info += " " + QString::number( mhz, 'f', 0 );
00085                 info += "MHz";
00086                 break;
00087             } else if ( s.find( "Processor" ) == 0 ) {
00088                 info += s.mid( s.find( ':' ) + 2 );
00089                 haveInfo = TRUE;
00090                 break;
00091 #ifdef __MIPSEL__
00092             } else if ( s.find( "cpu model" ) == 0 ) {
00093                 info += " " + s.mid( s.find( ':' ) + 2 );
00094                 break;
00095             } else if ( s.find( "cpu" ) == 0 ) {
00096                 info += s.mid( s.find( ':' ) + 2 );
00097                 haveInfo = TRUE;
00098 #endif
00099             }
00100         }
00101     }
00102 
00103     if ( !haveInfo )
00104         info = QString();
00105 
00106     return info;
00107 }
00108 
00109 Load::Load( QWidget *parent, const char *name, WFlags f )
00110     : QWidget( parent, name, f )
00111 {
00112     setMinimumHeight( 30 );
00113     setBackgroundColor( black );
00114     points = 100;
00115     setMinimumWidth( points );
00116     userLoad = new double [points];
00117     systemLoad = new double [points];
00118     for ( int i = 0; i < points; i++ ) {
00119         userLoad[i] = 0.0;
00120         systemLoad[i] = 0.0;
00121     }
00122     maxLoad = 1.3;
00123     QTimer *timer = new QTimer( this );
00124     connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
00125     timer->start( 2000 );
00126     gettimeofday( &last, 0 );
00127     first = TRUE;
00128     timeout();
00129 }
00130 
00131 void Load::paintEvent( QPaintEvent * )
00132 {
00133     QPainter p( this );
00134 
00135     int h = height() - 5;
00136 
00137     int mult = (int)(h / maxLoad);
00138 
00139     p.setPen( gray );
00140     p.drawLine( 0, h - mult, width(), h - mult );
00141     p.drawText( 0, h - mult, "100" );
00142     p.drawText( 0, h, "0" );
00143 
00144     p.setPen( green );
00145     for ( int i = 1; i < points; i++ ) {
00146         int x1 = (i - 1) * width() / points;
00147         int x2 = i * width() / points;
00148         p.drawLine( x1, h - systemLoad[i-1] * mult,
00149                     x2, h - systemLoad[i] * mult );
00150     }
00151 
00152     p.setPen( red );
00153     for ( int i = 1; i < points; i++ ) {
00154         int x1 = (i - 1) * width() / points;
00155         int x2 = i * width() / points;
00156         p.drawLine( x1, h - userLoad[i-1] * mult,
00157                     x2, h - userLoad[i] * mult );
00158     }
00159 }
00160 
00161 void Load::timeout()
00162 {
00163     int user;
00164     int usernice;
00165     int sys;
00166     int idle;
00167     FILE *fp;
00168     fp = fopen( "/proc/stat", "r" );
00169     fscanf( fp, "cpu %d %d %d %d", &user, &usernice, &sys, &idle );
00170     fclose( fp );
00171     struct timeval now;
00172     gettimeofday( &now, 0 );
00173     int tdiff = now.tv_usec - last.tv_usec;
00174     tdiff += (now.tv_sec - last.tv_sec) * 1000000;
00175     tdiff /= 10000;
00176 
00177     int udiff = user - lastUser;
00178     int sdiff = sys - lastSys;
00179     if ( tdiff > 0 ) {
00180         double uload = (double)udiff / (double)tdiff;
00181         double sload = (double)sdiff / (double)tdiff;
00182         if ( !first ) {
00183             for ( int i = 1; i < points; i++ ) {
00184                 userLoad[i-1] = userLoad[i];
00185                 systemLoad[i-1] = systemLoad[i];
00186             }
00187             userLoad[points-1] = uload;
00188             systemLoad[points-1] = sload;
00189 //          scroll( -width()/points, 0, QRect( 0, 0, width() - width()/points + 1, height() ) );
00190             repaint( TRUE );
00191             double ml = 1.3;
00192             /*
00193             for ( int i = 0; i < points; i++ ) {
00194                 if ( userLoad[i] > ml )
00195                     ml = userLoad[i];
00196             }
00197             */
00198             if ( maxLoad != ml ) {
00199                 maxLoad = ml;
00200                 update();
00201             }
00202         }
00203 
00204         last = now;
00205         lastUser = user;
00206         lastSys = sys;
00207         first = FALSE;
00208     } else if ( tdiff < 0 ) {
00209         last = now;
00210     }
00211 }
00212 

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