dtor first
[personal-kdebase.git] / workspace / ksysguard / gui / SensorDisplayLib / BarGraph.cc
blob6cf2bbdf93991a387cd87fa735f25b4a9412b927
1 /*
2 KSysGuard, the KDE System Guard
4 Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License version 2 or at your option version 3 as published by
9 the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <assert.h>
23 #include <string.h>
25 #include <QPainter>
27 #include <kdebug.h>
28 #include <kiconloader.h>
30 #include "StyleEngine.h"
32 #include "BarGraph.h"
34 BarGraph::BarGraph( QWidget *parent )
35 : QWidget( parent )
37 bars = 0;
38 minValue = 0.0;
39 maxValue = 100.0;
40 lowerLimit = upperLimit = 0.0;
41 lowerLimitActive = upperLimitActive = false;
43 normalColor = KSGRD::Style->firstForegroundColor();
44 alarmColor = KSGRD::Style->alarmColor();
45 mBackgroundColor = KSGRD::Style->backgroundColor();
46 fontSize = KSGRD::Style->fontSize();
48 // Anything smaller than this does not make sense.
49 setMinimumSize( 16, 16 );
50 setSizePolicy( QSizePolicy( QSizePolicy::Expanding,
51 QSizePolicy::Expanding ) );
54 BarGraph::~BarGraph()
58 bool BarGraph::addBar( const QString &footer )
60 samples.resize( bars + 1 );
61 samples[ bars++ ] = 0.0;
62 footers.append( footer );
64 return true;
67 bool BarGraph::removeBar( uint idx )
69 if ( idx >= bars ) {
70 kDebug(1215) << "BarGraph::removeBar: idx " << idx << " out of range "
71 << bars << endl;
72 return false;
75 samples.resize( --bars );
76 footers.removeAll( footers.at( idx ) );
77 update();
79 return true;
82 void BarGraph::updateSamples( const QVector<double> &newSamples )
84 samples = newSamples;
85 update();
88 void BarGraph::changeRange( double min, double max )
90 minValue = min;
91 maxValue = max;
94 void BarGraph::paintEvent( QPaintEvent* )
96 int w = width();
97 int h = height();
99 QPainter p( this );
101 p.fillRect(0,0,w, h, mBackgroundColor);
103 p.setBrush( palette().color( QPalette::Light) );
104 p.setFont( QFont( p.font().family(), fontSize ) );
105 QFontMetrics fm( p.font() );
107 /* Draw white line along the bottom and the right side of the
108 * widget to create a 3D like look. */
109 p.drawLine( 0, h - 1, w - 1, h - 1 );
110 p.drawLine( w - 1, 0, w - 1, h - 1 );
112 p.setClipRect( 1, 1, w - 2, h - 2 );
114 if ( bars > 0 ) {
115 int barWidth = ( w - 2 ) / bars;
116 uint b;
117 /* Labels are only printed underneath the bars if the labels
118 * for all bars are smaller than the bar width. If a single
119 * label does not fit no label is shown. */
120 bool showLabels = true;
121 for ( b = 0; b < bars; b++ )
122 if ( fm.width( footers[ b ] ) > barWidth )
123 showLabels = false;
125 int barHeight;
126 if ( showLabels )
127 barHeight = h - 2 - ( 2 * fm.lineSpacing() ) - 2;
128 else
129 barHeight = h - 2;
131 for ( uint b = 0; b < bars; b++ ) {
132 int topVal = (int) ( (float)barHeight / maxValue *
133 ( samples[ b ] - minValue ) );
134 /* TODO: This widget does not handle negative values properly. */
135 if ( topVal < 0 )
136 topVal = 0;
138 for ( int i = 0; i < barHeight && i < topVal; i += 2 ) {
139 if ( ( upperLimitActive && samples[ b ] > upperLimit ) ||
140 ( lowerLimitActive && samples[ b ] < lowerLimit ) )
141 p.setPen( alarmColor.light( static_cast<int>( 30 + ( 70.0 /
142 ( barHeight + 1 ) * i ) ) ) );
143 else
144 p.setPen( normalColor.light( static_cast<int>( 30 + ( 70.0 /
145 ( barHeight + 1 ) * i ) ) ) );
146 p.drawLine( b * barWidth + 3, barHeight - i, ( b + 1 ) * barWidth - 3,
147 barHeight - i );
150 if ( ( upperLimitActive && samples[ b ] > upperLimit ) ||
151 ( lowerLimitActive && samples[ b ] < lowerLimit ) )
152 p.setPen( alarmColor );
153 else
154 p.setPen( normalColor );
156 if ( showLabels ) {
157 p.drawText( b * barWidth + 3, h - ( 2 * fm.lineSpacing() ) - 2,
158 barWidth - 2 * 3, fm.lineSpacing(), Qt::AlignCenter,
159 footers[ b ] );
160 p.drawText( b * barWidth + 3, h - fm.lineSpacing() - 2,
161 barWidth - 2 * 3, fm.lineSpacing(), Qt::AlignCenter,
162 QString( "%1" ).arg( samples[ b ] ) );
167 p.end();
170 #include "BarGraph.moc"