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

ledmeter.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  * KAsteroids - Copyright (c) Martin R. Jones 1997
00021  *
00022  * Part of the KDE project
00023  */
00024 
00025 #include <qpainter.h>
00026 #include "ledmeter.h"
00027 
00028 KALedMeter::KALedMeter( QWidget *parent ) : QFrame( parent )
00029 {
00030     mCRanges.setAutoDelete( TRUE );
00031     mRange = 100;
00032     mCount = 20;
00033     mCurrentCount = 0;
00034     mValue = 0;
00035     setMinimumWidth( mCount * 2 + frameWidth() );
00036 }
00037 
00038 void KALedMeter::setRange( int r )
00039 {
00040     mRange = r;
00041     if ( mRange < 1 )
00042         mRange = 1;
00043     setValue( mValue );
00044     update();
00045 }
00046 
00047 void KALedMeter::setCount( int c )
00048 {
00049     mCount = c;
00050     if ( mCount < 1 )
00051         mCount = 1;
00052     setMinimumWidth( mCount * 2 + frameWidth() );
00053     calcColorRanges();
00054     setValue( mValue );
00055     update();
00056 }
00057 
00058 void KALedMeter::setValue( int v )
00059 {
00060     mValue = v;
00061     if ( mValue > mRange )
00062         mValue = mRange;
00063     else if ( mValue < 0 )
00064         mValue = 0;
00065     int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange;
00066     if ( c != mCurrentCount )
00067     {
00068         mCurrentCount = c;
00069         update();
00070     }
00071 }
00072 
00073 void KALedMeter::addColorRange( int pc, const QColor &c )
00074 {
00075     ColorRange *cr = new ColorRange;
00076     cr->mPc = pc;
00077     cr->mColor = c;
00078     mCRanges.append( cr );
00079     calcColorRanges();
00080 }
00081 
00082 void KALedMeter::resizeEvent( QResizeEvent *e )
00083 {
00084     QFrame::resizeEvent( e );
00085     int w = ( width() - frameWidth() - 2 ) / mCount * mCount;
00086     w += frameWidth() + 2;
00087     setFrameRect( QRect( 0, 0, w, height() ) );
00088 }
00089 
00090 void KALedMeter::drawContents( QPainter *p )
00091 {
00092     QRect b = contentsRect();
00093 
00094     unsigned cidx = 0;
00095     int ncol = mCount;
00096     QColor col = colorGroup().foreground();
00097    
00098     if ( !mCRanges.isEmpty() )
00099     {
00100         col = mCRanges.at( cidx )->mColor;
00101         ncol = mCRanges.at( cidx )->mValue;
00102     }
00103     p->setBrush( col );
00104     p->setPen( col );
00105 
00106     int lw = b.width() / mCount;
00107     int lx = b.left() + 1;
00108     for ( int i = 0; i < mCurrentCount; i++, lx += lw )
00109     {
00110         if ( i > ncol )
00111         {
00112             if ( ++cidx < mCRanges.count() )
00113             {
00114                 col = mCRanges.at( cidx )->mColor;
00115                 ncol = mCRanges.at( cidx )->mValue;
00116                 p->setBrush( col );
00117                 p->setPen( col );
00118             }
00119         }
00120 
00121         p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
00122     }
00123 }
00124 
00125 void KALedMeter::calcColorRanges()
00126 {
00127     int prev = 0;
00128     ColorRange *cr;
00129     for ( cr = mCRanges.first(); cr; cr = mCRanges.next() )
00130     {
00131         cr->mValue = prev + cr->mPc * mCount / 100;
00132         prev = cr->mValue;
00133     }
00134 }
00135 

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