From 9d23aebb271ea534a66cb3aceb2e63d9a1c870d6 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 10 Apr 2017 12:44:20 +0200 Subject: Add QLocale::formattedDataSize and consolidate use cases It should be easier to translate sizes in bytes to human-readable strings consistently rather than having to repeat this code (and the string translations) in various places. The FileDialog in QtQuick.Controls has a use for this, too. [ChangeLog][QtCore][QLocale] Added QLocale::formattedDataSize() for formatting quantities of bytes as kB, MB, GB etc. Done-with: Edward Welbourne Change-Id: I27bca146c3eba90fa7a5d52ef6626ce85723e3f0 Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira --- src/widgets/dialogs/qfilesystemmodel.cpp | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'src/widgets/dialogs') diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index db1ce3fe0e..45a6b4f8a0 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -774,21 +774,7 @@ QString QFileSystemModelPrivate::size(const QModelIndex &index) const QString QFileSystemModelPrivate::size(qint64 bytes) { - // According to the Si standard KB is 1000 bytes, KiB is 1024 - // but on windows sizes are calculated by dividing by 1024 so we do what they do. - const qint64 kb = 1024; - const qint64 mb = 1024 * kb; - const qint64 gb = 1024 * mb; - const qint64 tb = 1024 * gb; - if (bytes >= tb) - return QFileSystemModel::tr("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', 3)); - if (bytes >= gb) - return QFileSystemModel::tr("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', 2)); - if (bytes >= mb) - return QFileSystemModel::tr("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', 1)); - if (bytes >= kb) - return QFileSystemModel::tr("%1 KB").arg(QLocale().toString(bytes / kb)); - return QFileSystemModel::tr("%1 bytes").arg(QLocale().toString(bytes)); + return QLocale::system().formattedDataSize(bytes); } /*! -- cgit v1.2.3