diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-06-14 19:08:52 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-06-16 08:45:49 +0000 |
commit | 0d851ddab069bd4373e90beacba0efa38ec021bb (patch) | |
tree | 7d80d3504c1f1e05a5b3caf5919989f4e37bd967 | |
parent | e96fd5f39158f775d45ae9a60564f00454b789ed (diff) |
QQuickSpinBox: emit valueModified() on long press
Move the emit inside QQuickSpinBoxPrivate::setValue() to avoid having
to store and compare the old value in so many places where the value
changes are interactive (mouse, keys, wheel).
Task-number: QTBUG-61426
Change-Id: I7f42fc09cafc403eb55a9748e3a93c2e9bf6df62
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/quicktemplates2/qquickspinbox.cpp | 51 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_spinbox.qml | 103 |
2 files changed, 56 insertions, 98 deletions
diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp index dbb67c43..aaddfc82 100644 --- a/src/quicktemplates2/qquickspinbox.cpp +++ b/src/quicktemplates2/qquickspinbox.cpp @@ -123,7 +123,9 @@ public: int boundValue(int value) const; void updateValue(); - bool setValue(int value); + bool setValue(int value, bool modified); + void increase(bool modified); + void decrease(bool modified); int effectiveStepSize() const; @@ -173,16 +175,13 @@ 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(); + setValue(val.toInt(), true); } } } } -bool QQuickSpinBoxPrivate::setValue(int newValue) +bool QQuickSpinBoxPrivate::setValue(int newValue, bool modified) { Q_Q(QQuickSpinBox); if (q->isComponentComplete()) @@ -197,9 +196,21 @@ bool QQuickSpinBoxPrivate::setValue(int newValue) updateDownEnabled(); emit q->valueChanged(); + if (modified) + emit q->valueModified(); return true; } +void QQuickSpinBoxPrivate::increase(bool modified) +{ + setValue(value + effectiveStepSize(), modified); +} + +void QQuickSpinBoxPrivate::decrease(bool modified) +{ + setValue(value - effectiveStepSize(), modified); +} + int QQuickSpinBoxPrivate::effectiveStepSize() const { return from > to ? -1 * stepSize : stepSize; @@ -373,7 +384,7 @@ void QQuickSpinBox::setFrom(int from) d->from = from; emit fromChanged(); if (isComponentComplete()) { - if (!d->setValue(d->value)) { + if (!d->setValue(d->value, false)) { d->updateUpEnabled(); d->updateDownEnabled(); } @@ -402,7 +413,7 @@ void QQuickSpinBox::setTo(int to) d->to = to; emit toChanged(); if (isComponentComplete()) { - if (!d->setValue(d->value)) { + if (!d->setValue(d->value, false)) { d->updateUpEnabled(); d->updateDownEnabled(); } @@ -423,7 +434,7 @@ int QQuickSpinBox::value() const void QQuickSpinBox::setValue(int value) { Q_D(QQuickSpinBox); - d->setValue(value); + d->setValue(value, false); } /*! @@ -704,7 +715,7 @@ bool QQuickSpinBox::isInputMethodComposing() const void QQuickSpinBox::increase() { Q_D(QQuickSpinBox); - setValue(d->value + d->effectiveStepSize()); + d->increase(false); } /*! @@ -717,7 +728,7 @@ void QQuickSpinBox::increase() void QQuickSpinBox::decrease() { Q_D(QQuickSpinBox); - setValue(d->value - d->effectiveStepSize()); + d->decrease(false); } void QQuickSpinBox::focusInEvent(QFocusEvent *event) @@ -757,11 +768,10 @@ 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()) { - increase(); + d->increase(true); d->up->setPressed(true); event->accept(); } @@ -769,7 +779,7 @@ void QQuickSpinBox::keyPressEvent(QKeyEvent *event) case Qt::Key_Down: if (d->downEnabled()) { - decrease(); + d->decrease(true); d->down->setPressed(true); event->accept(); } @@ -778,8 +788,6 @@ void QQuickSpinBox::keyPressEvent(QKeyEvent *event) default: break; } - if (d->value != oldValue) - emit valueModified(); setAccessibleProperty("pressed", d->up->isPressed() || d->down->isPressed()); } @@ -805,9 +813,9 @@ void QQuickSpinBox::timerEvent(QTimerEvent *event) d->startPressRepeat(); } else if (event->timerId() == d->repeatTimer) { if (d->up->isPressed()) - increase(); + d->increase(true); else if (d->down->isPressed()) - decrease(); + d->decrease(true); } } @@ -817,13 +825,10 @@ void QQuickSpinBox::wheelEvent(QWheelEvent *event) Q_D(QQuickSpinBox); QQuickControl::wheelEvent(event); if (d->wheelEnabled) { - const int oldValue = d->value; 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); + if (!d->setValue(d->value + qRound(d->effectiveStepSize() * delta), true)) + event->ignore(); } } #endif diff --git a/tests/auto/controls/data/tst_spinbox.qml b/tests/auto/controls/data/tst_spinbox.qml index 4b2e38b3..eced19df 100644 --- a/tests/auto/controls/data/tst_spinbox.qml +++ b/tests/auto/controls/data/tst_spinbox.qml @@ -189,91 +189,44 @@ TestCase { compare(control.down.indicator.enabled, true) } - function test_mouse() { - var control = createTemporaryObject(spinBox, testCase, {stepSize: 50}) + function test_mouse_data() { + return [ + { tag: "up", button: "up", value: 50, enabled: true, hold: false, modified: 1, expected: 51 }, + { tag: "down", button: "down", value: 50, enabled: true, hold: false, modified: 1, expected: 49 }, + { tag: "up:disabled", button: "up", value: 99, enabled: false, hold: false, modified: 0, expected: 99 }, + { tag: "down:disabled", button: "down", value: 0, enabled: false, hold: false, modified: 0, expected: 0 }, + { tag: "up:hold", button: "up", value: 95, enabled: true, hold: true, modified: 4, expected: 99 }, + { tag: "down:hold", button: "down", value: 5, enabled: true, hold: true, modified: 5, expected: 0 } + ] + } + + function test_mouse(data) { + var control = createTemporaryObject(spinBox, testCase, {value: data.value}) verify(control) - var upPressedSpy = signalSpy.createObject(control, {target: control.up, signalName: "pressedChanged"}) - verify(upPressedSpy.valid) + var button = control[data.button] + verify(button) - var downPressedSpy = signalSpy.createObject(control, {target: control.down, signalName: "pressedChanged"}) - verify(downPressedSpy.valid) + var pressedSpy = signalSpy.createObject(control, {target: button, signalName: "pressedChanged"}) + verify(pressedSpy.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) + mousePress(button.indicator) + compare(pressedSpy.count, data.enabled ? 1 : 0) + compare(button.pressed, data.enabled) + compare(control.value, data.value) compare(valueModifiedSpy.count, 0) - mouseRelease(control.up.indicator) - compare(upPressedSpy.count, 2) - compare(control.up.pressed, false) - 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 - compare(control.up.indicator.enabled, false) + if (data.hold) + tryCompare(control, "value", data.expected) - mousePress(control.up.indicator) - compare(upPressedSpy.count, 2) - compare(control.up.pressed, false) - 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) - compare(control.up.pressed, false) - 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) - compare(downPressedSpy.count, 1) - compare(control.down.pressed, true) - 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) - compare(control.down.pressed, false) - 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 - compare(control.down.indicator.enabled, false) - - mousePress(control.down.indicator) - compare(downPressedSpy.count, 2) - compare(control.down.pressed, false) - 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) - compare(control.down.pressed, false) - compare(upPressedSpy.count, 2) - compare(control.up.pressed, false) - compare(control.value, control.from) - compare(valueModifiedSpy.count, 2) + mouseRelease(button.indicator) + compare(pressedSpy.count, data.enabled ? 2 : 0) + compare(button.pressed, false) + compare(control.value, data.expected) + compare(valueModifiedSpy.count, data.modified) } function test_keys() { |