From 819d65a0a83cf89f089e7baf4017330289ac2db0 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 19 Aug 2024 14:42:56 +0500 Subject: [PATCH] UI: Format traffic bytes and speed per locale --- src/ui/form/opt/pages/statisticspage.cpp | 2 +- src/ui/model/traflistmodel.cpp | 2 +- src/ui/util/dateutil.cpp | 2 -- src/ui/util/dateutil.h | 12 ++++-------- src/ui/util/net/netutil.cpp | 19 +++---------------- src/ui/util/net/netutil.h | 1 - 6 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/ui/form/opt/pages/statisticspage.cpp b/src/ui/form/opt/pages/statisticspage.cpp index 9115abf6..fd596ac0 100644 --- a/src/ui/form/opt/pages/statisticspage.cpp +++ b/src/ui/form/opt/pages/statisticspage.cpp @@ -33,7 +33,7 @@ const std::array quotaValues = { 10, 0, 100, 500, 1024, 8 * 1024, 10 * 1024, 30 QString formatQuota(int mbytes) { - return NetUtil::formatDataSize1(qint64(mbytes) * 1024 * 1024); + return NetUtil::formatDataSize(qint64(mbytes) * 1024 * 1024, /*precision=*/0); } } diff --git a/src/ui/model/traflistmodel.cpp b/src/ui/model/traflistmodel.cpp index 3bf68ef0..08af52b3 100644 --- a/src/ui/model/traflistmodel.cpp +++ b/src/ui/model/traflistmodel.cpp @@ -212,7 +212,7 @@ QString TrafListModel::formatTrafUnit(qint64 bytes) const const qint64 unitMult = unitMults.at(unit()); - return QLocale::c().toString(qreal(bytes) / unitMult, 'f', trafPrec); + return QLocale().toString(qreal(bytes) / unitMult, 'f', trafPrec); } QString TrafListModel::formatTrafTime(qint32 trafTime) const diff --git a/src/ui/util/dateutil.cpp b/src/ui/util/dateutil.cpp index b4b3f1a9..2c5cd538 100644 --- a/src/ui/util/dateutil.cpp +++ b/src/ui/util/dateutil.cpp @@ -3,8 +3,6 @@ #include #include -DateUtil::DateUtil(QObject *parent) : QObject(parent) { } - QDateTime DateUtil::now() { return QDateTime::currentDateTime(); diff --git a/src/ui/util/dateutil.h b/src/ui/util/dateutil.h index 8cd59886..139ab175 100644 --- a/src/ui/util/dateutil.h +++ b/src/ui/util/dateutil.h @@ -6,13 +6,9 @@ #include #include -class DateUtil : public QObject +class DateUtil { - Q_OBJECT - public: - explicit DateUtil(QObject *parent = nullptr); - static QDateTime now(); static QDateTime startOfDayUTC(const QDate &date); @@ -35,13 +31,13 @@ public: static QString formatPeriod(const QString &from, const QString &to); - Q_INVOKABLE static QString formatTime(quint8 hour, quint8 minute); + static QString formatTime(quint8 hour, quint8 minute); static QString reformatTime(const QString &time); static void parseTime(const QString &time, quint8 &hour, quint8 &minute); - Q_INVOKABLE static quint8 parseTimeHour(const QString &period); - Q_INVOKABLE static quint8 parseTimeMinute(const QString &period); + static quint8 parseTimeHour(const QString &period); + static quint8 parseTimeMinute(const QString &period); static QString localeDateTime( const QDateTime &dateTime, QLocale::FormatType format = QLocale::ShortFormat); diff --git a/src/ui/util/net/netutil.cpp b/src/ui/util/net/netutil.cpp index 51aa180c..318f7e6b 100644 --- a/src/ui/util/net/netutil.cpp +++ b/src/ui/util/net/netutil.cpp @@ -128,27 +128,14 @@ const ip6_addr_t &NetUtil::rawArrayToIp6(const QByteArray &buf) QString NetUtil::formatDataSize(qint64 bytes, int precision) { - return QLocale::c().formattedDataSize(bytes, precision, QLocale::DataSizeTraditionalFormat); -} - -QString NetUtil::formatDataSize1(qint64 bytes) -{ - QString text = formatDataSize(bytes, 1); - text.remove(QLatin1String(".0")); - return text; + return QLocale().formattedDataSize(bytes, precision, QLocale::DataSizeTraditionalFormat); } QString NetUtil::formatSpeed(quint32 bitsPerSecond) { - QString text = formatDataSize1(bitsPerSecond); + const QString text = formatDataSize(bitsPerSecond, /*precision=*/0); - if (bitsPerSecond < 1024) { - text.replace("bytes", "b"); - } else { - text.replace('B', 'b'); - } - - return text + QLatin1String("ps"); + return text + QObject::tr("ps"); } QString NetUtil::getHostName(const QString &address) diff --git a/src/ui/util/net/netutil.h b/src/ui/util/net/netutil.h index 31ffed7f..5b2e9198 100644 --- a/src/ui/util/net/netutil.h +++ b/src/ui/util/net/netutil.h @@ -35,7 +35,6 @@ public: static const ip6_addr_t &rawArrayToIp6(const QByteArray &buf); static QString formatDataSize(qint64 bytes, int precision = 2); - static QString formatDataSize1(qint64 bytes); static QString formatSpeed(quint32 bitsPerSecond); static QString getHostName(const QString &address);