diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-04-10 12:44:20 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-06-20 09:53:14 +0000 |
commit | 9d23aebb271ea534a66cb3aceb2e63d9a1c870d6 (patch) | |
tree | a78a9b2739aabc594e04cb73cae3a81a24c54dd1 /examples/widgets/itemviews | |
parent | ccca8c94359e7f07333b710119bfdf00b25b78fd (diff) |
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 <edward.welbourne@qt.io>
Change-Id: I27bca146c3eba90fa7a5d52ef6626ce85723e3f0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'examples/widgets/itemviews')
-rw-r--r-- | examples/widgets/itemviews/storageview/storagemodel.cpp | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/examples/widgets/itemviews/storageview/storagemodel.cpp b/examples/widgets/itemviews/storageview/storagemodel.cpp index b7c594f8f7..1395c9f208 100644 --- a/examples/widgets/itemviews/storageview/storagemodel.cpp +++ b/examples/widgets/itemviews/storageview/storagemodel.cpp @@ -52,25 +52,10 @@ #include "storagemodel.h" #include <QDir> +#include <QLocale> #include <qmath.h> #include <cmath> -static QString sizeToString(qint64 size) -{ - static const char *const strings[] = { "b", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; - - if (size <= 0) - return StorageModel::tr("0 b"); - - double power = std::log((double)size)/std::log(1024.0); - int intPower = (int)power; - intPower = intPower >= 8 ? 8 - 1 : intPower; - - double normSize = size / std::pow(1024.0, intPower); - //: this should expand to "1.23 GB" - return StorageModel::tr("%1 %2").arg(normSize, 0, 'f', intPower > 0 ? 2 : 0).arg(strings[intPower]); -} - StorageModel::StorageModel(QObject *parent) : QAbstractTableModel(parent), m_volumes(QStorageInfo::mountedVolumes()) @@ -106,11 +91,11 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const case ColumnFileSystemName: return volume.fileSystemType(); case ColumnTotal: - return sizeToString(volume.bytesTotal()); + return QLocale().formattedDataSize(volume.bytesTotal()); case ColumnFree: - return sizeToString(volume.bytesFree()); + return QLocale().formattedDataSize(volume.bytesFree()); case ColumnAvailable: - return sizeToString(volume.bytesAvailable()); + return QLocale().formattedDataSize(volume.bytesAvailable()); case ColumnIsReady: return volume.isReady(); case ColumnIsReadOnly: @@ -121,6 +106,7 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const break; } } else if (role == Qt::ToolTipRole) { + QLocale locale; const QStorageInfo &volume = m_volumes.at(index.row()); return tr("Root path : %1\n" "Name: %2\n" @@ -140,9 +126,9 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const arg(volume.displayName()). arg(QString::fromUtf8(volume.device())). arg(QString::fromUtf8(volume.fileSystemType())). - arg(sizeToString(volume.bytesTotal())). - arg(sizeToString(volume.bytesFree())). - arg(sizeToString(volume.bytesAvailable())). + arg(locale.formattedDataSize(volume.bytesTotal())). + arg(locale.formattedDataSize(volume.bytesFree())). + arg(locale.formattedDataSize(volume.bytesAvailable())). arg(volume.isReady() ? tr("true") : tr("false")). arg(volume.isReadOnly() ? tr("true") : tr("false")). arg(volume.isValid() ? tr("true") : tr("false")). |