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