Skip to content

Commit 9c4583e

Browse files
committed
qt: Add formatBytesps function for bytes per second display
Add a new GUI utility function to format bytes per second values with appropriate units (B/s, kB/s, MB/s, GB/s) and precision, ensuring consistent and readable display of network traffic rates in the traffic graph widget.
1 parent 874da96 commit 9c4583e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/qt/guiutil.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,40 @@ QString formatBytes(uint64_t bytes)
833833
return QObject::tr("%1 GB").arg(bytes / 1'000'000'000);
834834
}
835835

836+
QString formatBytesps(float val)
837+
{
838+
if (val < 10)
839+
//: "Bytes per second"
840+
return QObject::tr("%1 B/s").arg(0.01 * int(val * 100 + 0.5));
841+
if (val < 100)
842+
//: "Bytes per second"
843+
return QObject::tr("%1 B/s").arg(0.1 * int(val * 10 + 0.5));
844+
if (val < 1'000)
845+
//: "Bytes per second"
846+
return QObject::tr("%1 B/s").arg(int(val + 0.5));
847+
if (val < 10'000)
848+
//: "Kilobytes per second"
849+
return QObject::tr("%1 kB/s").arg(0.01 * int(val / 10 + 0.5));
850+
if (val < 100'000)
851+
//: "Kilobytes per second"
852+
return QObject::tr("%1 kB/s").arg(0.1 * int(val / 100 + 0.5));
853+
if (val < 1'000'000)
854+
//: "Kilobytes per second"
855+
return QObject::tr("%1 kB/s").arg(int(val / 1'000 + 0.5));
856+
if (val < 10'000'000)
857+
//: "Megabytes per second"
858+
return QObject::tr("%1 MB/s").arg(0.01 * int(val / 10'000 + 0.5));
859+
if (val < 100'000'000)
860+
//: "Megabytes per second"
861+
return QObject::tr("%1 MB/s").arg(0.1 * int(val / 100'000 + 0.5));
862+
if (val < 10'000'000'000)
863+
//: "Megabytes per second"
864+
return QObject::tr("%1 MB/s").arg(long(val / 1'000'000 + 0.5));
865+
866+
//: "Gigabytes per second"
867+
return QObject::tr("%1 GB/s").arg(long(val / 1'000'000'000 + 0.5));
868+
}
869+
836870
qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize, qreal font_size) {
837871
while(font_size >= minPointSize) {
838872
font.setPointSizeF(font_size);

0 commit comments

Comments
 (0)