Enable storage backend.
[kdenetwork.git] / wifi / statistics.cpp
blob66cdd2857f57bc1943cfed1bc197a728d059249b
1 /***************************************************************************
2 statistics.cpp - description
3 -------------------
4 begin : Mon Aug 19 2002
5 copyright : (C) 2002 by Stefan Winter
6 email : mail@stefan-winter.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <QWidget>
19 #include <QPainter>
20 #include <QPolygon>
21 #include <QPaintEvent>
22 #include <klocale.h>
23 #include "statistics.h"
24 #include "interface_wireless.h"
25 #include "kwifi_settings.h"
27 Statistics::Statistics ( Interface_wireless * device, QWidget * parent ):QWidget ( parent )
29 this->device = device;
32 int
33 Statistics::determineTop ( )
35 return ( device->sigLevelMax > device->noiseLevelMax ) ? device->sigLevelMax : device->noiseLevelMax;
38 int
39 Statistics::determineBottom ( )
41 return ( device->sigLevelMin > device->noiseLevelMin ) ? device->noiseLevelMin : device->sigLevelMin;
44 void
45 Statistics::paintEvent ( QPaintEvent * )
47 statpainter = new QPainter ( this );
48 QColor farbe ( 255, 255, 255 );
49 statpainter->fillRect ( 0, 0, 480, 201, farbe );
50 QPolygon signal ( 240 );
51 QPolygon noise ( 240 );
53 int datarange = determineTop ( ) - determineBottom ( );
55 // if values are all below 0, this indicates proper dBm values
57 double scaleRatio;
58 if ( datarange != 0 )
59 scaleRatio = 200. / datarange;
60 else
61 scaleRatio = 1.;
63 int i = 0;
64 bool atLeastOneValid = false;
65 for ( int j = device->current; j < device->current + MAX_HISTORY; j++ )
67 if ( device->valid[j % MAX_HISTORY] )
69 atLeastOneValid = true;
70 signal.setPoint ( i, ( i * 2 ),
71 200 -
72 ( int ) ( ( ( device->sigLevel[j % MAX_HISTORY] +
73 device->sigLevel[( j - 1 ) % MAX_HISTORY] ) / 2 -
74 determineBottom ( ) ) * scaleRatio ) );
75 noise.setPoint ( i, ( i * 2 ),
76 200 -
77 ( int ) ( ( ( device->noiseLevel[j % MAX_HISTORY] +
78 device->noiseLevel[( j - 1 ) % MAX_HISTORY] ) / 2 -
79 determineBottom ( ) ) * scaleRatio ) );
81 else
83 signal.setPoint ( i, ( i * 2 ), 200 );
84 noise.setPoint ( i, ( i * 2 ), 200 );
86 i++;
88 // the above point array is only useful if at least one entry has its
89 // valid flag set. This fact is determined in the iteration before.
90 // Only paint the point array if there is one valid entry
91 if ( atLeastOneValid )
93 if ( KWiFiSettings::self()->showNoiseInStats() )
95 statpainter->setPen ( Qt::red );
96 statpainter->drawPolyline ( noise );
98 statpainter->setPen ( Qt::blue );
99 statpainter->drawPolyline ( signal );
101 delete statpainter;