2014-08-25 15:58:48 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2020-12-26 19:21:23 +03:00
|
|
|
* Copyright (C) 2021 Prince Gupta <guptaprince8832@gmail.com>
|
2014-08-25 15:58:48 +04:00
|
|
|
* Copyright (C) 2015 Anton Lashkov <lenton_91@mail.ru>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "speedplotview.h"
|
|
|
|
|
2020-11-29 15:44:26 +03:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-04-02 19:53:44 +03:00
|
|
|
#include <QLocale>
|
2014-08-25 15:58:48 +04:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPen>
|
2020-11-29 15:44:26 +03:00
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
#include "base/bittorrent/session.h"
|
2018-02-16 21:31:48 +03:00
|
|
|
#include "base/global.h"
|
2018-04-02 19:53:44 +03:00
|
|
|
#include "base/unicodestrings.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/misc.h"
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2018-09-24 17:44:46 +03:00
|
|
|
namespace
|
|
|
|
{
|
2018-04-02 19:53:44 +03:00
|
|
|
// table of supposed nice steps for grid marks to get nice looking quarters of scale
|
|
|
|
const double roundingTable[] = {1.2, 1.6, 2, 2.4, 2.8, 3.2, 4, 6, 8};
|
|
|
|
|
|
|
|
struct SplittedValue
|
|
|
|
{
|
|
|
|
double arg;
|
|
|
|
Utils::Misc::SizeUnit unit;
|
|
|
|
qint64 sizeInBytes() const
|
|
|
|
{
|
|
|
|
return Utils::Misc::sizeInBytes(arg, unit);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SplittedValue getRoundedYScale(double value)
|
|
|
|
{
|
|
|
|
using Utils::Misc::SizeUnit;
|
|
|
|
|
|
|
|
if (value == 0.0) return {0, SizeUnit::Byte};
|
|
|
|
if (value <= 12.0) return {12, SizeUnit::Byte};
|
|
|
|
|
|
|
|
SizeUnit calculatedUnit = SizeUnit::Byte;
|
2020-11-16 10:02:11 +03:00
|
|
|
while (value > 1024)
|
|
|
|
{
|
2018-04-02 19:53:44 +03:00
|
|
|
value /= 1024;
|
|
|
|
calculatedUnit = static_cast<SizeUnit>(static_cast<int>(calculatedUnit) + 1);
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:44:26 +03:00
|
|
|
if (value > 100)
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2020-11-29 15:44:26 +03:00
|
|
|
const double roundedValue {std::ceil(value / 40) * 40};
|
|
|
|
return {roundedValue, calculatedUnit};
|
2018-04-02 19:53:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:44:26 +03:00
|
|
|
if (value > 10)
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2020-11-29 15:44:26 +03:00
|
|
|
const double roundedValue {std::ceil(value / 4) * 4};
|
|
|
|
return {roundedValue, calculatedUnit};
|
2018-04-02 19:53:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
for (const auto &roundedValue : roundingTable)
|
|
|
|
{
|
2018-04-02 19:53:44 +03:00
|
|
|
if (value <= roundedValue)
|
|
|
|
return {roundedValue, calculatedUnit};
|
|
|
|
}
|
|
|
|
return {10.0, calculatedUnit};
|
|
|
|
}
|
|
|
|
|
|
|
|
QString formatLabel(const double argValue, const Utils::Misc::SizeUnit unit)
|
|
|
|
{
|
|
|
|
// check is there need for digits after decimal separator
|
|
|
|
const int precision = (argValue < 10) ? friendlyUnitPrecision(unit) : 0;
|
|
|
|
return QLocale::system().toString(argValue, 'f', precision)
|
|
|
|
+ QString::fromUtf8(C_NON_BREAKING_SPACE)
|
|
|
|
+ unitString(unit, true);
|
|
|
|
}
|
2018-09-24 17:44:46 +03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
SpeedPlotView::Averager::Averager(const milliseconds duration, const milliseconds resolution)
|
|
|
|
: m_resolution {resolution}
|
|
|
|
, m_maxDuration {duration}
|
|
|
|
, m_sink {static_cast<DataCircularBuffer::size_type>(duration / resolution)}
|
2018-09-24 17:44:46 +03:00
|
|
|
{
|
2020-12-26 19:21:23 +03:00
|
|
|
m_lastSampleTime.start();
|
2018-09-24 17:44:46 +03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
bool SpeedPlotView::Averager::push(const SampleData &sampleData)
|
2018-09-24 17:44:46 +03:00
|
|
|
{
|
|
|
|
// Accumulator overflow will be hit in worst case on longest used averaging span,
|
2019-04-17 18:00:17 +03:00
|
|
|
// defined by divider value. Maximum divider is DIVIDER_24HOUR = 144
|
|
|
|
// Using int32 for accumulator we get overflow when transfer speed reaches 2^31/144 ~~ 14.2 MBytes/s.
|
|
|
|
// With quint64 this speed limit is 2^64/144 ~~ 114 PBytes/s.
|
2018-09-24 17:44:46 +03:00
|
|
|
// This speed is inaccessible to an ordinary user.
|
2020-12-26 19:21:23 +03:00
|
|
|
++m_counter;
|
2018-09-24 17:44:46 +03:00
|
|
|
for (int id = UP; id < NB_GRAPHS; ++id)
|
2020-12-26 19:21:23 +03:00
|
|
|
m_accumulator[id] += sampleData[id];
|
|
|
|
|
|
|
|
// system may go to sleep, that can cause very big elapsed interval
|
|
|
|
const milliseconds updateInterval {static_cast<int64_t>(BitTorrent::Session::instance()->refreshInterval() * 1.25)};
|
|
|
|
const milliseconds maxElapsed {std::max(updateInterval, m_resolution)};
|
|
|
|
const milliseconds elapsed {std::min(milliseconds {m_lastSampleTime.elapsed()}, maxElapsed)};
|
|
|
|
if (elapsed < m_resolution)
|
|
|
|
return false; // still accumulating
|
|
|
|
|
2018-09-24 17:44:46 +03:00
|
|
|
// it is time final averaging calculations
|
|
|
|
for (int id = UP; id < NB_GRAPHS; ++id)
|
2020-12-26 19:21:23 +03:00
|
|
|
m_accumulator[id] /= m_counter;
|
|
|
|
|
|
|
|
m_currentDuration += elapsed;
|
|
|
|
|
|
|
|
// remove extra data from front if we reached max duration
|
|
|
|
if (m_currentDuration > m_maxDuration)
|
|
|
|
{
|
|
|
|
// once we go above the max duration never go below that
|
|
|
|
// otherwise it will cause empty space in graphs
|
|
|
|
while (!m_sink.empty()
|
2021-03-09 19:52:10 +03:00
|
|
|
&& ((m_currentDuration - m_sink.front().duration) >= m_maxDuration))
|
2020-12-26 19:21:23 +03:00
|
|
|
{
|
|
|
|
m_currentDuration -= m_sink.front().duration;
|
|
|
|
m_sink.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:44:46 +03:00
|
|
|
// now flush out averaged data
|
2020-12-26 19:21:23 +03:00
|
|
|
Q_ASSERT(m_sink.size() < m_sink.capacity());
|
|
|
|
m_sink.push_back({elapsed, m_accumulator});
|
|
|
|
|
|
|
|
// reset
|
2018-09-24 17:44:46 +03:00
|
|
|
m_accumulator = {};
|
2020-12-26 19:21:23 +03:00
|
|
|
m_counter = 0;
|
|
|
|
m_lastSampleTime.restart();
|
|
|
|
return true;
|
2018-09-24 17:44:46 +03:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
const SpeedPlotView::DataCircularBuffer &SpeedPlotView::Averager::data() const
|
2018-09-24 17:44:46 +03:00
|
|
|
{
|
2020-12-26 19:21:23 +03:00
|
|
|
return m_sink;
|
2018-09-24 17:44:46 +03:00
|
|
|
}
|
|
|
|
|
2014-08-25 15:58:48 +04:00
|
|
|
SpeedPlotView::SpeedPlotView(QWidget *parent)
|
2020-12-26 19:21:23 +03:00
|
|
|
: QGraphicsView {parent}
|
2014-08-25 15:58:48 +04:00
|
|
|
{
|
|
|
|
QPen greenPen;
|
|
|
|
greenPen.setWidthF(1.5);
|
|
|
|
greenPen.setColor(QColor(134, 196, 63));
|
|
|
|
QPen bluePen;
|
|
|
|
bluePen.setWidthF(1.5);
|
|
|
|
bluePen.setColor(QColor(50, 153, 255));
|
|
|
|
|
|
|
|
m_properties[UP] = GraphProperties(tr("Total Upload"), bluePen);
|
|
|
|
m_properties[DOWN] = GraphProperties(tr("Total Download"), greenPen);
|
|
|
|
|
|
|
|
bluePen.setStyle(Qt::DashLine);
|
|
|
|
greenPen.setStyle(Qt::DashLine);
|
|
|
|
m_properties[PAYLOAD_UP] = GraphProperties(tr("Payload Upload"), bluePen);
|
|
|
|
m_properties[PAYLOAD_DOWN] = GraphProperties(tr("Payload Download"), greenPen);
|
|
|
|
|
|
|
|
bluePen.setStyle(Qt::DashDotLine);
|
|
|
|
greenPen.setStyle(Qt::DashDotLine);
|
|
|
|
m_properties[OVERHEAD_UP] = GraphProperties(tr("Overhead Upload"), bluePen);
|
|
|
|
m_properties[OVERHEAD_DOWN] = GraphProperties(tr("Overhead Download"), greenPen);
|
|
|
|
|
|
|
|
bluePen.setStyle(Qt::DashDotDotLine);
|
|
|
|
greenPen.setStyle(Qt::DashDotDotLine);
|
|
|
|
m_properties[DHT_UP] = GraphProperties(tr("DHT Upload"), bluePen);
|
|
|
|
m_properties[DHT_DOWN] = GraphProperties(tr("DHT Download"), greenPen);
|
|
|
|
|
|
|
|
bluePen.setStyle(Qt::DotLine);
|
|
|
|
greenPen.setStyle(Qt::DotLine);
|
|
|
|
m_properties[TRACKER_UP] = GraphProperties(tr("Tracker Upload"), bluePen);
|
|
|
|
m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen);
|
|
|
|
}
|
|
|
|
|
2015-07-18 19:16:03 +03:00
|
|
|
void SpeedPlotView::setGraphEnable(GraphID id, bool enable)
|
2014-08-25 15:58:48 +04:00
|
|
|
{
|
2016-03-20 11:31:10 +03:00
|
|
|
m_properties[id].enable = enable;
|
|
|
|
viewport()->update();
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
void SpeedPlotView::pushPoint(const SpeedPlotView::SampleData &point)
|
2014-08-25 15:58:48 +04:00
|
|
|
{
|
2020-12-26 19:21:23 +03:00
|
|
|
for (Averager *averager : {&m_averager5Min, &m_averager30Min
|
|
|
|
, &m_averager6Hour, &m_averager12Hour
|
|
|
|
, &m_averager24Hour})
|
|
|
|
{
|
|
|
|
if (averager->push(point))
|
|
|
|
{
|
|
|
|
if (m_currentAverager == averager)
|
|
|
|
viewport()->update();
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:00:17 +03:00
|
|
|
void SpeedPlotView::setPeriod(const TimePeriod period)
|
2014-08-25 15:58:48 +04:00
|
|
|
{
|
2020-11-16 10:02:11 +03:00
|
|
|
switch (period)
|
|
|
|
{
|
2015-07-18 19:16:03 +03:00
|
|
|
case SpeedPlotView::MIN1:
|
2020-12-26 19:21:23 +03:00
|
|
|
m_currentMaxDuration = 1min;
|
|
|
|
m_currentAverager = &m_averager5Min;
|
2015-07-18 19:16:03 +03:00
|
|
|
break;
|
|
|
|
case SpeedPlotView::MIN5:
|
2020-12-26 19:21:23 +03:00
|
|
|
m_currentMaxDuration = 5min;
|
|
|
|
m_currentAverager = &m_averager5Min;
|
2015-07-18 19:16:03 +03:00
|
|
|
break;
|
|
|
|
case SpeedPlotView::MIN30:
|
2020-12-26 19:21:23 +03:00
|
|
|
m_currentMaxDuration = 30min;
|
|
|
|
m_currentAverager = &m_averager30Min;
|
2015-07-18 19:16:03 +03:00
|
|
|
break;
|
2020-12-30 09:38:21 +03:00
|
|
|
case SpeedPlotView::HOUR3:
|
|
|
|
m_currentMaxDuration = 3h;
|
|
|
|
m_currentAverager = &m_averager6Hour;
|
|
|
|
break;
|
2015-07-18 19:16:03 +03:00
|
|
|
case SpeedPlotView::HOUR6:
|
2020-12-26 19:21:23 +03:00
|
|
|
m_currentMaxDuration = 6h;
|
|
|
|
m_currentAverager = &m_averager6Hour;
|
2019-04-17 18:00:17 +03:00
|
|
|
break;
|
|
|
|
case SpeedPlotView::HOUR12:
|
2020-12-26 19:21:23 +03:00
|
|
|
m_currentMaxDuration = 12h;
|
|
|
|
m_currentAverager = &m_averager12Hour;
|
2019-04-17 18:00:17 +03:00
|
|
|
break;
|
|
|
|
case SpeedPlotView::HOUR24:
|
2020-12-26 19:21:23 +03:00
|
|
|
m_currentMaxDuration = 24h;
|
|
|
|
m_currentAverager = &m_averager24Hour;
|
2015-07-18 19:16:03 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-01-17 20:05:54 +03:00
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
viewport()->update();
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
const SpeedPlotView::DataCircularBuffer &SpeedPlotView::currentData() const
|
2016-01-17 20:05:54 +03:00
|
|
|
{
|
2020-12-26 19:21:23 +03:00
|
|
|
return m_currentAverager->data();
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
quint64 SpeedPlotView::maxYValue() const
|
2014-08-25 15:58:48 +04:00
|
|
|
{
|
2020-12-26 19:21:23 +03:00
|
|
|
const DataCircularBuffer &queue = currentData();
|
2016-01-17 20:05:54 +03:00
|
|
|
|
2018-09-24 17:44:46 +03:00
|
|
|
quint64 maxYValue = 0;
|
2020-11-16 10:02:11 +03:00
|
|
|
for (int id = UP; id < NB_GRAPHS; ++id)
|
|
|
|
{
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
if (!m_properties[static_cast<GraphID>(id)].enable)
|
2014-08-25 15:58:48 +04:00
|
|
|
continue;
|
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
milliseconds duration {0ms};
|
|
|
|
for (int i = static_cast<int>(queue.size()) - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
maxYValue = std::max(maxYValue, queue[i].data[id]);
|
|
|
|
duration += queue[i].duration;
|
|
|
|
if (duration >= m_currentMaxDuration)
|
|
|
|
break;
|
|
|
|
}
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return maxYValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedPlotView::paintEvent(QPaintEvent *)
|
|
|
|
{
|
2016-03-20 11:31:10 +03:00
|
|
|
QPainter painter(viewport());
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
QRect fullRect = viewport()->rect();
|
|
|
|
QRect rect = viewport()->rect();
|
2016-01-17 20:05:54 +03:00
|
|
|
QFontMetrics fontMetrics = painter.fontMetrics();
|
2014-08-25 15:58:48 +04:00
|
|
|
|
|
|
|
rect.adjust(4, 4, 0, -4); // Add padding
|
2018-04-02 19:53:44 +03:00
|
|
|
const SplittedValue niceScale = getRoundedYScale(maxYValue());
|
2016-01-17 20:05:54 +03:00
|
|
|
rect.adjust(0, fontMetrics.height(), 0, 0); // Add top padding for top speed text
|
2014-08-25 15:58:48 +04:00
|
|
|
|
|
|
|
// draw Y axis speed labels
|
2020-11-16 10:02:11 +03:00
|
|
|
const QVector<QString> speedLabels =
|
|
|
|
{
|
2018-04-02 19:53:44 +03:00
|
|
|
formatLabel(niceScale.arg, niceScale.unit),
|
|
|
|
formatLabel((0.75 * niceScale.arg), niceScale.unit),
|
|
|
|
formatLabel((0.50 * niceScale.arg), niceScale.unit),
|
|
|
|
formatLabel((0.25 * niceScale.arg), niceScale.unit),
|
|
|
|
formatLabel(0.0, niceScale.unit),
|
2016-03-20 11:31:10 +03:00
|
|
|
};
|
2016-01-17 20:05:54 +03:00
|
|
|
|
2017-10-26 10:10:30 +03:00
|
|
|
int yAxisWidth = 0;
|
2016-04-13 01:53:54 +03:00
|
|
|
for (const QString &label : speedLabels)
|
2021-02-18 11:51:00 +03:00
|
|
|
{
|
2019-07-01 09:34:02 +03:00
|
|
|
if (fontMetrics.horizontalAdvance(label) > yAxisWidth)
|
|
|
|
yAxisWidth = fontMetrics.horizontalAdvance(label);
|
2021-02-18 11:51:00 +03:00
|
|
|
}
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
int i = 0;
|
2020-11-16 10:02:11 +03:00
|
|
|
for (const QString &label : speedLabels)
|
|
|
|
{
|
2017-10-26 10:10:30 +03:00
|
|
|
QRectF labelRect(rect.topLeft() + QPointF(-yAxisWidth, (i++) * 0.25 * rect.height() - fontMetrics.height()),
|
|
|
|
QSizeF(2 * yAxisWidth, fontMetrics.height()));
|
2016-03-20 11:31:10 +03:00
|
|
|
painter.drawText(labelRect, label, Qt::AlignRight | Qt::AlignTop);
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw grid lines
|
2017-10-26 10:10:30 +03:00
|
|
|
rect.adjust(yAxisWidth + 4, 0, 0, 0);
|
2016-01-17 20:05:54 +03:00
|
|
|
|
|
|
|
QPen gridPen;
|
|
|
|
gridPen.setStyle(Qt::DashLine);
|
|
|
|
gridPen.setWidthF(1);
|
|
|
|
gridPen.setColor(QColor(128, 128, 128, 128));
|
|
|
|
painter.setPen(gridPen);
|
|
|
|
|
|
|
|
painter.drawLine(fullRect.left(), rect.top(), rect.right(), rect.top());
|
|
|
|
painter.drawLine(fullRect.left(), rect.top() + 0.25 * rect.height(), rect.right(), rect.top() + 0.25 * rect.height());
|
|
|
|
painter.drawLine(fullRect.left(), rect.top() + 0.50 * rect.height(), rect.right(), rect.top() + 0.50 * rect.height());
|
|
|
|
painter.drawLine(fullRect.left(), rect.top() + 0.75 * rect.height(), rect.right(), rect.top() + 0.75 * rect.height());
|
|
|
|
painter.drawLine(fullRect.left(), rect.bottom(), rect.right(), rect.bottom());
|
|
|
|
|
2019-04-21 10:00:27 +03:00
|
|
|
const int TIME_AXIS_DIVISIONS = 6;
|
2020-11-16 10:02:11 +03:00
|
|
|
for (int i = 0; i < TIME_AXIS_DIVISIONS; ++i)
|
|
|
|
{
|
2019-04-21 10:00:27 +03:00
|
|
|
const int x = rect.left() + (i * rect.width()) / TIME_AXIS_DIVISIONS;
|
|
|
|
painter.drawLine(x, fullRect.top(), x, fullRect.bottom());
|
|
|
|
}
|
2014-08-25 15:58:48 +04:00
|
|
|
|
|
|
|
// Set antialiasing for graphs
|
2020-05-16 16:09:06 +03:00
|
|
|
painter.setRenderHints(QPainter::Antialiasing);
|
2014-08-25 15:58:48 +04:00
|
|
|
|
|
|
|
// draw graphs
|
2020-12-26 19:21:23 +03:00
|
|
|
// averager is duration based, it may go little above the maxDuration
|
|
|
|
painter.setClipping(true);
|
|
|
|
painter.setClipRect(rect);
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
const DataCircularBuffer &queue = currentData();
|
2016-01-17 20:05:54 +03:00
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
// last point will be drawn at x=0, so we don't need it in the calculation of xTickSize
|
|
|
|
const milliseconds lastDuration {queue.empty() ? 0ms : queue.back().duration};
|
|
|
|
const double xTickSize = static_cast<double>(rect.width()) / (m_currentMaxDuration - lastDuration).count();
|
|
|
|
const double yMultiplier = (niceScale.arg == 0) ? 0 : (static_cast<double>(rect.height()) / niceScale.sizeInBytes());
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
for (int id = UP; id < NB_GRAPHS; ++id)
|
|
|
|
{
|
2016-03-20 11:31:10 +03:00
|
|
|
if (!m_properties[static_cast<GraphID>(id)].enable)
|
2014-08-25 15:58:48 +04:00
|
|
|
continue;
|
|
|
|
|
2015-12-05 14:07:16 +03:00
|
|
|
QVector<QPoint> points;
|
2020-12-26 19:21:23 +03:00
|
|
|
milliseconds duration {0ms};
|
2015-12-05 14:07:16 +03:00
|
|
|
|
2020-12-26 19:21:23 +03:00
|
|
|
for (int i = static_cast<int>(queue.size()) - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
const int newX = rect.right() - (duration.count() * xTickSize);
|
|
|
|
const int newY = rect.bottom() - (queue[i].data[id] * yMultiplier);
|
2017-10-26 10:10:30 +03:00
|
|
|
points.push_back(QPoint(newX, newY));
|
2020-12-26 19:21:23 +03:00
|
|
|
|
|
|
|
duration += queue[i].duration;
|
|
|
|
if (duration >= m_currentMaxDuration)
|
|
|
|
break;
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
painter.setPen(m_properties[static_cast<GraphID>(id)].pen);
|
2014-08-25 15:58:48 +04:00
|
|
|
painter.drawPolyline(points.data(), points.size());
|
|
|
|
}
|
2020-12-26 19:21:23 +03:00
|
|
|
painter.setClipping(false);
|
2014-08-25 15:58:48 +04:00
|
|
|
|
|
|
|
// draw legend
|
2016-01-17 20:05:54 +03:00
|
|
|
QPoint legendTopLeft(rect.left() + 4, fullRect.top() + 4);
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2016-01-17 20:05:54 +03:00
|
|
|
double legendHeight = 0;
|
|
|
|
int legendWidth = 0;
|
2020-11-16 10:02:11 +03:00
|
|
|
for (const auto &property : asConst(m_properties))
|
|
|
|
{
|
2016-03-20 11:31:10 +03:00
|
|
|
if (!property.enable)
|
2014-08-25 15:58:48 +04:00
|
|
|
continue;
|
|
|
|
|
2019-07-01 09:34:02 +03:00
|
|
|
if (fontMetrics.horizontalAdvance(property.name) > legendWidth)
|
|
|
|
legendWidth = fontMetrics.horizontalAdvance(property.name);
|
2016-01-17 20:05:54 +03:00
|
|
|
legendHeight += 1.5 * fontMetrics.height();
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
|
2016-01-17 20:05:54 +03:00
|
|
|
QRectF legendBackgroundRect(QPoint(legendTopLeft.x() - 4, legendTopLeft.y() - 4), QSizeF(legendWidth + 8, legendHeight + 8));
|
2015-11-18 09:41:29 +03:00
|
|
|
QColor legendBackgroundColor = QWidget::palette().color(QWidget::backgroundRole());
|
|
|
|
legendBackgroundColor.setAlpha(128); // 50% transparent
|
2016-01-17 20:05:54 +03:00
|
|
|
painter.fillRect(legendBackgroundRect, legendBackgroundColor);
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
i = 0;
|
2020-11-16 10:02:11 +03:00
|
|
|
for (const auto &property : asConst(m_properties))
|
|
|
|
{
|
2016-03-20 11:31:10 +03:00
|
|
|
if (!property.enable)
|
2014-08-25 15:58:48 +04:00
|
|
|
continue;
|
|
|
|
|
2019-07-01 09:34:02 +03:00
|
|
|
int nameSize = fontMetrics.horizontalAdvance(property.name);
|
2016-03-20 11:31:10 +03:00
|
|
|
double indent = 1.5 * (i++) * fontMetrics.height();
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2016-03-20 11:31:10 +03:00
|
|
|
painter.setPen(property.pen);
|
2016-01-17 20:05:54 +03:00
|
|
|
painter.drawLine(legendTopLeft + QPointF(0, indent + fontMetrics.height()),
|
|
|
|
legendTopLeft + QPointF(nameSize, indent + fontMetrics.height()));
|
|
|
|
painter.drawText(QRectF(legendTopLeft + QPointF(0, indent), QSizeF(2 * nameSize, fontMetrics.height())),
|
2016-03-20 11:31:10 +03:00
|
|
|
property.name, QTextOption(Qt::AlignVCenter));
|
2014-08-25 15:58:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SpeedPlotView::GraphProperties::GraphProperties()
|
2016-03-20 11:31:10 +03:00
|
|
|
: enable(false)
|
2017-10-26 10:10:30 +03:00
|
|
|
{
|
|
|
|
}
|
2014-08-25 15:58:48 +04:00
|
|
|
|
2015-07-18 19:16:03 +03:00
|
|
|
SpeedPlotView::GraphProperties::GraphProperties(const QString &name, const QPen &pen, bool enable)
|
2016-03-20 11:31:10 +03:00
|
|
|
: name(name)
|
|
|
|
, pen(pen)
|
|
|
|
, enable(enable)
|
2017-10-26 10:10:30 +03:00
|
|
|
{
|
|
|
|
}
|