diff options
Diffstat (limited to 'src/quicktemplates2/qquickspinbox.cpp')
-rw-r--r-- | src/quicktemplates2/qquickspinbox.cpp | 152 |
1 files changed, 133 insertions, 19 deletions
diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp index 24ec19f8..525de945 100644 --- a/src/quicktemplates2/qquickspinbox.cpp +++ b/src/quicktemplates2/qquickspinbox.cpp @@ -109,6 +109,7 @@ class QQuickSpinBoxPrivate : public QQuickControlPrivate public: QQuickSpinBoxPrivate() : editable(false), + wrap(false), from(0), to(99), value(0), @@ -122,14 +123,18 @@ public: { } - int boundValue(int value) const; + int boundValue(int value, bool wrap) const; void updateValue(); - bool setValue(int value, bool modified); + bool setValue(int value, bool wrap, bool modified); + bool stepBy(int steps, bool modified); void increase(bool modified); void decrease(bool modified); int effectiveStepSize() const; + void updateDisplayText(); + void setDisplayText(const QString &displayText); + bool upEnabled() const; void updateUpEnabled(); bool downEnabled() const; @@ -146,12 +151,14 @@ public: void handleUngrab() override; bool editable; + bool wrap; int from; int to; int value; int stepSize; int delayTimer; int repeatTimer; + QString displayText; QQuickSpinButton *up; QQuickSpinButton *down; QValidator *validator; @@ -185,9 +192,20 @@ public: QQuickDeferredPointer<QQuickItem> indicator; }; -int QQuickSpinBoxPrivate::boundValue(int value) const +int QQuickSpinBoxPrivate::boundValue(int value, bool wrap) const { - return from > to ? qBound(to, value, from) : qBound(from, value, to); + bool inverted = from > to; + if (!wrap) + return inverted ? qBound(to, value, from) : qBound(from, value, to); + + int f = inverted ? to : from; + int t = inverted ? from : to; + if (value < f) + value = t; + else if (value > t) + value = f; + + return value; } void QQuickSpinBoxPrivate::updateValue() @@ -196,28 +214,32 @@ 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(), true); + val = valueFromText.call(QJSValueList() << text.toString() << loc).toInt(); + } else { + val = locale.toInt(text.toString()); } + setValue(val, /* allowWrap = */ false, /* modified = */ true); } } } -bool QQuickSpinBoxPrivate::setValue(int newValue, bool modified) +bool QQuickSpinBoxPrivate::setValue(int newValue, bool allowWrap, bool modified) { Q_Q(QQuickSpinBox); if (q->isComponentComplete()) - newValue = boundValue(newValue); + newValue = boundValue(newValue, allowWrap); if (value == newValue) return false; value = newValue; + updateDisplayText(); updateUpEnabled(); updateDownEnabled(); @@ -227,14 +249,19 @@ bool QQuickSpinBoxPrivate::setValue(int newValue, bool modified) return true; } +bool QQuickSpinBoxPrivate::stepBy(int steps, bool modified) +{ + return setValue(value + steps, wrap, modified); +} + void QQuickSpinBoxPrivate::increase(bool modified) { - setValue(value + effectiveStepSize(), modified); + setValue(value + effectiveStepSize(), wrap, modified); } void QQuickSpinBoxPrivate::decrease(bool modified) { - setValue(value - effectiveStepSize(), modified); + setValue(value - effectiveStepSize(), wrap, modified); } int QQuickSpinBoxPrivate::effectiveStepSize() const @@ -242,6 +269,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(); @@ -254,7 +306,7 @@ void QQuickSpinBoxPrivate::updateUpEnabled() if (!upIndicator) return; - upIndicator->setEnabled(from < to ? value < to : value > to); + upIndicator->setEnabled(wrap || (from < to ? value < to : value > to)); } bool QQuickSpinBoxPrivate::downEnabled() const @@ -269,7 +321,7 @@ void QQuickSpinBoxPrivate::updateDownEnabled() if (!downIndicator) return; - downIndicator->setEnabled(from < to ? value > from : value < from); + downIndicator->setEnabled(wrap || (from < to ? value > from : value < from)); } void QQuickSpinBoxPrivate::updateHover(const QPointF &pos) @@ -410,7 +462,7 @@ void QQuickSpinBox::setFrom(int from) d->from = from; emit fromChanged(); if (isComponentComplete()) { - if (!d->setValue(d->value, false)) { + if (!d->setValue(d->value, /* allowWrap = */ false, /* modified = */ false)) { d->updateUpEnabled(); d->updateDownEnabled(); } @@ -439,7 +491,7 @@ void QQuickSpinBox::setTo(int to) d->to = to; emit toChanged(); if (isComponentComplete()) { - if (!d->setValue(d->value, false)) { + if (!d->setValue(d->value, /* allowWrap = */false, /* modified = */ false)) { d->updateUpEnabled(); d->updateDownEnabled(); } @@ -460,7 +512,7 @@ int QQuickSpinBox::value() const void QQuickSpinBox::setValue(int value) { Q_D(QQuickSpinBox); - d->setValue(value, false); + d->setValue(value, /* allowWrap = */ false, /* modified = */ false); } /*! @@ -732,6 +784,55 @@ bool QQuickSpinBox::isInputMethodComposing() const } /*! + \since QtQuick.Controls 2.3 (Qt 5.10) + \qmlproperty bool QtQuick.Controls::SpinBox::wrap + + This property holds whether the spinbox wraps. The default value is \c false. + + If wrap is \c true, stepping past \l to changes the value to \l from and vice versa. +*/ +bool QQuickSpinBox::wrap() const +{ + Q_D(const QQuickSpinBox); + return d->wrap; +} + +void QQuickSpinBox::setWrap(bool wrap) +{ + Q_D(QQuickSpinBox); + if (d->wrap == wrap) + return; + + d->wrap = wrap; + if (d->value == d->from || d->value == d->to) { + d->updateUpEnabled(); + d->updateDownEnabled(); + } + emit wrapChanged(); +} + +/*! + \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. @@ -853,8 +954,8 @@ void QQuickSpinBox::wheelEvent(QWheelEvent *event) if (d->wheelEnabled) { const QPointF angle = event->angleDelta(); const qreal delta = (qFuzzyIsNull(angle.y()) ? angle.x() : angle.y()) / QWheelEvent::DefaultDeltasPerStep; - if (!d->setValue(d->value + qRound(d->effectiveStepSize() * delta), true)) - event->ignore(); + if (!d->stepBy(qRound(d->effectiveStepSize() * delta), true)) + event->ignore(); } } #endif @@ -878,7 +979,8 @@ void QQuickSpinBox::componentComplete() QQuickSpinButtonPrivate::get(d->down)->executeIndicator(true); QQuickControl::componentComplete(); - if (!d->setValue(d->value, false)) { + if (!d->setValue(d->value, /* allowWrap = */ false, /* modified = */ false)) { + d->updateDisplayText(); d->updateUpEnabled(); d->updateDownEnabled(); } @@ -912,11 +1014,23 @@ 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); } +QPalette QQuickSpinBox::defaultPalette() const +{ + return QQuickControlPrivate::themePalette(QPlatformTheme::TextLineEditPalette); +} + #if QT_CONFIG(accessibility) QAccessible::Role QQuickSpinBox::accessibleRole() const { |