Fabcoin Core  0.16.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 <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 sampleCount = samples.size();
51  if(sampleCount > 0) {
52  int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
53  int x = XMARGIN + w;
54  path.moveTo(x, YMARGIN + h);
55  for(int i = 0; i < sampleCount; ++i) {
56  x = XMARGIN + w - w * i / DESIRED_SAMPLES;
57  int 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  for (float f : vSamplesIn) {
144  if(f > tmax) tmax = f;
145  }
146  for (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 }
void paintPath(QPainterPath &path, QQueue< float > &samples)
ClientModel * clientModel
#define h(i)
Definition: sha.cpp:736
quint64 getTotalBytesRecv() const
QQueue< float > vSamplesIn
size_t count
Definition: ExecStats.cpp:37
QQueue< float > vSamplesOut
void paintEvent(QPaintEvent *)
#define x(i)
int getGraphRangeMins() const
Model for Fabcoin network client.
Definition: clientmodel.h:38
#define XMARGIN
quint64 getTotalBytesSent() const
#define f(x)
Definition: gost.cpp:57
void setGraphRangeMins(int mins)
void setClientModel(ClientModel *model)
vector< pair< u256, string > > const & units()
Get information concerning the currency denominations.
Definition: Common.cpp:74
TrafficGraphWidget(QWidget *parent=0)
#define YMARGIN
#define DESIRED_SAMPLES