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.
28 #include <kiconloader.h>
30 #include "StyleEngine.h"
34 BarGraph::BarGraph( QWidget
*parent
)
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
) );
58 bool BarGraph::addBar( const QString
&footer
)
60 samples
.resize( bars
+ 1 );
61 samples
[ bars
++ ] = 0.0;
62 footers
.append( footer
);
67 bool BarGraph::removeBar( uint idx
)
70 kDebug(1215) << "BarGraph::removeBar: idx " << idx
<< " out of range "
75 samples
.resize( --bars
);
76 footers
.removeAll( footers
.at( idx
) );
82 void BarGraph::updateSamples( const QVector
<double> &newSamples
)
88 void BarGraph::changeRange( double min
, double max
)
94 void BarGraph::paintEvent( QPaintEvent
* )
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 );
115 int barWidth
= ( w
- 2 ) / bars
;
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
)
127 barHeight
= h
- 2 - ( 2 * fm
.lineSpacing() ) - 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. */
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
) ) ) );
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,
150 if ( ( upperLimitActive
&& samples
[ b
] > upperLimit
) ||
151 ( lowerLimitActive
&& samples
[ b
] < lowerLimit
) )
152 p
.setPen( alarmColor
);
154 p
.setPen( normalColor
);
157 p
.drawText( b
* barWidth
+ 3, h
- ( 2 * fm
.lineSpacing() ) - 2,
158 barWidth
- 2 * 3, fm
.lineSpacing(), Qt::AlignCenter
,
160 p
.drawText( b
* barWidth
+ 3, h
- fm
.lineSpacing() - 2,
161 barWidth
- 2 * 3, fm
.lineSpacing(), Qt::AlignCenter
,
162 QString( "%1" ).arg( samples
[ b
] ) );
170 #include "BarGraph.moc"