summaryrefslogtreecommitdiff
path: root/noncore/games/qasteroids/ledmeter.cpp
Unidiff
Diffstat (limited to 'noncore/games/qasteroids/ledmeter.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/qasteroids/ledmeter.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/noncore/games/qasteroids/ledmeter.cpp b/noncore/games/qasteroids/ledmeter.cpp
new file mode 100644
index 0000000..f4d4d1a
--- a/dev/null
+++ b/noncore/games/qasteroids/ledmeter.cpp
@@ -0,0 +1,135 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#include <qpainter.h>
26#include "ledmeter.h"
27
28KALedMeter::KALedMeter( QWidget *parent ) : QFrame( parent )
29{
30 mCRanges.setAutoDelete( TRUE );
31 mRange = 100;
32 mCount = 20;
33 mCurrentCount = 0;
34 mValue = 0;
35 setMinimumWidth( mCount * 2 + frameWidth() );
36}
37
38void KALedMeter::setRange( int r )
39{
40 mRange = r;
41 if ( mRange < 1 )
42 mRange = 1;
43 setValue( mValue );
44 update();
45}
46
47void KALedMeter::setCount( int c )
48{
49 mCount = c;
50 if ( mCount < 1 )
51 mCount = 1;
52 setMinimumWidth( mCount * 2 + frameWidth() );
53 calcColorRanges();
54 setValue( mValue );
55 update();
56}
57
58void KALedMeter::setValue( int v )
59{
60 mValue = v;
61 if ( mValue > mRange )
62 mValue = mRange;
63 else if ( mValue < 0 )
64 mValue = 0;
65 int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange;
66 if ( c != mCurrentCount )
67 {
68 mCurrentCount = c;
69 update();
70 }
71}
72
73void KALedMeter::addColorRange( int pc, const QColor &c )
74{
75 ColorRange *cr = new ColorRange;
76 cr->mPc = pc;
77 cr->mColor = c;
78 mCRanges.append( cr );
79 calcColorRanges();
80}
81
82void KALedMeter::resizeEvent( QResizeEvent *e )
83{
84 QFrame::resizeEvent( e );
85 int w = ( width() - frameWidth() - 2 ) / mCount * mCount;
86 w += frameWidth() + 2;
87 setFrameRect( QRect( 0, 0, w, height() ) );
88}
89
90void KALedMeter::drawContents( QPainter *p )
91{
92 QRect b = contentsRect();
93
94 unsigned cidx = 0;
95 int ncol = mCount;
96 QColor col = colorGroup().foreground();
97
98 if ( !mCRanges.isEmpty() )
99 {
100 col = mCRanges.at( cidx )->mColor;
101 ncol = mCRanges.at( cidx )->mValue;
102 }
103 p->setBrush( col );
104 p->setPen( col );
105
106 int lw = b.width() / mCount;
107 int lx = b.left() + 1;
108 for ( int i = 0; i < mCurrentCount; i++, lx += lw )
109 {
110 if ( i > ncol )
111 {
112 if ( ++cidx < mCRanges.count() )
113 {
114 col = mCRanges.at( cidx )->mColor;
115 ncol = mCRanges.at( cidx )->mValue;
116 p->setBrush( col );
117 p->setPen( col );
118 }
119 }
120
121 p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
122 }
123}
124
125void KALedMeter::calcColorRanges()
126{
127 int prev = 0;
128 ColorRange *cr;
129 for ( cr = mCRanges.first(); cr; cr = mCRanges.next() )
130 {
131 cr->mValue = prev + cr->mPc * mCount / 100;
132 prev = cr->mValue;
133 }
134}
135