aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2021-02-23 12:01:32 +0100
committerMitch Curtis <mitch.curtis@qt.io>2021-03-13 20:03:49 +0100
commitd61ececdb8a53c88885e11f5f6f993fcb98f3dab (patch)
treed8f73b06554ddf42ad0b4b9377a520c3b80e0203 /src/qml
parentacf3a16800d4aa451ef8540d58de710c99d43d2c (diff)
Expose formattedDataSize() in QML Locale type
This was added to qtbase in 9d23aeb. Qt Quick Dialogs needs it to display file sizes in FileDialog. [ChangeLog][QML][Locale] Added formattedDataSize() for formatting quantities of bytes as kB, MB, GB etc. Fixes: QTBUG-91283 Change-Id: I8ea64f961c04d4900d18fa45398670df89882c56 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r--src/qml/qml/qqmllocale.cpp50
-rw-r--r--src/qml/qml/qqmllocale_p.h11
2 files changed, 61 insertions, 0 deletions
diff --git a/src/qml/qml/qqmllocale.cpp b/src/qml/qml/qqmllocale.cpp
index 8046a76b92..0d3b5d9883 100644
--- a/src/qml/qml/qqmllocale.cpp
+++ b/src/qml/qml/qqmllocale.cpp
@@ -495,6 +495,41 @@ ReturnedValue QQmlLocaleData::method_set_numberOptions(const QV4::FunctionObject
return Encode::undefined();
}
+ReturnedValue QQmlLocaleData::method_get_formattedDataSize(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
+{
+ QV4::Scope scope(b);
+ const QLocale *locale = getThisLocale(scope, thisObject);
+ if (!locale)
+ return Encode::undefined();
+
+ if (argc < 1 || argc > 3) {
+ THROW_ERROR(QString::fromLatin1(
+ "Locale: formattedDataSize(): Expected 1-3 arguments, but received %1").arg(argc).toLatin1());
+ }
+
+ const qint64 bytes = static_cast<qint64>(argv[0].toInteger());
+ if (argc == 1)
+ RETURN_RESULT(scope.engine->newString(locale->formattedDataSize(bytes)));
+
+ int precision = 0;
+ if (argc >= 2) {
+ if (!argv[1].isInteger())
+ THROW_ERROR("Locale: formattedDataSize(): Invalid argument ('precision' must be an int)");
+
+ precision = argv[1].toInt32();
+ if (argc == 2)
+ RETURN_RESULT(scope.engine->newString(locale->formattedDataSize(bytes, precision)));
+ }
+
+ // argc >= 3
+ if (!argv[2].isNumber())
+ THROW_ERROR("Locale: formattedDataSize(): Invalid argument ('format' must be DataSizeFormat)");
+
+ const quint32 intFormat = argv[2].toUInt32();
+ const auto format = QLocale::DataSizeFormats(intFormat);
+ RETURN_RESULT(scope.engine->newString(locale->formattedDataSize(bytes, precision, format)));
+}
+
ReturnedValue QQmlLocaleData::method_get_measurementSystem(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int)
{
QV4::Scope scope(b);
@@ -701,6 +736,7 @@ QV4LocaleDataDeletable::QV4LocaleDataDeletable(QV4::ExecutionEngine *engine)
o->defineDefaultProperty(QStringLiteral("monthName"), QQmlLocaleData::method_monthName, 0);
o->defineDefaultProperty(QStringLiteral("currencySymbol"), QQmlLocaleData::method_currencySymbol, 0);
o->defineDefaultProperty(QStringLiteral("dateTimeFormat"), QQmlLocaleData::method_dateTimeFormat, 0);
+ o->defineDefaultProperty(QStringLiteral("formattedDataSize"), QQmlLocaleData::method_get_formattedDataSize, 0);
o->defineAccessorProperty(QStringLiteral("name"), QQmlLocaleData::method_get_name, nullptr);
o->defineAccessorProperty(QStringLiteral("positiveSign"), QQmlLocaleData::method_get_positiveSign, nullptr);
o->defineAccessorProperty(QStringLiteral("uiLanguages"), QQmlLocaleData::method_get_uiLanguages, nullptr);
@@ -951,6 +987,20 @@ ReturnedValue QQmlLocale::method_localeCompare(const QV4::FunctionObject *b, con
*/
/*!
+ \qmlmethod string QtQml::Locale::formattedDataSize(int bytes, int precision, DataSizeFormat format)
+ \since 6.2
+
+ Converts a size in \a bytes to a human-readable localized string, comprising a
+ number and a quantified unit.
+
+ The \a precision and \a format arguments are optional.
+
+ For more information, see \l QLocale::formattedDataSize().
+
+ \sa QLocale::DataSizeFormats
+*/
+
+/*!
\qmlmethod string QtQml::Locale::monthName(month, type)
Returns the localized name of \a month (0-11), in the optional
diff --git a/src/qml/qml/qqmllocale_p.h b/src/qml/qml/qqmllocale_p.h
index 73eae259fb..475bc639cf 100644
--- a/src/qml/qml/qqmllocale_p.h
+++ b/src/qml/qml/qqmllocale_p.h
@@ -139,6 +139,15 @@ namespace QQmlLocale
};
Q_ENUM_NS(NumberOptions)
+ enum DataSizeFormat {
+ DataSizeBase1000 = QLocale::DataSizeBase1000,
+ DataSizeSIQuantifiers = QLocale::DataSizeSIQuantifiers,
+ DataSizeIecFormat = QLocale::DataSizeIecFormat,
+ DataSizeTraditionalFormat = QLocale::DataSizeTraditionalFormat,
+ DataSizeSIFormat = QLocale::DataSizeSIFormat
+ };
+ Q_ENUM_NS(DataSizeFormat)
+
Q_QML_PRIVATE_EXPORT QV4::ReturnedValue locale(QV4::ExecutionEngine *engine, const QString &localeName);
Q_QML_PRIVATE_EXPORT QV4::ReturnedValue wrap(QV4::ExecutionEngine *engine, const QLocale &locale);
Q_QML_PRIVATE_EXPORT void registerStringLocaleCompare(QV4::ExecutionEngine *engine);
@@ -205,6 +214,8 @@ struct QQmlLocaleData : public QV4::Object
static QV4::ReturnedValue method_get_numberOptions(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc);
static QV4::ReturnedValue method_set_numberOptions(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc);
+
+ static QV4::ReturnedValue method_get_formattedDataSize(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc);
};
}