diff options
author | Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com> | 2015-04-29 12:47:38 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-04-29 14:34:30 +0000 |
commit | e661fa1057641092428b5cb2db74b2f3a9496331 (patch) | |
tree | 7e80ccf771f0fd05d38f3ba43dbbf6773215f604 | |
parent | 758ecffe6450e3e64282161eeaa5f76fb8b158c3 (diff) |
QQuickButton: Add canceled signal
We now support the following pattern:
1. Press the mouse button => emit pressed
2. Move the mouse cursor outside the button => pressed = false
3. Release the mouse button => emit canceled
Note that getting the mouse grabbed away by another item, at any
stage, will also emit the canceled signal.
Change-Id: I97e485327370ea47943dfef75553000cee449a01
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
-rw-r--r-- | src/controls/qquickbutton.cpp | 39 | ||||
-rw-r--r-- | src/controls/qquickbutton_p.h | 1 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_button.qml | 21 |
3 files changed, 49 insertions, 12 deletions
diff --git a/src/controls/qquickbutton.cpp b/src/controls/qquickbutton.cpp index 240c4397..4bf2c08a 100644 --- a/src/controls/qquickbutton.cpp +++ b/src/controls/qquickbutton.cpp @@ -121,6 +121,14 @@ QT_BEGIN_NAMESPACE */ /*! + \qmlsignal QtQuickControls2::Button::canceled() + + This signal is emitted when the button loses mouse grab + while being pressed, or when it would emit the \l release + signal but the mouse cursor is not inside the button. +*/ + +/*! \qmlsignal QtQuickControls2::Button::clicked() This signal is emitted when the button is clicked. @@ -202,10 +210,6 @@ void QQuickButton::setPressed(bool isPressed) if (d->pressed != isPressed) { d->pressed = isPressed; emit pressedChanged(); - if (isPressed) - emit pressed(); - else - emit released(); } } @@ -242,8 +246,12 @@ void QQuickButton::setLabel(QQuickItem *label) void QQuickButton::focusOutEvent(QFocusEvent *event) { + Q_D(QQuickButton); QQuickControl::focusOutEvent(event); - setPressed(false); + if (d->pressed) { + setPressed(false); + emit canceled(); + } } void QQuickButton::keyPressEvent(QKeyEvent *event) @@ -251,6 +259,7 @@ void QQuickButton::keyPressEvent(QKeyEvent *event) QQuickControl::keyPressEvent(event); if (event->key() == Qt::Key_Space) { setPressed(true); + emit pressed(); event->accept(); } } @@ -259,8 +268,9 @@ void QQuickButton::keyReleaseEvent(QKeyEvent *event) { QQuickControl::keyReleaseEvent(event); if (event->key() == Qt::Key_Space) { - emit clicked(); setPressed(false); + emit released(); + emit clicked(); event->accept(); } } @@ -269,6 +279,7 @@ void QQuickButton::mousePressEvent(QMouseEvent *event) { QQuickControl::mousePressEvent(event); setPressed(true); + emit pressed(); event->accept(); } @@ -281,17 +292,27 @@ void QQuickButton::mouseMoveEvent(QMouseEvent *event) void QQuickButton::mouseReleaseEvent(QMouseEvent *event) { + Q_D(QQuickButton); QQuickControl::mouseReleaseEvent(event); - if (contains(event->pos())) - emit clicked(); + bool wasPressed = d->pressed; setPressed(false); + if (wasPressed) { + emit released(); + emit clicked(); + } else { + emit canceled(); + } event->accept(); } void QQuickButton::mouseUngrabEvent() { + Q_D(QQuickButton); QQuickControl::mouseUngrabEvent(); - setPressed(false); + if (d->pressed) { + setPressed(false); + emit canceled(); + } } void QQuickButton::componentComplete() diff --git a/src/controls/qquickbutton_p.h b/src/controls/qquickbutton_p.h index 1622be76..903af206 100644 --- a/src/controls/qquickbutton_p.h +++ b/src/controls/qquickbutton_p.h @@ -76,6 +76,7 @@ public: Q_SIGNALS: void pressed(); void released(); + void canceled(); void clicked(); void textChanged(); void pressedChanged(); diff --git a/tests/auto/controls/data/tst_button.qml b/tests/auto/controls/data/tst_button.qml index c721518d..f4ada30c 100644 --- a/tests/auto/controls/data/tst_button.qml +++ b/tests/auto/controls/data/tst_button.qml @@ -61,6 +61,11 @@ TestCase { } SignalSpy { + id: canceledSpy + signalName: "canceled" + } + + SignalSpy { id: clickedSpy signalName: "clicked" } @@ -112,21 +117,25 @@ TestCase { pressedChangedSpy.target = control releasedSpy.target = control + canceledSpy.target = control clickedSpy.target = control verify(pressedChangedSpy.valid) verify(releasedSpy.valid) + verify(canceledSpy.valid) verify(clickedSpy.valid) // check mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton) compare(pressedChangedSpy.count, 1) compare(releasedSpy.count, 0) + compare(canceledSpy.count, 0) compare(clickedSpy.count, 0) compare(control.pressed, true) mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton) compare(pressedChangedSpy.count, 2) compare(releasedSpy.count, 1) + compare(canceledSpy.count, 0) compare(clickedSpy.count, 1) compare(control.pressed, false) @@ -134,12 +143,14 @@ TestCase { mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton) compare(pressedChangedSpy.count, 3) compare(releasedSpy.count, 1) + compare(canceledSpy.count, 0) compare(clickedSpy.count, 1) compare(control.pressed, true) mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton) compare(pressedChangedSpy.count, 4) compare(releasedSpy.count, 2) + compare(canceledSpy.count, 0) compare(clickedSpy.count, 2) compare(control.pressed, false) @@ -147,6 +158,7 @@ TestCase { mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton) compare(pressedChangedSpy.count, 5) compare(releasedSpy.count, 2) + compare(canceledSpy.count, 0) compare(clickedSpy.count, 2) compare(control.pressed, true) @@ -155,20 +167,23 @@ TestCase { mouseRelease(control, control.width * 2, control.height * 2, Qt.LeftButton) compare(pressedChangedSpy.count, 6) - compare(releasedSpy.count, 3) + compare(releasedSpy.count, 2) + compare(canceledSpy.count, 1) compare(clickedSpy.count, 2) compare(control.pressed, false) // right button mousePress(control, control.width / 2, control.height / 2, Qt.RightButton) compare(pressedChangedSpy.count, 6) - compare(releasedSpy.count, 3) + compare(releasedSpy.count, 2) + compare(canceledSpy.count, 1) compare(clickedSpy.count, 2) compare(control.pressed, false) mouseRelease(control, control.width / 2, control.height / 2, Qt.RightButton) compare(pressedChangedSpy.count, 6) - compare(releasedSpy.count, 3) + compare(releasedSpy.count, 2) + compare(canceledSpy.count, 1) compare(clickedSpy.count, 2) compare(control.pressed, false) |