diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-10-24 21:04:43 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-10-25 13:03:27 +0000 |
commit | 459842c6d880c1a754829b258d4ca4d1a136cdb9 (patch) | |
tree | e4d218ea1df436a5a7791efbb28d9c8afd1a171e /src/quicktemplates2/qquickspinbox.cpp | |
parent | 90659c9bed2464d87602c3d25eed2db2c4004b9a (diff) |
Add QQuickSpinBox::displayText
Allows styles to create a simple binding to display the textual value
instead of calling the textFromValue() JS-function. Furthermore, this
allows us to do the text<->value conversion in C++ using QLocale by
default, unless custom textFromValue and/or valueFromText JS-functions
are provided.
Before:
running: qmlbench/benchmarks/auto/creation/quick.controls2/delegates_spinbox.qml
100 frames
100 frames
99 frames
Average: 99.6667 frames; using samples; MedianAll=100; StdDev=0.57735, CoV=0.00579281
After:
running: qmlbench/benchmarks/auto/creation/quick.controls2/delegates_spinbox.qml
152 frames
150 frames
151 frames
Average: 151 frames; using samples; MedianAll=151; StdDev=1, CoV=0.00662252
Change-Id: I66a5ebaf685d2c30613b58099724e6e7bbe00504
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickspinbox.cpp')
-rw-r--r-- | src/quicktemplates2/qquickspinbox.cpp | 68 |
1 files changed, 65 insertions, 3 deletions
diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp index 714a7ec5..4f57c305 100644 --- a/src/quicktemplates2/qquickspinbox.cpp +++ b/src/quicktemplates2/qquickspinbox.cpp @@ -131,6 +131,9 @@ public: int effectiveStepSize() const; + void updateDisplayText(); + void setDisplayText(const QString &displayText); + bool upEnabled() const; void updateUpEnabled(); bool downEnabled() const; @@ -154,6 +157,7 @@ public: int stepSize; int delayTimer; int repeatTimer; + QString displayText; QQuickSpinButton *up; QQuickSpinButton *down; QValidator *validator; @@ -184,13 +188,16 @@ void QQuickSpinBoxPrivate::updateValue() if (contentItem) { QVariant text = contentItem->property("text"); if (text.isValid()) { + int val = 0; QQmlEngine *engine = qmlEngine(q); - if (engine) { + if (engine && valueFromText.isCallable()) { QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); QJSValue loc(v4, QQmlLocale::wrap(v4, locale)); - QJSValue val = q->valueFromText().call(QJSValueList() << text.toString() << loc); - setValue(val.toInt(), /* modified = */ true, /* allowWrap = */ false); + val = valueFromText.call(QJSValueList() << text.toString() << loc).toInt(); + } else { + val = locale.toInt(text.toString()); } + setValue(val, /* modified = */ true, /* allowWrap = */ false); } } } @@ -206,6 +213,7 @@ bool QQuickSpinBoxPrivate::setValue(int newValue, bool allowWrap, bool modified) value = newValue; + updateDisplayText(); updateUpEnabled(); updateDownEnabled(); @@ -235,6 +243,31 @@ int QQuickSpinBoxPrivate::effectiveStepSize() const return from > to ? -1 * stepSize : stepSize; } +void QQuickSpinBoxPrivate::updateDisplayText() +{ + Q_Q(QQuickSpinBox); + QString text; + QQmlEngine *engine = qmlEngine(q); + if (engine && textFromValue.isCallable()) { + QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); + QJSValue loc(v4, QQmlLocale::wrap(v4, locale)); + text = textFromValue.call(QJSValueList() << value << loc).toString(); + } else { + text = locale.toString(value); + } + setDisplayText(text); +} + +void QQuickSpinBoxPrivate::setDisplayText(const QString &text) +{ + Q_Q(QQuickSpinBox); + if (displayText == text) + return; + + displayText = text; + emit q->displayTextChanged(); +} + bool QQuickSpinBoxPrivate::upEnabled() const { const QQuickItem *upIndicator = up->indicator(); @@ -753,6 +786,27 @@ void QQuickSpinBox::setWrap(bool wrap) } /*! + \since QtQuick.Controls 2.4 (Qt 5.11) + \qmlproperty string QtQuick.Controls::SpinBox::displayText + \readonly + + This property holds the textual value of the spinbox. + + The value of the property is based on \l textFromValue() and \l {Control::} + {locale}, and equal to: + \badcode + var text = spinBox.textFromValue(spinBox.value, spinBox.locale) + \endcode + + \sa textFromValue +*/ +QString QQuickSpinBox::displayText() const +{ + Q_D(const QQuickSpinBox); + return d->displayText; +} + +/*! \qmlmethod void QtQuick.Controls::SpinBox::increase() Increases the value by \l stepSize, or \c 1 if stepSize is not defined. @@ -885,6 +939,7 @@ void QQuickSpinBox::componentComplete() Q_D(QQuickSpinBox); QQuickControl::componentComplete(); if (!d->setValue(d->value, /* modified = */ false, /* allowWrap = */ false)) { + d->updateDisplayText(); d->updateUpEnabled(); d->updateDownEnabled(); } @@ -916,6 +971,13 @@ void QQuickSpinBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) } } +void QQuickSpinBox::localeChange(const QLocale &newLocale, const QLocale &oldLocale) +{ + Q_D(QQuickSpinBox); + QQuickControl::localeChange(newLocale, oldLocale); + d->updateDisplayText(); +} + QFont QQuickSpinBox::defaultFont() const { return QQuickControlPrivate::themeFont(QPlatformTheme::EditorFont); |