|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /* |
|
43 * KAsteroids - Copyright (c) Martin R. Jones 1997 |
|
44 * |
|
45 * Part of the KDE project |
|
46 */ |
|
47 |
|
48 #include <qpainter.h> |
|
49 //Added by qt3to4: |
|
50 #include <QResizeEvent> |
|
51 #include <Q3Frame> |
|
52 #include "ledmeter.h" |
|
53 |
|
54 KALedMeter::KALedMeter( QWidget *parent ) : Q3Frame( parent ) |
|
55 { |
|
56 mCRanges.setAutoDelete( TRUE ); |
|
57 mRange = 100; |
|
58 mCount = 20; |
|
59 mCurrentCount = 0; |
|
60 mValue = 0; |
|
61 setMinimumWidth( mCount * 2 + frameWidth() ); |
|
62 } |
|
63 |
|
64 void KALedMeter::setRange( int r ) |
|
65 { |
|
66 mRange = r; |
|
67 if ( mRange < 1 ) |
|
68 mRange = 1; |
|
69 setValue( mValue ); |
|
70 update(); |
|
71 } |
|
72 |
|
73 void KALedMeter::setCount( int c ) |
|
74 { |
|
75 mCount = c; |
|
76 if ( mCount < 1 ) |
|
77 mCount = 1; |
|
78 setMinimumWidth( mCount * 2 + frameWidth() ); |
|
79 calcColorRanges(); |
|
80 setValue( mValue ); |
|
81 update(); |
|
82 } |
|
83 |
|
84 void KALedMeter::setValue( int v ) |
|
85 { |
|
86 mValue = v; |
|
87 if ( mValue > mRange ) |
|
88 mValue = mRange; |
|
89 else if ( mValue < 0 ) |
|
90 mValue = 0; |
|
91 int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange; |
|
92 if ( c != mCurrentCount ) |
|
93 { |
|
94 mCurrentCount = c; |
|
95 update(); |
|
96 } |
|
97 } |
|
98 |
|
99 void KALedMeter::addColorRange( int pc, const QColor &c ) |
|
100 { |
|
101 ColorRange *cr = new ColorRange; |
|
102 cr->mPc = pc; |
|
103 cr->mColor = c; |
|
104 mCRanges.append( cr ); |
|
105 calcColorRanges(); |
|
106 } |
|
107 |
|
108 void KALedMeter::resizeEvent( QResizeEvent *e ) |
|
109 { |
|
110 Q3Frame::resizeEvent( e ); |
|
111 int w = ( width() - frameWidth() - 2 ) / mCount * mCount; |
|
112 w += frameWidth() + 2; |
|
113 setFrameRect( QRect( 0, 0, w, height() ) ); |
|
114 } |
|
115 |
|
116 void KALedMeter::drawContents( QPainter *p ) |
|
117 { |
|
118 QRect b = contentsRect(); |
|
119 |
|
120 unsigned cidx = 0; |
|
121 int ncol = mCount; |
|
122 QColor col = colorGroup().foreground(); |
|
123 |
|
124 if ( !mCRanges.isEmpty() ) |
|
125 { |
|
126 col = mCRanges.at( cidx )->mColor; |
|
127 ncol = mCRanges.at( cidx )->mValue; |
|
128 } |
|
129 p->setBrush( col ); |
|
130 p->setPen( col ); |
|
131 |
|
132 int lw = b.width() / mCount; |
|
133 int lx = b.left() + 1; |
|
134 for ( int i = 0; i < mCurrentCount; i++, lx += lw ) |
|
135 { |
|
136 if ( i > ncol ) |
|
137 { |
|
138 if ( ++cidx < mCRanges.count() ) |
|
139 { |
|
140 col = mCRanges.at( cidx )->mColor; |
|
141 ncol = mCRanges.at( cidx )->mValue; |
|
142 p->setBrush( col ); |
|
143 p->setPen( col ); |
|
144 } |
|
145 } |
|
146 |
|
147 p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 ); |
|
148 } |
|
149 } |
|
150 |
|
151 void KALedMeter::calcColorRanges() |
|
152 { |
|
153 int prev = 0; |
|
154 ColorRange *cr; |
|
155 for ( cr = mCRanges.first(); cr; cr = mCRanges.next() ) |
|
156 { |
|
157 cr->mValue = prev + cr->mPc * mCount / 100; |
|
158 prev = cr->mValue; |
|
159 } |
|
160 } |
|
161 |