Skip to content

Commit

Permalink
Merge bitcoin-core/gui#524: Replace int with std::chrono in for the t…
Browse files Browse the repository at this point in the history
…imer->setInterval() argument

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

Pull request description:

  The PR is a follow-up to ElementsProject#517

  - It addresses the change suggested in [this](bitcoin-core/gui#517 (review)) 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 f7a19ef
  hebasto:
    ACK f7a19ef
  promag:
    Code review ACK f7a19ef.

Tree-SHA512: 5094ba894f3051fc99148cb8f408fc6f9d6571188673dcb7bf24366cdfb3eaf6d4e41083685d578ad2a9fbe31cc491a5f3fa9b7c9ab6eb90e4dc1356f89ae18a
  • Loading branch information
hebasto committed Feb 4, 2022
2 parents 5152002 + f7a19ef commit 5c6b3d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include <QTimer>
#include <QVariant>

#include <chrono>

const int CONSOLE_HISTORY = 50;
const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
const QSize FONT_RANGE(4, 40);
Expand Down Expand Up @@ -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}));
}

Expand Down
15 changes: 6 additions & 9 deletions src/qt/trafficgraphwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QColor>
#include <QTimer>

#include <chrono>
#include <cmath>

#define DESIRED_SAMPLES 800
Expand All @@ -22,7 +23,6 @@ TrafficGraphWidget::TrafficGraphWidget(QWidget *parent) :
QWidget(parent),
timer(nullptr),
fMax(0.0f),
nMins(0),
vSamplesIn(),
vSamplesOut(),
nLastBytesIn(0),
Expand All @@ -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<float> &samples)
{
Expand Down Expand Up @@ -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<std::chrono::milliseconds>(m_range) / DESIRED_SAMPLES};
timer->stop();
timer->setInterval(msecsPerSample);
timer->setInterval(msecs_per_sample);

clear();
}
Expand Down
8 changes: 5 additions & 3 deletions src/qt/trafficgraphwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <QWidget>
#include <QQueue>

#include <chrono>

class ClientModel;

QT_BEGIN_NAMESPACE
Expand All @@ -22,22 +24,22 @@ 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:
void paintPath(QPainterPath &path, QQueue<float> &samples);

QTimer *timer;
float fMax;
int nMins;
std::chrono::minutes m_range{0};
QQueue<float> vSamplesIn;
QQueue<float> vSamplesOut;
quint64 nLastBytesIn;
Expand Down

0 comments on commit 5c6b3d5

Please sign in to comment.