Raven Core  3.0.0
P2P Digital Currency
trafficgraphwidget.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "trafficgraphwidget.h"
7 #include "clientmodel.h"
8 
9 #include <QPainter>
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 sampleCount = samples.size();
52  if(sampleCount > 0) {
53  int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
54  int x = XMARGIN + w;
55  path.moveTo(x, YMARGIN + h);
56  for(int i = 0; i < sampleCount; ++i) {
57  x = XMARGIN + w - w * i / DESIRED_SAMPLES;
58  int y = YMARGIN + h - (int)(h * samples.at(i) / fMax);
59  path.lineTo(x, y);
60  }
61  path.lineTo(x, YMARGIN + h);
62  }
63 }
64 
65 void TrafficGraphWidget::paintEvent(QPaintEvent *)
66 {
67  QPainter painter(this);
68  painter.fillRect(rect(), Qt::black);
69 
70  if(fMax <= 0.0f) return;
71 
72  QColor axisCol(Qt::gray);
73  int h = height() - YMARGIN * 2;
74  painter.setPen(axisCol);
75  painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);
76 
77  // decide what order of magnitude we are
78  int base = floor(log10(fMax));
79  float val = pow(10.0f, base);
80 
81  const QString units = tr("KB/s");
82  const float yMarginText = 2.0;
83 
84  // draw lines
85  painter.setPen(axisCol);
86  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
87  for(float y = val; y < fMax; y += val) {
88  int yy = YMARGIN + h - h * y / fMax;
89  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
90  }
91  // if we drew 3 or fewer lines, break them up at the next lower order of magnitude
92  if(fMax / val <= 3.0f) {
93  axisCol = axisCol.darker();
94  val = pow(10.0f, base - 1);
95  painter.setPen(axisCol);
96  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
97  int count = 1;
98  for(float y = val; y < fMax; y += val, count++) {
99  // don't overwrite lines drawn above
100  if(count % 10 == 0)
101  continue;
102  int yy = YMARGIN + h - h * y / fMax;
103  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
104  }
105  }
106 
107  if(!vSamplesIn.empty()) {
108  QPainterPath p;
109  paintPath(p, vSamplesIn);
110  painter.fillPath(p, QColor(0, 255, 0, 128));
111  painter.setPen(Qt::green);
112  painter.drawPath(p);
113  }
114  if(!vSamplesOut.empty()) {
115  QPainterPath p;
117  painter.fillPath(p, QColor(255, 0, 0, 128));
118  painter.setPen(Qt::red);
119  painter.drawPath(p);
120  }
121 }
122 
124 {
125  if(!clientModel) return;
126 
127  quint64 bytesIn = clientModel->getTotalBytesRecv(),
128  bytesOut = clientModel->getTotalBytesSent();
129  float inRate = (bytesIn - nLastBytesIn) / 1024.0f * 1000 / timer->interval();
130  float outRate = (bytesOut - nLastBytesOut) / 1024.0f * 1000 / timer->interval();
131  vSamplesIn.push_front(inRate);
132  vSamplesOut.push_front(outRate);
133  nLastBytesIn = bytesIn;
134  nLastBytesOut = bytesOut;
135 
136  while(vSamplesIn.size() > DESIRED_SAMPLES) {
137  vSamplesIn.pop_back();
138  }
139  while(vSamplesOut.size() > DESIRED_SAMPLES) {
140  vSamplesOut.pop_back();
141  }
142 
143  float tmax = 0.0f;
144  for (float f : vSamplesIn) {
145  if(f > tmax) tmax = f;
146  }
147  for (float f : vSamplesOut) {
148  if(f > tmax) tmax = f;
149  }
150  fMax = tmax;
151  update();
152 }
153 
155 {
156  nMins = mins;
157  int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES;
158  timer->stop();
159  timer->setInterval(msecsPerSample);
160 
161  clear();
162 }
163 
165 {
166  timer->stop();
167 
168  vSamplesOut.clear();
169  vSamplesIn.clear();
170  fMax = 0.0f;
171 
172  if(clientModel) {
175  }
176  timer->start();
177 }
void paintPath(QPainterPath &path, QQueue< float > &samples)
ClientModel * clientModel
QQueue< float > vSamplesIn
QQueue< float > vSamplesOut
void paintEvent(QPaintEvent *)
quint64 getTotalBytesSent() const
quint64 getTotalBytesRecv() const
Model for Raven network client.
Definition: clientmodel.h:39
#define XMARGIN
void setGraphRangeMins(int mins)
void setClientModel(ClientModel *model)
TrafficGraphWidget(QWidget *parent=0)
#define YMARGIN
#define DESIRED_SAMPLES