Skip to content

Commit c25d3f4

Browse files
shaavanRandyMcMillan
authored andcommitted
qt:refactor: Use std::chrono in TrafficGraphWidget class
1 parent fba8ef0 commit c25d3f4

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/qt/rpcconsole.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#include <QTimer>
5555
#include <QVariant>
5656

57+
#include <chrono>
58+
5759
const int CONSOLE_HISTORY = 50;
5860
const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
5961
const QSize FONT_RANGE(4, 40);
@@ -1140,7 +1142,7 @@ void RPCConsole::on_sldGraphRange_valueChanged(int value)
11401142

11411143
void RPCConsole::setTrafficGraphRange(int mins)
11421144
{
1143-
ui->trafficGraph->setGraphRangeMins(mins);
1145+
ui->trafficGraph->setGraphRange(std::chrono::minutes{mins});
11441146
ui->lblGraphRange->setText(GUIUtil::formatDurationStr(std::chrono::minutes{mins}));
11451147
}
11461148

src/qt/trafficgraphwidget.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <QColor>
1212
#include <QTimer>
1313

14+
#include <chrono>
1415
#include <cmath>
1516
#include <cfloat>
1617

@@ -145,22 +146,25 @@ void TrafficGraphWidget::updateRates()
145146
}
146147

147148
float tmax = 0.0f;
148-
for (const float f : vSamplesIn.mid(0,(DESIRED_SAMPLES*(getGraphRangeMins()/5)))) {
149+
for (const float f : vSamplesIn.mid(0,(DESIRED_SAMPLES*getGraphRangeMins()/5))) {
149150
if(f > tmax) tmax = f;
150151
}
151-
for (const float f : vSamplesOut.mid(0,(DESIRED_SAMPLES*(getGraphRangeMins()/5)))) {
152+
for (const float f : vSamplesOut.mid(0,(DESIRED_SAMPLES*getGraphRangeMins()/5))) {
152153
if(f > tmax) tmax = f;
153154
}
154155
fMax = tmax;
155156
update();
156157
}
157158

158-
void TrafficGraphWidget::setGraphRangeMins(int mins)
159+
void TrafficGraphWidget::setGraphRange(std::chrono::minutes new_range)
159160
{
160-
nMins = mins;
161-
int msecsPerSample = nMins * 60 * 1000 / (DESIRED_SAMPLES * (getGraphRangeMins() / 5));
161+
using namespace std::chrono_literals;
162+
const auto range{new_range};
163+
nMins = new_range.count();
164+
auto msecs_per_sample = nMins * 60 * 1000 / (DESIRED_SAMPLES * (range.count() / 5));
162165
timer->stop();
163-
timer->setInterval(msecsPerSample);
166+
timer->setInterval(msecs_per_sample);
167+
164168
update();
165169
timer->start();
166170
}

src/qt/trafficgraphwidget.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <QWidget>
99
#include <QQueue>
1010

11+
#include <chrono>
12+
1113
class ClientModel;
1214

1315
QT_BEGIN_NAMESPACE
@@ -29,15 +31,15 @@ class TrafficGraphWidget : public QWidget
2931

3032
public Q_SLOTS:
3133
void updateRates();
32-
void setGraphRangeMins(int mins);
34+
void setGraphRange(std::chrono::minutes new_range);
3335
void clear();
3436

3537
private:
3638
void paintPath(QPainterPath &path, QQueue<float> &samples);
3739

3840
QTimer *timer;
3941
float fMax;
40-
int nMins;
42+
long nMins;
4143
QQueue<float> vSamplesIn;
4244
QQueue<float> vSamplesOut;
4345
quint64 nLastBytesIn;

0 commit comments

Comments
 (0)