Dash Core  0.12.2.1
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 <boost/bind.hpp>
9 
10 #include <QPainter>
11 #include <QColor>
12 #include <QTimer>
13 
14 #include <cmath>
15 
16 #define XMARGIN 10
17 #define YMARGIN 10
18 
19 #define DEFAULT_SAMPLE_HEIGHT 1.1f
20 
22  QWidget(parent),
23  timer(0),
25  nMins(0),
26  clientModel(0),
27  trafficGraphData(TrafficGraphData::Range_30m)
28 {
29  timer = new QTimer(this);
30  connect(timer, SIGNAL(timeout()), SLOT(updateRates()));
32  timer->start();
33 }
34 
36 {
37  clientModel = model;
38  if(model) {
40  }
41 }
42 
44 {
45  return nMins;
46 }
47 
48 
49 void TrafficGraphWidget::paintPath(QPainterPath &path, const TrafficGraphData::SampleQueue &queue, SampleChooser chooser)
50 {
51  int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
52  int sampleCount = queue.size(), x = XMARGIN + w, y;
53  if(sampleCount > 0) {
54  path.moveTo(x, YMARGIN + h);
55  for(int i = 0; i < sampleCount; ++i) {
57  y = YMARGIN + h - (int)(h * chooser(queue.at(i)) / fMax);
58  path.lineTo(x, y);
59  }
60  path.lineTo(x, YMARGIN + h);
61  }
62 }
63 
64 namespace
65 {
66  float chooseIn(const TrafficSample& sample)
67  {
68  return sample.in;
69  }
70  float chooseOut(const TrafficSample& sample)
71  {
72  return sample.out;
73  }
74 }
75 
76 void TrafficGraphWidget::paintEvent(QPaintEvent *)
77 {
78  QPainter painter(this);
79  painter.fillRect(rect(), Qt::black);
80 
81  if(fMax <= 0.0f) return;
82 
83  QColor axisCol(Qt::gray);
84  int h = height() - YMARGIN * 2;
85  painter.setPen(axisCol);
86  painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);
87 
88  // decide what order of magnitude we are
89  int base = floor(log10(fMax));
90  float val = pow(10.0f, base);
91 
92  const QString units = tr("KB/s");
93  const float yMarginText = 2.0;
94 
95  // draw lines
96  painter.setPen(axisCol);
97  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
98  for(float y = val; y < fMax; y += val) {
99  int yy = YMARGIN + h - h * y / fMax;
100  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
101  }
102  // if we drew 3 or fewer lines, break them up at the next lower order of magnitude
103  if(fMax / val <= 3.0f) {
104  axisCol = axisCol.darker();
105  val = pow(10.0f, base - 1);
106  painter.setPen(axisCol);
107  painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
108  int count = 1;
109  for(float y = val; y < fMax; y += val, count++) {
110  // don't overwrite lines drawn above
111  if(count % 10 == 0)
112  continue;
113  int yy = YMARGIN + h - h * y / fMax;
114  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
115  }
116  }
117 
119 
120  if(!queue.empty()) {
121  QPainterPath pIn;
122  paintPath(pIn, queue, boost::bind(chooseIn,_1));
123  painter.fillPath(pIn, QColor(0, 255, 0, 128));
124  painter.setPen(Qt::green);
125  painter.drawPath(pIn);
126 
127  QPainterPath pOut;
128  paintPath(pOut, queue, boost::bind(chooseOut,_1));
129  painter.fillPath(pOut, QColor(255, 0, 0, 128));
130  painter.setPen(Qt::red);
131  painter.drawPath(pOut);
132  }
133 }
134 
136 {
137  if(!clientModel) return;
138 
140 
141  if (updated){
142  float tmax = DEFAULT_SAMPLE_HEIGHT;
144  if(sample.in > tmax) tmax = sample.in;
145  if(sample.out > tmax) tmax = sample.out;
146  }
147  fMax = tmax;
148  update();
149  }
150 }
151 
153 {
154  trafficGraphData.switchRange(static_cast<TrafficGraphData::GraphRange>(value));
155  update();
156 }
157 
159 {
162  if(clientModel) {
164  }
165  update();
166 }
void setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut)
ClientModel * clientModel
static const int SMALLEST_SAMPLE_PERIOD
struct event_base * base
Definition: torcontrol.cpp:659
void switchRange(GraphRange newRange)
boost::function< float(const TrafficSample &)> SampleChooser
TrafficGraphData trafficGraphData
void paintPath(QPainterPath &path, const TrafficGraphData::SampleQueue &queue, SampleChooser chooser)
TrafficGraphWidget(QWidget *parent=0)
#define XMARGIN
quint64 getTotalBytesRecv() const
bool update(const TrafficSample &trafficSample)
static int count
Definition: tests.c:41
void paintEvent(QPaintEvent *)
static const int DESIRED_DATA_SAMPLES
SampleQueue getCurrentRangeQueueWithAverageBandwidth()
QQueue< TrafficSample > SampleQueue
quint64 getTotalBytesSent() const
#define DEFAULT_SAMPLE_HEIGHT
void setClientModel(ClientModel *model)
void setGraphRangeMins(int value)
#define YMARGIN