diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2016-07-11 14:51:16 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2016-07-12 09:21:21 +0000 |
commit | f06db0f1c6538305d226311044cca367fc031195 (patch) | |
tree | 5c93e7c24a875498c9ad7e4a930da1a4c0f8a34f | |
parent | 6a8513909cf3aaad6be41a325129cf22f61842bf (diff) |
SwipeDelegate: reposition contentItem, background on width changes
Change-Id: I6af4f1a4517e79f2e34b0bbca401b0eefd0c6ff4
Task-number: QTBUG-54660
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
-rw-r--r-- | src/quicktemplates2/qquickswipedelegate.cpp | 48 | ||||
-rw-r--r-- | src/quicktemplates2/qquickswipedelegate_p.h | 2 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_swipedelegate.qml | 18 |
3 files changed, 61 insertions, 7 deletions
diff --git a/src/quicktemplates2/qquickswipedelegate.cpp b/src/quicktemplates2/qquickswipedelegate.cpp index aedaf29a..dbf4fc95 100644 --- a/src/quicktemplates2/qquickswipedelegate.cpp +++ b/src/quicktemplates2/qquickswipedelegate.cpp @@ -92,6 +92,13 @@ QT_BEGIN_NAMESPACE \sa {Customizing SwipeDelegate}, {Delegate Controls} */ +namespace { + enum PositionAnimation { + DontAnimatePosition, + AnimatePosition + }; +} + class QQuickSwipePrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QQuickSwipe) @@ -117,6 +124,7 @@ public: QQuickItem *createDelegateItem(QQmlComponent *component); QQuickItem *showRelevantItemForPosition(qreal position); QQuickItem *createRelevantItemForDistance(qreal distance); + void reposition(PositionAnimation animationPolicy); void createLeftItem(); void createBehindItem(); void createRightItem(); @@ -234,6 +242,27 @@ QQuickItem *QQuickSwipePrivate::createRelevantItemForDistance(qreal distance) return nullptr; } +void QQuickSwipePrivate::reposition(PositionAnimation animationPolicy) +{ + QQuickItem *relevantItem = showRelevantItemForPosition(position); + const qreal relevantWidth = relevantItem ? relevantItem->width() : 0.0; + const qreal contentItemX = position * relevantWidth + control->leftPadding(); + + // "Behavior on x" relies on the property system to know when it should update, + // so we can prevent it from animating by setting the x position directly. + if (animationPolicy == AnimatePosition) { + if (QQuickItem *contentItem = control->contentItem()) + contentItem->setProperty("x", contentItemX); + if (QQuickItem *background = control->background()) + background->setProperty("x", position * relevantWidth); + } else { + if (QQuickItem *contentItem = control->contentItem()) + contentItem->setX(contentItemX); + if (QQuickItem *background = control->background()) + background->setX(position * relevantWidth); + } +} + void QQuickSwipePrivate::createLeftItem() { if (!leftItem) { @@ -496,13 +525,7 @@ void QQuickSwipe::setPosition(qreal position) return; d->position = adjustedPosition; - - QQuickItem *relevantItem = d->showRelevantItemForPosition(d->position); - const qreal relevantWidth = relevantItem ? relevantItem->width() : 0.0; - d->control->contentItem()->setProperty("x", d->position * relevantWidth + d->control->leftPadding()); - if (QQuickItem *background = d->control->background()) - background->setProperty("x", d->position * relevantWidth); - + d->reposition(AnimatePosition); emit positionChanged(); } @@ -845,6 +868,17 @@ void QQuickSwipeDelegate::mouseReleaseEvent(QMouseEvent *event) d->handleMouseReleaseEvent(this, event); } +void QQuickSwipeDelegate::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + Q_D(QQuickSwipeDelegate); + QQuickControl::geometryChanged(newGeometry, oldGeometry); + + if (!qFuzzyCompare(newGeometry.width(), oldGeometry.width())) { + QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&d->swipe); + swipePrivate->reposition(DontAnimatePosition); + } +} + QFont QQuickSwipeDelegate::defaultFont() const { return QQuickControlPrivate::themeFont(QPlatformTheme::ListViewFont); diff --git a/src/quicktemplates2/qquickswipedelegate_p.h b/src/quicktemplates2/qquickswipedelegate_p.h index b2bea3eb..e71455b0 100644 --- a/src/quicktemplates2/qquickswipedelegate_p.h +++ b/src/quicktemplates2/qquickswipedelegate_p.h @@ -71,6 +71,8 @@ protected: void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; + void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; + QFont defaultFont() const override; #ifndef QT_NO_ACCESSIBILITY diff --git a/tests/auto/controls/data/tst_swipedelegate.qml b/tests/auto/controls/data/tst_swipedelegate.qml index f89893c3..42ea3542 100644 --- a/tests/auto/controls/data/tst_swipedelegate.qml +++ b/tests/auto/controls/data/tst_swipedelegate.qml @@ -910,4 +910,22 @@ TestCase { control.destroy(); } + + // When the width of a SwipeDelegate changes (as it does upon portrait => landscape + // rotation, for example), the positions of the contentItem and background items + // should be updated accordingly. + function test_contentItemPosOnWidthChanged() { + var control = swipeDelegateComponent.createObject(testCase); + verify(control); + + swipe(control, 0.0, 1.0); + + var oldContentItemX = control.contentItem.x; + var oldBackgroundX = control.background.x; + control.width += 100; + compare(control.contentItem.x, oldContentItemX + 100); + compare(control.background.x, oldBackgroundX + 100); + + control.destroy(); + } } |