mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-28 23:44:32 +00:00
SpeedPlotView: code correction
This commit is contained in:
parent
8a6866d409
commit
cdab0bb140
@ -74,8 +74,8 @@ SpeedPlotView::SpeedPlotView(QWidget *parent)
|
|||||||
|
|
||||||
void SpeedPlotView::setGraphEnable(GraphID id, bool enable)
|
void SpeedPlotView::setGraphEnable(GraphID id, bool enable)
|
||||||
{
|
{
|
||||||
m_properties[id].m_enable = enable;
|
m_properties[id].enable = enable;
|
||||||
this->viewport()->update();
|
viewport()->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::pushPoint(SpeedPlotView::PointData point)
|
void SpeedPlotView::pushPoint(SpeedPlotView::PointData point)
|
||||||
@ -85,24 +85,20 @@ void SpeedPlotView::pushPoint(SpeedPlotView::PointData point)
|
|||||||
|
|
||||||
m_data5Min.push_back(point);
|
m_data5Min.push_back(point);
|
||||||
|
|
||||||
if (m_counter30Min == 0) {
|
if (m_counter30Min == 0)
|
||||||
m_data30Min.push_back(point);
|
m_data30Min.push_back(point);
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
m_data30Min.back().x = (m_data30Min.back().x * m_counter30Min + point.x) / (m_counter30Min + 1);
|
m_data30Min.back().x = (m_data30Min.back().x * m_counter30Min + point.x) / (m_counter30Min + 1);
|
||||||
for (int id = UP; id < NB_GRAPHS; ++id) {
|
for (int id = UP; id < NB_GRAPHS; ++id)
|
||||||
m_data30Min.back().y[id] = (m_data30Min.back().y[id] * m_counter30Min + point.y[id]) / (m_counter30Min + 1);
|
m_data30Min.back().y[id] = (m_data30Min.back().y[id] * m_counter30Min + point.y[id]) / (m_counter30Min + 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_counter6Hour == 0) {
|
if (m_counter6Hour == 0)
|
||||||
m_data6Hour.push_back(point);
|
m_data6Hour.push_back(point);
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
m_data6Hour.back().x = (m_data6Hour.back().x * m_counter6Hour + point.x) / (m_counter6Hour + 1);
|
m_data6Hour.back().x = (m_data6Hour.back().x * m_counter6Hour + point.x) / (m_counter6Hour + 1);
|
||||||
for (int id = UP; id < NB_GRAPHS; ++id) {
|
for (int id = UP; id < NB_GRAPHS; ++id)
|
||||||
m_data6Hour.back().y[id] = (m_data6Hour.back().y[id] * m_counter6Hour + point.y[id]) / (m_counter6Hour + 1);
|
m_data6Hour.back().y[id] = (m_data6Hour.back().y[id] * m_counter6Hour + point.y[id]) / (m_counter6Hour + 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,23 +121,24 @@ void SpeedPlotView::setViewableLastPoints(TimePeriod period)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->viewport()->update();
|
viewport()->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpeedPlotView::replot()
|
void SpeedPlotView::replot()
|
||||||
{
|
{
|
||||||
if (m_period == MIN1 || m_period == MIN5 ||
|
if (m_period == MIN1
|
||||||
(m_period == MIN30 && m_counter30Min == 2) ||
|
|| m_period == MIN5
|
||||||
(m_period == HOUR6 && m_counter6Hour == 5))
|
|| (m_period == MIN30 && m_counter30Min == 2)
|
||||||
this->viewport()->update();
|
|| (m_period == HOUR6 && m_counter6Hour == 5))
|
||||||
|
viewport()->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::circular_buffer<SpeedPlotView::PointData> &SpeedPlotView::getCurrentData()
|
boost::circular_buffer<SpeedPlotView::PointData> &SpeedPlotView::getCurrentData()
|
||||||
{
|
{
|
||||||
switch (m_period) {
|
switch (m_period) {
|
||||||
default:
|
|
||||||
case SpeedPlotView::MIN1:
|
case SpeedPlotView::MIN1:
|
||||||
case SpeedPlotView::MIN5:
|
case SpeedPlotView::MIN5:
|
||||||
|
default:
|
||||||
return m_data5Min;
|
return m_data5Min;
|
||||||
case SpeedPlotView::MIN30:
|
case SpeedPlotView::MIN30:
|
||||||
return m_data30Min;
|
return m_data30Min;
|
||||||
@ -157,7 +154,7 @@ int SpeedPlotView::maxYValue()
|
|||||||
int maxYValue = 0;
|
int maxYValue = 0;
|
||||||
for (int id = UP; id < NB_GRAPHS; ++id) {
|
for (int id = UP; id < NB_GRAPHS; ++id) {
|
||||||
|
|
||||||
if (!m_properties[static_cast<GraphID>(id)].m_enable)
|
if (!m_properties[static_cast<GraphID>(id)].enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) {
|
for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) {
|
||||||
@ -171,10 +168,10 @@ int SpeedPlotView::maxYValue()
|
|||||||
|
|
||||||
void SpeedPlotView::paintEvent(QPaintEvent *)
|
void SpeedPlotView::paintEvent(QPaintEvent *)
|
||||||
{
|
{
|
||||||
QPainter painter(this->viewport());
|
QPainter painter(viewport());
|
||||||
|
|
||||||
QRect fullRect = this->viewport()->rect();
|
QRect fullRect = viewport()->rect();
|
||||||
QRect rect = this->viewport()->rect();
|
QRect rect = viewport()->rect();
|
||||||
QFontMetrics fontMetrics = painter.fontMetrics();
|
QFontMetrics fontMetrics = painter.fontMetrics();
|
||||||
|
|
||||||
rect.adjust(4, 4, 0, -4); // Add padding
|
rect.adjust(4, 4, 0, -4); // Add padding
|
||||||
@ -184,23 +181,25 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
rect.adjust(0, fontMetrics.height(), 0, 0); // Add top padding for top speed text
|
rect.adjust(0, fontMetrics.height(), 0, 0); // Add top padding for top speed text
|
||||||
|
|
||||||
// draw Y axis speed labels
|
// draw Y axis speed labels
|
||||||
QVector<QString> speedLabels(QVector<QString>() <<
|
QVector<QString> speedLabels = {
|
||||||
Utils::Misc::friendlyUnit(maxY, true) <<
|
Utils::Misc::friendlyUnit(maxY, true),
|
||||||
Utils::Misc::friendlyUnit(0.75 * maxY, true) <<
|
Utils::Misc::friendlyUnit(0.75 * maxY, true),
|
||||||
Utils::Misc::friendlyUnit(0.5 * maxY, true) <<
|
Utils::Misc::friendlyUnit(0.5 * maxY, true),
|
||||||
Utils::Misc::friendlyUnit(0.25 * maxY, true) <<
|
Utils::Misc::friendlyUnit(0.25 * maxY, true),
|
||||||
Utils::Misc::friendlyUnit(0, true));
|
Utils::Misc::friendlyUnit(0, true)
|
||||||
|
};
|
||||||
|
|
||||||
int yAxeWidth = 0;
|
int yAxeWidth = 0;
|
||||||
for (int i = 0; i < speedLabels.size(); ++i) {
|
for (const QString &label : speedLabels) {
|
||||||
if (fontMetrics.width(speedLabels[i]) > yAxeWidth)
|
if (fontMetrics.width(label) > yAxeWidth)
|
||||||
yAxeWidth = fontMetrics.width(speedLabels[i]);
|
yAxeWidth = fontMetrics.width(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < speedLabels.size(); ++i) {
|
int i = 0;
|
||||||
QRectF label_rect(rect.topLeft() + QPointF(-yAxeWidth, i * 0.25 * rect.height() - fontMetrics.height()),
|
for (const QString &label : speedLabels) {
|
||||||
QSizeF(2 * yAxeWidth, fontMetrics.height()));
|
QRectF labelRect(rect.topLeft() + QPointF(-yAxeWidth, (i++) * 0.25 * rect.height() - fontMetrics.height()),
|
||||||
painter.drawText(label_rect, speedLabels[i], QTextOption((Qt::AlignRight) | (Qt::AlignTop)));
|
QSizeF(2 * yAxeWidth, fontMetrics.height()));
|
||||||
|
painter.drawText(labelRect, label, Qt::AlignRight | Qt::AlignTop);
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw grid lines
|
// draw grid lines
|
||||||
@ -230,14 +229,14 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
// draw graphs
|
// draw graphs
|
||||||
rect.adjust(3, 0, 0, 0); // Need, else graphs cross left gridline
|
rect.adjust(3, 0, 0, 0); // Need, else graphs cross left gridline
|
||||||
|
|
||||||
double yMultiplier = (maxY == 0) ? 0.0 : double(rect.height()) / maxY;
|
double yMultiplier = (maxY == 0) ? 0.0 : static_cast<double>(rect.height()) / maxY;
|
||||||
double xTickSize = double(rect.width()) / m_viewablePointsCount;
|
double xTickSize = static_cast<double>(rect.width()) / m_viewablePointsCount;
|
||||||
|
|
||||||
boost::circular_buffer<PointData> &queue = getCurrentData();
|
boost::circular_buffer<PointData> &queue = getCurrentData();
|
||||||
|
|
||||||
for (int id = UP; id < NB_GRAPHS; ++id) {
|
for (int id = UP; id < NB_GRAPHS; ++id) {
|
||||||
|
|
||||||
if (!m_properties[static_cast<GraphID>(id)].m_enable)
|
if (!m_properties[static_cast<GraphID>(id)].enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QVector<QPoint> points;
|
QVector<QPoint> points;
|
||||||
@ -250,7 +249,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
points.push_back(QPoint(new_x, new_y));
|
points.push_back(QPoint(new_x, new_y));
|
||||||
}
|
}
|
||||||
|
|
||||||
painter.setPen(m_properties[static_cast<GraphID>(id)].m_pen);
|
painter.setPen(m_properties[static_cast<GraphID>(id)].pen);
|
||||||
painter.drawPolyline(points.data(), points.size());
|
painter.drawPolyline(points.data(), points.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,13 +258,13 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
|
|
||||||
double legendHeight = 0;
|
double legendHeight = 0;
|
||||||
int legendWidth = 0;
|
int legendWidth = 0;
|
||||||
for (QMap<GraphID, GraphProperties>::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) {
|
for (const auto &property : m_properties) {
|
||||||
|
|
||||||
if (!it.value().m_enable)
|
if (!property.enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (fontMetrics.width(it.value().m_name) > legendWidth)
|
if (fontMetrics.width(property.name) > legendWidth)
|
||||||
legendWidth = fontMetrics.width(it.value().m_name);
|
legendWidth = fontMetrics.width(property.name);
|
||||||
legendHeight += 1.5 * fontMetrics.height();
|
legendHeight += 1.5 * fontMetrics.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,33 +273,32 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
|
|||||||
legendBackgroundColor.setAlpha(128); // 50% transparent
|
legendBackgroundColor.setAlpha(128); // 50% transparent
|
||||||
painter.fillRect(legendBackgroundRect, legendBackgroundColor);
|
painter.fillRect(legendBackgroundRect, legendBackgroundColor);
|
||||||
|
|
||||||
int i = 0;
|
i = 0;
|
||||||
for (QMap<GraphID, GraphProperties>::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) {
|
for (const auto &property : m_properties) {
|
||||||
|
|
||||||
if (!it.value().m_enable)
|
if (!property.enable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int nameSize = fontMetrics.width(it.value().m_name);
|
int nameSize = fontMetrics.width(property.name);
|
||||||
double indent = 1.5 * i * fontMetrics.height();
|
double indent = 1.5 * (i++) * fontMetrics.height();
|
||||||
|
|
||||||
painter.setPen(it.value().m_pen);
|
painter.setPen(property.pen);
|
||||||
painter.drawLine(legendTopLeft + QPointF(0, indent + fontMetrics.height()),
|
painter.drawLine(legendTopLeft + QPointF(0, indent + fontMetrics.height()),
|
||||||
legendTopLeft + QPointF(nameSize, indent + fontMetrics.height()));
|
legendTopLeft + QPointF(nameSize, indent + fontMetrics.height()));
|
||||||
painter.drawText(QRectF(legendTopLeft + QPointF(0, indent), QSizeF(2 * nameSize, fontMetrics.height())),
|
painter.drawText(QRectF(legendTopLeft + QPointF(0, indent), QSizeF(2 * nameSize, fontMetrics.height())),
|
||||||
it.value().m_name, QTextOption(Qt::AlignVCenter));
|
property.name, QTextOption(Qt::AlignVCenter));
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SpeedPlotView::GraphProperties::GraphProperties()
|
SpeedPlotView::GraphProperties::GraphProperties()
|
||||||
: m_enable(false)
|
: enable(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SpeedPlotView::GraphProperties::GraphProperties(const QString &name, const QPen &pen, bool enable)
|
SpeedPlotView::GraphProperties::GraphProperties(const QString &name, const QPen &pen, bool enable)
|
||||||
: m_name(name)
|
: name(name)
|
||||||
, m_pen(pen)
|
, pen(pen)
|
||||||
, m_enable(enable)
|
, enable(enable)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,11 +73,10 @@ public:
|
|||||||
explicit SpeedPlotView(QWidget *parent = 0);
|
explicit SpeedPlotView(QWidget *parent = 0);
|
||||||
|
|
||||||
void setGraphEnable(GraphID id, bool enable);
|
void setGraphEnable(GraphID id, bool enable);
|
||||||
|
void setViewableLastPoints(TimePeriod period);
|
||||||
|
|
||||||
void pushPoint(PointData point);
|
void pushPoint(PointData point);
|
||||||
|
|
||||||
void setViewableLastPoints(TimePeriod period);
|
|
||||||
|
|
||||||
void replot();
|
void replot();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -104,9 +103,9 @@ private:
|
|||||||
GraphProperties();
|
GraphProperties();
|
||||||
GraphProperties(const QString &name, const QPen &pen, bool enable = false);
|
GraphProperties(const QString &name, const QPen &pen, bool enable = false);
|
||||||
|
|
||||||
QString m_name;
|
QString name;
|
||||||
QPen m_pen;
|
QPen pen;
|
||||||
bool m_enable;
|
bool enable;
|
||||||
};
|
};
|
||||||
|
|
||||||
boost::circular_buffer<PointData> m_data5Min;
|
boost::circular_buffer<PointData> m_data5Min;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user