Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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 
6 #include <qt/clientmodel.h>
7 #include <qt/guiutil.h>
8 
9 #include <boost/bind.hpp>
10 
11 #include <QPainter>
12 #include <QColor>
13 #include <QTimer>
14 
15 #include <cmath>
16 
17 #define XMARGIN 10
18 #define YMARGIN 10
19 
20 #define DEFAULT_SAMPLE_HEIGHT 1.1f
21 
23  QWidget(parent),
24  timer(0),
26  nMins(0),
27  clientModel(0),
28  trafficGraphData(TrafficGraphData::Range_30m)
29 {
30  timer = new QTimer(this);
31  connect(timer, SIGNAL(timeout()), SLOT(updateRates()));
33  timer->start();
34 }
35 
37 {
38  clientModel = model;
39  if(model) {
41  }
42 }
43 
45 {
46  return nMins;
47 }
48 
49 
50 void TrafficGraphWidget::paintPath(QPainterPath &path, const TrafficGraphData::SampleQueue &queue, SampleChooser chooser)
51 {
52  int sampleCount = queue.size();
53  if(sampleCount > 0) {
54  int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
55  int x = XMARGIN + w;
56  path.moveTo(x, YMARGIN + h);
57  for(int i = 0; i < sampleCount; ++i) {
59  int y = YMARGIN + h - (int)(h * chooser(queue.at(i)) / fMax);
60  path.lineTo(x, y);
61  }
62  path.lineTo(x, YMARGIN + h);
63  }
64 }
65 
66 namespace
67 {
68  float chooseIn(const TrafficSample& sample)
69  {
70  return sample.in;
71  }
72  float chooseOut(const TrafficSample& sample)
73  {
74  return sample.out;
75  }
76 }
77 
78 void TrafficGraphWidget::paintEvent(QPaintEvent *)
79 {
80  QPainter painter(this);
81  QRect drawRect = rect();
82  // First draw the border
84  painter.fillRect(drawRect.adjusted(1, 1, -1, -1), GUIUtil::getThemedQColor(GUIUtil::ThemedColor::BACKGROUND_WIDGET));
85 
86  if(fMax <= 0.0f) return;
87 
91  QColor axisCol2;
92  int h = height() - YMARGIN * 2;
93  painter.setPen(axisCol);
94  painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);
95 
96  // decide what order of magnitude we are
97  int base = floor(log10(fMax));
98  float val = pow(10.0f, base);
99  float val2 = val;
100 
101  const QString units = tr("KB/s");
102  const float yMarginText = 2.0;
103 
104  // draw lines
105  painter.setPen(axisCol);
106  for(float y = val; y < fMax; y += val) {
107  int yy = YMARGIN + h - h * y / fMax;
108  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
109  }
110  // if we drew 3 or fewer lines, break them up at the next lower order of magnitude
111  if(fMax / val <= 3.0f) {
112  axisCol2 = axisCol.darker();
113  val2 = pow(10.0f, base - 1);
114  painter.setPen(axisCol2);
115  int count = 1;
116  for(float y = val2; y < fMax; y += val2, count++) {
117  // don't overwrite lines drawn above
118  if(count % 10 == 0)
119  continue;
120  int yy = YMARGIN + h - h * y / fMax;
121  painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
122  }
123  }
124 
126 
127  if(!queue.empty()) {
128  QPainterPath pIn;
129  QColor lucentGreen = green;
130  lucentGreen.setAlpha(128);
131 
132  paintPath(pIn, queue, boost::bind(chooseIn,_1));
133  painter.fillPath(pIn, lucentGreen);
134  painter.setPen(green);
135  painter.drawPath(pIn);
136 
137  QPainterPath pOut;
138  QColor lucentRed = red;
139  lucentRed.setAlpha(128);
140 
141  paintPath(pOut, queue, boost::bind(chooseOut,_1));
142  painter.fillPath(pOut, lucentRed);
143  painter.setPen(red);
144  painter.drawPath(pOut);
145  }
146 
147  // draw text
148  QRect textRect = painter.boundingRect(QRect(XMARGIN, YMARGIN + h - (h * val / fMax) - yMarginText, 0, 0), Qt::AlignLeft, QString("%1 %2").arg(val).arg(units));
149  textRect.translate(0, -textRect.height());
151  painter.setPen(axisCol);
152  painter.drawText(textRect, Qt::AlignLeft, QString("%1 %2").arg(val).arg(units));
153  if(fMax / val <= 3.0f) {
154  QRect textRect2 = painter.boundingRect(QRect(XMARGIN, YMARGIN + h - (h * val2 / fMax) - yMarginText, 0, 0), Qt::AlignLeft, QString("%1 %2").arg(val2).arg(units));
155  textRect2.translate(0, -textRect2.height());
157  painter.setPen(axisCol2);
158  painter.drawText(textRect2, Qt::AlignLeft, QString("%1 %2").arg(val2).arg(units));
159  }
160 
161  // Draw statistic rect on top of everything else
162  const int nPadding = 5;
163  const int nMarginStats = 20;
164  const QString strTotal = tr("Total");
165  const QString strReceived = tr("Received");
166  const QString strSent = tr("Sent");
167  // Get a bold font for the title and a normal one for the rest
168  QFont fontTotal = GUIUtil::getFont(GUIUtil::FontWeight::Bold, false, 16);
169  QFont fontInOut = GUIUtil::getFont(GUIUtil::FontWeight::Normal, false, 12);
170  // Use font metrics to determine minimum rect sizes depending on the font scale
171  QFontMetrics fmTotal(fontTotal);
172  QFontMetrics fmInOut(fontInOut);
173  const int nSizeMark = fmInOut.height() + 2 * nPadding;
174  const int nWidthText = fmInOut.width(strReceived) + 2 * nPadding;
175  const int nWidthBytes = fmInOut.width("1000 GB") + 2 * nPadding;
176  const int nHeightTotals = fmTotal.height() + 2 * nPadding;
177  const int nHeightInOut = fmInOut.height() + 2 * nPadding;
178  const int nWidthStats = nSizeMark + nWidthText + nWidthBytes + 2 * nPadding;
179  const int nHeightStats = nHeightTotals + 2 * nHeightInOut + 2 * nPadding;
180  auto addPadding = [&](QRect& rect, int nPadding) {
181  rect.adjust(nPadding, nPadding, -nPadding, -nPadding);
182  };
183  // Create top-level rects
184  QRect rectOuter = QRect(drawRect.width() - nWidthStats - nMarginStats, nMarginStats, nWidthStats, nHeightStats);
185  QRect rectContent = rectOuter;
186  QRect rectContentPadded = rectContent;
187  addPadding(rectContentPadded, nPadding);
188  QRect rectTotals(rectContentPadded.topLeft(), QSize(rectContentPadded.width(), nHeightTotals));
189  QRect rectIn(rectTotals.bottomLeft(), QSize(rectContentPadded.width(), nHeightInOut));
190  QRect rectOut(rectIn.bottomLeft(), QSize(rectContentPadded.width(), nHeightInOut));
191  // Create subrects for received
192  QRect rectInMark(rectIn.topLeft(), QSize(nSizeMark, nSizeMark));
193  QRect rectInText(rectInMark.topRight(), QSize(nWidthText, nHeightInOut));
194  QRect rectInBytes(rectInText.topRight(), QSize(nWidthBytes, nHeightInOut));
195  // Create subrects for sent
196  QRect rectOutMark(rectOut.topLeft(), QSize(nSizeMark, nSizeMark));
197  QRect rectOutText(rectOutMark.topRight(), QSize(nWidthText, nHeightInOut));
198  QRect rectOutBytes(rectOutText.topRight(), QSize(nWidthBytes, nHeightInOut));
199  // Add padding where required
200  addPadding(rectTotals, nPadding);
201  addPadding(rectInMark, 1.6 * nPadding);
202  addPadding(rectInText, nPadding);
203  addPadding(rectInBytes, nPadding);
204  addPadding(rectOutMark, 1.6 * nPadding);
205  addPadding(rectOutText, nPadding);
206  addPadding(rectOutBytes, nPadding);
207  // Finally draw it all
209  painter.drawRect(rectOuter);
211  painter.setPen(axisCol);
212  painter.setFont(fontTotal);
213  painter.drawText(rectTotals, Qt::AlignLeft, strTotal);
214  painter.setFont(fontInOut);
215  painter.drawText(rectInText, Qt::AlignLeft, strReceived);
216  painter.drawText(rectInBytes, Qt::AlignRight, GUIUtil::formatBytes(trafficGraphData.getLastBytesIn()));
217  painter.drawText(rectOutText, Qt::AlignLeft, strSent);
218  painter.drawText(rectOutBytes, Qt::AlignRight, GUIUtil::formatBytes(trafficGraphData.getLastBytesOut()));
219  painter.setPen(green);
220  painter.setBrush(green);
221  painter.drawEllipse(rectInMark);
222  painter.setPen(red);
223  painter.setBrush(red);
224  painter.drawEllipse(rectOutMark);
225 }
226 
228 {
229  if(!clientModel) return;
230 
232 
233  if (updated){
234  float tmax = DEFAULT_SAMPLE_HEIGHT;
236  if(sample.in > tmax) tmax = sample.in;
237  if(sample.out > tmax) tmax = sample.out;
238  }
239  fMax = tmax;
240  update();
241  }
242 }
243 
245 {
246  trafficGraphData.switchRange(static_cast<TrafficGraphData::GraphRange>(value));
247  update();
248 }
249 
251 {
254  if(clientModel) {
256  }
257  update();
258 }
QFont getFont(FontFamily family, QFont::Weight qWeight, bool fItalic, int nPointSize)
Get a properly weighted QFont object with the selected font.
Definition: guiutil.cpp:1666
ClientModel * clientModel
void setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut)
QColor getThemedQColor(ThemedColor color)
Helper to get colors for various themes which can&#39;t be applied via css for some reason.
Definition: guiutil.cpp:204
void paintEvent(QPaintEvent *)
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:1935
quint64 getTotalBytesSent() const
QQueue< TrafficSample > SampleQueue
quint64 getLastBytesOut()
static const int SMALLEST_SAMPLE_PERIOD
void paintPath(QPainterPath &path, const TrafficGraphData::SampleQueue &queue, SampleChooser chooser)
quint64 getTotalBytesRecv() const
TrafficGraphData trafficGraphData
void setGraphRangeMins(int value)
static const int DESIRED_DATA_SAMPLES
Model for Dash network client.
Definition: clientmodel.h:42
quint64 getLastBytesIn()
#define XMARGIN
boost::function< float(const TrafficSample &)> SampleChooser
static int count
Definition: tests.c:45
bool update(const TrafficSample &trafficSample)
void switchRange(GraphRange newRange)
SampleQueue getCurrentRangeQueueWithAverageBandwidth()
#define DEFAULT_SAMPLE_HEIGHT
void setClientModel(ClientModel *model)
TrafficGraphWidget(QWidget *parent=0)
#define YMARGIN
Released under the MIT license