summaryrefslogtreecommitdiffstats
path: root/wifi/statistics.cpp
blob: 458b4abe0509f6422d3e292fbc95af9bc5178d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/***************************************************************************
                          statistics.cpp  -  description
                             -------------------
    begin                : Mon Aug 19 2002
    copyright            : (C) 2002 by Stefan Winter
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqwidget.h>
#include <tqpainter.h>
#include <tdelocale.h>
#include "statistics.h"
#include "interface_wireless.h"

Statistics::Statistics (Interface_wireless * device, bool showStatsNoise):TQWidget (0,
	 "KWiFiManager")
{
  this->setCaption (i18n ("Statistics - KWiFiManager"));
  this->device = device;
  this->showStatsNoise = showStatsNoise;
}

void
Statistics::paintEvent (TQPaintEvent *)
{
  statpainter = new TQPainter (this);
  TQColor farbe (255, 255, 255);
  statpainter->drawText (40, 30, i18n ("Noise/Signal Level Statistics"));
  statpainter->drawText (150, 260,
			 i18n ("BLUE = signal level, RED = noise level"));
  statpainter->drawLine (41, 244, 41, 260);
  statpainter->drawLine (520, 244, 520, 260);
  statpainter->drawText (21, 274, i18n ("-240 s"));
  statpainter->drawText (510, 274, i18n ("now"));
  statpainter->fillRect (41, 41, 480, 201, farbe);
  TQPointArray datensatz (240);
  TQPointArray datensatz2 (240);
  // we may need to scale the output to fit into the window, so here we 
  // determine the range of values
  int bottom =
    (device->sigLevelMin >
     device->noiseLevelMin) ? device->noiseLevelMin : device->sigLevelMin;
  int top =
    (device->sigLevelMax >
     device->noiseLevelMax) ? device->sigLevelMax : device->noiseLevelMax;
  int datarange = top - bottom;
  statpainter->drawText (10, 50, TQString ("%1").arg (top));
  statpainter->drawText (10, 240, TQString ("%1").arg (bottom));

  // if values are all below 0, this indicates proper dBm values

  if (top < 0) {
	statpainter->drawText (2,65, TQString("dBm"));
	statpainter->drawText (2,255, TQString("dBm"));
  }

  double scaleRatio;
  if (datarange != 0)
    {
      scaleRatio = 200. / datarange;
    }
  else
    {
      scaleRatio = 1.;
    }
  int i = 0;
  bool atLeastOneValid = false;
  for (int j = device->current; j < device->current + MAX_HISTORY; j++)
    {
      if (device->valid[j % MAX_HISTORY])
	{
	  atLeastOneValid = true;
	  datensatz.setPoint (i, 40 + (i * 2),
			      241 -
			      (int) (((device->sigLevel[j % MAX_HISTORY] + device->sigLevel[(j-1) % MAX_HISTORY]) / 2 -
				      bottom) * scaleRatio));
	  datensatz2.setPoint (i, 40 + (i * 2),
			       241 -
			       (int) (((device->noiseLevel[j % MAX_HISTORY] + device->noiseLevel[(j-1) % MAX_HISTORY]) / 2 -
				       bottom) * scaleRatio));
	}
      else
	{
	  datensatz.setPoint (i, 40 + (i * 2), 241);
	  datensatz2.setPoint (i, 40 + (i * 2), 241);
	}
      i++;
    };
  // the above point array is only useful if at least one entry has its
  // valid flag set. This fact is determined in the iteration before.
  // Only paint the point array if there is one valid entry
  if (atLeastOneValid)
    {
      if (showStatsNoise) {
        statpainter->setPen (red);
        statpainter->drawPolyline (datensatz2, 1);
      }
      statpainter->setPen (blue);
      statpainter->drawPolyline (datensatz, 1);
    }
  delete statpainter;
}