1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 |
|
---|