Skip to content

Commit

Permalink
Replace int with std::chrono in for the timer->setInterval() argument
Browse files Browse the repository at this point in the history
- This commit is a follow-up to 51250b0
(PR#517)
- It addresses the suggestion of changing time instance in
trafficGraphWidget from int to std::chrono.
- This commit addressed it and made some other subsequent changes.
  • Loading branch information
shaavan committed Jan 13, 2022
1 parent 16781e1 commit 9bb48bf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/qt/rpcconsole.cpp
Expand Up @@ -1140,7 +1140,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
14 changes: 8 additions & 6 deletions src/qt/trafficgraphwidget.cpp
Expand Up @@ -11,18 +11,20 @@
#include <QColor>
#include <QTimer>

#include <chrono>
#include <cmath>

#define DESIRED_SAMPLES 800

#define XMARGIN 10
#define YMARGIN 10

using namespace std::chrono_literals;

TrafficGraphWidget::TrafficGraphWidget(QWidget *parent) :
QWidget(parent),
timer(nullptr),
fMax(0.0f),
nMins(0),
vSamplesIn(),
vSamplesOut(),
nLastBytesIn(0),
Expand All @@ -42,9 +44,9 @@ void TrafficGraphWidget::setClientModel(ClientModel *model)
}
}

int TrafficGraphWidget::getGraphRangeMins() const
std::chrono::minutes TrafficGraphWidget::getGraphRange() const
{
return nMins;
return m_range;
}

void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples)
Expand Down Expand Up @@ -153,10 +155,10 @@ void TrafficGraphWidget::updateRates()
update();
}

void TrafficGraphWidget::setGraphRangeMins(int mins)
void TrafficGraphWidget::setGraphRange(std::chrono::minutes mins)
{
nMins = mins;
int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES;
m_range = std::chrono::minutes(mins);
auto msecsPerSample = m_range * 60 * 1000 / DESIRED_SAMPLES;
timer->stop();
timer->setInterval(msecsPerSample);

Expand Down
8 changes: 5 additions & 3 deletions src/qt/trafficgraphwidget.h
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 mins);
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 9bb48bf

Please sign in to comment.