diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-12-02 14:58:52 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-12-02 17:37:57 +0000 |
commit | 2a2a58a061bef4360708b8dcfda31a959b01ba66 (patch) | |
tree | 882dbae20b6ee4cac1acbdef58052843e4b022d8 | |
parent | 0a37852dd814159d901791b9ea7f59a6dd21837c (diff) |
Add QQuickSpinBox::valueModified()
Another name candidate was valueEdited(), but it was left available
in case we want to have the respective signal for text editing in the
future.
[ChangeLog][Controls][SpinBox] Added a valueModified() signal that
is emitted whenever the value of a spin box has been interactively
modified by the user by using either touch, mouse, wheel, or keys.
Task-number: QTBUG-57203
Change-Id: I705c7e63d23235f51d401abf27f3458f8a5b0589
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/imports/templates/qtquicktemplates2plugin.cpp | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickspinbox.cpp | 20 | ||||
-rw-r--r-- | src/quicktemplates2/qquickspinbox_p.h | 1 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_spinbox.qml | 40 |
4 files changed, 60 insertions, 2 deletions
diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp index 203cba30..b78d3146 100644 --- a/src/imports/templates/qtquicktemplates2plugin.cpp +++ b/src/imports/templates/qtquicktemplates2plugin.cpp @@ -229,6 +229,7 @@ void QtQuickTemplates2Plugin::registerTypes(const char *uri) qmlRegisterType<QQuickDrawer, 2>(uri, 2, 2, "Drawer"); qmlRegisterType<QQuickRangeSlider, 2>(uri, 2, 2, "RangeSlider"); qmlRegisterType<QQuickSlider, 2>(uri, 2, 2, "Slider"); + qmlRegisterType<QQuickSpinBox, 2>(uri, 2, 2, "SpinBox"); qmlRegisterType<QQuickSwipeDelegate, 2>(uri, 2, 2, "SwipeDelegate"); qmlRegisterType<QQuickTumbler, 2>(uri, 2, 2, "Tumbler"); } diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp index 829c64cc..4ece45cc 100644 --- a/src/quicktemplates2/qquickspinbox.cpp +++ b/src/quicktemplates2/qquickspinbox.cpp @@ -93,6 +93,14 @@ static const int AUTO_REPEAT_INTERVAL = 100; \sa Tumbler, {Customizing SpinBox} */ +/*! + \since QtQuick.Controls 2.2 + \qmlsignal QtQuick.Controls::SpinBox::valueModified() + + This signal is emitted when the spin box value has been interactively + modified by the user by either touch, mouse, wheel, or keys. +*/ + class QQuickSpinBoxPrivate : public QQuickControlPrivate { Q_DECLARE_PUBLIC(QQuickSpinBox) @@ -152,7 +160,10 @@ void QQuickSpinBoxPrivate::updateValue() QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine); QJSValue loc(v4, QQmlLocale::wrap(v4, locale)); QJSValue val = q->valueFromText().call(QJSValueList() << text.toString() << loc); + const int oldValue = value; q->setValue(val.toInt()); + if (oldValue != value) + emit q->valueModified(); } } } @@ -264,6 +275,8 @@ bool QQuickSpinBoxPrivate::handleMouseReleaseEvent(QQuickItem *child, QMouseEven Q_Q(QQuickSpinBox); QQuickItem *ui = up->indicator(); QQuickItem *di = down->indicator(); + + int oldValue = value; bool wasPressed = up->isPressed() || down->isPressed(); if (up->isPressed()) { up->setPressed(false); @@ -274,6 +287,8 @@ bool QQuickSpinBoxPrivate::handleMouseReleaseEvent(QQuickItem *child, QMouseEven if (repeatTimer <= 0 && di && di->contains(di->mapFromItem(child, event->pos()))) q->decrease(); } + if (value != oldValue) + emit q->valueModified(); q->setAccessibleProperty("pressed", false); stopPressRepeat(); @@ -692,6 +707,7 @@ void QQuickSpinBox::keyPressEvent(QKeyEvent *event) Q_D(QQuickSpinBox); QQuickControl::keyPressEvent(event); + const int oldValue = d->value; switch (event->key()) { case Qt::Key_Up: if (d->upEnabled()) { @@ -712,6 +728,8 @@ void QQuickSpinBox::keyPressEvent(QKeyEvent *event) default: break; } + if (d->value != oldValue) + emit valueModified(); setAccessibleProperty("pressed", d->up->isPressed() || d->down->isPressed()); } @@ -797,6 +815,8 @@ void QQuickSpinBox::wheelEvent(QWheelEvent *event) const QPointF angle = event->angleDelta(); const qreal delta = (qFuzzyIsNull(angle.y()) ? angle.x() : angle.y()) / QWheelEvent::DefaultDeltasPerStep; setValue(oldValue + qRound(d->effectiveStepSize() * delta)); + if (d->value != oldValue) + emit valueModified(); event->setAccepted(d->value != oldValue); } } diff --git a/src/quicktemplates2/qquickspinbox_p.h b/src/quicktemplates2/qquickspinbox_p.h index c3cd7ca8..e240c49c 100644 --- a/src/quicktemplates2/qquickspinbox_p.h +++ b/src/quicktemplates2/qquickspinbox_p.h @@ -122,6 +122,7 @@ Q_SIGNALS: void validatorChanged(); void textFromValueChanged(); void valueFromTextChanged(); + Q_REVISION(2) void valueModified(); Q_REVISION(2) void inputMethodHintsChanged(); Q_REVISION(2) void inputMethodComposingChanged(); diff --git a/tests/auto/controls/data/tst_spinbox.qml b/tests/auto/controls/data/tst_spinbox.qml index de44415d..db54fc15 100644 --- a/tests/auto/controls/data/tst_spinbox.qml +++ b/tests/auto/controls/data/tst_spinbox.qml @@ -177,12 +177,16 @@ TestCase { var downPressedSpy = signalSpy.createObject(control, {target: control.down, signalName: "pressedChanged"}) verify(downPressedSpy.valid) + var valueModifiedSpy = signalSpy.createObject(control, {target: control, signalName: "valueModified"}) + verify(valueModifiedSpy.valid) + mousePress(control.up.indicator) compare(upPressedSpy.count, 1) compare(control.up.pressed, true) compare(downPressedSpy.count, 0) compare(control.down.pressed, false) compare(control.value, 0) + compare(valueModifiedSpy.count, 0) mouseRelease(control.up.indicator) compare(upPressedSpy.count, 2) @@ -190,6 +194,7 @@ TestCase { compare(downPressedSpy.count, 0) compare(control.down.pressed, false) compare(control.value, 50) + compare(valueModifiedSpy.count, 1) // Disable the up button and try again. control.value = control.to @@ -201,6 +206,7 @@ TestCase { compare(downPressedSpy.count, 0) compare(control.down.pressed, false) compare(control.value, control.to) + compare(valueModifiedSpy.count, 1) mouseRelease(control.up.indicator) compare(upPressedSpy.count, 2) @@ -208,6 +214,7 @@ TestCase { compare(downPressedSpy.count, 0) compare(control.down.pressed, false) compare(control.value, control.to) + compare(valueModifiedSpy.count, 1) control.value = 50; mousePress(control.down.indicator) @@ -216,6 +223,7 @@ TestCase { compare(upPressedSpy.count, 2) compare(control.up.pressed, false) compare(control.value, 50) + compare(valueModifiedSpy.count, 1) mouseRelease(control.down.indicator) compare(downPressedSpy.count, 2) @@ -223,6 +231,7 @@ TestCase { compare(upPressedSpy.count, 2) compare(control.up.pressed, false) compare(control.value, 0) + compare(valueModifiedSpy.count, 2) // Disable the down button and try again. control.value = control.from @@ -234,6 +243,7 @@ TestCase { compare(upPressedSpy.count, 2) compare(control.up.pressed, false) compare(control.value, control.from) + compare(valueModifiedSpy.count, 2) mouseRelease(control.down.indicator) compare(downPressedSpy.count, 2) @@ -241,6 +251,7 @@ TestCase { compare(upPressedSpy.count, 2) compare(control.up.pressed, false) compare(control.value, control.from) + compare(valueModifiedSpy.count, 2) control.destroy() } @@ -251,6 +262,7 @@ TestCase { var upPressedCount = 0 var downPressedCount = 0 + var valueModifiedCount = 0 var upPressedSpy = signalSpy.createObject(control, {target: control.up, signalName: "pressedChanged"}) verify(upPressedSpy.valid) @@ -258,6 +270,9 @@ TestCase { var downPressedSpy = signalSpy.createObject(control, {target: control.down, signalName: "pressedChanged"}) verify(downPressedSpy.valid) + var valueModifiedSpy = signalSpy.createObject(control, {target: control, signalName: "valueModified"}) + verify(valueModifiedSpy.valid) + control.forceActiveFocus() verify(control.activeFocus) @@ -269,6 +284,7 @@ TestCase { compare(control.down.pressed, true) compare(control.up.pressed, false) compare(downPressedSpy.count, ++downPressedCount) + compare(valueModifiedSpy.count, ++valueModifiedCount) compare(control.value, 50 - d1) @@ -276,6 +292,7 @@ TestCase { compare(control.down.pressed, false) compare(control.up.pressed, false) compare(downPressedSpy.count, ++downPressedCount) + compare(valueModifiedSpy.count, valueModifiedCount) } compare(control.value, 40) @@ -284,6 +301,7 @@ TestCase { compare(control.up.pressed, true) compare(control.down.pressed, false) compare(upPressedSpy.count, ++upPressedCount) + compare(valueModifiedSpy.count, ++valueModifiedCount) compare(control.value, 40 + i1) @@ -291,6 +309,7 @@ TestCase { compare(control.down.pressed, false) compare(control.up.pressed, false) compare(upPressedSpy.count, ++upPressedCount) + compare(valueModifiedSpy.count, valueModifiedCount) } compare(control.value, 50) @@ -302,9 +321,12 @@ TestCase { keyPress(Qt.Key_Down) compare(control.down.pressed, wasDownEnabled) compare(control.up.pressed, false) - if (wasDownEnabled) + if (wasDownEnabled) { ++downPressedCount + ++valueModifiedCount + } compare(downPressedSpy.count, downPressedCount) + compare(valueModifiedSpy.count, valueModifiedCount) compare(control.value, Math.max(0, 50 - d2 * 25)) @@ -314,6 +336,7 @@ TestCase { if (wasDownEnabled) ++downPressedCount compare(downPressedSpy.count, downPressedCount) + compare(valueModifiedSpy.count, valueModifiedCount) } compare(control.value, 0) @@ -322,9 +345,12 @@ TestCase { keyPress(Qt.Key_Up) compare(control.up.pressed, wasUpEnabled) compare(control.down.pressed, false) - if (wasUpEnabled) + if (wasUpEnabled) { ++upPressedCount + ++valueModifiedCount + } compare(upPressedSpy.count, upPressedCount) + compare(valueModifiedSpy.count, valueModifiedCount) compare(control.value, Math.min(99, i2 * 25)) @@ -334,6 +360,7 @@ TestCase { if (wasUpEnabled) ++upPressedCount compare(upPressedSpy.count, upPressedCount) + compare(valueModifiedSpy.count, valueModifiedCount) } compare(control.value, 99) @@ -410,33 +437,42 @@ TestCase { var control = spinBox.createObject(testCase, {wheelEnabled: true}) verify(control) + var valueModifiedSpy = signalSpy.createObject(control, {target: control, signalName: "valueModified"}) + verify(valueModifiedSpy.valid) + var delta = 120 compare(control.value, 0) mouseWheel(control, control.width / 2, control.height / 2, delta, delta) compare(control.value, 1) + compare(valueModifiedSpy.count, 1) control.stepSize = 2 mouseWheel(control, control.width / 2, control.height / 2, delta, delta) compare(control.value, 3) + compare(valueModifiedSpy.count, 2) control.stepSize = 10 mouseWheel(control, control.width / 2, control.height / 2, -delta, -delta) compare(control.value, 0) + compare(valueModifiedSpy.count, 3) control.stepSize = 5 mouseWheel(control, control.width / 2, control.height / 2, delta, delta) compare(control.value, 5) + compare(valueModifiedSpy.count, 4) mouseWheel(control, control.width / 2, control.height / 2, 0.5 * delta, 0.5 * delta) compare(control.value, 8) + compare(valueModifiedSpy.count, 5) mouseWheel(control, control.width / 2, control.height / 2, -delta, -delta) compare(control.value, 3) + compare(valueModifiedSpy.count, 6) control.destroy() } |