Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
trafficgraphwidget.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "trafficgraphwidget.h"
6 #include "clientmodel.h"
7 
8 #include <QPainter>
9 #include <QColor>
10 #include <QTimer>
11 
12 #include <cmath>
13 
14 #define DESIRED_SAMPLES 800
15 
16 #define XMARGIN 10
17 #define YMARGIN 10
18 
20  QWidget(parent),
21  timer(0),
22  fMax(0.0f),
23  nMins(0),
24  vSamplesIn(),
25  vSamplesOut(),
26  nLastBytesIn(0),
27  nLastBytesOut(0),
28  clientModel(0)
29 {
30  timer = new QTimer(this);
31  connect(timer, SIGNAL(timeout()), SLOT(updateRates()));
32 }
33 
35 {
36  clientModel = model;
37  if(model) {
40  }
41 }
42 
44 {
45  return nMins;
46 }
47 
48 void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples)
49 {
50  int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
51  int sampleCount = samples.size(), x = XMARGIN + w, y;
52  if(sampleCount > 0) {
53  path.moveTo(x, YMARGIN + h);
54  for(int i = 0; i < sampleCount; ++i) {
55  x = XMARGIN + w - w * i / DESIRED_SAMPLES;
56  y = YMARGIN + h - (int)(h * samples.at(i) / fMax);
57  path.lineTo(x, y);
58  }
59  path.lineTo(x, YMARGIN + h);
60  }
61 }
62 
63 void TrafficGraphWidget::paintEvent(QPaintEvent *)
64 {
65  QPainter painter(this);
66  painter.fillRect(rect(), Qt::black);
67 
68  if(fMax <= 0.0f) return;
69 
70  QColor axisCol(Qt::gray);
71  int h = height() - YMARGIN * 2;
72  painter.setPen(axisCol);
73  painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);
74 
75  // decide what order of magnitude we are
76  int base = floor(log10(fMax));
77  float val = pow(10.0f, base);
78 
79  const QString units = tr("KB/s");
80  // draw lines
81  painter.setPen(axisCol);
82  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax, QString("%1 %2").arg(val).arg(units));
83  for(float y = val; y < fMax; y += val) {
84  int yy = YMARGIN + h - h * y / fMax;
85  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
86  }
87  // if we drew 3 or fewer lines, break them up at the next lower order of magnitude
88  if(fMax / val <= 3.0f) {
89  axisCol = axisCol.darker();
90  val = pow(10.0f, base - 1);
91  painter.setPen(axisCol);
92  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax, QString("%1 %2").arg(val).arg(units));
93  int count = 1;
94  for(float y = val; y < fMax; y += val, count++) {
95  // don't overwrite lines drawn above
96  if(count % 10 == 0)
97  continue;
98  int yy = YMARGIN + h - h * y / fMax;
99  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
100  }
101  }
102 
103  if(!vSamplesIn.empty()) {
104  QPainterPath p;
105  paintPath(p, vSamplesIn);
106  painter.fillPath(p, QColor(0, 255, 0, 128));
107  painter.setPen(Qt::green);
108  painter.drawPath(p);
109  }
110  if(!vSamplesOut.empty()) {
111  QPainterPath p;
113  painter.fillPath(p, QColor(255, 0, 0, 128));
114  painter.setPen(Qt::red);
115  painter.drawPath(p);
116  }
117 }
118 
120 {
121  if(!clientModel) return;
122 
123  quint64 bytesIn = clientModel->getTotalBytesRecv(),
124  bytesOut = clientModel->getTotalBytesSent();
125  float inRate = (bytesIn - nLastBytesIn) / 1024.0f * 1000 / timer->interval();
126  float outRate = (bytesOut - nLastBytesOut) / 1024.0f * 1000 / timer->interval();
127  vSamplesIn.push_front(inRate);
128  vSamplesOut.push_front(outRate);
129  nLastBytesIn = bytesIn;
130  nLastBytesOut = bytesOut;
131 
132  while(vSamplesIn.size() > DESIRED_SAMPLES) {
133  vSamplesIn.pop_back();
134  }
135  while(vSamplesOut.size() > DESIRED_SAMPLES) {
136  vSamplesOut.pop_back();
137  }
138 
139  float tmax = 0.0f;
140  foreach(float f, vSamplesIn) {
141  if(f > tmax) tmax = f;
142  }
143  foreach(float f, vSamplesOut) {
144  if(f > tmax) tmax = f;
145  }
146  fMax = tmax;
147  update();
148 }
149 
151 {
152  nMins = mins;
153  int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES;
154  timer->stop();
155  timer->setInterval(msecsPerSample);
156 
157  clear();
158 }
159 
161 {
162  timer->stop();
163 
164  vSamplesOut.clear();
165  vSamplesIn.clear();
166  fMax = 0.0f;
167 
168  if(clientModel) {
171  }
172  timer->start();
173 }
void paintPath(QPainterPath &path, QQueue< float > &samples)
ClientModel * clientModel
quint64 getTotalBytesRecv() const
Definition: clientmodel.cpp:68
QQueue< float > vSamplesIn
QQueue< float > vSamplesOut
void paintEvent(QPaintEvent *)
int getGraphRangeMins() const
Model for Bitcoin network client.
Definition: clientmodel.h:36
#define XMARGIN
quint64 getTotalBytesSent() const
Definition: clientmodel.cpp:73
void setGraphRangeMins(int mins)
void setClientModel(ClientModel *model)
TrafficGraphWidget(QWidget *parent=0)
#define YMARGIN
#define DESIRED_SAMPLES