2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 16:20:43 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-08-22 18:09:32 +02:00
|
|
|
#include "trafficgraphwidget.h"
|
|
|
|
#include "clientmodel.h"
|
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2013-08-22 18:09:32 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#define XMARGIN 10
|
|
|
|
#define YMARGIN 10
|
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
#define DEFAULT_SAMPLE_HEIGHT 1.1f
|
|
|
|
|
2013-08-22 18:09:32 +02:00
|
|
|
TrafficGraphWidget::TrafficGraphWidget(QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
timer(0),
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
fMax(DEFAULT_SAMPLE_HEIGHT),
|
2013-08-22 18:09:32 +02:00
|
|
|
nMins(0),
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
clientModel(0),
|
|
|
|
trafficGraphData(TrafficGraphData::Range_30m)
|
2013-08-22 18:09:32 +02:00
|
|
|
{
|
|
|
|
timer = new QTimer(this);
|
|
|
|
connect(timer, SIGNAL(timeout()), SLOT(updateRates()));
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
timer->setInterval(TrafficGraphData::SMALLEST_SAMPLE_PERIOD);
|
|
|
|
timer->start();
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrafficGraphWidget::setClientModel(ClientModel *model)
|
|
|
|
{
|
|
|
|
clientModel = model;
|
|
|
|
if(model) {
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
trafficGraphData.setLastBytes(model->getTotalBytesRecv(), model->getTotalBytesSent());
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int TrafficGraphWidget::getGraphRangeMins() const
|
|
|
|
{
|
|
|
|
return nMins;
|
|
|
|
}
|
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
|
|
|
|
void TrafficGraphWidget::paintPath(QPainterPath &path, const TrafficGraphData::SampleQueue &queue, SampleChooser chooser)
|
2013-08-22 18:09:32 +02:00
|
|
|
{
|
|
|
|
int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
int sampleCount = queue.size(), x = XMARGIN + w, y;
|
2013-08-22 18:09:32 +02:00
|
|
|
if(sampleCount > 0) {
|
|
|
|
path.moveTo(x, YMARGIN + h);
|
|
|
|
for(int i = 0; i < sampleCount; ++i) {
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
x = XMARGIN + w - w * i / TrafficGraphData::DESIRED_DATA_SAMPLES;
|
|
|
|
y = YMARGIN + h - (int)(h * chooser(queue.at(i)) / fMax);
|
2013-08-22 18:09:32 +02:00
|
|
|
path.lineTo(x, y);
|
|
|
|
}
|
|
|
|
path.lineTo(x, YMARGIN + h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
float chooseIn(const TrafficSample& sample)
|
|
|
|
{
|
|
|
|
return sample.in;
|
|
|
|
}
|
|
|
|
float chooseOut(const TrafficSample& sample)
|
|
|
|
{
|
|
|
|
return sample.out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 18:09:32 +02:00
|
|
|
void TrafficGraphWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.fillRect(rect(), Qt::black);
|
|
|
|
|
|
|
|
if(fMax <= 0.0f) return;
|
|
|
|
|
|
|
|
QColor axisCol(Qt::gray);
|
2018-03-10 13:34:50 +01:00
|
|
|
QColor axisCol2;
|
2013-08-22 18:09:32 +02:00
|
|
|
int h = height() - YMARGIN * 2;
|
|
|
|
painter.setPen(axisCol);
|
|
|
|
painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);
|
|
|
|
|
|
|
|
// decide what order of magnitude we are
|
|
|
|
int base = floor(log10(fMax));
|
|
|
|
float val = pow(10.0f, base);
|
2018-03-10 13:34:50 +01:00
|
|
|
float val2;
|
2013-08-22 18:09:32 +02:00
|
|
|
|
2014-11-05 11:39:47 +01:00
|
|
|
const QString units = tr("KB/s");
|
|
|
|
const float yMarginText = 2.0;
|
|
|
|
|
2013-08-22 18:09:32 +02:00
|
|
|
// draw lines
|
|
|
|
painter.setPen(axisCol);
|
|
|
|
for(float y = val; y < fMax; y += val) {
|
|
|
|
int yy = YMARGIN + h - h * y / fMax;
|
|
|
|
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
|
|
|
|
}
|
|
|
|
// if we drew 3 or fewer lines, break them up at the next lower order of magnitude
|
|
|
|
if(fMax / val <= 3.0f) {
|
2018-03-10 13:34:50 +01:00
|
|
|
axisCol2 = axisCol.darker();
|
|
|
|
val2 = pow(10.0f, base - 1);
|
|
|
|
painter.setPen(axisCol2);
|
2013-08-22 18:09:32 +02:00
|
|
|
int count = 1;
|
2018-03-10 13:34:50 +01:00
|
|
|
for(float y = val2; y < fMax; y += val2, count++) {
|
2013-08-22 18:09:32 +02:00
|
|
|
// don't overwrite lines drawn above
|
|
|
|
if(count % 10 == 0)
|
|
|
|
continue;
|
|
|
|
int yy = YMARGIN + h - h * y / fMax;
|
|
|
|
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 16:33:48 +02:00
|
|
|
const TrafficGraphData::SampleQueue& queue = trafficGraphData.getCurrentRangeQueueWithAverageBandwidth();
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
|
|
|
|
if(!queue.empty()) {
|
|
|
|
QPainterPath pIn;
|
|
|
|
paintPath(pIn, queue, boost::bind(chooseIn,_1));
|
|
|
|
painter.fillPath(pIn, QColor(0, 255, 0, 128));
|
2013-08-22 18:09:32 +02:00
|
|
|
painter.setPen(Qt::green);
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
painter.drawPath(pIn);
|
|
|
|
|
|
|
|
QPainterPath pOut;
|
|
|
|
paintPath(pOut, queue, boost::bind(chooseOut,_1));
|
|
|
|
painter.fillPath(pOut, QColor(255, 0, 0, 128));
|
2013-08-22 18:09:32 +02:00
|
|
|
painter.setPen(Qt::red);
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
painter.drawPath(pOut);
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
2018-03-10 13:34:50 +01:00
|
|
|
|
|
|
|
// draw text on top of everything else
|
|
|
|
QRect textRect = painter.boundingRect(QRect(XMARGIN, YMARGIN + h - (h * val / fMax) - yMarginText, 0, 0), Qt::AlignLeft, QString("%1 %2").arg(val).arg(units));
|
|
|
|
textRect.translate(0, -textRect.height());
|
|
|
|
painter.fillRect(textRect, Qt::black);
|
|
|
|
painter.setPen(axisCol);
|
|
|
|
painter.drawText(textRect, Qt::AlignLeft, QString("%1 %2").arg(val).arg(units));
|
|
|
|
if(fMax / val <= 3.0f) {
|
|
|
|
QRect textRect2 = painter.boundingRect(QRect(XMARGIN, YMARGIN + h - (h * val2 / fMax) - yMarginText, 0, 0), Qt::AlignLeft, QString("%1 %2").arg(val2).arg(units));
|
|
|
|
textRect2.translate(0, -textRect2.height());
|
|
|
|
painter.fillRect(textRect2, Qt::black);
|
|
|
|
painter.setPen(axisCol2);
|
|
|
|
painter.drawText(textRect2, Qt::AlignLeft, QString("%1 %2").arg(val2).arg(units));
|
|
|
|
}
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrafficGraphWidget::updateRates()
|
|
|
|
{
|
|
|
|
if(!clientModel) return;
|
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
bool updated = trafficGraphData.update(clientModel->getTotalBytesRecv(),clientModel->getTotalBytesSent());
|
2013-08-22 18:09:32 +02:00
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
if (updated){
|
|
|
|
float tmax = DEFAULT_SAMPLE_HEIGHT;
|
2017-09-12 16:33:48 +02:00
|
|
|
Q_FOREACH(const TrafficSample& sample, trafficGraphData.getCurrentRangeQueueWithAverageBandwidth()) {
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
if(sample.in > tmax) tmax = sample.in;
|
|
|
|
if(sample.out > tmax) tmax = sample.out;
|
|
|
|
}
|
|
|
|
fMax = tmax;
|
|
|
|
update();
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
void TrafficGraphWidget::setGraphRangeMins(int value)
|
2013-08-22 18:09:32 +02:00
|
|
|
{
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
trafficGraphData.switchRange(static_cast<TrafficGraphData::GraphRange>(value));
|
|
|
|
update();
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrafficGraphWidget::clear()
|
|
|
|
{
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
trafficGraphData.clear();
|
|
|
|
fMax = DEFAULT_SAMPLE_HEIGHT;
|
2013-08-22 18:09:32 +02:00
|
|
|
if(clientModel) {
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
trafficGraphData.setLastBytes(clientModel->getTotalBytesRecv(), clientModel->getTotalBytesSent());
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|
Qt: bug fixes and enhancement to traffic graph widget (#1429)
* clear trafficgraph on clear button click
* set default sample height
set default sample height so after clearing traffic graph have some
scale
* reduce available traffic graph ranges, add optimized graph data storage
reduce available traffic graph ranges to 10
(5m,10m,15m,30m,1h,2h,3h,6h,12h,24h),
store graph data so range change is possible,
data storage contains only necessary data to create graphs for all
supported ranges
eg. for 10m range storage only half of 10m samples - the second half is
calculated from 5m range samples,
encapsulate all traffic graph related data into one class
* code formatting corrections
2017-05-28 15:49:34 +02:00
|
|
|
update();
|
2013-08-22 18:09:32 +02:00
|
|
|
}
|