Qt 4.8: ledmeter.cpp Example File (graphicsview/portedasteroids/ledmeter.cpp)

Qt Reference Documentation

- Qt 4.8
- ALL VERSIONS

- A
- A
- A
- Print

# ledmeter.cpp Example File

graphicsview/portedasteroids/ledmeter.cpp

     /****************************************************************************
     **
     ** Copyright (C) 2015 The Qt Company Ltd.
     ** Contact: http://www.qt.io/licensing/
     **
     ** This file is part of the examples of the Qt Toolkit.
     **
     ** $QT_BEGIN_LICENSE:BSD$
     ** You may use this file under the terms of the BSD license as follows:
     **
     ** "Redistribution and use in source and binary forms, with or without
     ** modification, are permitted provided that the following conditions are
     ** met:
     **   * Redistributions of source code must retain the above copyright
     **     notice, this list of conditions and the following disclaimer.
     **   * Redistributions in binary form must reproduce the above copyright
     **     notice, this list of conditions and the following disclaimer in
     **     the documentation and/or other materials provided with the
     **     distribution.
     **   * Neither the name of The Qt Company Ltd nor the names of its
     **     contributors may be used to endorse or promote products derived
     **     from this software without specific prior written permission.
     **
     **
     ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
     **
     ** $QT_END_LICENSE$
     **
     ****************************************************************************/

     /*
      * KAsteroids - Copyright (c) Martin R. Jones 1997
      *
      * Part of the KDE project
      */

     #include <QPainter>
     #include <QResizeEvent>
     #include <QFrame>
     #include <QColorGroup>
     #include "ledmeter.h"

     KALedMeter::KALedMeter( QWidget *parent ) : QFrame( parent )
     {
         mRange = 100;
         mCount = 20;
         mCurrentCount = 0;
         mValue = 0;
         setMinimumWidth( mCount * 2 + frameWidth() );
     }

     KALedMeter::~KALedMeter()
     {
         qDeleteAll(mCRanges);
         mCRanges.clear();
     }

     void KALedMeter::setRange( int r )
     {
         mRange = r;
         if ( mRange < 1 )
             mRange = 1;
         setValue( mValue );
         update();
     }

     void KALedMeter::setCount( int c )
     {
         mCount = c;
         if ( mCount < 1 )
             mCount = 1;
         setMinimumWidth( mCount * 2 + frameWidth() );
         calcColorRanges();
         setValue( mValue );
         update();
     }

     void KALedMeter::setValue( int v )
     {
         mValue = v;
         if ( mValue > mRange )
             mValue = mRange;
         else if ( mValue < 0 )
             mValue = 0;
         int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange;
         if ( c != mCurrentCount )
         {
             mCurrentCount = c;
             update();
         }
     }

     void KALedMeter::addColorRange( int pc, const QColor &c )
     {
         ColorRange *cr = new ColorRange;
         cr->mPc = pc;
         cr->mColor = c;
         mCRanges.append( cr );
         calcColorRanges();
     }

     void KALedMeter::resizeEvent( QResizeEvent *e )
     {
         QFrame::resizeEvent( e );
         int w = ( width() - frameWidth() - 2 ) / mCount * mCount;
         w += frameWidth() + 2;
         setFrameRect( QRect( 0, 0, w, height() ) );
     }

     void KALedMeter::paintEvent(QPaintEvent *event)
     {
         QFrame::paintEvent(event);

         QRect b = contentsRect();
         QPainter p(this);

         unsigned cidx = 0;
         int ncol = mCount;
         QColor col = palette().foreground().color();

         if ( !mCRanges.isEmpty() )
         {
             col = mCRanges.at( cidx )->mColor;
             ncol = mCRanges.at( cidx )->mValue;
         }
         p.setBrush( col );
         p.setPen( col );

         int lw = b.width() / mCount;
         int lx = b.left() + 1;
         for ( int i = 0; i < mCurrentCount; i++, lx += lw )
         {
             if ( i > ncol )
             {
                 if ( ++cidx < mCRanges.count() )
                 {
                     col = mCRanges.at( cidx )->mColor;
                     ncol = mCRanges.at( cidx )->mValue;
                     p.setBrush( col );
                     p.setPen( col );
                 }
             }

             p.drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
         }
     }

     void KALedMeter::calcColorRanges()
     {
         int prev = 0;

         for(QList<ColorRange*>::iterator it = mCRanges.begin(); it != mCRanges.end(); it++)
         {
             ColorRange *cr = *it;
             cr->mValue = prev + cr->mPc * mCount / 100;
             prev = cr->mValue;
         }
     }