2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2012-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
#include <array>
|
2015-06-12 22:41:36 +00:00
|
|
|
#include <cstdint> // int64_t
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
#include <QCoreApplication> // Q_DECLARE_TR_FUNCTIONS
|
2010-07-03 00:25:22 +00:00
|
|
|
#include <QString>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2022-09-09 01:17:29 +00:00
|
|
|
|
|
|
|
#include "Speed.h"
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2015-06-12 22:12:12 +00:00
|
|
|
class Formatter
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_DECLARE_TR_FUNCTIONS(Formatter)
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
2015-06-12 22:12:12 +00:00
|
|
|
enum Size
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
B,
|
|
|
|
KB,
|
|
|
|
MB,
|
|
|
|
GB,
|
2020-06-05 19:02:11 +00:00
|
|
|
TB,
|
|
|
|
|
|
|
|
NUM_SIZES
|
2015-06-12 22:12:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Type
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
SPEED,
|
|
|
|
SIZE,
|
2020-06-05 19:02:11 +00:00
|
|
|
MEM,
|
|
|
|
|
|
|
|
NUM_TYPES
|
2015-06-12 22:12:12 +00:00
|
|
|
};
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
static constexpr int SpeedBase = 1000;
|
|
|
|
static constexpr int SizeBase = 1000;
|
|
|
|
static constexpr int MemBase = 1024;
|
|
|
|
|
2022-09-09 01:17:29 +00:00
|
|
|
[[nodiscard]] static Formatter& get();
|
|
|
|
|
|
|
|
[[nodiscard]] QString memToString(int64_t bytes) const;
|
|
|
|
[[nodiscard]] QString sizeToString(int64_t bytes) const;
|
|
|
|
[[nodiscard]] QString sizeToString(uint64_t bytes) const;
|
|
|
|
[[nodiscard]] QString timeToString(int seconds) const;
|
|
|
|
[[nodiscard]] QString unitStr(Type t, Size s) const;
|
|
|
|
|
|
|
|
[[nodiscard]] auto speedToString(Speed const& speed) const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(tr_formatter_speed_KBps(speed.getKBps()));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto uploadSpeedToString(Speed const& upload_speed) const
|
|
|
|
{
|
|
|
|
static auto constexpr UploadSymbol = QChar{ 0x25B4 };
|
|
|
|
|
|
|
|
return tr("%1 %2").arg(speedToString(upload_speed)).arg(UploadSymbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto downloadSpeedToString(Speed const& download_speed) const
|
|
|
|
{
|
|
|
|
static auto constexpr DownloadSymbol = QChar{ 0x25BE };
|
|
|
|
|
|
|
|
return tr("%1 %2").arg(speedToString(download_speed)).arg(DownloadSymbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto percentToString(double x) const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(tr_strpercent(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto ratioToString(double ratio) const
|
|
|
|
{
|
|
|
|
static auto constexpr InfinitySymbol = "\xE2\x88\x9E";
|
|
|
|
|
|
|
|
return QString::fromStdString(tr_strratio(ratio, InfinitySymbol));
|
|
|
|
}
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
protected:
|
|
|
|
Formatter();
|
2010-07-07 00:30:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
private:
|
2020-08-15 15:42:51 +00:00
|
|
|
std::array<std::array<QString, Formatter::NUM_SIZES>, Formatter::NUM_TYPES> const UnitStrings;
|
2010-07-03 00:25:22 +00:00
|
|
|
};
|