From dd9728334a768ee635bfe92c95b83fb7005a9bc2 Mon Sep 17 00:00:00 2001 From: Anton Lashkov Date: Sat, 5 Dec 2015 15:07:16 +0400 Subject: [PATCH 1/5] SpeedPlotView: Replace QQueue by boost::circular_buffer, reduce number of points to draw, increase legend background size --- src/gui/properties/speedplotview.cpp | 57 +++++++++++++++++++--------- src/gui/properties/speedplotview.h | 9 +++-- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/gui/properties/speedplotview.cpp b/src/gui/properties/speedplotview.cpp index 67ea81922..50cdd7ed3 100644 --- a/src/gui/properties/speedplotview.cpp +++ b/src/gui/properties/speedplotview.cpp @@ -34,6 +34,7 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) : QGraphicsView(parent) + , m_xData(HOUR6_SEC) , m_viewablePointsCount(MIN5_SEC) , m_maxCapacity(HOUR6_SEC) { @@ -66,6 +67,17 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) greenPen.setStyle(Qt::DotLine); m_properties[TRACKER_UP] = GraphProperties(tr("Tracker Upload"), bluePen); m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen); + + m_yData[UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[PAYLOAD_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[PAYLOAD_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[OVERHEAD_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[OVERHEAD_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[DHT_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[DHT_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[TRACKER_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[TRACKER_DOWN] = boost::circular_buffer(HOUR6_SEC); } void SpeedPlotView::setGraphEnable(GraphID id, bool enable) @@ -75,18 +87,12 @@ void SpeedPlotView::setGraphEnable(GraphID id, bool enable) void SpeedPlotView::pushXPoint(double x) { - while (m_xData.size() >= m_maxCapacity) - m_xData.pop_front(); - - m_xData.append(x); + m_xData.push_back(x); } void SpeedPlotView::pushYPoint(GraphID id, double y) { - while (m_yData[id].size() >= m_maxCapacity) - m_yData[id].pop_front(); - - m_yData[id].append(y); + m_yData[id].push_back(y); } void SpeedPlotView::setViewableLastPoints(TimePeriod period) @@ -117,16 +123,16 @@ void SpeedPlotView::replot() double SpeedPlotView::maxYValue() { double maxYValue = 0; - for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { + for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { if (!m_properties[it.key()].m_enable) continue; - QQueue &queue = m_yData[it.key()]; + const boost::circular_buffer &queue = it.value(); for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { - if (queue.at(i) > maxYValue) - maxYValue = queue.at(i); + if (queue[i] > maxYValue) + maxYValue = queue[i]; } } @@ -197,17 +203,32 @@ void SpeedPlotView::paintEvent(QPaintEvent *) double y_multiplier = (max_y == 0.0) ? 0.0 : rect.height() / max_y; double x_tick_size = double(rect.width()) / m_viewablePointsCount; - for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { + for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { if (!m_properties[it.key()].m_enable) continue; - QQueue &queue = m_yData[it.key()]; - QVector points; + const boost::circular_buffer &queue = it.value(); + QVector points; for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { - points.push_back(QPointF(rect.right() - j * x_tick_size, - rect.bottom() - queue.at(i) * y_multiplier)); + + int new_x = rect.right() - j * x_tick_size; + int new_y = rect.bottom() - queue[i] * y_multiplier; + + if (points.size() > 1) { + QPoint &last_point = points[points.size()-1]; + QPoint &prelast_point = points[points.size()-2]; + + if (last_point.x() == new_x && prelast_point.x() == new_x) + { + last_point.setY(qMax(last_point.y(), new_y)); + prelast_point.setY(qMin(prelast_point.y(), new_y)); + continue; + } + } + + points.push_back(QPoint(new_x, new_y)); } painter.setPen(m_properties[it.key()].m_pen); @@ -229,7 +250,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *) legend_height += 1.5 * font_metrics.height(); } - QRectF legend_background_rect(legend_top_left, QSizeF(legend_width, legend_height)); + QRectF legend_background_rect(QPoint(legend_top_left.x()-4, legend_top_left.y()-4), QSizeF(legend_width+8, legend_height+8)); QColor legendBackgroundColor = QWidget::palette().color(QWidget::backgroundRole()); legendBackgroundColor.setAlpha(128); // 50% transparent painter.fillRect(legend_background_rect, legendBackgroundColor); diff --git a/src/gui/properties/speedplotview.h b/src/gui/properties/speedplotview.h index bb8ef54cb..db1a34384 100644 --- a/src/gui/properties/speedplotview.h +++ b/src/gui/properties/speedplotview.h @@ -29,9 +29,12 @@ #ifndef SPEEDPLOTVIEW_H #define SPEEDPLOTVIEW_H +#ifndef Q_MOC_RUN +#include +#endif + #include #include -#include class QPen; class SpeedPlotView : public QGraphicsView @@ -94,8 +97,8 @@ private: bool m_enable; }; - QQueue m_xData; - QMap > m_yData; + boost::circular_buffer m_xData; + QMap > m_yData; QMap m_properties; PeriodInSeconds m_viewablePointsCount; From baadf34134fc26dd5913f4a035bdb9bcbe9924b5 Mon Sep 17 00:00:00 2001 From: Anton Lashkov Date: Sat, 5 Dec 2015 18:14:16 +0400 Subject: [PATCH 2/5] SpeedPlotView: Save int's instead of double's --- src/gui/properties/speedplotview.cpp | 40 ++++++++++++++-------------- src/gui/properties/speedplotview.h | 10 +++---- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/gui/properties/speedplotview.cpp b/src/gui/properties/speedplotview.cpp index 50cdd7ed3..6461cc7c0 100644 --- a/src/gui/properties/speedplotview.cpp +++ b/src/gui/properties/speedplotview.cpp @@ -68,16 +68,16 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) m_properties[TRACKER_UP] = GraphProperties(tr("Tracker Upload"), bluePen); m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen); - m_yData[UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[PAYLOAD_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[PAYLOAD_DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[OVERHEAD_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[OVERHEAD_DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[DHT_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[DHT_DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[TRACKER_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[TRACKER_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[PAYLOAD_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[PAYLOAD_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[OVERHEAD_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[OVERHEAD_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[DHT_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[DHT_DOWN] = boost::circular_buffer(HOUR6_SEC); + m_yData[TRACKER_UP] = boost::circular_buffer(HOUR6_SEC); + m_yData[TRACKER_DOWN] = boost::circular_buffer(HOUR6_SEC); } void SpeedPlotView::setGraphEnable(GraphID id, bool enable) @@ -85,12 +85,12 @@ void SpeedPlotView::setGraphEnable(GraphID id, bool enable) m_properties[id].m_enable = enable; } -void SpeedPlotView::pushXPoint(double x) +void SpeedPlotView::pushXPoint(uint x) { m_xData.push_back(x); } -void SpeedPlotView::pushYPoint(GraphID id, double y) +void SpeedPlotView::pushYPoint(GraphID id, int y) { m_yData[id].push_back(y); } @@ -120,15 +120,15 @@ void SpeedPlotView::replot() this->viewport()->update(); } -double SpeedPlotView::maxYValue() +int SpeedPlotView::maxYValue() { - double maxYValue = 0; - for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { + int maxYValue = 0; + for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { if (!m_properties[it.key()].m_enable) continue; - const boost::circular_buffer &queue = it.value(); + const boost::circular_buffer &queue = it.value(); for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { if (queue[i] > maxYValue) @@ -149,7 +149,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *) rect.adjust(4, 4, 0, -4); // Add padding - double max_y = maxYValue(); + int max_y = maxYValue(); rect.adjust(0, font_metrics.height(), 0, 0); // Add top padding for top speed text @@ -200,15 +200,15 @@ void SpeedPlotView::paintEvent(QPaintEvent *) // draw graphs rect.adjust(3, 0, 0, 0); // Need, else graphs cross left gridline - double y_multiplier = (max_y == 0.0) ? 0.0 : rect.height() / max_y; + double y_multiplier = (max_y == 0) ? 0.0 : double(rect.height()) / max_y; double x_tick_size = double(rect.width()) / m_viewablePointsCount; - for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { + for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { if (!m_properties[it.key()].m_enable) continue; - const boost::circular_buffer &queue = it.value(); + const boost::circular_buffer &queue = it.value(); QVector points; for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { diff --git a/src/gui/properties/speedplotview.h b/src/gui/properties/speedplotview.h index db1a34384..98cb2e6a1 100644 --- a/src/gui/properties/speedplotview.h +++ b/src/gui/properties/speedplotview.h @@ -68,8 +68,8 @@ public: void setGraphEnable(GraphID id, bool enable); - void pushXPoint(double x); - void pushYPoint(GraphID id, double y); + void pushXPoint(uint x); + void pushYPoint(GraphID id, int y); void setViewableLastPoints(TimePeriod period); @@ -97,14 +97,14 @@ private: bool m_enable; }; - boost::circular_buffer m_xData; - QMap > m_yData; + boost::circular_buffer m_xData; + QMap > m_yData; QMap m_properties; PeriodInSeconds m_viewablePointsCount; PeriodInSeconds m_maxCapacity; - double maxYValue(); + int maxYValue(); }; #endif // SPEEDPLOTVIEW_H From b599a8e2fb3575cf422adbf42e161ef40f563778 Mon Sep 17 00:00:00 2001 From: Anton Lashkov Date: Sun, 17 Jan 2016 21:05:54 +0400 Subject: [PATCH 3/5] SpeedPlotView: Use separate buffers for periods with reduce number of points. Fix coding style. --- src/gui/properties/speedplotview.cpp | 236 +++++++++++++++++---------- src/gui/properties/speedplotview.h | 28 +++- src/gui/properties/speedwidget.cpp | 3 - 3 files changed, 170 insertions(+), 97 deletions(-) diff --git a/src/gui/properties/speedplotview.cpp b/src/gui/properties/speedplotview.cpp index 6461cc7c0..2a53ff318 100644 --- a/src/gui/properties/speedplotview.cpp +++ b/src/gui/properties/speedplotview.cpp @@ -34,9 +34,9 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) : QGraphicsView(parent) - , m_xData(HOUR6_SEC) , m_viewablePointsCount(MIN5_SEC) - , m_maxCapacity(HOUR6_SEC) + , m_counter30Min(-1) + , m_counter6Hour(-1) { QPen greenPen; greenPen.setWidthF(1.5); @@ -68,62 +68,128 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) m_properties[TRACKER_UP] = GraphProperties(tr("Tracker Upload"), bluePen); m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen); - m_yData[UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[PAYLOAD_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[PAYLOAD_DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[OVERHEAD_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[OVERHEAD_DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[DHT_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[DHT_DOWN] = boost::circular_buffer(HOUR6_SEC); - m_yData[TRACKER_UP] = boost::circular_buffer(HOUR6_SEC); - m_yData[TRACKER_DOWN] = boost::circular_buffer(HOUR6_SEC); + initBuffers(MIN5_BUF_SIZE, m_xData5Min, m_yData5Min); + initBuffers(MIN30_BUF_SIZE, m_xData30Min, m_yData30Min); + initBuffers(HOUR6_BUF_SIZE, m_xData6Hour, m_yData6Hour); +} + +void SpeedPlotView::initBuffers(int capacity, boost::circular_buffer &xBuffer, + QMap > &yBuffers) +{ + xBuffer = boost::circular_buffer(capacity); + yBuffers[UP] = boost::circular_buffer(capacity); + yBuffers[DOWN] = boost::circular_buffer(capacity); + yBuffers[PAYLOAD_UP] = boost::circular_buffer(capacity); + yBuffers[PAYLOAD_DOWN] = boost::circular_buffer(capacity); + yBuffers[OVERHEAD_UP] = boost::circular_buffer(capacity); + yBuffers[OVERHEAD_DOWN] = boost::circular_buffer(capacity); + yBuffers[DHT_UP] = boost::circular_buffer(capacity); + yBuffers[DHT_DOWN] = boost::circular_buffer(capacity); + yBuffers[TRACKER_UP] = boost::circular_buffer(capacity); + yBuffers[TRACKER_DOWN] = boost::circular_buffer(capacity); } void SpeedPlotView::setGraphEnable(GraphID id, bool enable) { m_properties[id].m_enable = enable; + this->viewport()->update(); } void SpeedPlotView::pushXPoint(uint x) { - m_xData.push_back(x); + ++m_counter30Min; + m_counter30Min %= 3; + ++m_counter6Hour; + m_counter6Hour %= 6; + + m_xData5Min.push_back(x); + + if (m_counter30Min == 0) + m_xData30Min.push_back(x); + + if (m_counter6Hour == 0) + m_xData6Hour.push_back(x); } void SpeedPlotView::pushYPoint(GraphID id, int y) { - m_yData[id].push_back(y); + m_yData5Min[id].push_back(y); + + if (m_counter30Min == 0) + m_yData30Min[id].push_back(y); + else + m_yData30Min[id].back() = (m_yData30Min[id].back() * m_counter30Min + y) / (m_counter30Min + 1); + + if (m_counter6Hour == 0) + m_yData6Hour[id].push_back(y); + else + m_yData6Hour[id].back() = (m_yData6Hour[id].back() * m_counter6Hour + y) / (m_counter6Hour + 1); } void SpeedPlotView::setViewableLastPoints(TimePeriod period) { + m_period = period; + switch (period) { case SpeedPlotView::MIN1: - m_viewablePointsCount = SpeedPlotView::MIN1_SEC; + m_viewablePointsCount = MIN1_SEC; break; case SpeedPlotView::MIN5: - m_viewablePointsCount = SpeedPlotView::MIN5_SEC; + m_viewablePointsCount = MIN5_SEC; break; case SpeedPlotView::MIN30: - m_viewablePointsCount = SpeedPlotView::MIN30_SEC; + m_viewablePointsCount = MIN30_BUF_SIZE; break; case SpeedPlotView::HOUR6: - m_viewablePointsCount = SpeedPlotView::HOUR6_SEC; - break; - default: + m_viewablePointsCount = HOUR6_BUF_SIZE; break; } + + this->viewport()->update(); } void SpeedPlotView::replot() { - this->viewport()->update(); + if (m_period == MIN1 || m_period == MIN5 || + (m_period == MIN30 && m_counter30Min == 2) || + (m_period == HOUR6 && m_counter6Hour == 5)) + this->viewport()->update(); +} + +boost::circular_buffer &SpeedPlotView::getCurrentXData() +{ + switch (m_period) { + default: + case SpeedPlotView::MIN1: + case SpeedPlotView::MIN5: + return m_xData5Min; + case SpeedPlotView::MIN30: + return m_xData30Min; + case SpeedPlotView::HOUR6: + return m_xData6Hour; + } +} + +QMap > &SpeedPlotView::getCurrentYData() +{ + switch (m_period) { + default: + case SpeedPlotView::MIN1: + case SpeedPlotView::MIN5: + return m_yData5Min; + case SpeedPlotView::MIN30: + return m_yData30Min; + case SpeedPlotView::HOUR6: + return m_yData6Hour; + } } int SpeedPlotView::maxYValue() { + QMap > &yData = getCurrentYData(); + int maxYValue = 0; - for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { + for (QMap >::const_iterator it = yData.begin(); it != yData.end(); ++it) { if (!m_properties[it.key()].m_enable) continue; @@ -143,56 +209,56 @@ void SpeedPlotView::paintEvent(QPaintEvent *) { QPainter painter(this->viewport()); - QRect full_rect = this->viewport()->rect(); + QRect fullRect = this->viewport()->rect(); QRect rect = this->viewport()->rect(); - QFontMetrics font_metrics = painter.fontMetrics(); + QFontMetrics fontMetrics = painter.fontMetrics(); rect.adjust(4, 4, 0, -4); // Add padding - int max_y = maxYValue(); + int maxY = maxYValue(); - rect.adjust(0, font_metrics.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 - QVector speed_labels(QVector() << - Utils::Misc::friendlyUnit(max_y, true) << - Utils::Misc::friendlyUnit(0.75 * max_y, true) << - Utils::Misc::friendlyUnit(0.5 * max_y, true) << - Utils::Misc::friendlyUnit(0.25 * max_y, true) << - Utils::Misc::friendlyUnit(0, true)); - - int y_axe_width = 0; - for (int i = 0; i < speed_labels.size(); ++i) { - if (font_metrics.width(speed_labels[i]) > y_axe_width) - y_axe_width = font_metrics.width(speed_labels[i]); + QVector speedLabels(QVector() << + Utils::Misc::friendlyUnit(maxY, true) << + Utils::Misc::friendlyUnit(0.75 * maxY, true) << + Utils::Misc::friendlyUnit(0.5 * maxY, true) << + Utils::Misc::friendlyUnit(0.25 * maxY, true) << + Utils::Misc::friendlyUnit(0, true)); + + int yAxeWidth = 0; + for (int i = 0; i < speedLabels.size(); ++i) { + if (fontMetrics.width(speedLabels[i]) > yAxeWidth) + yAxeWidth = fontMetrics.width(speedLabels[i]); } - for (int i = 0; i < speed_labels.size(); ++i) { - QRectF label_rect(rect.topLeft() + QPointF(-y_axe_width, i * 0.25 * rect.height() - font_metrics.height()), - QSizeF(2 * y_axe_width, font_metrics.height())); - painter.drawText(label_rect, speed_labels[i], QTextOption((Qt::AlignRight) | (Qt::AlignTop))); + for (int i = 0; i < speedLabels.size(); ++i) { + QRectF label_rect(rect.topLeft() + QPointF(-yAxeWidth, i * 0.25 * rect.height() - fontMetrics.height()), + QSizeF(2 * yAxeWidth, fontMetrics.height())); + painter.drawText(label_rect, speedLabels[i], QTextOption((Qt::AlignRight) | (Qt::AlignTop))); } // draw grid lines - rect.adjust(y_axe_width + 4, 0, 0, 0); - - QPen grid_pen; - grid_pen.setStyle(Qt::DashLine); - grid_pen.setWidthF(1); - grid_pen.setColor(QColor(128, 128, 128, 128)); - painter.setPen(grid_pen); - - painter.drawLine(full_rect.left(), rect.top(), rect.right(), rect.top()); - painter.drawLine(full_rect.left(), rect.top() + 0.25 * rect.height(), rect.right(), rect.top() + 0.25 * rect.height()); - painter.drawLine(full_rect.left(), rect.top() + 0.50 * rect.height(), rect.right(), rect.top() + 0.50 * rect.height()); - painter.drawLine(full_rect.left(), rect.top() + 0.75 * rect.height(), rect.right(), rect.top() + 0.75 * rect.height()); - painter.drawLine(full_rect.left(), rect.bottom(), rect.right(), rect.bottom()); - - painter.drawLine(rect.left(), full_rect.top(), rect.left(), full_rect.bottom()); - painter.drawLine(rect.left() + 0.2 * rect.width(), full_rect.top(), rect.left() + 0.2 * rect.width(), full_rect.bottom()); - painter.drawLine(rect.left() + 0.4 * rect.width(), full_rect.top(), rect.left() + 0.4 * rect.width(), full_rect.bottom()); - painter.drawLine(rect.left() + 0.6 * rect.width(), full_rect.top(), rect.left() + 0.6 * rect.width(), full_rect.bottom()); - painter.drawLine(rect.left() + 0.8 * rect.width(), full_rect.top(), rect.left() + 0.8 * rect.width(), full_rect.bottom()); + rect.adjust(yAxeWidth + 4, 0, 0, 0); + + 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()); + + painter.drawLine(rect.left(), fullRect.top(), rect.left(), fullRect.bottom()); + painter.drawLine(rect.left() + 0.2 * rect.width(), fullRect.top(), rect.left() + 0.2 * rect.width(), fullRect.bottom()); + painter.drawLine(rect.left() + 0.4 * rect.width(), fullRect.top(), rect.left() + 0.4 * rect.width(), fullRect.bottom()); + painter.drawLine(rect.left() + 0.6 * rect.width(), fullRect.top(), rect.left() + 0.6 * rect.width(), fullRect.bottom()); + painter.drawLine(rect.left() + 0.8 * rect.width(), fullRect.top(), rect.left() + 0.8 * rect.width(), fullRect.bottom()); // Set antialiasing for graphs painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing); @@ -200,10 +266,12 @@ void SpeedPlotView::paintEvent(QPaintEvent *) // draw graphs rect.adjust(3, 0, 0, 0); // Need, else graphs cross left gridline - double y_multiplier = (max_y == 0) ? 0.0 : double(rect.height()) / max_y; - double x_tick_size = double(rect.width()) / m_viewablePointsCount; + double yMultiplier = (maxY == 0) ? 0.0 : double(rect.height()) / maxY; + double xTickSize = double(rect.width()) / m_viewablePointsCount; + + QMap > &yData = getCurrentYData(); - for (QMap >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { + for (QMap >::const_iterator it = yData.begin(); it != yData.end(); ++it) { if (!m_properties[it.key()].m_enable) continue; @@ -213,20 +281,8 @@ void SpeedPlotView::paintEvent(QPaintEvent *) for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { - int new_x = rect.right() - j * x_tick_size; - int new_y = rect.bottom() - queue[i] * y_multiplier; - - if (points.size() > 1) { - QPoint &last_point = points[points.size()-1]; - QPoint &prelast_point = points[points.size()-2]; - - if (last_point.x() == new_x && prelast_point.x() == new_x) - { - last_point.setY(qMax(last_point.y(), new_y)); - prelast_point.setY(qMin(prelast_point.y(), new_y)); - continue; - } - } + int new_x = rect.right() - j * xTickSize; + int new_y = rect.bottom() - queue[i] * yMultiplier; points.push_back(QPoint(new_x, new_y)); } @@ -236,24 +292,24 @@ void SpeedPlotView::paintEvent(QPaintEvent *) } // draw legend - QPoint legend_top_left(rect.left() + 4, full_rect.top() + 4); + QPoint legendTopLeft(rect.left() + 4, fullRect.top() + 4); - double legend_height = 0; - int legend_width = 0; + double legendHeight = 0; + int legendWidth = 0; for (QMap::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) { if (!it.value().m_enable) continue; - if (font_metrics.width(it.value().m_name) > legend_width) - legend_width = font_metrics.width(it.value().m_name); - legend_height += 1.5 * font_metrics.height(); + if (fontMetrics.width(it.value().m_name) > legendWidth) + legendWidth = fontMetrics.width(it.value().m_name); + legendHeight += 1.5 * fontMetrics.height(); } - QRectF legend_background_rect(QPoint(legend_top_left.x()-4, legend_top_left.y()-4), QSizeF(legend_width+8, legend_height+8)); + QRectF legendBackgroundRect(QPoint(legendTopLeft.x() - 4, legendTopLeft.y() - 4), QSizeF(legendWidth + 8, legendHeight + 8)); QColor legendBackgroundColor = QWidget::palette().color(QWidget::backgroundRole()); legendBackgroundColor.setAlpha(128); // 50% transparent - painter.fillRect(legend_background_rect, legendBackgroundColor); + painter.fillRect(legendBackgroundRect, legendBackgroundColor); int i = 0; for (QMap::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) { @@ -261,13 +317,13 @@ void SpeedPlotView::paintEvent(QPaintEvent *) if (!it.value().m_enable) continue; - int name_size = font_metrics.width(it.value().m_name); - double indent = 1.5 * i * font_metrics.height(); + int nameSize = fontMetrics.width(it.value().m_name); + double indent = 1.5 * i * fontMetrics.height(); painter.setPen(it.value().m_pen); - painter.drawLine(legend_top_left + QPointF(0, indent + font_metrics.height()), - legend_top_left + QPointF(name_size, indent + font_metrics.height())); - painter.drawText(QRectF(legend_top_left + QPointF(0, indent), QSizeF(2 * name_size, font_metrics.height())), + 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())), it.value().m_name, QTextOption(Qt::AlignVCenter)); ++i; } diff --git a/src/gui/properties/speedplotview.h b/src/gui/properties/speedplotview.h index 98cb2e6a1..15863dd49 100644 --- a/src/gui/properties/speedplotview.h +++ b/src/gui/properties/speedplotview.h @@ -87,6 +87,13 @@ private: HOUR6_SEC = 6 * 60 * 60 }; + enum PointsToSave + { + MIN5_BUF_SIZE = 5 * 60, + MIN30_BUF_SIZE = 10 * 60, + HOUR6_BUF_SIZE = 20 * 60 + }; + struct GraphProperties { GraphProperties(); @@ -97,14 +104,27 @@ private: bool m_enable; }; - boost::circular_buffer m_xData; - QMap > m_yData; + boost::circular_buffer m_xData5Min; + boost::circular_buffer m_xData30Min; + boost::circular_buffer m_xData6Hour; + QMap > m_yData5Min; + QMap > m_yData30Min; + QMap > m_yData6Hour; QMap m_properties; - PeriodInSeconds m_viewablePointsCount; - PeriodInSeconds m_maxCapacity; + TimePeriod m_period; + int m_viewablePointsCount; + + int m_counter30Min; + int m_counter6Hour; int maxYValue(); + + void initBuffers(int capacity, boost::circular_buffer &xBuffer, + QMap > &yBuffers); + + boost::circular_buffer &getCurrentXData(); + QMap > &getCurrentYData(); }; #endif // SPEEDPLOTVIEW_H diff --git a/src/gui/properties/speedwidget.cpp b/src/gui/properties/speedwidget.cpp index 6a321c2f8..903b03342 100644 --- a/src/gui/properties/speedwidget.cpp +++ b/src/gui/properties/speedwidget.cpp @@ -163,15 +163,12 @@ void SpeedWidget::graphUpdate() void SpeedWidget::onPeriodChange(int period) { m_plot->setViewableLastPoints(static_cast(period)); - graphUpdate(); } void SpeedWidget::onGraphChange(int id) { QAction *action = m_graphsMenuActions.at(id); m_plot->setGraphEnable(static_cast(id), action->isChecked()); - - graphUpdate(); } void SpeedWidget::loadSettings() From 8a6866d409c13bceaa044eab45a96a24283d05c7 Mon Sep 17 00:00:00 2001 From: Anton Lashkov Date: Sat, 27 Feb 2016 14:22:43 +0400 Subject: [PATCH 4/5] SpeedPlotView: Simplify code using PointData struct --- src/gui/properties/speedplotview.cpp | 115 +++++++++------------------ src/gui/properties/speedplotview.h | 24 +++--- src/gui/properties/speedwidget.cpp | 25 +++--- 3 files changed, 64 insertions(+), 100 deletions(-) diff --git a/src/gui/properties/speedplotview.cpp b/src/gui/properties/speedplotview.cpp index 2a53ff318..297f7123c 100644 --- a/src/gui/properties/speedplotview.cpp +++ b/src/gui/properties/speedplotview.cpp @@ -34,6 +34,9 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) : QGraphicsView(parent) + , m_data5Min(MIN5_BUF_SIZE) + , m_data30Min(MIN30_BUF_SIZE) + , m_data6Hour(HOUR6_BUF_SIZE) , m_viewablePointsCount(MIN5_SEC) , m_counter30Min(-1) , m_counter6Hour(-1) @@ -67,26 +70,6 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) greenPen.setStyle(Qt::DotLine); m_properties[TRACKER_UP] = GraphProperties(tr("Tracker Upload"), bluePen); m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen); - - initBuffers(MIN5_BUF_SIZE, m_xData5Min, m_yData5Min); - initBuffers(MIN30_BUF_SIZE, m_xData30Min, m_yData30Min); - initBuffers(HOUR6_BUF_SIZE, m_xData6Hour, m_yData6Hour); -} - -void SpeedPlotView::initBuffers(int capacity, boost::circular_buffer &xBuffer, - QMap > &yBuffers) -{ - xBuffer = boost::circular_buffer(capacity); - yBuffers[UP] = boost::circular_buffer(capacity); - yBuffers[DOWN] = boost::circular_buffer(capacity); - yBuffers[PAYLOAD_UP] = boost::circular_buffer(capacity); - yBuffers[PAYLOAD_DOWN] = boost::circular_buffer(capacity); - yBuffers[OVERHEAD_UP] = boost::circular_buffer(capacity); - yBuffers[OVERHEAD_DOWN] = boost::circular_buffer(capacity); - yBuffers[DHT_UP] = boost::circular_buffer(capacity); - yBuffers[DHT_DOWN] = boost::circular_buffer(capacity); - yBuffers[TRACKER_UP] = boost::circular_buffer(capacity); - yBuffers[TRACKER_DOWN] = boost::circular_buffer(capacity); } void SpeedPlotView::setGraphEnable(GraphID id, bool enable) @@ -95,35 +78,32 @@ void SpeedPlotView::setGraphEnable(GraphID id, bool enable) this->viewport()->update(); } -void SpeedPlotView::pushXPoint(uint x) +void SpeedPlotView::pushPoint(SpeedPlotView::PointData point) { - ++m_counter30Min; - m_counter30Min %= 3; - ++m_counter6Hour; - m_counter6Hour %= 6; + m_counter30Min = (m_counter30Min + 1) % 3; + m_counter6Hour = (m_counter6Hour + 1) % 6; - m_xData5Min.push_back(x); - - if (m_counter30Min == 0) - m_xData30Min.push_back(x); - - if (m_counter6Hour == 0) - m_xData6Hour.push_back(x); -} + m_data5Min.push_back(point); -void SpeedPlotView::pushYPoint(GraphID id, int y) -{ - m_yData5Min[id].push_back(y); - - if (m_counter30Min == 0) - m_yData30Min[id].push_back(y); - else - m_yData30Min[id].back() = (m_yData30Min[id].back() * m_counter30Min + y) / (m_counter30Min + 1); + if (m_counter30Min == 0) { + m_data30Min.push_back(point); + } + else { + m_data30Min.back().x = (m_data30Min.back().x * m_counter30Min + point.x) / (m_counter30Min + 1); + 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); + } + } - if (m_counter6Hour == 0) - m_yData6Hour[id].push_back(y); - else - m_yData6Hour[id].back() = (m_yData6Hour[id].back() * m_counter6Hour + y) / (m_counter6Hour + 1); + if (m_counter6Hour == 0) { + m_data6Hour.push_back(point); + } + else { + m_data6Hour.back().x = (m_data6Hour.back().x * m_counter6Hour + point.x) / (m_counter6Hour + 1); + 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); + } + } } void SpeedPlotView::setViewableLastPoints(TimePeriod period) @@ -156,49 +136,33 @@ void SpeedPlotView::replot() this->viewport()->update(); } -boost::circular_buffer &SpeedPlotView::getCurrentXData() +boost::circular_buffer &SpeedPlotView::getCurrentData() { switch (m_period) { default: case SpeedPlotView::MIN1: case SpeedPlotView::MIN5: - return m_xData5Min; + return m_data5Min; case SpeedPlotView::MIN30: - return m_xData30Min; + return m_data30Min; case SpeedPlotView::HOUR6: - return m_xData6Hour; - } -} - -QMap > &SpeedPlotView::getCurrentYData() -{ - switch (m_period) { - default: - case SpeedPlotView::MIN1: - case SpeedPlotView::MIN5: - return m_yData5Min; - case SpeedPlotView::MIN30: - return m_yData30Min; - case SpeedPlotView::HOUR6: - return m_yData6Hour; + return m_data6Hour; } } int SpeedPlotView::maxYValue() { - QMap > &yData = getCurrentYData(); + boost::circular_buffer &queue = getCurrentData(); int maxYValue = 0; - for (QMap >::const_iterator it = yData.begin(); it != yData.end(); ++it) { + for (int id = UP; id < NB_GRAPHS; ++id) { - if (!m_properties[it.key()].m_enable) + if (!m_properties[static_cast(id)].m_enable) continue; - const boost::circular_buffer &queue = it.value(); - for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { - if (queue[i] > maxYValue) - maxYValue = queue[i]; + if (queue[i].y[id] > maxYValue) + maxYValue = queue[i].y[id]; } } @@ -269,25 +233,24 @@ void SpeedPlotView::paintEvent(QPaintEvent *) double yMultiplier = (maxY == 0) ? 0.0 : double(rect.height()) / maxY; double xTickSize = double(rect.width()) / m_viewablePointsCount; - QMap > &yData = getCurrentYData(); + boost::circular_buffer &queue = getCurrentData(); - for (QMap >::const_iterator it = yData.begin(); it != yData.end(); ++it) { + for (int id = UP; id < NB_GRAPHS; ++id) { - if (!m_properties[it.key()].m_enable) + if (!m_properties[static_cast(id)].m_enable) continue; - const boost::circular_buffer &queue = it.value(); QVector points; for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) { int new_x = rect.right() - j * xTickSize; - int new_y = rect.bottom() - queue[i] * yMultiplier; + int new_y = rect.bottom() - queue[i].y[id] * yMultiplier; points.push_back(QPoint(new_x, new_y)); } - painter.setPen(m_properties[it.key()].m_pen); + painter.setPen(m_properties[static_cast(id)].m_pen); painter.drawPolyline(points.data(), points.size()); } diff --git a/src/gui/properties/speedplotview.h b/src/gui/properties/speedplotview.h index 15863dd49..39cb4bc3d 100644 --- a/src/gui/properties/speedplotview.h +++ b/src/gui/properties/speedplotview.h @@ -64,12 +64,17 @@ public: HOUR6 }; + struct PointData + { + uint x; + int y[NB_GRAPHS]; + }; + explicit SpeedPlotView(QWidget *parent = 0); void setGraphEnable(GraphID id, bool enable); - void pushXPoint(uint x); - void pushYPoint(GraphID id, int y); + void pushPoint(PointData point); void setViewableLastPoints(TimePeriod period); @@ -104,12 +109,9 @@ private: bool m_enable; }; - boost::circular_buffer m_xData5Min; - boost::circular_buffer m_xData30Min; - boost::circular_buffer m_xData6Hour; - QMap > m_yData5Min; - QMap > m_yData30Min; - QMap > m_yData6Hour; + boost::circular_buffer m_data5Min; + boost::circular_buffer m_data30Min; + boost::circular_buffer m_data6Hour; QMap m_properties; TimePeriod m_period; @@ -120,11 +122,7 @@ private: int maxYValue(); - void initBuffers(int capacity, boost::circular_buffer &xBuffer, - QMap > &yBuffers); - - boost::circular_buffer &getCurrentXData(); - QMap > &getCurrentYData(); + boost::circular_buffer &getCurrentData(); }; #endif // SPEEDPLOTVIEW_H diff --git a/src/gui/properties/speedwidget.cpp b/src/gui/properties/speedwidget.cpp index 903b03342..196d3a79b 100644 --- a/src/gui/properties/speedwidget.cpp +++ b/src/gui/properties/speedwidget.cpp @@ -138,17 +138,20 @@ void SpeedWidget::update() BitTorrent::SessionStatus btStatus = BitTorrent::Session::instance()->status(); - m_plot->pushXPoint(QDateTime::currentDateTime().toTime_t()); - m_plot->pushYPoint(SpeedPlotView::UP, btStatus.uploadRate()); - m_plot->pushYPoint(SpeedPlotView::DOWN, btStatus.downloadRate()); - m_plot->pushYPoint(SpeedPlotView::PAYLOAD_UP, btStatus.payloadUploadRate()); - m_plot->pushYPoint(SpeedPlotView::PAYLOAD_DOWN, btStatus.payloadDownloadRate()); - m_plot->pushYPoint(SpeedPlotView::OVERHEAD_UP, btStatus.ipOverheadUploadRate()); - m_plot->pushYPoint(SpeedPlotView::OVERHEAD_DOWN, btStatus.ipOverheadDownloadRate()); - m_plot->pushYPoint(SpeedPlotView::DHT_UP, btStatus.dhtUploadRate()); - m_plot->pushYPoint(SpeedPlotView::DHT_DOWN, btStatus.dhtDownloadRate()); - m_plot->pushYPoint(SpeedPlotView::TRACKER_UP, btStatus.trackerUploadRate()); - m_plot->pushYPoint(SpeedPlotView::TRACKER_DOWN, btStatus.trackerDownloadRate()); + SpeedPlotView::PointData point; + point.x = QDateTime::currentDateTime().toTime_t(); + point.y[SpeedPlotView::UP] = btStatus.uploadRate(); + point.y[SpeedPlotView::DOWN] = btStatus.downloadRate(); + point.y[SpeedPlotView::PAYLOAD_UP] = btStatus.payloadUploadRate(); + point.y[SpeedPlotView::PAYLOAD_DOWN] = btStatus.payloadDownloadRate(); + point.y[SpeedPlotView::OVERHEAD_UP] = btStatus.ipOverheadUploadRate(); + point.y[SpeedPlotView::OVERHEAD_DOWN] = btStatus.ipOverheadDownloadRate(); + point.y[SpeedPlotView::DHT_UP] = btStatus.dhtUploadRate(); + point.y[SpeedPlotView::DHT_DOWN] = btStatus.dhtDownloadRate(); + point.y[SpeedPlotView::TRACKER_UP] = btStatus.trackerUploadRate(); + point.y[SpeedPlotView::TRACKER_DOWN] = btStatus.trackerDownloadRate(); + + m_plot->pushPoint(point); QMetaObject::invokeMethod(this, "graphUpdate", Qt::QueuedConnection); Utils::Misc::msleep(1000); From cdab0bb140b76083dbc77dea3caaeba445d08a37 Mon Sep 17 00:00:00 2001 From: Anton Lashkov Date: Sun, 20 Mar 2016 12:31:10 +0400 Subject: [PATCH 5/5] SpeedPlotView: code correction --- src/gui/properties/speedplotview.cpp | 104 +++++++++++++-------------- src/gui/properties/speedplotview.h | 9 ++- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/src/gui/properties/speedplotview.cpp b/src/gui/properties/speedplotview.cpp index 297f7123c..cea241ded 100644 --- a/src/gui/properties/speedplotview.cpp +++ b/src/gui/properties/speedplotview.cpp @@ -74,8 +74,8 @@ SpeedPlotView::SpeedPlotView(QWidget *parent) void SpeedPlotView::setGraphEnable(GraphID id, bool enable) { - m_properties[id].m_enable = enable; - this->viewport()->update(); + m_properties[id].enable = enable; + viewport()->update(); } void SpeedPlotView::pushPoint(SpeedPlotView::PointData point) @@ -85,24 +85,20 @@ void SpeedPlotView::pushPoint(SpeedPlotView::PointData point) m_data5Min.push_back(point); - if (m_counter30Min == 0) { + if (m_counter30Min == 0) m_data30Min.push_back(point); - } else { 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); - } } - if (m_counter6Hour == 0) { + if (m_counter6Hour == 0) m_data6Hour.push_back(point); - } else { 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); - } } } @@ -125,23 +121,24 @@ void SpeedPlotView::setViewableLastPoints(TimePeriod period) break; } - this->viewport()->update(); + viewport()->update(); } void SpeedPlotView::replot() { - if (m_period == MIN1 || m_period == MIN5 || - (m_period == MIN30 && m_counter30Min == 2) || - (m_period == HOUR6 && m_counter6Hour == 5)) - this->viewport()->update(); + if (m_period == MIN1 + || m_period == MIN5 + || (m_period == MIN30 && m_counter30Min == 2) + || (m_period == HOUR6 && m_counter6Hour == 5)) + viewport()->update(); } boost::circular_buffer &SpeedPlotView::getCurrentData() { switch (m_period) { - default: case SpeedPlotView::MIN1: case SpeedPlotView::MIN5: + default: return m_data5Min; case SpeedPlotView::MIN30: return m_data30Min; @@ -157,7 +154,7 @@ int SpeedPlotView::maxYValue() int maxYValue = 0; for (int id = UP; id < NB_GRAPHS; ++id) { - if (!m_properties[static_cast(id)].m_enable) + if (!m_properties[static_cast(id)].enable) continue; 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 *) { - QPainter painter(this->viewport()); + QPainter painter(viewport()); - QRect fullRect = this->viewport()->rect(); - QRect rect = this->viewport()->rect(); + QRect fullRect = viewport()->rect(); + QRect rect = viewport()->rect(); QFontMetrics fontMetrics = painter.fontMetrics(); 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 // draw Y axis speed labels - QVector speedLabels(QVector() << - Utils::Misc::friendlyUnit(maxY, true) << - Utils::Misc::friendlyUnit(0.75 * maxY, true) << - Utils::Misc::friendlyUnit(0.5 * maxY, true) << - Utils::Misc::friendlyUnit(0.25 * maxY, true) << - Utils::Misc::friendlyUnit(0, true)); + QVector speedLabels = { + Utils::Misc::friendlyUnit(maxY, true), + Utils::Misc::friendlyUnit(0.75 * maxY, true), + Utils::Misc::friendlyUnit(0.5 * maxY, true), + Utils::Misc::friendlyUnit(0.25 * maxY, true), + Utils::Misc::friendlyUnit(0, true) + }; int yAxeWidth = 0; - for (int i = 0; i < speedLabels.size(); ++i) { - if (fontMetrics.width(speedLabels[i]) > yAxeWidth) - yAxeWidth = fontMetrics.width(speedLabels[i]); + for (const QString &label : speedLabels) { + if (fontMetrics.width(label) > yAxeWidth) + yAxeWidth = fontMetrics.width(label); } - for (int i = 0; i < speedLabels.size(); ++i) { - QRectF label_rect(rect.topLeft() + QPointF(-yAxeWidth, i * 0.25 * rect.height() - fontMetrics.height()), - QSizeF(2 * yAxeWidth, fontMetrics.height())); - painter.drawText(label_rect, speedLabels[i], QTextOption((Qt::AlignRight) | (Qt::AlignTop))); + int i = 0; + for (const QString &label : speedLabels) { + QRectF labelRect(rect.topLeft() + QPointF(-yAxeWidth, (i++) * 0.25 * rect.height() - fontMetrics.height()), + QSizeF(2 * yAxeWidth, fontMetrics.height())); + painter.drawText(labelRect, label, Qt::AlignRight | Qt::AlignTop); } // draw grid lines @@ -230,14 +229,14 @@ void SpeedPlotView::paintEvent(QPaintEvent *) // draw graphs rect.adjust(3, 0, 0, 0); // Need, else graphs cross left gridline - double yMultiplier = (maxY == 0) ? 0.0 : double(rect.height()) / maxY; - double xTickSize = double(rect.width()) / m_viewablePointsCount; + double yMultiplier = (maxY == 0) ? 0.0 : static_cast(rect.height()) / maxY; + double xTickSize = static_cast(rect.width()) / m_viewablePointsCount; boost::circular_buffer &queue = getCurrentData(); for (int id = UP; id < NB_GRAPHS; ++id) { - if (!m_properties[static_cast(id)].m_enable) + if (!m_properties[static_cast(id)].enable) continue; QVector points; @@ -250,7 +249,7 @@ void SpeedPlotView::paintEvent(QPaintEvent *) points.push_back(QPoint(new_x, new_y)); } - painter.setPen(m_properties[static_cast(id)].m_pen); + painter.setPen(m_properties[static_cast(id)].pen); painter.drawPolyline(points.data(), points.size()); } @@ -259,13 +258,13 @@ void SpeedPlotView::paintEvent(QPaintEvent *) double legendHeight = 0; int legendWidth = 0; - for (QMap::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; - if (fontMetrics.width(it.value().m_name) > legendWidth) - legendWidth = fontMetrics.width(it.value().m_name); + if (fontMetrics.width(property.name) > legendWidth) + legendWidth = fontMetrics.width(property.name); legendHeight += 1.5 * fontMetrics.height(); } @@ -274,33 +273,32 @@ void SpeedPlotView::paintEvent(QPaintEvent *) legendBackgroundColor.setAlpha(128); // 50% transparent painter.fillRect(legendBackgroundRect, legendBackgroundColor); - int i = 0; - for (QMap::const_iterator it = m_properties.begin(); it != m_properties.end(); ++it) { + i = 0; + for (const auto &property : m_properties) { - if (!it.value().m_enable) + if (!property.enable) continue; - int nameSize = fontMetrics.width(it.value().m_name); - double indent = 1.5 * i * fontMetrics.height(); + int nameSize = fontMetrics.width(property.name); + 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()), legendTopLeft + QPointF(nameSize, indent + fontMetrics.height())); painter.drawText(QRectF(legendTopLeft + QPointF(0, indent), QSizeF(2 * nameSize, fontMetrics.height())), - it.value().m_name, QTextOption(Qt::AlignVCenter)); - ++i; + property.name, QTextOption(Qt::AlignVCenter)); } } SpeedPlotView::GraphProperties::GraphProperties() - : m_enable(false) + : enable(false) { } SpeedPlotView::GraphProperties::GraphProperties(const QString &name, const QPen &pen, bool enable) - : m_name(name) - , m_pen(pen) - , m_enable(enable) + : name(name) + , pen(pen) + , enable(enable) { } diff --git a/src/gui/properties/speedplotview.h b/src/gui/properties/speedplotview.h index 39cb4bc3d..f4d91ed10 100644 --- a/src/gui/properties/speedplotview.h +++ b/src/gui/properties/speedplotview.h @@ -73,11 +73,10 @@ public: explicit SpeedPlotView(QWidget *parent = 0); void setGraphEnable(GraphID id, bool enable); + void setViewableLastPoints(TimePeriod period); void pushPoint(PointData point); - void setViewableLastPoints(TimePeriod period); - void replot(); protected: @@ -104,9 +103,9 @@ private: GraphProperties(); GraphProperties(const QString &name, const QPen &pen, bool enable = false); - QString m_name; - QPen m_pen; - bool m_enable; + QString name; + QPen pen; + bool enable; }; boost::circular_buffer m_data5Min;