source: trunk/examples/demo/qasteroids/ledmeter.cpp@ 164

Last change on this file since 164 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1/*
2 * KAsteroids - Copyright (c) Martin R. Jones 1997
3 *
4 * Part of the KDE project
5 */
6
7#include <qpainter.h>
8#include "ledmeter.h"
9
10KALedMeter::KALedMeter( QWidget *parent ) : QFrame( parent )
11{
12 mCRanges.setAutoDelete( TRUE );
13 mRange = 100;
14 mCount = 20;
15 mCurrentCount = 0;
16 mValue = 0;
17 setMinimumWidth( mCount * 2 + frameWidth() );
18}
19
20void KALedMeter::setRange( int r )
21{
22 mRange = r;
23 if ( mRange < 1 )
24 mRange = 1;
25 setValue( mValue );
26 update();
27}
28
29void KALedMeter::setCount( int c )
30{
31 mCount = c;
32 if ( mCount < 1 )
33 mCount = 1;
34 setMinimumWidth( mCount * 2 + frameWidth() );
35 calcColorRanges();
36 setValue( mValue );
37 update();
38}
39
40void KALedMeter::setValue( int v )
41{
42 mValue = v;
43 if ( mValue > mRange )
44 mValue = mRange;
45 else if ( mValue < 0 )
46 mValue = 0;
47 int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange;
48 if ( c != mCurrentCount )
49 {
50 mCurrentCount = c;
51 update();
52 }
53}
54
55void KALedMeter::addColorRange( int pc, const QColor &c )
56{
57 ColorRange *cr = new ColorRange;
58 cr->mPc = pc;
59 cr->mColor = c;
60 mCRanges.append( cr );
61 calcColorRanges();
62}
63
64void KALedMeter::resizeEvent( QResizeEvent *e )
65{
66 QFrame::resizeEvent( e );
67 int w = ( width() - frameWidth() - 2 ) / mCount * mCount;
68 w += frameWidth() + 2;
69 setFrameRect( QRect( 0, 0, w, height() ) );
70}
71
72void KALedMeter::drawContents( QPainter *p )
73{
74 QRect b = contentsRect();
75
76 unsigned cidx = 0;
77 int ncol = mCount;
78 QColor col = colorGroup().foreground();
79
80 if ( !mCRanges.isEmpty() )
81 {
82 col = mCRanges.at( cidx )->mColor;
83 ncol = mCRanges.at( cidx )->mValue;
84 }
85 p->setBrush( col );
86 p->setPen( col );
87
88 int lw = b.width() / mCount;
89 int lx = b.left() + 1;
90 for ( int i = 0; i < mCurrentCount; i++, lx += lw )
91 {
92 if ( i > ncol )
93 {
94 if ( ++cidx < mCRanges.count() )
95 {
96 col = mCRanges.at( cidx )->mColor;
97 ncol = mCRanges.at( cidx )->mValue;
98 p->setBrush( col );
99 p->setPen( col );
100 }
101 }
102
103 p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
104 }
105}
106
107void KALedMeter::calcColorRanges()
108{
109 int prev = 0;
110 ColorRange *cr;
111 for ( cr = mCRanges.first(); cr; cr = mCRanges.next() )
112 {
113 cr->mValue = prev + cr->mPc * mCount / 100;
114 prev = cr->mValue;
115 }
116}
117
Note: See TracBrowser for help on using the repository browser.