~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorDisplayLib/BarGraph.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
 
 
22
#include <assert.h>
 
23
#include <string.h>
 
24
 
 
25
#include <QPainter>
 
26
 
 
27
#include <kdebug.h>
 
28
#include <kiconloader.h>
 
29
 
 
30
#include "StyleEngine.h"
 
31
 
 
32
#include "BarGraph.h"
 
33
 
 
34
BarGraph::BarGraph( QWidget *parent )
 
35
  : QWidget( parent )
 
36
{
 
37
  bars = 0;
 
38
  minValue = 0.0;
 
39
  maxValue = 100.0;
 
40
  lowerLimit = upperLimit = 0.0;
 
41
  lowerLimitActive = upperLimitActive = false;
 
42
 
 
43
  normalColor = KSGRD::Style->firstForegroundColor();
 
44
  alarmColor = KSGRD::Style->alarmColor();
 
45
  mBackgroundColor = KSGRD::Style->backgroundColor();
 
46
  fontSize = KSGRD::Style->fontSize();
 
47
 
 
48
  // Anything smaller than this does not make sense.
 
49
  setMinimumSize( 16, 16 );
 
50
  setSizePolicy( QSizePolicy( QSizePolicy::Expanding,
 
51
                 QSizePolicy::Expanding ) );
 
52
}
 
53
 
 
54
BarGraph::~BarGraph()
 
55
{
 
56
}
 
57
 
 
58
bool BarGraph::addBar( const QString &footer )
 
59
{
 
60
  samples.resize( bars + 1 );
 
61
  samples[ bars++ ] = 0.0;
 
62
  footers.append( footer );
 
63
 
 
64
  return true;
 
65
}
 
66
 
 
67
bool BarGraph::removeBar( uint idx )
 
68
{
 
69
  if ( idx >= bars ) {
 
70
    kDebug(1215) << "BarGraph::removeBar: idx " << idx << " out of range "
 
71
                  << bars << endl;
 
72
    return false;
 
73
  }
 
74
 
 
75
  samples.resize( --bars );
 
76
  footers.removeAll( footers.at( idx ) );
 
77
  update();
 
78
 
 
79
  return true;
 
80
}
 
81
 
 
82
void BarGraph::updateSamples( const QVector<double> &newSamples )
 
83
{
 
84
  samples = newSamples;
 
85
  update();
 
86
}
 
87
 
 
88
void BarGraph::changeRange( double min, double max )
 
89
{
 
90
  minValue = min;
 
91
  maxValue = max;
 
92
}
 
93
 
 
94
void BarGraph::paintEvent( QPaintEvent* )
 
95
{
 
96
  int w = width();
 
97
  int h = height();
 
98
 
 
99
  QPainter p( this );
 
100
 
 
101
  p.fillRect(0,0,w, h, mBackgroundColor);
 
102
 
 
103
  p.setBrush( palette().color( QPalette::Light) );
 
104
  p.setFont( QFont( p.font().family(), fontSize ) );
 
105
  QFontMetrics fm( p.font() );
 
106
 
 
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 );
 
111
 
 
112
  p.setClipRect( 1, 1, w - 2, h - 2 );
 
113
 
 
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;
 
124
 
 
125
    int barHeight;
 
126
    if ( showLabels )
 
127
      barHeight = h - 2 - ( 2 * fm.lineSpacing() ) - 2;
 
128
    else
 
129
      barHeight = h - 2;
 
130
 
 
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;
 
137
 
 
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 );
 
148
      }
 
149
 
 
150
      if ( ( upperLimitActive && samples[ b ] > upperLimit ) ||
 
151
           ( lowerLimitActive && samples[ b ] < lowerLimit ) )
 
152
        p.setPen( alarmColor );
 
153
      else
 
154
        p.setPen( normalColor );
 
155
 
 
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 ] ) );
 
163
      }
 
164
    }
 
165
  }
 
166
 
 
167
  p.end();
 
168
}
 
169
 
 
170
#include "BarGraph.moc"