Skip to content

Commit

Permalink
qt:refactor: Use std::chrono in TrafficGraphWidget class
Browse files Browse the repository at this point in the history
  • Loading branch information
shaavan authored and RandyMcMillan committed Jan 28, 2022
1 parent fba8ef0 commit c25d3f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/qt/rpcconsole.cpp
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
16 changes: 10 additions & 6 deletions src/qt/trafficgraphwidget.cpp
Expand Up @@ -11,6 +11,7 @@
#include <QColor>
#include <QTimer>

#include <chrono>
#include <cmath>
#include <cfloat>

Expand Down Expand Up @@ -145,22 +146,25 @@ void TrafficGraphWidget::updateRates()
}

float tmax = 0.0f;
for (const float f : vSamplesIn.mid(0,(DESIRED_SAMPLES*(getGraphRangeMins()/5)))) {
for (const float f : vSamplesIn.mid(0,(DESIRED_SAMPLES*getGraphRangeMins()/5))) {
if(f > tmax) tmax = f;
}
for (const float f : vSamplesOut.mid(0,(DESIRED_SAMPLES*(getGraphRangeMins()/5)))) {
for (const float f : vSamplesOut.mid(0,(DESIRED_SAMPLES*getGraphRangeMins()/5))) {
if(f > tmax) tmax = f;
}
fMax = tmax;
update();
}

void TrafficGraphWidget::setGraphRangeMins(int mins)
void TrafficGraphWidget::setGraphRange(std::chrono::minutes new_range)
{
nMins = mins;
int msecsPerSample = nMins * 60 * 1000 / (DESIRED_SAMPLES * (getGraphRangeMins() / 5));
using namespace std::chrono_literals;
const auto range{new_range};
nMins = new_range.count();
auto msecs_per_sample = nMins * 60 * 1000 / (DESIRED_SAMPLES * (range.count() / 5));
timer->stop();
timer->setInterval(msecsPerSample);
timer->setInterval(msecs_per_sample);

update();
timer->start();
}
Expand Down
6 changes: 4 additions & 2 deletions src/qt/trafficgraphwidget.h
Expand Up @@ -8,6 +8,8 @@
#include <QWidget>
#include <QQueue>

#include <chrono>

class ClientModel;

QT_BEGIN_NAMESPACE
Expand All @@ -29,15 +31,15 @@ class TrafficGraphWidget : public QWidget

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;
long nMins;
QQueue<float> vSamplesIn;
QQueue<float> vSamplesOut;
quint64 nLastBytesIn;
Expand Down

0 comments on commit c25d3f4

Please sign in to comment.