diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2016-11-21 11:52:53 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-11-25 14:36:47 +0000 |
commit | d705558a28e013aea904214deb8f35c708b61ac3 (patch) | |
tree | 14d5a71088827523ce183a8c88b30a37c179352f | |
parent | 22769ac6a7667bf1d2d5b5e6421a15e4f6aadce1 (diff) |
SwipeDelegate: Add swipe.enabled property
[ChangeLog][Controls][SwipeDelegate] Added swipe.enabled property
to allow disabling of swiping.
Task-number: QTBUG-57192
Change-Id: I733336690368ea3fb56a144a335a37e60a02f1b9
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
-rw-r--r-- | src/imports/templates/qtquicktemplates2plugin.cpp | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickswipe_p.h | 5 | ||||
-rw-r--r-- | src/quicktemplates2/qquickswipedelegate.cpp | 35 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_swipedelegate.qml | 83 |
4 files changed, 123 insertions, 1 deletions
diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp index 57e8f88e..76cce2d3 100644 --- a/src/imports/templates/qtquicktemplates2plugin.cpp +++ b/src/imports/templates/qtquicktemplates2plugin.cpp @@ -227,6 +227,7 @@ void QtQuickTemplates2Plugin::registerTypes(const char *uri) qmlRegisterType<QQuickDial, 2>(uri, 2, 2, "Dial"); qmlRegisterType<QQuickRangeSlider, 2>(uri, 2, 2, "RangeSlider"); qmlRegisterType<QQuickSlider, 2>(uri, 2, 2, "Slider"); + qmlRegisterType<QQuickSwipeDelegate, 2>(uri, 2, 2, "SwipeDelegate"); qmlRegisterType<QQuickTumbler, 2>(uri, 2, 2, "Tumbler"); } diff --git a/src/quicktemplates2/qquickswipe_p.h b/src/quicktemplates2/qquickswipe_p.h index c1a28819..76472c67 100644 --- a/src/quicktemplates2/qquickswipe_p.h +++ b/src/quicktemplates2/qquickswipe_p.h @@ -63,6 +63,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickSwipe : public QObject Q_OBJECT Q_PROPERTY(qreal position READ position NOTIFY positionChanged FINAL) Q_PROPERTY(bool complete READ isComplete NOTIFY completeChanged FINAL) + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged FINAL /*REVISION 2*/) Q_PROPERTY(QQmlComponent *left READ left WRITE setLeft NOTIFY leftChanged FINAL) Q_PROPERTY(QQmlComponent *behind READ behind WRITE setBehind NOTIFY behindChanged FINAL) Q_PROPERTY(QQmlComponent *right READ right WRITE setRight NOTIFY rightChanged FINAL) @@ -79,6 +80,9 @@ public: bool isComplete() const; void setComplete(bool complete); + bool isEnabled() const; + void setEnabled(bool enabled); + QQmlComponent *left() const; void setLeft(QQmlComponent *left); @@ -102,6 +106,7 @@ public: Q_SIGNALS: void positionChanged(); void completeChanged(); + /*Q_REVISION(2)*/ void enabledChanged(); /*Q_REVISION(1)*/ void completed(); void leftChanged(); void behindChanged(); diff --git a/src/quicktemplates2/qquickswipedelegate.cpp b/src/quicktemplates2/qquickswipedelegate.cpp index 5430a4de..e1830a9f 100644 --- a/src/quicktemplates2/qquickswipedelegate.cpp +++ b/src/quicktemplates2/qquickswipedelegate.cpp @@ -124,6 +124,7 @@ public: position(0), wasComplete(false), complete(false), + enabled(true), left(nullptr), behind(nullptr), right(nullptr), @@ -160,6 +161,7 @@ public: // before the last press event. bool wasComplete; bool complete; + bool enabled; QQuickVelocityCalculator velocityCalculator; QQmlComponent *left; QQmlComponent *behind; @@ -574,6 +576,22 @@ void QQuickSwipe::setComplete(bool complete) emit completed(); } +bool QQuickSwipe::isEnabled() const +{ + Q_D(const QQuickSwipe); + return d->enabled; +} + +void QQuickSwipe::setEnabled(bool enabled) +{ + Q_D(QQuickSwipe); + if (enabled == d->enabled) + return; + + d->enabled = enabled; + emit enabledChanged(); +} + void QQuickSwipe::close() { Q_D(QQuickSwipe); @@ -633,12 +651,17 @@ bool QQuickSwipeDelegatePrivate::handleMouseMoveEvent(QQuickItem *item, QMouseEv stopPressAndHold(); } + // The delegate can still be pressed when swipe.enabled is false, + // but the mouse moving shouldn't have any effect on swipe.position. + QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&swipe); + if (!swipePrivate->enabled) + return false; + // Protect against division by zero. if (width == 0) return false; // Don't bother reacting to events if we don't have any delegates. - QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&swipe); if (!swipePrivate->left && !swipePrivate->right && !swipePrivate->behind) return false; @@ -819,6 +842,7 @@ QQuickSwipeDelegate::QQuickSwipeDelegate(QQuickItem *parent) : \qmlpropertygroup QtQuick.Controls::SwipeDelegate::swipe \qmlproperty real QtQuick.Controls::SwipeDelegate::swipe.position \qmlproperty bool QtQuick.Controls::SwipeDelegate::swipe.complete + \qmlproperty bool QtQuick.Controls::SwipeDelegate::swipe.enabled \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.left \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.behind \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.right @@ -844,6 +868,11 @@ QQuickSwipeDelegate::QQuickSwipeDelegate(QQuickItem *parent) : When complete is \c true, any interactive items declared in \c left, \c right, or \c behind will receive mouse events. \row + \li enabled + \li This property determines whether or not the control can be swiped. + + This property was added in QtQuick.Controls 2.2. + \row \li left \li This property holds the left delegate. @@ -963,7 +992,11 @@ void QQuickSwipeDelegate::mousePressEvent(QMouseEvent *event) { Q_D(QQuickSwipeDelegate); QQuickItemDelegate::mousePressEvent(event); + QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&d->swipe); + if (!swipePrivate->enabled) + return; + swipePrivate->positionBeforePress = swipePrivate->position; swipePrivate->velocityCalculator.startMeasuring(event->pos(), event->timestamp()); } diff --git a/tests/auto/controls/data/tst_swipedelegate.qml b/tests/auto/controls/data/tst_swipedelegate.qml index 92f25e2a..c2e99df1 100644 --- a/tests/auto/controls/data/tst_swipedelegate.qml +++ b/tests/auto/controls/data/tst_swipedelegate.qml @@ -1367,4 +1367,87 @@ TestCase { control.destroy(); } + + Component { + id: swipeDelegateDisabledComponent + + SwipeDelegate { + id: swipeDelegate + text: "SwipeDelegate" + width: parent.width + height: checked ? implicitHeight * 2 : implicitHeight + checkable: true + + swipe.enabled: false + swipe.right: Label { + text: swipeDelegate.checked ? qsTr("Expanded") : qsTr("Collapsed") + width: parent.width + height: parent.height + padding: 12 + color: "white" + verticalAlignment: Label.AlignVCenter + horizontalAlignment: Label.AlignRight + } + } + } + + function test_swipeEnabled() { + var control = swipeDelegateDisabledComponent.createObject(testCase); + + mousePress(control, control.width / 2, control.height / 2); + verify(control.pressed); + compare(control.swipe.position, 0.0); + verify(!control.swipe.complete); + verify(!control.swipe.leftItem); + verify(!control.swipe.rightItem); + + // It shouldn't be possible to swipe. + var overDragDistance = Math.round(dragDistance * 1.1); + mouseMove(control, control.width / 2 - overDragDistance, control.height / 2); + verify(control.pressed); + compare(control.swipe.position, 0.0); + verify(!control.swipe.complete); + verify(!control.swipe.leftItem); + verify(!control.swipe.rightItem); + + // Now move outside the right edge of the control and release. + mouseMove(control, control.width * 1.1, control.height / 2); + verify(control.pressed); + compare(control.swipe.position, 0.0); + verify(!control.swipe.complete); + verify(!control.swipe.leftItem); + verify(!control.swipe.rightItem); + + mouseRelease(control, control.width / 2, control.height / 2); + verify(!control.pressed); + compare(control.swipe.position, 0.0); + verify(!control.swipe.complete); + verify(!control.swipe.leftItem); + verify(!control.swipe.rightItem); + + // Now enabled swiping so that we can swipe to the left. + control.swipe.enabled = true; + swipe(control, 0, -1); + verify(control.swipe.complete); + + // Now that the swipe is complete, disable swiping and then try to swipe again. + // It should stay at its position of -1. + control.swipe.enabled = false; + + mousePress(control, control.width / 2, control.height / 2); + verify(control.pressed); + compare(control.swipe.position, -1.0); + + mouseMove(control, control.width / 2 + overDragDistance, control.height / 2); + verify(control.pressed); + compare(control.swipe.position, -1.0); + verify(control.swipe.complete); + + mouseRelease(control, control.width / 2 + overDragDistance, control.height / 2); + verify(!control.pressed); + compare(control.swipe.position, -1.0); + verify(control.swipe.complete); + + control.destroy(); + } } |