Browse Source

Merge pull request #4261 from onto/master

SpeedPlotView: speedup
adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
5e796054ba
  1. 240
      src/gui/properties/speedplotview.cpp
  2. 46
      src/gui/properties/speedplotview.h
  3. 28
      src/gui/properties/speedwidget.cpp

240
src/gui/properties/speedplotview.cpp

@ -34,8 +34,12 @@
SpeedPlotView::SpeedPlotView(QWidget *parent) SpeedPlotView::SpeedPlotView(QWidget *parent)
: QGraphicsView(parent) : QGraphicsView(parent)
, m_data5Min(MIN5_BUF_SIZE)
, m_data30Min(MIN30_BUF_SIZE)
, m_data6Hour(HOUR6_BUF_SIZE)
, m_viewablePointsCount(MIN5_SEC) , m_viewablePointsCount(MIN5_SEC)
, m_maxCapacity(HOUR6_SEC) , m_counter30Min(-1)
, m_counter6Hour(-1)
{ {
QPen greenPen; QPen greenPen;
greenPen.setWidthF(1.5); greenPen.setWidthF(1.5);
@ -70,63 +74,92 @@ 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;
viewport()->update();
} }
void SpeedPlotView::pushXPoint(double x) void SpeedPlotView::pushPoint(SpeedPlotView::PointData point)
{ {
while (m_xData.size() >= m_maxCapacity) m_counter30Min = (m_counter30Min + 1) % 3;
m_xData.pop_front(); m_counter6Hour = (m_counter6Hour + 1) % 6;
m_xData.append(x); m_data5Min.push_back(point);
}
void SpeedPlotView::pushYPoint(GraphID id, double y) if (m_counter30Min == 0)
{ m_data30Min.push_back(point);
while (m_yData[id].size() >= m_maxCapacity) else {
m_yData[id].pop_front(); 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);
}
m_yData[id].append(y); 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) void SpeedPlotView::setViewableLastPoints(TimePeriod period)
{ {
m_period = period;
switch (period) { switch (period) {
case SpeedPlotView::MIN1: case SpeedPlotView::MIN1:
m_viewablePointsCount = SpeedPlotView::MIN1_SEC; m_viewablePointsCount = MIN1_SEC;
break; break;
case SpeedPlotView::MIN5: case SpeedPlotView::MIN5:
m_viewablePointsCount = SpeedPlotView::MIN5_SEC; m_viewablePointsCount = MIN5_SEC;
break; break;
case SpeedPlotView::MIN30: case SpeedPlotView::MIN30:
m_viewablePointsCount = SpeedPlotView::MIN30_SEC; m_viewablePointsCount = MIN30_BUF_SIZE;
break; break;
case SpeedPlotView::HOUR6: case SpeedPlotView::HOUR6:
m_viewablePointsCount = SpeedPlotView::HOUR6_SEC; m_viewablePointsCount = HOUR6_BUF_SIZE;
break;
default:
break; break;
} }
viewport()->update();
} }
void SpeedPlotView::replot() 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))
viewport()->update();
}
boost::circular_buffer<SpeedPlotView::PointData> &SpeedPlotView::getCurrentData()
{
switch (m_period) {
case SpeedPlotView::MIN1:
case SpeedPlotView::MIN5:
default:
return m_data5Min;
case SpeedPlotView::MIN30:
return m_data30Min;
case SpeedPlotView::HOUR6:
return m_data6Hour;
}
} }
double SpeedPlotView::maxYValue() int SpeedPlotView::maxYValue()
{ {
double maxYValue = 0; boost::circular_buffer<PointData> &queue = getCurrentData();
for (QMap<GraphID, QQueue<double> >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) {
if (!m_properties[it.key()].m_enable) int maxYValue = 0;
continue; for (int id = UP; id < NB_GRAPHS; ++id) {
QQueue<double> &queue = m_yData[it.key()]; if (!m_properties[static_cast<GraphID>(id)].enable)
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) {
if (queue.at(i) > maxYValue) if (queue[i].y[id] > maxYValue)
maxYValue = queue.at(i); maxYValue = queue[i].y[id];
} }
} }
@ -135,58 +168,60 @@ double SpeedPlotView::maxYValue()
void SpeedPlotView::paintEvent(QPaintEvent *) void SpeedPlotView::paintEvent(QPaintEvent *)
{ {
QPainter painter(this->viewport()); QPainter painter(viewport());
QRect full_rect = this->viewport()->rect(); QRect fullRect = viewport()->rect();
QRect rect = this->viewport()->rect(); QRect rect = viewport()->rect();
QFontMetrics font_metrics = painter.fontMetrics(); QFontMetrics fontMetrics = painter.fontMetrics();
rect.adjust(4, 4, 0, -4); // Add padding rect.adjust(4, 4, 0, -4); // Add padding
double 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 // draw Y axis speed labels
QVector<QString> speed_labels(QVector<QString>() << QVector<QString> speedLabels = {
Utils::Misc::friendlyUnit(max_y, true) << Utils::Misc::friendlyUnit(maxY, true),
Utils::Misc::friendlyUnit(0.75 * max_y, true) << Utils::Misc::friendlyUnit(0.75 * maxY, true),
Utils::Misc::friendlyUnit(0.5 * max_y, true) << Utils::Misc::friendlyUnit(0.5 * maxY, true),
Utils::Misc::friendlyUnit(0.25 * max_y, true) << Utils::Misc::friendlyUnit(0.25 * maxY, true),
Utils::Misc::friendlyUnit(0, true)); Utils::Misc::friendlyUnit(0, true)
};
int y_axe_width = 0;
for (int i = 0; i < speed_labels.size(); ++i) { int yAxeWidth = 0;
if (font_metrics.width(speed_labels[i]) > y_axe_width) for (const QString &label : speedLabels) {
y_axe_width = font_metrics.width(speed_labels[i]); if (fontMetrics.width(label) > yAxeWidth)
yAxeWidth = fontMetrics.width(label);
} }
for (int i = 0; i < speed_labels.size(); ++i) { int i = 0;
QRectF label_rect(rect.topLeft() + QPointF(-y_axe_width, i * 0.25 * rect.height() - font_metrics.height()), for (const QString &label : speedLabels) {
QSizeF(2 * y_axe_width, font_metrics.height())); QRectF labelRect(rect.topLeft() + QPointF(-yAxeWidth, (i++) * 0.25 * rect.height() - fontMetrics.height()),
painter.drawText(label_rect, speed_labels[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
rect.adjust(y_axe_width + 4, 0, 0, 0); rect.adjust(yAxeWidth + 4, 0, 0, 0);
QPen grid_pen; QPen gridPen;
grid_pen.setStyle(Qt::DashLine); gridPen.setStyle(Qt::DashLine);
grid_pen.setWidthF(1); gridPen.setWidthF(1);
grid_pen.setColor(QColor(128, 128, 128, 128)); gridPen.setColor(QColor(128, 128, 128, 128));
painter.setPen(grid_pen); painter.setPen(gridPen);
painter.drawLine(full_rect.left(), rect.top(), rect.right(), rect.top()); painter.drawLine(fullRect.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(fullRect.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(fullRect.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(fullRect.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(fullRect.left(), rect.bottom(), rect.right(), rect.bottom());
painter.drawLine(rect.left(), full_rect.top(), rect.left(), full_rect.bottom()); painter.drawLine(rect.left(), fullRect.top(), rect.left(), fullRect.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.2 * rect.width(), fullRect.top(), rect.left() + 0.2 * rect.width(), fullRect.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.4 * rect.width(), fullRect.top(), rect.left() + 0.4 * rect.width(), fullRect.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.6 * rect.width(), fullRect.top(), rect.left() + 0.6 * rect.width(), fullRect.bottom());
painter.drawLine(rect.left() + 0.8 * rect.width(), full_rect.top(), rect.left() + 0.8 * rect.width(), full_rect.bottom()); painter.drawLine(rect.left() + 0.8 * rect.width(), fullRect.top(), rect.left() + 0.8 * rect.width(), fullRect.bottom());
// Set antialiasing for graphs // Set antialiasing for graphs
painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing); painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing);
@ -194,73 +229,76 @@ 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 y_multiplier = (max_y == 0.0) ? 0.0 : rect.height() / max_y; double yMultiplier = (maxY == 0) ? 0.0 : static_cast<double>(rect.height()) / maxY;
double x_tick_size = double(rect.width()) / m_viewablePointsCount; double xTickSize = static_cast<double>(rect.width()) / m_viewablePointsCount;
boost::circular_buffer<PointData> &queue = getCurrentData();
for (QMap<GraphID, QQueue<double> >::const_iterator it = m_yData.begin(); it != m_yData.end(); ++it) { for (int id = UP; id < NB_GRAPHS; ++id) {
if (!m_properties[it.key()].m_enable) if (!m_properties[static_cast<GraphID>(id)].enable)
continue; continue;
QQueue<double> &queue = m_yData[it.key()]; QVector<QPoint> points;
QVector<QPointF> points;
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) {
points.push_back(QPointF(rect.right() - j * x_tick_size,
rect.bottom() - queue.at(i) * y_multiplier)); int new_x = rect.right() - j * xTickSize;
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<GraphID>(id)].pen);
painter.drawPolyline(points.data(), points.size()); painter.drawPolyline(points.data(), points.size());
} }
// draw legend // 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; double legendHeight = 0;
int legend_width = 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 (font_metrics.width(it.value().m_name) > legend_width) if (fontMetrics.width(property.name) > legendWidth)
legend_width = font_metrics.width(it.value().m_name); legendWidth = fontMetrics.width(property.name);
legend_height += 1.5 * font_metrics.height(); legendHeight += 1.5 * fontMetrics.height();
} }
QRectF legend_background_rect(legend_top_left, QSizeF(legend_width, legend_height)); QRectF legendBackgroundRect(QPoint(legendTopLeft.x() - 4, legendTopLeft.y() - 4), QSizeF(legendWidth + 8, legendHeight + 8));
QColor legendBackgroundColor = QWidget::palette().color(QWidget::backgroundRole()); QColor legendBackgroundColor = QWidget::palette().color(QWidget::backgroundRole());
legendBackgroundColor.setAlpha(128); // 50% transparent legendBackgroundColor.setAlpha(128); // 50% transparent
painter.fillRect(legend_background_rect, 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 name_size = font_metrics.width(it.value().m_name); int nameSize = fontMetrics.width(property.name);
double indent = 1.5 * i * font_metrics.height(); double indent = 1.5 * (i++) * fontMetrics.height();
painter.setPen(it.value().m_pen); painter.setPen(property.pen);
painter.drawLine(legend_top_left + QPointF(0, indent + font_metrics.height()), painter.drawLine(legendTopLeft + QPointF(0, indent + fontMetrics.height()),
legend_top_left + QPointF(name_size, indent + font_metrics.height())); legendTopLeft + QPointF(nameSize, indent + fontMetrics.height()));
painter.drawText(QRectF(legend_top_left + QPointF(0, indent), QSizeF(2 * name_size, font_metrics.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)
{ {
} }

46
src/gui/properties/speedplotview.h

@ -29,9 +29,12 @@
#ifndef SPEEDPLOTVIEW_H #ifndef SPEEDPLOTVIEW_H
#define SPEEDPLOTVIEW_H #define SPEEDPLOTVIEW_H
#ifndef Q_MOC_RUN
#include <boost/circular_buffer.hpp>
#endif
#include <QGraphicsView> #include <QGraphicsView>
#include <QMap> #include <QMap>
#include <QQueue>
class QPen; class QPen;
class SpeedPlotView : public QGraphicsView class SpeedPlotView : public QGraphicsView
@ -61,15 +64,19 @@ public:
HOUR6 HOUR6
}; };
struct PointData
{
uint x;
int y[NB_GRAPHS];
};
explicit SpeedPlotView(QWidget *parent = 0); explicit SpeedPlotView(QWidget *parent = 0);
void setGraphEnable(GraphID id, bool enable); void setGraphEnable(GraphID id, bool enable);
void pushXPoint(double x);
void pushYPoint(GraphID id, double y);
void setViewableLastPoints(TimePeriod period); void setViewableLastPoints(TimePeriod period);
void pushPoint(PointData point);
void replot(); void replot();
protected: protected:
@ -84,24 +91,37 @@ private:
HOUR6_SEC = 6 * 60 * 60 HOUR6_SEC = 6 * 60 * 60
}; };
enum PointsToSave
{
MIN5_BUF_SIZE = 5 * 60,
MIN30_BUF_SIZE = 10 * 60,
HOUR6_BUF_SIZE = 20 * 60
};
struct GraphProperties struct GraphProperties
{ {
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;
}; };
QQueue<double> m_xData; boost::circular_buffer<PointData> m_data5Min;
QMap<GraphID, QQueue<double> > m_yData; boost::circular_buffer<PointData> m_data30Min;
boost::circular_buffer<PointData> m_data6Hour;
QMap<GraphID, GraphProperties> m_properties; QMap<GraphID, GraphProperties> m_properties;
PeriodInSeconds m_viewablePointsCount; TimePeriod m_period;
PeriodInSeconds m_maxCapacity; int m_viewablePointsCount;
int m_counter30Min;
int m_counter6Hour;
int maxYValue();
double maxYValue(); boost::circular_buffer<PointData> &getCurrentData();
}; };
#endif // SPEEDPLOTVIEW_H #endif // SPEEDPLOTVIEW_H

28
src/gui/properties/speedwidget.cpp

@ -138,17 +138,20 @@ void SpeedWidget::update()
BitTorrent::SessionStatus btStatus = BitTorrent::Session::instance()->status(); BitTorrent::SessionStatus btStatus = BitTorrent::Session::instance()->status();
m_plot->pushXPoint(QDateTime::currentDateTime().toTime_t()); SpeedPlotView::PointData point;
m_plot->pushYPoint(SpeedPlotView::UP, btStatus.uploadRate()); point.x = QDateTime::currentDateTime().toTime_t();
m_plot->pushYPoint(SpeedPlotView::DOWN, btStatus.downloadRate()); point.y[SpeedPlotView::UP] = btStatus.uploadRate();
m_plot->pushYPoint(SpeedPlotView::PAYLOAD_UP, btStatus.payloadUploadRate()); point.y[SpeedPlotView::DOWN] = btStatus.downloadRate();
m_plot->pushYPoint(SpeedPlotView::PAYLOAD_DOWN, btStatus.payloadDownloadRate()); point.y[SpeedPlotView::PAYLOAD_UP] = btStatus.payloadUploadRate();
m_plot->pushYPoint(SpeedPlotView::OVERHEAD_UP, btStatus.ipOverheadUploadRate()); point.y[SpeedPlotView::PAYLOAD_DOWN] = btStatus.payloadDownloadRate();
m_plot->pushYPoint(SpeedPlotView::OVERHEAD_DOWN, btStatus.ipOverheadDownloadRate()); point.y[SpeedPlotView::OVERHEAD_UP] = btStatus.ipOverheadUploadRate();
m_plot->pushYPoint(SpeedPlotView::DHT_UP, btStatus.dhtUploadRate()); point.y[SpeedPlotView::OVERHEAD_DOWN] = btStatus.ipOverheadDownloadRate();
m_plot->pushYPoint(SpeedPlotView::DHT_DOWN, btStatus.dhtDownloadRate()); point.y[SpeedPlotView::DHT_UP] = btStatus.dhtUploadRate();
m_plot->pushYPoint(SpeedPlotView::TRACKER_UP, btStatus.trackerUploadRate()); point.y[SpeedPlotView::DHT_DOWN] = btStatus.dhtDownloadRate();
m_plot->pushYPoint(SpeedPlotView::TRACKER_DOWN, btStatus.trackerDownloadRate()); point.y[SpeedPlotView::TRACKER_UP] = btStatus.trackerUploadRate();
point.y[SpeedPlotView::TRACKER_DOWN] = btStatus.trackerDownloadRate();
m_plot->pushPoint(point);
QMetaObject::invokeMethod(this, "graphUpdate", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "graphUpdate", Qt::QueuedConnection);
Utils::Misc::msleep(1000); Utils::Misc::msleep(1000);
@ -163,15 +166,12 @@ void SpeedWidget::graphUpdate()
void SpeedWidget::onPeriodChange(int period) void SpeedWidget::onPeriodChange(int period)
{ {
m_plot->setViewableLastPoints(static_cast<SpeedPlotView::TimePeriod>(period)); m_plot->setViewableLastPoints(static_cast<SpeedPlotView::TimePeriod>(period));
graphUpdate();
} }
void SpeedWidget::onGraphChange(int id) void SpeedWidget::onGraphChange(int id)
{ {
QAction *action = m_graphsMenuActions.at(id); QAction *action = m_graphsMenuActions.at(id);
m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), action->isChecked()); m_plot->setGraphEnable(static_cast<SpeedPlotView::GraphID>(id), action->isChecked());
graphUpdate();
} }
void SpeedWidget::loadSettings() void SpeedWidget::loadSettings()

Loading…
Cancel
Save