mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-11 07:46:17 -07:00
Display download/upload speed in dock icon
Implementation is based on Transmission sources. Closes #2761 Closes #3671 Closes #7098 Closes #11350 Closes #18527 PR #19595
This commit is contained in:
parent
d60f9c6142
commit
e6ec3d0c2b
9 changed files with 317 additions and 12 deletions
|
@ -94,7 +94,7 @@ namespace
|
|||
Utils::Misc::SizeUnit unit;
|
||||
};
|
||||
|
||||
std::optional<SplitToFriendlyUnitResult> splitToFriendlyUnit(const qint64 bytes)
|
||||
std::optional<SplitToFriendlyUnitResult> splitToFriendlyUnit(const qint64 bytes, const int unitThreshold = 1024)
|
||||
{
|
||||
if (bytes < 0)
|
||||
return std::nullopt;
|
||||
|
@ -102,7 +102,7 @@ namespace
|
|||
int i = 0;
|
||||
auto value = static_cast<qreal>(bytes);
|
||||
|
||||
while ((value >= 1024) && (i < static_cast<int>(Utils::Misc::SizeUnit::ExbiByte)))
|
||||
while ((value >= unitThreshold) && (i < static_cast<int>(Utils::Misc::SizeUnit::ExbiByte)))
|
||||
{
|
||||
value /= 1024;
|
||||
++i;
|
||||
|
@ -270,6 +270,24 @@ QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed, const
|
|||
+ QChar::Nbsp + unitString(result->unit, isSpeed);
|
||||
}
|
||||
|
||||
QString Utils::Misc::friendlyUnitCompact(const qint64 bytes)
|
||||
{
|
||||
// avoid 1000-1023 values, use next larger unit instead
|
||||
const std::optional<SplitToFriendlyUnitResult> result = splitToFriendlyUnit(bytes, 1000);
|
||||
if (!result)
|
||||
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
|
||||
|
||||
int precision = 0; // >= 100
|
||||
if (result->value < 10)
|
||||
precision = 2; // 0 - 9.99
|
||||
if (result->value < 100)
|
||||
precision = 1; // 10 - 99.9
|
||||
|
||||
return Utils::String::fromDouble(result->value, precision)
|
||||
// use only one character for unit representation
|
||||
+ QChar::Nbsp + unitString(result->unit, false)[0];
|
||||
}
|
||||
|
||||
int Utils::Misc::friendlyUnitPrecision(const SizeUnit unit)
|
||||
{
|
||||
// friendlyUnit's number of digits after the decimal point
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue