Anoncoin  0.9.4
P2P Digital Currency
trafficgraphwidget.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 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 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  // draw lines
82  painter.setPen(axisCol);
83  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax, QString("%1 %2").arg(val).arg(units));
84  for(float y = val; y < fMax; y += val) {
85  int yy = YMARGIN + h - h * y / fMax;
86  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
87  }
88  // if we drew 3 or fewer lines, break them up at the next lower order of magnitude
89  if(fMax / val <= 3.0f) {
90  axisCol = axisCol.darker();
91  val = pow(10.0f, base - 1);
92  painter.setPen(axisCol);
93  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax, QString("%1 %2").arg(val).arg(units));
94  int count = 1;
95  for(float y = val; y < fMax; y += val, count++) {
96  // don't overwrite lines drawn above
97  if(count % 10 == 0)
98  continue;
99  int yy = YMARGIN + h - h * y / fMax;
100  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
101  }
102  }
103 
104  if(!vSamplesIn.empty()) {
105  QPainterPath p;
106  paintPath(p, vSamplesIn);
107  painter.fillPath(p, QColor(0, 255, 0, 128));
108  painter.setPen(Qt::green);
109  painter.drawPath(p);
110  }
111  if(!vSamplesOut.empty()) {
112  QPainterPath p;
114  painter.fillPath(p, QColor(255, 0, 0, 128));
115  painter.setPen(Qt::red);
116  painter.drawPath(p);
117  }
118 }
119 
121 {
122  if(!clientModel) return;
123 
124  quint64 bytesIn = clientModel->getTotalBytesRecv(),
125  bytesOut = clientModel->getTotalBytesSent();
126  float inRate = (bytesIn - nLastBytesIn) / 1024.0f * 1000 / timer->interval();
127  float outRate = (bytesOut - nLastBytesOut) / 1024.0f * 1000 / timer->interval();
128  vSamplesIn.push_front(inRate);
129  vSamplesOut.push_front(outRate);
130  nLastBytesIn = bytesIn;
131  nLastBytesOut = bytesOut;
132 
133  while(vSamplesIn.size() > DESIRED_SAMPLES) {
134  vSamplesIn.pop_back();
135  }
136  while(vSamplesOut.size() > DESIRED_SAMPLES) {
137  vSamplesOut.pop_back();
138  }
139 
140  float tmax = 0.0f;
141  foreach(float f, vSamplesIn) {
142  if(f > tmax) tmax = f;
143  }
144  foreach(float f, vSamplesOut) {
145  if(f > tmax) tmax = f;
146  }
147  fMax = tmax;
148  update();
149 }
150 
152 {
153  nMins = mins;
154  int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES;
155  timer->stop();
156  timer->setInterval(msecsPerSample);
157 
158  clear();
159 }
160 
162 {
163  timer->stop();
164 
165  vSamplesOut.clear();
166  vSamplesIn.clear();
167  fMax = 0.0f;
168 
169  if(clientModel) {
172  }
173  timer->start();
174 }
void paintPath(QPainterPath &path, QQueue< float > &samples)
ClientModel * clientModel
quint64 getTotalBytesRecv() const
Definition: clientmodel.cpp:77
QQueue< float > vSamplesIn
QQueue< float > vSamplesOut
void paintEvent(QPaintEvent *)
int getGraphRangeMins() const
Model for Anoncoin network client.
Definition: clientmodel.h:45
#define XMARGIN
quint64 getTotalBytesSent() const
Definition: clientmodel.cpp:82
Timer timer
Definition: Benchmark.cpp:81
void setGraphRangeMins(int mins)
void setClientModel(ClientModel *model)
TrafficGraphWidget(QWidget *parent=0)
#define YMARGIN
#define DESIRED_SAMPLES