Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickswipedelegate.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
9
10#include <QtGui/qstylehints.h>
11#include <QtGui/private/qguiapplication_p.h>
12#include <QtGui/private/qeventpoint_p.h>
13#include <QtGui/qpa/qplatformtheme.h>
14#include <QtQml/qqmlinfo.h>
15#include <QtQuick/private/qquickanimation_p.h>
16#include <QtQuick/private/qquicktransition_p.h>
17#include <QtQuick/private/qquicktransitionmanager_p_p.h>
18
20
21/*!
22 \qmltype SwipeDelegate
23 \inherits ItemDelegate
24//! \nativetype QQuickSwipeDelegate
25 \inqmlmodule QtQuick.Controls
26 \since 5.7
27 \ingroup qtquickcontrols-delegates
28 \brief Swipable item delegate.
29
30 SwipeDelegate presents a view item that can be swiped left or right to
31 expose more options or information. It is used as a delegate in views such
32 as \l ListView.
33
34 In the following example, SwipeDelegate is used in a \l ListView to allow
35 items to be removed from it by swiping to the left:
36
37 \snippet qtquickcontrols-swipedelegate.qml 1
38
39 SwipeDelegate inherits its API from \l ItemDelegate, which is inherited
40 from AbstractButton. For instance, you can set \l {AbstractButton::text}{text},
41 and react to \l {AbstractButton::clicked}{clicks} using the AbstractButton
42 API.
43
44 Information regarding the progress of a swipe, as well as the components
45 that should be shown upon swiping, are both available through the
46 \l {SwipeDelegate::}{swipe} grouped property object. For example,
47 \c swipe.position holds the position of the
48 swipe within the range \c -1.0 to \c 1.0. The \c swipe.left
49 property determines which item will be displayed when the control is swiped
50 to the right, and vice versa for \c swipe.right. The positioning of these
51 components is left to applications to decide. For example, without specifying
52 any position for \c swipe.left or \c swipe.right, the following will
53 occur:
54
55 \image qtquickcontrols-swipedelegate.gif
56
57 If \c swipe.left and \c swipe.right are anchored to the left and
58 right of the \l {Control::}{background} item (respectively), they'll behave like this:
59
60 \image qtquickcontrols-swipedelegate-leading-trailing.gif
61
62 When using \c swipe.left and \c swipe.right, the control cannot be
63 swiped past the left and right edges. To achieve this type of "wrapping"
64 behavior, set \c swipe.behind instead. This will result in the same
65 item being shown regardless of which direction the control is swiped. For
66 example, in the image below, we set \c swipe.behind and then swipe the
67 control repeatedly in both directions:
68
69 \image qtquickcontrols-swipedelegate-behind.gif
70
71 \sa {Customizing SwipeDelegate}, {Delegate Controls}, {Qt Quick Controls 2 - Gallery}{Gallery Example}
72*/
73
74namespace {
75 typedef QQuickSwipeDelegateAttached Attached;
76
77 Attached *attachedObject(QQuickItem *item) {
78 return qobject_cast<Attached*>(qmlAttachedPropertiesObject<QQuickSwipeDelegate>(item, false));
79 }
80
85}
86
88{
89public:
90 QQuickSwipeTransitionManager(QQuickSwipe *swipe);
91
92 void transition(QQuickTransition *transition, qreal position);
93
94protected:
96
97private:
98 QQuickSwipe *m_swipe = nullptr;
99};
100
102{
103 Q_DECLARE_PUBLIC(QQuickSwipe)
104
105public:
107
109
110 QQuickItem *createDelegateItem(QQmlComponent *component);
113 void reposition(PositionAnimation animationPolicy);
120
123
124 bool hasDelegates() const;
125
126 bool isTransitioning() const;
127 void beginTransition(qreal position);
129
131 // Same range as position, but is set before press events so that we can
132 // keep track of which direction the user must swipe when using left and right delegates.
135 // A "less strict" version of complete that is true if complete was true
136 // before the last press event.
137 bool wasComplete = false;
138 bool complete = false;
139 bool enabled = true;
140 bool waitForTransition = false;
142 QQmlComponent *left = nullptr;
150};
151
153 : m_swipe(swipe)
154{
155}
156
157void QQuickSwipeTransitionManager::transition(QQuickTransition *transition, qreal position)
158{
159 qmlExecuteDeferred(transition);
160
161 QQmlProperty defaultTarget(m_swipe, QLatin1String("position"));
162 QQmlListProperty<QQuickAbstractAnimation> animations = transition->animations();
163 const int count = animations.count(&animations);
164 for (int i = 0; i < count; ++i) {
165 QQuickAbstractAnimation *anim = animations.at(&animations, i);
166 anim->setDefaultTarget(defaultTarget);
167 }
168
169 QList<QQuickStateAction> actions;
170 actions << QQuickStateAction(m_swipe, QLatin1String("position"), position);
171 QQuickTransitionManager::transition(actions, transition, m_swipe);
172}
173
175{
176 QQuickSwipePrivate::get(m_swipe)->finishTransition();
177}
178
179QQuickSwipePrivate *QQuickSwipePrivate::get(QQuickSwipe *swipe)
180{
181 return swipe->d_func();
182}
183
185{
186 // If we don't use the correct context, it won't be possible to refer to
187 // the control's id from within the delegates.
188 QQmlContext *context = component->creationContext();
189 // The component might not have been created in QML, in which case
190 // the creation context will be null and we have to create it ourselves.
191 if (!context)
192 context = qmlContext(control);
193 QQuickItem *item = qobject_cast<QQuickItem*>(component->beginCreate(context));
194 if (item) {
195 item->setParentItem(control);
196 component->completeCreate();
197 QJSEngine::setObjectOwnership(item, QJSEngine::JavaScriptOwnership);
198 }
199 return item;
200}
201
203{
204 if (qFuzzyIsNull(position))
205 return nullptr;
206
207 if (behind) {
209 return behindItem;
210 }
211
212 if (right && position < 0.0) {
214 return rightItem;
215 }
216
217 if (left && position > 0.0) {
219 return leftItem;
220 }
221
222 return nullptr;
223}
224
226{
227 if (qFuzzyIsNull(distance))
228 return nullptr;
229
230 if (behind) {
232 return behindItem;
233 }
234
235 // a) If the position before the press was 0.0, we know that *any* movement
236 // whose distance is negative will result in the right item being shown and
237 // vice versa.
238 // b) Once the control has been exposed (that is, swiped to the left or right,
239 // and hence the position is either -1.0 or 1.0), we must use the width of the
240 // relevant item to determine if the distance is larger than that item,
241 // in order to know whether or not to display it.
242 // c) If the control has been exposed, and the swipe is larger than the width
243 // of the relevant item from which the swipe started from, we must show the
244 // item on the other side (if any).
245
246 if (right) {
247 if ((distance < 0.0 && positionBeforePress == 0.0) /* a) */
248 || (rightItem && positionBeforePress == -1.0 && distance < rightItem->width()) /* b) */
249 || (leftItem && positionBeforePress == 1.0 && qAbs(distance) > leftItem->width())) /* c) */ {
251 return rightItem;
252 }
253 }
254
255 if (left) {
256 if ((distance > 0.0 && positionBeforePress == 0.0) /* a) */
257 || (leftItem && positionBeforePress == 1.0 && qAbs(distance) < leftItem->width()) /* b) */
258 || (rightItem && positionBeforePress == -1.0 && qAbs(distance) > rightItem->width())) /* c) */ {
260 return leftItem;
261 }
262 }
263
264 return nullptr;
265}
266
268{
269 QQuickItem *relevantItem = showRelevantItemForPosition(position);
270 const qreal relevantWidth = relevantItem ? relevantItem->width() : 0.0;
271 const qreal contentItemX = position * relevantWidth + control->leftPadding();
272
273 // "Behavior on x" relies on the property system to know when it should update,
274 // so we can prevent it from animating by setting the x position directly.
275 if (animationPolicy == AnimatePosition) {
276 if (QQuickItem *contentItem = control->contentItem())
277 contentItem->setProperty("x", contentItemX);
278 if (QQuickItem *background = control->background())
279 background->setProperty("x", position * relevantWidth);
280 } else {
281 if (QQuickItem *contentItem = control->contentItem())
282 contentItem->setX(contentItemX);
283 if (QQuickItem *background = control->background())
284 background->setX(position * relevantWidth);
285 }
286}
287
289{
290 if (!leftItem) {
291 Q_Q(QQuickSwipe);
292 q->setLeftItem(createDelegateItem(left));
293 if (!leftItem)
294 qmlWarning(control) << "Failed to create left item:" << left->errors();
295 }
296}
297
299{
300 if (!behindItem) {
301 Q_Q(QQuickSwipe);
302 q->setBehindItem(createDelegateItem(behind));
303 if (!behindItem)
304 qmlWarning(control) << "Failed to create behind item:" << behind->errors();
305 }
306}
307
309{
310 if (!rightItem) {
311 Q_Q(QQuickSwipe);
312 q->setRightItem(createDelegateItem(right));
313 if (!rightItem)
314 qmlWarning(control) << "Failed to create right item:" << right->errors();
315 }
316}
317
319{
321
322 if (leftItem)
323 leftItem->setVisible(true);
324
325 if (rightItem)
326 rightItem->setVisible(false);
327}
328
330{
332
333 if (behindItem)
334 behindItem->setVisible(true);
335}
336
338{
340
341 // This item may have already existed but was hidden.
342 if (rightItem)
343 rightItem->setVisible(true);
344
345 // The left item isn't visible when the right item is visible, so save rendering effort by hiding it.
346 if (leftItem)
347 leftItem->setVisible(false);
348}
349
351{
352 qmlWarning(control) << "cannot set both behind and left/right properties";
353}
354
356{
357 qmlWarning(control) << "left/right/behind properties may only be set when swipe.position is 0";
358}
359
361{
362 return left || right || behind;
363}
364
366{
367 return transitionManager && transitionManager->isRunning();
368}
369
370void QQuickSwipePrivate::beginTransition(qreal newPosition)
371{
373 return;
374 Q_Q(QQuickSwipe);
375 if (!transition) {
376 q->setPosition(newPosition);
378 return;
379 }
380
381 if (!transitionManager)
382 transitionManager.reset(new QQuickSwipeTransitionManager(q));
383
384 transitionManager->transition(transition, newPosition);
385}
386
388{
389 Q_Q(QQuickSwipe);
390 waitForTransition = false;
391 q->setComplete(qFuzzyCompare(qAbs(position), qreal(1.0)));
392 if (complete) {
393 emit q->opened();
394 } else {
395 if (qFuzzyIsNull(position))
396 wasComplete = false;
397 emit q->closed();
398 }
399}
400
401QQuickSwipe::QQuickSwipe(QQuickSwipeDelegate *control)
402 : QObject(*(new QQuickSwipePrivate(control)))
403{
404}
405
406QQmlComponent *QQuickSwipe::left() const
407{
408 Q_D(const QQuickSwipe);
409 return d->left;
410}
411
412void QQuickSwipe::setLeft(QQmlComponent *left)
413{
414 Q_D(QQuickSwipe);
415 if (left == d->left)
416 return;
417
418 if (d->behind) {
419 d->warnAboutMixingDelegates();
420 return;
421 }
422
423 if (!qFuzzyIsNull(d->position)) {
424 d->warnAboutSettingDelegatesWhileVisible();
425 return;
426 }
427
428 d->left = left;
429
430 if (!d->left) {
431 delete d->leftItem;
432 d->leftItem = nullptr;
433 }
434
435 d->control->setFiltersChildMouseEvents(d->hasDelegates());
436
437 emit leftChanged();
438}
439
440QQmlComponent *QQuickSwipe::behind() const
441{
442 Q_D(const QQuickSwipe);
443 return d->behind;
444}
445
446void QQuickSwipe::setBehind(QQmlComponent *behind)
447{
448 Q_D(QQuickSwipe);
449 if (behind == d->behind)
450 return;
451
452 if (d->left || d->right) {
453 d->warnAboutMixingDelegates();
454 return;
455 }
456
457 if (!qFuzzyIsNull(d->position)) {
458 d->warnAboutSettingDelegatesWhileVisible();
459 return;
460 }
461
462 d->behind = behind;
463
464 if (!d->behind) {
465 delete d->behindItem;
466 d->behindItem = nullptr;
467 }
468
469 d->control->setFiltersChildMouseEvents(d->hasDelegates());
470
471 emit behindChanged();
472}
473
474QQmlComponent *QQuickSwipe::right() const
475{
476 Q_D(const QQuickSwipe);
477 return d->right;
478}
479
480void QQuickSwipe::setRight(QQmlComponent *right)
481{
482 Q_D(QQuickSwipe);
483 if (right == d->right)
484 return;
485
486 if (d->behind) {
487 d->warnAboutMixingDelegates();
488 return;
489 }
490
491 if (!qFuzzyIsNull(d->position)) {
492 d->warnAboutSettingDelegatesWhileVisible();
493 return;
494 }
495
496 d->right = right;
497
498 if (!d->right) {
499 delete d->rightItem;
500 d->rightItem = nullptr;
501 }
502
503 d->control->setFiltersChildMouseEvents(d->hasDelegates());
504
505 emit rightChanged();
506}
507
508QQuickItem *QQuickSwipe::leftItem() const
509{
510 Q_D(const QQuickSwipe);
511 return d->leftItem;
512}
513
514void QQuickSwipe::setLeftItem(QQuickItem *item)
515{
516 Q_D(QQuickSwipe);
517 if (item == d->leftItem)
518 return;
519
520 delete d->leftItem;
521 d->leftItem = item;
522
523 if (d->leftItem) {
524 d->leftItem->setParentItem(d->control);
525
526 if (qFuzzyIsNull(d->leftItem->z()))
527 d->leftItem->setZ(-2);
528 }
529
530 emit leftItemChanged();
531}
532
533QQuickItem *QQuickSwipe::behindItem() const
534{
535 Q_D(const QQuickSwipe);
536 return d->behindItem;
537}
538
539void QQuickSwipe::setBehindItem(QQuickItem *item)
540{
541 Q_D(QQuickSwipe);
542 if (item == d->behindItem)
543 return;
544
545 delete d->behindItem;
546 d->behindItem = item;
547
548 if (d->behindItem) {
549 d->behindItem->setParentItem(d->control);
550
551 if (qFuzzyIsNull(d->behindItem->z()))
552 d->behindItem->setZ(-2);
553 }
554
555 emit behindItemChanged();
556}
557
558QQuickItem *QQuickSwipe::rightItem() const
559{
560 Q_D(const QQuickSwipe);
561 return d->rightItem;
562}
563
564void QQuickSwipe::setRightItem(QQuickItem *item)
565{
566 Q_D(QQuickSwipe);
567 if (item == d->rightItem)
568 return;
569
570 delete d->rightItem;
571 d->rightItem = item;
572
573 if (d->rightItem) {
574 d->rightItem->setParentItem(d->control);
575
576 if (qFuzzyIsNull(d->rightItem->z()))
577 d->rightItem->setZ(-2);
578 }
579
580 emit rightItemChanged();
581}
582
583qreal QQuickSwipe::position() const
584{
585 Q_D(const QQuickSwipe);
586 return d->position;
587}
588
589void QQuickSwipe::setPosition(qreal position)
590{
591 Q_D(QQuickSwipe);
592 const qreal adjustedPosition = qBound<qreal>(-1.0, position, 1.0);
593 if (adjustedPosition == d->position)
594 return;
595
596 d->position = adjustedPosition;
597 d->reposition(AnimatePosition);
598 emit positionChanged();
599}
600
601bool QQuickSwipe::isComplete() const
602{
603 Q_D(const QQuickSwipe);
604 return d->complete;
605}
606
607void QQuickSwipe::setComplete(bool complete)
608{
609 Q_D(QQuickSwipe);
610 if (complete == d->complete)
611 return;
612
613 d->complete = complete;
614 emit completeChanged();
615 if (d->complete)
616 emit completed();
617}
618
619bool QQuickSwipe::isEnabled() const
620{
621 Q_D(const QQuickSwipe);
622 return d->enabled;
623}
624
625void QQuickSwipe::setEnabled(bool enabled)
626{
627 Q_D(QQuickSwipe);
628 if (enabled == d->enabled)
629 return;
630
631 d->enabled = enabled;
632 emit enabledChanged();
633}
634
635QQuickTransition *QQuickSwipe::transition() const
636{
637 Q_D(const QQuickSwipe);
638 return d->transition;
639}
640
641void QQuickSwipe::setTransition(QQuickTransition *transition)
642{
643 Q_D(QQuickSwipe);
644 if (transition == d->transition)
645 return;
646
647 d->transition = transition;
648 emit transitionChanged();
649}
650
651void QQuickSwipe::open(QQuickSwipeDelegate::Side side)
652{
653 Q_D(QQuickSwipe);
654 if (qFuzzyCompare(qAbs(d->position), qreal(1.0)))
655 return;
656
657 if ((side != QQuickSwipeDelegate::Left && side != QQuickSwipeDelegate::Right)
658 || (!d->left && !d->behind && side == QQuickSwipeDelegate::Left)
659 || (!d->right && !d->behind && side == QQuickSwipeDelegate::Right))
660 return;
661
662 d->beginTransition(side);
663 d->wasComplete = true;
664 d->velocityCalculator.reset();
665 d->positionBeforePress = d->position;
666}
667
668void QQuickSwipe::close()
669{
670 Q_D(QQuickSwipe);
671 if (qFuzzyIsNull(d->position))
672 return;
673
674 if (d->control->isPressed()) {
675 // We don't support closing when we're pressed; release() or clicked() should be used instead.
676 return;
677 }
678
679 d->beginTransition(0.0);
680 d->waitForTransition = true;
681 d->wasComplete = false;
682 d->positionBeforePress = 0.0;
683 d->velocityCalculator.reset();
684}
685
686QQuickSwipeDelegatePrivate::QQuickSwipeDelegatePrivate(QQuickSwipeDelegate *control)
687 : swipe(control)
688{
689}
690
692{
693 if (!background)
694 return;
695
696 resizingBackground = true;
697
698 QQuickItemPrivate *p = QQuickItemPrivate::get(background);
699 const bool extraAllocated = extra.isAllocated();
700 // Don't check for or set the x here since it will just be overwritten by reposition().
701 if (((!p->widthValid() || !extraAllocated || !extra->hasBackgroundWidth))
702 || (extraAllocated && (extra->hasLeftInset || extra->hasRightInset))) {
703 background->setWidth(width - getLeftInset() - getRightInset());
704 }
705 if (((!p->heightValid() || !extraAllocated || !extra->hasBackgroundHeight) && qFuzzyIsNull(background->y()))
706 || (extraAllocated && (extra->hasTopInset || extra->hasBottomInset))) {
707 background->setY(getTopInset());
708 background->setHeight(height - getTopInset() - getBottomInset());
709 }
710
711 resizingBackground = false;
712}
713
714bool QQuickSwipeDelegatePrivate::handleMousePressEvent(QQuickItem *item, QMouseEvent *event)
715{
716 Q_Q(QQuickSwipeDelegate);
717 const auto posInItem = item->mapToItem(q, event->position().toPoint());
718 QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&swipe);
719 // If the position is 0, we want to handle events ourselves - we don't want child items to steal them.
720 // This code will only get called when a child item has been created;
721 // events will go through the regular channels (mousePressEvent()) until then.
722 if (qFuzzyIsNull(swipePrivate->position)) {
723 q->mousePressEvent(event);
724 // The press point could be incorrect if the press happened over a child item,
725 // so we correct it after calling the base class' mousePressEvent(), rather
726 // than having to duplicate its code just so we can set the pressPoint.
727 setPressPoint(posInItem);
728 return true;
729 }
730
731 // If the delegate is swiped open, send the event to the exposed item,
732 // in case it's an interactive child (like a Button).
733 if (swipePrivate->complete)
734 forwardMouseEvent(event, item, posInItem);
735
736 // The position is non-zero, this press could be either for a delegate or the control itself
737 // (the control can be clicked to e.g. close the swipe). Either way, we must begin measuring
738 // mouse movement in case it turns into a swipe, in which case we grab the mouse.
739 swipePrivate->positionBeforePress = swipePrivate->position;
740 swipePrivate->velocityCalculator.startMeasuring(event->position().toPoint(), event->timestamp());
741 setPressPoint(item->mapToItem(q, event->position().toPoint()));
742
743 // When a delegate or any of its children uses the attached properties and signals,
744 // it declares that it wants mouse events.
745 const bool delivered = attachedObjectsSetPressed(item, event->scenePosition(), true);
746 if (delivered)
747 event->accept();
748 return delivered;
749}
750
751bool QQuickSwipeDelegatePrivate::handleMouseMoveEvent(QQuickItem *item, QMouseEvent *event)
752{
753 Q_Q(QQuickSwipeDelegate);
754
755 if (holdTimer > 0) {
756 if (QLineF(pressPoint, event->position()).length() > QGuiApplication::styleHints()->startDragDistance())
757 stopPressAndHold();
758 }
759
760 // The delegate can still be pressed when swipe.enabled is false,
761 // but the mouse moving shouldn't have any effect on swipe.position.
762 QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&swipe);
763 if (!swipePrivate->enabled)
764 return false;
765
766 // Protect against division by zero.
767 if (width == 0)
768 return false;
769
770 // Don't bother reacting to events if we don't have any delegates.
771 if (!swipePrivate->left && !swipePrivate->right && !swipePrivate->behind)
772 return false;
773
774 if (item != q && swipePrivate->complete) {
775 // If the delegate is swiped open, send the event to the exposed item,
776 // in case it's an interactive child (like a Button).
777 const auto posInItem = item->mapToItem(q, event->position().toPoint());
778 forwardMouseEvent(event, item, posInItem);
779 }
780
781 // Don't handle move events for the control if it wasn't pressed.
782 if (item == q && !pressed)
783 return false;
784
785 const qreal distance = (event->globalPosition().x() != qInf() && event->globalPosition().y() != qInf()) ?
786 (item->mapFromGlobal(event->globalPosition()) -
787 item->mapFromGlobal(event->points().first().globalPressPosition())).x() : 0;
788 if (!q->keepMouseGrab()) {
789 // We used to use the custom threshold that QQuickDrawerPrivate::grabMouse used,
790 // but since it's larger than what Flickable uses, it results in Flickable
791 // stealing events from us (QTBUG-50045), so now we use the default.
792 const bool overThreshold = QQuickDeliveryAgentPrivate::dragOverThreshold(distance,
793 Qt::XAxis, event);
794 if (window && overThreshold) {
795 QQuickItem *grabber = q->window()->mouseGrabberItem();
796 if (!grabber || !grabber->keepMouseGrab()) {
797 q->grabMouse();
798 q->setKeepMouseGrab(true);
799 q->setPressed(true);
800 swipe.setComplete(false);
801
802 attachedObjectsSetPressed(item, event->scenePosition(), false, true);
803 }
804 }
805 }
806
807 if (q->keepMouseGrab()) {
808 // Ensure we don't try to calculate a position when the user tried to drag
809 // to the left when the left item is already exposed, and vice versa.
810 // The code below assumes that the drag is valid, so if we don't have this check,
811 // the wrong items are visible and the swiping wraps.
812 if (swipePrivate->behind
813 || ((swipePrivate->left || swipePrivate->right)
814 && (qFuzzyIsNull(swipePrivate->positionBeforePress)
815 || (swipePrivate->positionBeforePress == -1.0 && distance >= 0.0)
816 || (swipePrivate->positionBeforePress == 1.0 && distance <= 0.0)))) {
817
818 // We must instantiate the items here so that we can calculate the
819 // position against the width of the relevant item.
820 QQuickItem *relevantItem = swipePrivate->createRelevantItemForDistance(distance);
821 // If there isn't any relevant item, the user may have swiped back to the 0 position,
822 // or they swiped back to a position that is equal to positionBeforePress.
823 const qreal normalizedDistance = relevantItem ? distance / relevantItem->width() : 0.0;
824 qreal position = 0;
825
826 // If the control was exposed before the drag begun, the distance should be inverted.
827 // For example, if the control had been swiped to the right, the position would be 1.0.
828 // If the control was then swiped to the left by a distance of -20 pixels, the normalized
829 // distance might be -0.2, for example, which cannot be used as the position; the swipe
830 // started from the right, so we account for that by adding the position.
831 if (qFuzzyIsNull(normalizedDistance)) {
832 // There are two cases when the normalizedDistance can be 0,
833 // and we must distinguish between them:
834 //
835 // a) The swipe returns to the position that it was at before the press event.
836 // In this case, the distance will be 0.
837 // There would have been many position changes in the meantime, so we can't just
838 // ignore the move event; we have to set position to what it was before the press.
839 //
840 // b) If the position was at, 1.0, for example, and the control was then swiped
841 // to the left by the exact width of the left item, there won't be any relevant item
842 // (because the swipe's position would be at 0.0). In turn, the normalizedDistance
843 // would be 0 (because of the lack of a relevant item), but the distance will be non-zero.
844 position = qFuzzyIsNull(distance) ? swipePrivate->positionBeforePress : 0;
845 } else if (!swipePrivate->wasComplete) {
846 position = normalizedDistance;
847 } else {
848 position = distance > 0 ? normalizedDistance - 1.0 : normalizedDistance + 1.0;
849 }
850
851 if (swipePrivate->isTransitioning())
852 swipePrivate->transitionManager->cancel();
853 swipe.setPosition(position);
854 }
855 } else {
856 // The swipe wasn't initiated.
857 if (event->position().toPoint().y() < 0 || event->position().toPoint().y() > height) {
858 // The mouse went outside the vertical bounds of the control, so
859 // we should no longer consider it pressed.
860 q->setPressed(false);
861 }
862 }
863
864 event->accept();
865
866 return q->keepMouseGrab();
867}
868
869static const qreal exposeVelocityThreshold = 300.0;
870
871bool QQuickSwipeDelegatePrivate::handleMouseReleaseEvent(QQuickItem *item, QMouseEvent *event)
872{
873 Q_Q(QQuickSwipeDelegate);
874 QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&swipe);
875 swipePrivate->velocityCalculator.stopMeasuring(event->position().toPoint(), event->timestamp());
876
877 const bool hadGrabbedMouse = q->keepMouseGrab();
878 q->setKeepMouseGrab(false);
879
880 // QQuickSwipe::close() doesn't allow closing while pressed, but now we're releasing.
881 // So set the pressed state false if this release _could_ result in closing, so that check can be bypassed.
882 if (!qIsNull(swipePrivate->position))
883 q->setPressed(false);
884
885 // Animations for the background and contentItem delegates are typically
886 // only enabled when !control.down, so that the animations aren't running
887 // when the user is swiping. To ensure that the animations are enabled
888 // *before* the positions of these delegates change (via the swipe.setPosition() calls below),
889 // we must cancel the press. QQuickAbstractButton::mouseUngrabEvent() does this
890 // for us, but by then it's too late.
891 if (hadGrabbedMouse) {
892 // TODO: this is copied from QQuickAbstractButton::mouseUngrabEvent().
893 // Eventually it should be moved into a private helper so that we don't have to duplicate it.
894 q->setPressed(false);
895 stopPressRepeat();
896 stopPressAndHold();
897 emit q->canceled();
898 }
899
900 // Inform the given item that the mouse is released, in case it's an interactive child.
901 if (item != q && (swipePrivate->complete || swipePrivate->wasComplete))
902 forwardMouseEvent(event, item, item->mapFromScene(event->scenePosition()));
903
904 // The control can be exposed by either swiping past the halfway mark, or swiping fast enough.
905 const qreal swipeVelocity = swipePrivate->velocityCalculator.velocity().x();
906 if (swipePrivate->position > 0.5 ||
907 (swipePrivate->position > 0.0 && swipeVelocity > exposeVelocityThreshold)) {
908 swipePrivate->beginTransition(1.0);
909 swipePrivate->wasComplete = true;
910 } else if (swipePrivate->position < -0.5 ||
911 (swipePrivate->position < 0.0 && swipeVelocity < -exposeVelocityThreshold)) {
912 swipePrivate->beginTransition(-1.0);
913 swipePrivate->wasComplete = true;
914 } else if (!swipePrivate->isTransitioning()) {
915 // The position is either <= 0.5 or >= -0.5, so the position should go to 0.
916 // However, if the position was already 0 or close to it, we were just clicked,
917 // and we don't need to start a transition.
918 if (!qFuzzyIsNull(swipePrivate->position))
919 swipePrivate->beginTransition(0.0);
920 swipePrivate->wasComplete = false;
921 }
922
923 // Inform any QQuickSwipeDelegateAttached objects that the mouse is released.
924 attachedObjectsSetPressed(item, event->scenePosition(), false);
925
926 // Only consume child events if we had grabbed the mouse.
927 return hadGrabbedMouse;
928}
929
930/*! \internal
931 Send a localized copy of \a event with \a localPos to the \a destination item.
932*/
933void QQuickSwipeDelegatePrivate::forwardMouseEvent(QMouseEvent *event, QQuickItem *destination, QPointF localPos)
934{
935 Q_Q(QQuickSwipeDelegate);
936 QMutableSinglePointEvent localizedEvent(*event);
937 QMutableEventPoint::setPosition(localizedEvent.point(0), localPos);
938 QGuiApplication::sendEvent(destination, &localizedEvent);
939 q->setPressed(!localizedEvent.isAccepted());
940}
941
942/*! \internal
943 For each QQuickSwipeDelegateAttached object on children of \a item:
944 if \a scenePos is in the attachee (the item to which it's attached), then
945 set its \a pressed state. Unless \a cancel is \c true, when the state
946 transitions from pressed to released, also emit \l QQuickSwipeDelegateAttached::clicked().
947 Returns \c true if at least one relevant attached object was found.
948*/
949bool QQuickSwipeDelegatePrivate::attachedObjectsSetPressed(QQuickItem *item, QPointF scenePos, bool pressed, bool cancel)
950{
951 bool found = false;
952 QVarLengthArray<QQuickItem *, 16> itemAndChildren;
953 itemAndChildren.append(item);
954 for (int i = 0; i < itemAndChildren.size(); ++i) {
955 auto item = itemAndChildren.at(i);
956 auto posInItem = item->mapFromScene(scenePos);
957 if (item->contains(posInItem)) {
958 if (Attached *attached = attachedObject(item)) {
959 const bool wasPressed = attached->isPressed();
960 attached->setPressed(pressed);
961 if (wasPressed && !pressed && !cancel)
962 emit attached->clicked();
963 found = true;
964 }
965 }
966 for (auto child : item->childItems())
967 itemAndChildren.append(child);
968 }
969 return found;
970}
971
972static void warnIfHorizontallyAnchored(QQuickItem *item, const QString &itemName)
973{
974 if (!item)
975 return;
976
977 QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors;
978 if (anchors && (anchors->fill() || anchors->centerIn() || anchors->left().item || anchors->right().item)
979 && !item->property("_q_QQuickSwipeDelegate_warned").toBool()) {
980 qmlWarning(item) << QString::fromLatin1("SwipeDelegate: cannot use horizontal anchors with %1; unable to layout the item.").arg(itemName);
981 item->setProperty("_q_QQuickSwipeDelegate_warned", true);
982 }
983}
984
986{
987 warnIfHorizontallyAnchored(background, QStringLiteral("background"));
988 warnIfHorizontallyAnchored(contentItem, QStringLiteral("contentItem"));
989
990 // If the background and contentItem are repositioned due to a swipe,
991 // we don't want to call QQuickControlPrivate's implementation of this function,
992 // as it repositions the contentItem to be visible.
993 // However, we still want to position the contentItem vertically
994 // and resize it (in case the control was resized while open).
995 QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&swipe);
996 if (!swipePrivate->complete) {
997 QQuickItemDelegatePrivate::resizeContent();
998 } else if (contentItem) {
999 Q_Q(QQuickSwipeDelegate);
1000 contentItem->setY(q->topPadding());
1001 contentItem->setWidth(q->availableWidth());
1002 contentItem->setHeight(q->availableHeight());
1003 }
1004}
1005
1007{
1008 return QQuickTheme::palette(QQuickTheme::ListView);
1009}
1010
1011/*! \internal
1012 Recursively search right and/or left item tree of swipe delegate for any item that
1013 contains the \a event position.
1014
1015 Returns the first such item found, otherwise \c nullptr.
1016*/
1017QQuickItem *QQuickSwipeDelegatePrivate::getPressedItem(QQuickItem *childItem, QMouseEvent *event) const
1018{
1019 if (!childItem || !event)
1020 return nullptr;
1021
1022 QQuickItem *item = nullptr;
1023
1024 if (childItem->acceptedMouseButtons() &&
1025 childItem->contains(childItem->mapFromScene(event->scenePosition()))) {
1026 item = childItem;
1027 } else {
1028 const auto &childItems = childItem->childItems();
1029 for (const auto &child: childItems) {
1030 if ((item = getPressedItem(child, event)))
1031 break;
1032 }
1033 }
1034
1035 return item;
1036}
1037
1038QQuickSwipeDelegate::QQuickSwipeDelegate(QQuickItem *parent)
1039 : QQuickItemDelegate(*(new QQuickSwipeDelegatePrivate(this)), parent)
1040{
1041 // QQuickSwipeDelegate still depends on synthesized mouse events
1042 setAcceptTouchEvents(false);
1043}
1044
1045/*!
1046 \since QtQuick.Controls 2.2 (Qt 5.9)
1047 \qmlmethod void QtQuick.Controls::SwipeDelegate::swipe.open(enumeration side)
1048
1049 This method sets the \c position of the swipe so that it opens
1050 from the specified \a side.
1051
1052 Available values:
1053 \value SwipeDelegate.Left The \c position is set to \c 1, which makes the swipe open
1054 from the left. Either \c swipe.left or \c swipe.behind must
1055 have been specified; otherwise the call is ignored.
1056 \value SwipeDelegate.Right The \c position is set to \c -1, which makes the swipe open
1057 from the right. Either \c swipe.right or \c swipe.behind must
1058 have been specified; otherwise the call is ignored.
1059
1060 Any animations defined for the \l {Item::}{x} position of \l {Control::}{contentItem}
1061 and \l {Control::}{background} will be triggered.
1062
1063 \sa swipe, swipe.close()
1064*/
1065
1066/*!
1067 \since QtQuick.Controls 2.1 (Qt 5.8)
1068 \qmlmethod void QtQuick.Controls::SwipeDelegate::swipe.close()
1069
1070 This method sets the \c position of the swipe to \c 0. Any animations
1071 defined for the \l {Item::}{x} position of \l {Control::}{contentItem}
1072 and \l {Control::}{background} will be triggered.
1073
1074 \sa swipe, swipe.open()
1075*/
1076
1077/*!
1078 \since QtQuick.Controls 2.2 (Qt 5.9)
1079 \qmlsignal void QtQuick.Controls::SwipeDelegate::swipe.opened()
1080
1081 This signal is emitted when the delegate has been swiped open
1082 and the transition has finished.
1083
1084 It is useful for performing some action upon completion of a swipe.
1085 For example, it can be used to remove the delegate from the list
1086 that it is in.
1087
1088 \sa swipe, swipe.closed()
1089*/
1090
1091/*!
1092 \since QtQuick.Controls 2.2 (Qt 5.9)
1093 \qmlsignal void QtQuick.Controls::SwipeDelegate::swipe.closed()
1094
1095 This signal is emitted when the delegate has been swiped to closed
1096 and the transition has finished.
1097
1098 It is useful for performing some action upon cancellation of a swipe.
1099 For example, it can be used to cancel the removal of the delegate from
1100 the list that it is in.
1101
1102 \sa swipe, swipe.opened()
1103*/
1104
1105/*!
1106 \since QtQuick.Controls 2.1 (Qt 5.8)
1107 \qmlsignal void QtQuick.Controls::SwipeDelegate::swipe.completed()
1108
1109 This signal is emitted when \c swipe.complete becomes \c true.
1110
1111 It is useful for performing some action upon completion of a swipe.
1112 For example, it can be used to remove the delegate from the list
1113 that it is in.
1114
1115 \sa swipe
1116*/
1117
1118/*!
1119 \qmlproperty real QtQuick.Controls::SwipeDelegate::swipe.position
1120 \qmlproperty bool QtQuick.Controls::SwipeDelegate::swipe.complete
1121 \qmlproperty bool QtQuick.Controls::SwipeDelegate::swipe.enabled
1122 \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.left
1123 \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.behind
1124 \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.right
1125 \qmlproperty Item QtQuick.Controls::SwipeDelegate::swipe.leftItem
1126 \qmlproperty Item QtQuick.Controls::SwipeDelegate::swipe.behindItem
1127 \qmlproperty Item QtQuick.Controls::SwipeDelegate::swipe.rightItem
1128 \qmlproperty Transition QtQuick.Controls::SwipeDelegate::swipe.transition
1129
1130 \table
1131 \header
1132 \li Name
1133 \li Description
1134 \row
1135 \li position
1136 \li This read-only property holds the position of the swipe relative to either
1137 side of the control. When this value reaches either
1138 \c -1.0 (left side) or \c 1.0 (right side) and the mouse button is
1139 released, \c complete will be \c true.
1140 \row
1141 \li complete
1142 \li This read-only property holds whether the control is fully exposed after
1143 having been swiped to the left or right.
1144
1145 When complete is \c true, any interactive items declared in \c left,
1146 \c right, or \c behind will receive mouse events.
1147 \row
1148 \li enabled
1149 \li This property determines whether or not the control can be swiped.
1150
1151 This property was added in QtQuick.Controls 2.2.
1152 \row
1153 \li left
1154 \li This property holds the left delegate.
1155
1156 The left delegate sits behind both \l {Control::}{contentItem} and
1157 \l {Control::}{background}. When the SwipeDelegate is swiped to the right,
1158 this item will be gradually revealed.
1159
1160 \include qquickswipedelegate-interaction.qdocinc
1161 \row
1162 \li behind
1163 \li This property holds the delegate that is shown when the
1164 SwipeDelegate is swiped to both the left and right.
1165
1166 As with the \c left and \c right delegates, it sits behind both
1167 \l {Control::}{contentItem} and \l {Control::}{background}. However, a
1168 SwipeDelegate whose \c behind has been set can be continuously swiped
1169 from either side, and will always show the same item.
1170
1171 \include qquickswipedelegate-interaction.qdocinc
1172 \row
1173 \li right
1174 \li This property holds the right delegate.
1175
1176 The right delegate sits behind both \l {Control::}{contentItem} and
1177 \l {Control::}{background}. When the SwipeDelegate is swiped to the left,
1178 this item will be gradually revealed.
1179
1180 \include qquickswipedelegate-interaction.qdocinc
1181 \row
1182 \li leftItem
1183 \li This read-only property holds the item instantiated from the \c left component.
1184
1185 If \c left has not been set, or the position hasn't changed since
1186 creation of the SwipeDelegate, this property will be \c null.
1187 \row
1188 \li behindItem
1189 \li This read-only property holds the item instantiated from the \c behind component.
1190
1191 If \c behind has not been set, or the position hasn't changed since
1192 creation of the SwipeDelegate, this property will be \c null.
1193 \row
1194 \li rightItem
1195 \li This read-only property holds the item instantiated from the \c right component.
1196
1197 If \c right has not been set, or the position hasn't changed since
1198 creation of the SwipeDelegate, this property will be \c null.
1199 \row
1200 \li transition
1201 \li This property holds the transition that is applied when a swipe is released,
1202 or \l swipe.open() or \l swipe.close() is called.
1203
1204 \snippet qtquickcontrols-swipedelegate-transition.qml 1
1205
1206 This property was added in Qt Quick Controls 2.2.
1207 \endtable
1208
1209 \sa {Control::}{contentItem}, {Control::}{background}, swipe.open(), swipe.close()
1210*/
1211QQuickSwipe *QQuickSwipeDelegate::swipe() const
1212{
1213 Q_D(const QQuickSwipeDelegate);
1214 return const_cast<QQuickSwipe*>(&d->swipe);
1215}
1216
1217QQuickSwipeDelegateAttached *QQuickSwipeDelegate::qmlAttachedProperties(QObject *object)
1218{
1219 return new QQuickSwipeDelegateAttached(object);
1220}
1221
1222static bool isChildOrGrandchildOf(QQuickItem *child, QQuickItem *item)
1223{
1224 return item && (child == item || item->isAncestorOf(child));
1225}
1226
1227bool QQuickSwipeDelegate::childMouseEventFilter(QQuickItem *child, QEvent *event)
1228{
1229 Q_D(QQuickSwipeDelegate);
1230 // The contentItem is, by default, usually a non-interactive item like Text, and
1231 // the same applies to the background. This means that simply stacking the left/right/behind
1232 // items before these items won't allow us to get mouse events when the control is not currently exposed
1233 // but has been previously. Therefore, we instead call setFiltersChildMouseEvents(true) in the constructor
1234 // and filter out child events only when the child is the left/right/behind item.
1235 const QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&d->swipe);
1236 if (!isChildOrGrandchildOf(child, swipePrivate->leftItem) && !isChildOrGrandchildOf(child, swipePrivate->behindItem)
1237 && !isChildOrGrandchildOf(child, swipePrivate->rightItem)) {
1238 return false;
1239 }
1240
1241 switch (event->type()) {
1242 case QEvent::MouseButtonPress: {
1243 return d->handleMousePressEvent(child, static_cast<QMouseEvent *>(event));
1244 } case QEvent::MouseMove: {
1245 return d->handleMouseMoveEvent(child, static_cast<QMouseEvent *>(event));
1246 } case QEvent::MouseButtonRelease: {
1247 // Make sure that the control gets release events if it has created child
1248 // items that are stealing events from it.
1249 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
1250 QQuickItemDelegate::mouseReleaseEvent(mouseEvent);
1251 return d->handleMouseReleaseEvent(child, mouseEvent);
1252 } case QEvent::UngrabMouse: {
1253 // If the mouse was pressed over e.g. rightItem and then dragged down,
1254 // the ListView would eventually grab the mouse, at which point we must
1255 // clear the pressed flag so that it doesn't stay pressed after the release.
1256 Attached *attached = attachedObject(child);
1257 if (attached)
1258 attached->setPressed(false);
1259 return false;
1260 } default:
1261 return false;
1262 }
1263}
1264
1265// We only override this to set positionBeforePress;
1266// otherwise, it's the same as the base class implementation.
1267void QQuickSwipeDelegate::mousePressEvent(QMouseEvent *event)
1268{
1269 Q_D(QQuickSwipeDelegate);
1270 QQuickItemDelegate::mousePressEvent(event);
1271
1272 QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&d->swipe);
1273 if (!swipePrivate->enabled)
1274 return;
1275
1276 swipePrivate->positionBeforePress = swipePrivate->position;
1277 swipePrivate->velocityCalculator.startMeasuring(event->position().toPoint(), event->timestamp());
1278
1279 if (swipePrivate->complete) {
1280 d->pressedItem = d->getPressedItem(d->swipe.rightItem(), event);
1281 if (!d->pressedItem)
1282 d->pressedItem = d->getPressedItem(d->swipe.leftItem(), event);
1283 if (d->pressedItem)
1284 d->handleMousePressEvent(d->pressedItem, event);
1285 }
1286}
1287
1288void QQuickSwipeDelegate::mouseMoveEvent(QMouseEvent *event)
1289{
1290 Q_D(QQuickSwipeDelegate);
1291 if (filtersChildMouseEvents())
1292 d->handleMouseMoveEvent(this, event);
1293 else
1294 QQuickItemDelegate::mouseMoveEvent(event);
1295 if (d->pressedItem)
1296 d->handleMouseMoveEvent(d->pressedItem, event);
1297}
1298
1299void QQuickSwipeDelegate::mouseReleaseEvent(QMouseEvent *event)
1300{
1301 Q_D(QQuickSwipeDelegate);
1302 if (!filtersChildMouseEvents() || !d->handleMouseReleaseEvent(this, event))
1303 QQuickItemDelegate::mouseReleaseEvent(event);
1304
1305 if (d->pressedItem) {
1306 if (d->pressedItem->acceptedMouseButtons())
1307 d->handleMouseReleaseEvent(d->pressedItem, event);
1308 d->pressedItem = nullptr;
1309 }
1310}
1311
1312void QQuickSwipeDelegate::mouseUngrabEvent()
1313{
1314 Q_D(QQuickSwipeDelegate);
1315 setPressed(false);
1316
1317 auto item = d->swipe.rightItem();
1318 if (item) {
1319 if (auto control = qmlobject_cast<QQuickControl *>(item))
1320 QQuickControlPrivate::get(control)->handleUngrab();
1321 Attached *attached = attachedObject(item);
1322 if (attached)
1323 attached->setPressed(false);
1324 } else {
1325 item = d->swipe.leftItem();
1326 if (item) {
1327 if (auto control = qmlobject_cast<QQuickControl *>(item))
1328 QQuickControlPrivate::get(control)->handleUngrab();
1329 Attached *attached = attachedObject(item);
1330 if (attached)
1331 attached->setPressed(false);
1332 }
1333 }
1334
1335 d->pressedItem = nullptr;
1336}
1337
1338void QQuickSwipeDelegate::touchEvent(QTouchEvent *event)
1339{
1340 // Don't allow QQuickControl accept the touch event, because QQuickSwipeDelegate
1341 // is still based on synthesized mouse events
1342 event->ignore();
1343}
1344
1345void QQuickSwipeDelegate::componentComplete()
1346{
1347 Q_D(QQuickSwipeDelegate);
1348 QQuickItemDelegate::componentComplete();
1349 QQuickSwipePrivate::get(&d->swipe)->reposition(DontAnimatePosition);
1350}
1351
1352void QQuickSwipeDelegate::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
1353{
1354 Q_D(QQuickSwipeDelegate);
1355 QQuickControl::geometryChange(newGeometry, oldGeometry);
1356
1357 if (isComponentComplete() && !qFuzzyCompare(newGeometry.width(), oldGeometry.width())) {
1358 QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(&d->swipe);
1359 swipePrivate->reposition(DontAnimatePosition);
1360 }
1361}
1362
1363QFont QQuickSwipeDelegate::defaultFont() const
1364{
1365 return QQuickTheme::font(QQuickTheme::ListView);
1366}
1367
1368#if QT_CONFIG(accessibility)
1369QAccessible::Role QQuickSwipeDelegate::accessibleRole() const
1370{
1371 return QAccessible::ListItem;
1372}
1373#endif
1374
1376{
1377 Q_DECLARE_PUBLIC(QQuickSwipeDelegateAttached)
1378
1379public:
1380 // True when left/right/behind is non-interactive and is pressed.
1381 bool pressed = false;
1382};
1383
1384/*!
1385 \since QtQuick.Controls 2.1 (Qt 5.8)
1386 \qmlattachedsignal QtQuick.Controls::SwipeDelegate::clicked()
1387
1388 This signal can be attached to a non-interactive item declared in
1389 \c swipe.left, \c swipe.right, or \c swipe.behind, in order to react to
1390 clicks. Items can only be clicked when \c swipe.complete is \c true.
1391
1392 For interactive controls (such as \l Button) declared in these
1393 items, use their respective \c clicked() signal instead.
1394
1395 To respond to clicks on the SwipeDelegate itself, use its
1396 \l {AbstractButton::}{clicked()} signal.
1397
1398 \note See the documentation for \l pressed for information on
1399 how to use the event-related properties correctly.
1400
1401 \sa pressed
1402*/
1403
1404QQuickSwipeDelegateAttached::QQuickSwipeDelegateAttached(QObject *object)
1405 : QObject(*(new QQuickSwipeDelegateAttachedPrivate), object)
1406{
1407 QQuickItem *item = qobject_cast<QQuickItem *>(object);
1408 if (item) {
1409 // This allows us to be notified when an otherwise non-interactive item
1410 // is pressed and clicked. The alternative is much more more complex:
1411 // iterating through children that contain the event pos and finding
1412 // the first one with an attached object.
1413 item->setAcceptedMouseButtons(Qt::AllButtons);
1414 } else {
1415 qWarning() << "SwipeDelegate attached property must be attached to an object deriving from Item";
1416 }
1417}
1418
1419/*!
1420 \since QtQuick.Controls 2.1 (Qt 5.8)
1421 \qmlattachedproperty bool QtQuick.Controls::SwipeDelegate::pressed
1422 \readonly
1423
1424 This property can be attached to a non-interactive item declared in
1425 \c swipe.left, \c swipe.right, or \c swipe.behind, in order to detect if it
1426 is pressed. Items can only be pressed when \c swipe.complete is \c true.
1427
1428 For example:
1429
1430 \code
1431 swipe.right: Label {
1432 anchors.right: parent.right
1433 height: parent.height
1434 text: "Action"
1435 color: "white"
1436 padding: 12
1437 background: Rectangle {
1438 color: SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato"
1439 }
1440 }
1441 \endcode
1442
1443 It is possible to have multiple items which individually receive mouse and
1444 touch events. For example, to have two actions in the \c swipe.right item,
1445 use the following code:
1446
1447 \code
1448 swipe.right: Row {
1449 anchors.right: parent.right
1450 height: parent.height
1451
1452 Label {
1453 id: moveLabel
1454 text: qsTr("Move")
1455 color: "white"
1456 verticalAlignment: Label.AlignVCenter
1457 padding: 12
1458 height: parent.height
1459
1460 SwipeDelegate.onClicked: console.log("Moving...")
1461
1462 background: Rectangle {
1463 color: moveLabel.SwipeDelegate.pressed ? Qt.darker("#ffbf47", 1.1) : "#ffbf47"
1464 }
1465 }
1466 Label {
1467 id: deleteLabel
1468 text: qsTr("Delete")
1469 color: "white"
1470 verticalAlignment: Label.AlignVCenter
1471 padding: 12
1472 height: parent.height
1473
1474 SwipeDelegate.onClicked: console.log("Deleting...")
1475
1476 background: Rectangle {
1477 color: deleteLabel.SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato"
1478 }
1479 }
1480 }
1481 \endcode
1482
1483 Note how the \c color assignment in each \l {Control::}{background} item
1484 qualifies the attached property with the \c id of the label. This
1485 is important; using the attached properties on an item causes that item
1486 to accept events. Suppose we had left out the \c id in the previous example:
1487
1488 \code
1489 color: SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato"
1490 \endcode
1491
1492 The \l Rectangle background item is a child of the label, so it naturally
1493 receives events before it. In practice, this means that the background
1494 color will change, but the \c onClicked handler in the label will never
1495 get called.
1496
1497 For interactive controls (such as \l Button) declared in these
1498 items, use their respective \c pressed property instead.
1499
1500 For presses on the SwipeDelegate itself, use its
1501 \l {AbstractButton::}{pressed} property.
1502
1503 \sa clicked()
1504*/
1505bool QQuickSwipeDelegateAttached::isPressed() const
1506{
1507 Q_D(const QQuickSwipeDelegateAttached);
1508 return d->pressed;
1509}
1510
1511void QQuickSwipeDelegateAttached::setPressed(bool pressed)
1512{
1513 Q_D(QQuickSwipeDelegateAttached);
1514 if (pressed == d->pressed)
1515 return;
1516
1517 d->pressed = pressed;
1518 emit pressedChanged();
1519}
1520
1521QT_END_NAMESPACE
1522
1523#include "moc_qquickswipe_p.cpp"
1524#include "moc_qquickswipedelegate_p.cpp"
bool handleMouseMoveEvent(QQuickItem *item, QMouseEvent *event)
void forwardMouseEvent(QMouseEvent *event, QQuickItem *destination, QPointF localPos)
QPalette defaultPalette() const override
bool attachedObjectsSetPressed(QQuickItem *item, QPointF scenePos, bool pressed, bool cancel=false)
QQuickItem * getPressedItem(QQuickItem *childItem, QMouseEvent *event) const
bool handleMouseReleaseEvent(QQuickItem *item, QMouseEvent *event)
bool handleMousePressEvent(QQuickItem *item, QMouseEvent *event)
QQuickVelocityCalculator velocityCalculator
QQuickItem * showRelevantItemForPosition(qreal position)
QScopedPointer< QQuickSwipeTransitionManager > transitionManager
void reposition(PositionAnimation animationPolicy)
QQuickTransition * transition
QQuickItem * createDelegateItem(QQmlComponent *component)
QQuickItem * createRelevantItemForDistance(qreal distance)
void beginTransition(qreal position)
QQuickSwipeDelegate * control
QQuickSwipeTransitionManager(QQuickSwipe *swipe)
void transition(QQuickTransition *transition, qreal position)
Combined button and popup list for selecting options.
QQuickSwipeDelegateAttached Attached
Attached * attachedObject(QQuickItem *item)
static bool isChildOrGrandchildOf(QQuickItem *child, QQuickItem *item)
static const qreal exposeVelocityThreshold
static void warnIfHorizontallyAnchored(QQuickItem *item, const QString &itemName)