From f7a19ef774ef92ce348215593e3590a750c345e1 Mon Sep 17 00:00:00 2001 From: Shashwat Date: Wed, 12 Jan 2022 19:06:28 +0530 Subject: [PATCH] qt,refactor: Use std::chrono in TrafficGraphWidget class --- src/qt/rpcconsole.cpp | 4 +++- src/qt/trafficgraphwidget.cpp | 15 ++++++--------- src/qt/trafficgraphwidget.h | 8 +++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 1cadfaeeb9..38bb78cb4a 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -54,6 +54,8 @@ #include #include +#include + const int CONSOLE_HISTORY = 50; const int INITIAL_TRAFFIC_GRAPH_MINS = 30; const QSize FONT_RANGE(4, 40); @@ -1140,7 +1142,7 @@ void RPCConsole::on_sldGraphRange_valueChanged(int value) void RPCConsole::setTrafficGraphRange(int mins) { - ui->trafficGraph->setGraphRangeMins(mins); + ui->trafficGraph->setGraphRange(std::chrono::minutes{mins}); ui->lblGraphRange->setText(GUIUtil::formatDurationStr(std::chrono::minutes{mins})); } diff --git a/src/qt/trafficgraphwidget.cpp b/src/qt/trafficgraphwidget.cpp index ac103e9dc3..aebd44d5f7 100644 --- a/src/qt/trafficgraphwidget.cpp +++ b/src/qt/trafficgraphwidget.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #define DESIRED_SAMPLES 800 @@ -22,7 +23,6 @@ TrafficGraphWidget::TrafficGraphWidget(QWidget *parent) : QWidget(parent), timer(nullptr), fMax(0.0f), - nMins(0), vSamplesIn(), vSamplesOut(), nLastBytesIn(0), @@ -42,10 +42,7 @@ void TrafficGraphWidget::setClientModel(ClientModel *model) } } -int TrafficGraphWidget::getGraphRangeMins() const -{ - return nMins; -} +std::chrono::minutes TrafficGraphWidget::getGraphRange() const { return m_range; } void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue &samples) { @@ -153,12 +150,12 @@ void TrafficGraphWidget::updateRates() update(); } -void TrafficGraphWidget::setGraphRangeMins(int mins) +void TrafficGraphWidget::setGraphRange(std::chrono::minutes new_range) { - nMins = mins; - int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES; + m_range = new_range; + const auto msecs_per_sample{std::chrono::duration_cast(m_range) / DESIRED_SAMPLES}; timer->stop(); - timer->setInterval(msecsPerSample); + timer->setInterval(msecs_per_sample); clear(); } diff --git a/src/qt/trafficgraphwidget.h b/src/qt/trafficgraphwidget.h index 2d8c825815..a40b734540 100644 --- a/src/qt/trafficgraphwidget.h +++ b/src/qt/trafficgraphwidget.h @@ -8,6 +8,8 @@ #include #include +#include + class ClientModel; QT_BEGIN_NAMESPACE @@ -22,14 +24,14 @@ class TrafficGraphWidget : public QWidget public: explicit TrafficGraphWidget(QWidget *parent = nullptr); void setClientModel(ClientModel *model); - int getGraphRangeMins() const; + std::chrono::minutes getGraphRange() const; protected: void paintEvent(QPaintEvent *) override; public Q_SLOTS: void updateRates(); - void setGraphRangeMins(int mins); + void setGraphRange(std::chrono::minutes new_range); void clear(); private: @@ -37,7 +39,7 @@ private: QTimer *timer; float fMax; - int nMins; + std::chrono::minutes m_range{0}; QQueue vSamplesIn; QQueue vSamplesOut; quint64 nLastBytesIn;