Merge bitcoin-core/gui#524: Replace int with std::chrono in for the timer->setInterval() argument

f7a19ef774 qt,refactor: Use std::chrono in TrafficGraphWidget class (Shashwat)

Pull request description:

  The PR is a follow-up to #517

  - It addresses the change suggested in [this](https://github.com/bitcoin-core/gui/pull/517#pullrequestreview-850260826) comment.
  - This PR changes the type of `msecsPerSample` from **int** to **std::chrono::minutes** and makes other relevant subsequent changes that were limited to the **trafficgraphwidget** file.

ACKs for top commit:
  RandyMcMillan:
    tACK f7a19ef774
  hebasto:
    ACK f7a19ef774
  promag:
    Code review ACK f7a19ef774.

Tree-SHA512: 5094ba894f3051fc99148cb8f408fc6f9d6571188673dcb7bf24366cdfb3eaf6d4e41083685d578ad2a9fbe31cc491a5f3fa9b7c9ab6eb90e4dc1356f89ae18a
This commit is contained in:
Hennadii Stepanov 2022-02-04 12:30:14 +02:00
commit 5c6b3d5b35
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
3 changed files with 14 additions and 13 deletions

View file

@ -54,6 +54,8 @@
#include <QTimer> #include <QTimer>
#include <QVariant> #include <QVariant>
#include <chrono>
const int CONSOLE_HISTORY = 50; const int CONSOLE_HISTORY = 50;
const int INITIAL_TRAFFIC_GRAPH_MINS = 30; const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
const QSize FONT_RANGE(4, 40); const QSize FONT_RANGE(4, 40);
@ -1140,7 +1142,7 @@ void RPCConsole::on_sldGraphRange_valueChanged(int value)
void RPCConsole::setTrafficGraphRange(int mins) 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})); ui->lblGraphRange->setText(GUIUtil::formatDurationStr(std::chrono::minutes{mins}));
} }

View file

@ -11,6 +11,7 @@
#include <QColor> #include <QColor>
#include <QTimer> #include <QTimer>
#include <chrono>
#include <cmath> #include <cmath>
#define DESIRED_SAMPLES 800 #define DESIRED_SAMPLES 800
@ -22,7 +23,6 @@ TrafficGraphWidget::TrafficGraphWidget(QWidget *parent) :
QWidget(parent), QWidget(parent),
timer(nullptr), timer(nullptr),
fMax(0.0f), fMax(0.0f),
nMins(0),
vSamplesIn(), vSamplesIn(),
vSamplesOut(), vSamplesOut(),
nLastBytesIn(0), nLastBytesIn(0),
@ -42,10 +42,7 @@ void TrafficGraphWidget::setClientModel(ClientModel *model)
} }
} }
int TrafficGraphWidget::getGraphRangeMins() const std::chrono::minutes TrafficGraphWidget::getGraphRange() const { return m_range; }
{
return nMins;
}
void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples) void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples)
{ {
@ -153,12 +150,12 @@ void TrafficGraphWidget::updateRates()
update(); update();
} }
void TrafficGraphWidget::setGraphRangeMins(int mins) void TrafficGraphWidget::setGraphRange(std::chrono::minutes new_range)
{ {
nMins = mins; m_range = new_range;
int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES; const auto msecs_per_sample{std::chrono::duration_cast<std::chrono::milliseconds>(m_range) / DESIRED_SAMPLES};
timer->stop(); timer->stop();
timer->setInterval(msecsPerSample); timer->setInterval(msecs_per_sample);
clear(); clear();
} }

View file

@ -8,6 +8,8 @@
#include <QWidget> #include <QWidget>
#include <QQueue> #include <QQueue>
#include <chrono>
class ClientModel; class ClientModel;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -22,14 +24,14 @@ class TrafficGraphWidget : public QWidget
public: public:
explicit TrafficGraphWidget(QWidget *parent = nullptr); explicit TrafficGraphWidget(QWidget *parent = nullptr);
void setClientModel(ClientModel *model); void setClientModel(ClientModel *model);
int getGraphRangeMins() const; std::chrono::minutes getGraphRange() const;
protected: protected:
void paintEvent(QPaintEvent *) override; void paintEvent(QPaintEvent *) override;
public Q_SLOTS: public Q_SLOTS:
void updateRates(); void updateRates();
void setGraphRangeMins(int mins); void setGraphRange(std::chrono::minutes new_range);
void clear(); void clear();
private: private:
@ -37,7 +39,7 @@ private:
QTimer *timer; QTimer *timer;
float fMax; float fMax;
int nMins; std::chrono::minutes m_range{0};
QQueue<float> vSamplesIn; QQueue<float> vSamplesIn;
QQueue<float> vSamplesOut; QQueue<float> vSamplesOut;
quint64 nLastBytesIn; quint64 nLastBytesIn;