| 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 | |
| 4 | #include "qquickswipedelegate_p.h" |
| 5 | #include "qquickswipedelegate_p_p.h" |
| 6 | #include "qquickcontrol_p_p.h" |
| 7 | #include "qquickitemdelegate_p_p.h" |
| 8 | #include "qquickvelocitycalculator_p_p.h" |
| 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 | |
| 19 | QT_BEGIN_NAMESPACE |
| 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 | |
| 74 | namespace { |
| 75 | typedef QQuickSwipeDelegateAttached Attached; |
| 76 | |
| 77 | Attached *attachedObject(QQuickItem *item) { |
| 78 | return qobject_cast<Attached*>(object: qmlAttachedPropertiesObject<QQuickSwipeDelegate>(obj: item, create: false)); |
| 79 | } |
| 80 | |
| 81 | enum PositionAnimation { |
| 82 | DontAnimatePosition, |
| 83 | AnimatePosition |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | class QQuickSwipeTransitionManager : public QQuickTransitionManager |
| 88 | { |
| 89 | public: |
| 90 | QQuickSwipeTransitionManager(QQuickSwipe *swipe); |
| 91 | |
| 92 | void transition(QQuickTransition *transition, qreal position); |
| 93 | |
| 94 | protected: |
| 95 | void finished() override; |
| 96 | |
| 97 | private: |
| 98 | QQuickSwipe *m_swipe = nullptr; |
| 99 | }; |
| 100 | |
| 101 | class QQuickSwipePrivate : public QObjectPrivate |
| 102 | { |
| 103 | Q_DECLARE_PUBLIC(QQuickSwipe) |
| 104 | |
| 105 | public: |
| 106 | QQuickSwipePrivate(QQuickSwipeDelegate *control) : control(control) { } |
| 107 | |
| 108 | static QQuickSwipePrivate *get(QQuickSwipe *swipe); |
| 109 | |
| 110 | QQuickItem *createDelegateItem(QQmlComponent *component); |
| 111 | QQuickItem *showRelevantItemForPosition(qreal position); |
| 112 | QQuickItem *createRelevantItemForDistance(qreal distance); |
| 113 | void reposition(PositionAnimation animationPolicy); |
| 114 | void createLeftItem(); |
| 115 | void createBehindItem(); |
| 116 | void createRightItem(); |
| 117 | void createAndShowLeftItem(); |
| 118 | void createAndShowBehindItem(); |
| 119 | void createAndShowRightItem(); |
| 120 | |
| 121 | void warnAboutMixingDelegates(); |
| 122 | void warnAboutSettingDelegatesWhileVisible(); |
| 123 | |
| 124 | bool hasDelegates() const; |
| 125 | |
| 126 | bool isTransitioning() const; |
| 127 | void beginTransition(qreal position); |
| 128 | void finishTransition(); |
| 129 | |
| 130 | QQuickSwipeDelegate *control = nullptr; |
| 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. |
| 133 | qreal positionBeforePress = 0; |
| 134 | qreal position = 0; |
| 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; |
| 141 | QQuickVelocityCalculator velocityCalculator; |
| 142 | QQmlComponent *left = nullptr; |
| 143 | QQmlComponent *behind = nullptr; |
| 144 | QQmlComponent *right = nullptr; |
| 145 | QQuickItem *leftItem = nullptr; |
| 146 | QQuickItem *behindItem = nullptr; |
| 147 | QQuickItem *rightItem = nullptr; |
| 148 | QQuickTransition *transition = nullptr; |
| 149 | QScopedPointer<QQuickSwipeTransitionManager> transitionManager; |
| 150 | }; |
| 151 | |
| 152 | QQuickSwipeTransitionManager::QQuickSwipeTransitionManager(QQuickSwipe *swipe) |
| 153 | : m_swipe(swipe) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | void 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, defaultTarget: m_swipe); |
| 172 | } |
| 173 | |
| 174 | void QQuickSwipeTransitionManager::finished() |
| 175 | { |
| 176 | QQuickSwipePrivate::get(swipe: m_swipe)->finishTransition(); |
| 177 | } |
| 178 | |
| 179 | QQuickSwipePrivate *QQuickSwipePrivate::get(QQuickSwipe *swipe) |
| 180 | { |
| 181 | return swipe->d_func(); |
| 182 | } |
| 183 | |
| 184 | QQuickItem *QQuickSwipePrivate::createDelegateItem(QQmlComponent *component) |
| 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*>(o: 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 | |
| 202 | QQuickItem *QQuickSwipePrivate::showRelevantItemForPosition(qreal position) |
| 203 | { |
| 204 | if (qFuzzyIsNull(d: position)) |
| 205 | return nullptr; |
| 206 | |
| 207 | if (behind) { |
| 208 | createAndShowBehindItem(); |
| 209 | return behindItem; |
| 210 | } |
| 211 | |
| 212 | if (right && position < 0.0) { |
| 213 | createAndShowRightItem(); |
| 214 | return rightItem; |
| 215 | } |
| 216 | |
| 217 | if (left && position > 0.0) { |
| 218 | createAndShowLeftItem(); |
| 219 | return leftItem; |
| 220 | } |
| 221 | |
| 222 | return nullptr; |
| 223 | } |
| 224 | |
| 225 | QQuickItem *QQuickSwipePrivate::createRelevantItemForDistance(qreal distance) |
| 226 | { |
| 227 | if (qFuzzyIsNull(d: distance)) |
| 228 | return nullptr; |
| 229 | |
| 230 | if (behind) { |
| 231 | createBehindItem(); |
| 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(t: distance) > leftItem->width())) /* c) */ { |
| 250 | createRightItem(); |
| 251 | return rightItem; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (left) { |
| 256 | if ((distance > 0.0 && positionBeforePress == 0.0) /* a) */ |
| 257 | || (leftItem && positionBeforePress == 1.0 && qAbs(t: distance) < leftItem->width()) /* b) */ |
| 258 | || (rightItem && positionBeforePress == -1.0 && qAbs(t: distance) > rightItem->width())) /* c) */ { |
| 259 | createLeftItem(); |
| 260 | return leftItem; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
| 267 | void QQuickSwipePrivate::reposition(PositionAnimation animationPolicy) |
| 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(name: "x" , value: contentItemX); |
| 278 | if (QQuickItem *background = control->background()) |
| 279 | background->setProperty(name: "x" , value: 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 | |
| 288 | void QQuickSwipePrivate::createLeftItem() |
| 289 | { |
| 290 | if (!leftItem) { |
| 291 | Q_Q(QQuickSwipe); |
| 292 | q->setLeftItem(createDelegateItem(component: left)); |
| 293 | if (!leftItem) |
| 294 | qmlWarning(me: control) << "Failed to create left item:" << left->errors(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void QQuickSwipePrivate::createBehindItem() |
| 299 | { |
| 300 | if (!behindItem) { |
| 301 | Q_Q(QQuickSwipe); |
| 302 | q->setBehindItem(createDelegateItem(component: behind)); |
| 303 | if (!behindItem) |
| 304 | qmlWarning(me: control) << "Failed to create behind item:" << behind->errors(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void QQuickSwipePrivate::createRightItem() |
| 309 | { |
| 310 | if (!rightItem) { |
| 311 | Q_Q(QQuickSwipe); |
| 312 | q->setRightItem(createDelegateItem(component: right)); |
| 313 | if (!rightItem) |
| 314 | qmlWarning(me: control) << "Failed to create right item:" << right->errors(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void QQuickSwipePrivate::createAndShowLeftItem() |
| 319 | { |
| 320 | createLeftItem(); |
| 321 | |
| 322 | if (leftItem) |
| 323 | leftItem->setVisible(true); |
| 324 | |
| 325 | if (rightItem) |
| 326 | rightItem->setVisible(false); |
| 327 | } |
| 328 | |
| 329 | void QQuickSwipePrivate::createAndShowBehindItem() |
| 330 | { |
| 331 | createBehindItem(); |
| 332 | |
| 333 | if (behindItem) |
| 334 | behindItem->setVisible(true); |
| 335 | } |
| 336 | |
| 337 | void QQuickSwipePrivate::createAndShowRightItem() |
| 338 | { |
| 339 | createRightItem(); |
| 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 | |
| 350 | void QQuickSwipePrivate::warnAboutMixingDelegates() |
| 351 | { |
| 352 | qmlWarning(me: control) << "cannot set both behind and left/right properties" ; |
| 353 | } |
| 354 | |
| 355 | void QQuickSwipePrivate::warnAboutSettingDelegatesWhileVisible() |
| 356 | { |
| 357 | qmlWarning(me: control) << "left/right/behind properties may only be set when swipe.position is 0" ; |
| 358 | } |
| 359 | |
| 360 | bool QQuickSwipePrivate::hasDelegates() const |
| 361 | { |
| 362 | return left || right || behind; |
| 363 | } |
| 364 | |
| 365 | bool QQuickSwipePrivate::isTransitioning() const |
| 366 | { |
| 367 | return transitionManager && transitionManager->isRunning(); |
| 368 | } |
| 369 | |
| 370 | void QQuickSwipePrivate::beginTransition(qreal newPosition) |
| 371 | { |
| 372 | if (waitForTransition) |
| 373 | return; |
| 374 | Q_Q(QQuickSwipe); |
| 375 | if (!transition) { |
| 376 | q->setPosition(newPosition); |
| 377 | finishTransition(); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (!transitionManager) |
| 382 | transitionManager.reset(other: new QQuickSwipeTransitionManager(q)); |
| 383 | |
| 384 | transitionManager->transition(transition, position: newPosition); |
| 385 | } |
| 386 | |
| 387 | void QQuickSwipePrivate::finishTransition() |
| 388 | { |
| 389 | Q_Q(QQuickSwipe); |
| 390 | waitForTransition = false; |
| 391 | q->setComplete(qFuzzyCompare(p1: qAbs(t: position), p2: qreal(1.0))); |
| 392 | if (complete) { |
| 393 | emit q->opened(); |
| 394 | } else { |
| 395 | if (qFuzzyIsNull(d: position)) |
| 396 | wasComplete = false; |
| 397 | emit q->closed(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | QQuickSwipe::QQuickSwipe(QQuickSwipeDelegate *control) |
| 402 | : QObject(*(new QQuickSwipePrivate(control))) |
| 403 | { |
| 404 | } |
| 405 | |
| 406 | QQmlComponent *QQuickSwipe::left() const |
| 407 | { |
| 408 | Q_D(const QQuickSwipe); |
| 409 | return d->left; |
| 410 | } |
| 411 | |
| 412 | void 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: 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 | |
| 440 | QQmlComponent *QQuickSwipe::behind() const |
| 441 | { |
| 442 | Q_D(const QQuickSwipe); |
| 443 | return d->behind; |
| 444 | } |
| 445 | |
| 446 | void 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: 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 | |
| 474 | QQmlComponent *QQuickSwipe::right() const |
| 475 | { |
| 476 | Q_D(const QQuickSwipe); |
| 477 | return d->right; |
| 478 | } |
| 479 | |
| 480 | void 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: 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 | |
| 508 | QQuickItem *QQuickSwipe::leftItem() const |
| 509 | { |
| 510 | Q_D(const QQuickSwipe); |
| 511 | return d->leftItem; |
| 512 | } |
| 513 | |
| 514 | void 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: d->leftItem->z())) |
| 527 | d->leftItem->setZ(-2); |
| 528 | } |
| 529 | |
| 530 | emit leftItemChanged(); |
| 531 | } |
| 532 | |
| 533 | QQuickItem *QQuickSwipe::behindItem() const |
| 534 | { |
| 535 | Q_D(const QQuickSwipe); |
| 536 | return d->behindItem; |
| 537 | } |
| 538 | |
| 539 | void 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: d->behindItem->z())) |
| 552 | d->behindItem->setZ(-2); |
| 553 | } |
| 554 | |
| 555 | emit behindItemChanged(); |
| 556 | } |
| 557 | |
| 558 | QQuickItem *QQuickSwipe::rightItem() const |
| 559 | { |
| 560 | Q_D(const QQuickSwipe); |
| 561 | return d->rightItem; |
| 562 | } |
| 563 | |
| 564 | void 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: d->rightItem->z())) |
| 577 | d->rightItem->setZ(-2); |
| 578 | } |
| 579 | |
| 580 | emit rightItemChanged(); |
| 581 | } |
| 582 | |
| 583 | qreal QQuickSwipe::position() const |
| 584 | { |
| 585 | Q_D(const QQuickSwipe); |
| 586 | return d->position; |
| 587 | } |
| 588 | |
| 589 | void QQuickSwipe::setPosition(qreal position) |
| 590 | { |
| 591 | Q_D(QQuickSwipe); |
| 592 | const qreal adjustedPosition = qBound<qreal>(min: -1.0, val: position, max: 1.0); |
| 593 | if (adjustedPosition == d->position) |
| 594 | return; |
| 595 | |
| 596 | d->position = adjustedPosition; |
| 597 | d->reposition(animationPolicy: AnimatePosition); |
| 598 | emit positionChanged(); |
| 599 | } |
| 600 | |
| 601 | bool QQuickSwipe::isComplete() const |
| 602 | { |
| 603 | Q_D(const QQuickSwipe); |
| 604 | return d->complete; |
| 605 | } |
| 606 | |
| 607 | void 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 | |
| 619 | bool QQuickSwipe::isEnabled() const |
| 620 | { |
| 621 | Q_D(const QQuickSwipe); |
| 622 | return d->enabled; |
| 623 | } |
| 624 | |
| 625 | void 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 | |
| 635 | QQuickTransition *QQuickSwipe::transition() const |
| 636 | { |
| 637 | Q_D(const QQuickSwipe); |
| 638 | return d->transition; |
| 639 | } |
| 640 | |
| 641 | void 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 | |
| 651 | void QQuickSwipe::open(QQuickSwipeDelegate::Side side) |
| 652 | { |
| 653 | Q_D(QQuickSwipe); |
| 654 | if (qFuzzyCompare(p1: qAbs(t: d->position), p2: 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(newPosition: side); |
| 663 | d->wasComplete = true; |
| 664 | d->velocityCalculator.reset(); |
| 665 | d->positionBeforePress = d->position; |
| 666 | } |
| 667 | |
| 668 | void QQuickSwipe::close() |
| 669 | { |
| 670 | Q_D(QQuickSwipe); |
| 671 | if (qFuzzyIsNull(d: 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(newPosition: 0.0); |
| 680 | d->waitForTransition = true; |
| 681 | d->wasComplete = false; |
| 682 | d->positionBeforePress = 0.0; |
| 683 | d->velocityCalculator.reset(); |
| 684 | } |
| 685 | |
| 686 | QQuickSwipeDelegatePrivate::QQuickSwipeDelegatePrivate(QQuickSwipeDelegate *control) |
| 687 | : swipe(control) |
| 688 | { |
| 689 | } |
| 690 | |
| 691 | void QQuickSwipeDelegatePrivate::resizeBackground() |
| 692 | { |
| 693 | if (!background) |
| 694 | return; |
| 695 | |
| 696 | resizingBackground = true; |
| 697 | |
| 698 | QQuickItemPrivate *p = QQuickItemPrivate::get(item: background); |
| 699 | const bool = 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(d: 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 | |
| 714 | bool QQuickSwipeDelegatePrivate::handleMousePressEvent(QQuickItem *item, QMouseEvent *event) |
| 715 | { |
| 716 | Q_Q(QQuickSwipeDelegate); |
| 717 | const auto posInItem = item->mapToItem(item: q, point: event->position().toPoint()); |
| 718 | QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(swipe: &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(d: 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, destination: item, localPos: 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(point1: event->position().toPoint(), timestamp: event->timestamp()); |
| 741 | setPressPoint(item->mapToItem(item: q, point: 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, scenePos: event->scenePosition(), pressed: true); |
| 746 | if (delivered) |
| 747 | event->accept(); |
| 748 | return delivered; |
| 749 | } |
| 750 | |
| 751 | bool 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: &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(item: q, point: event->position().toPoint()); |
| 778 | forwardMouseEvent(event, destination: item, localPos: 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(point: event->globalPosition()) - |
| 787 | item->mapFromGlobal(point: 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 = QQuickWindowPrivate::dragOverThreshold(d: distance, axis: Qt::XAxis, event); |
| 793 | if (window && overThreshold) { |
| 794 | QQuickItem *grabber = q->window()->mouseGrabberItem(); |
| 795 | if (!grabber || !grabber->keepMouseGrab()) { |
| 796 | q->grabMouse(); |
| 797 | q->setKeepMouseGrab(true); |
| 798 | q->setPressed(true); |
| 799 | swipe.setComplete(false); |
| 800 | |
| 801 | attachedObjectsSetPressed(item, scenePos: event->scenePosition(), pressed: false, cancel: true); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (q->keepMouseGrab()) { |
| 807 | // Ensure we don't try to calculate a position when the user tried to drag |
| 808 | // to the left when the left item is already exposed, and vice versa. |
| 809 | // The code below assumes that the drag is valid, so if we don't have this check, |
| 810 | // the wrong items are visible and the swiping wraps. |
| 811 | if (swipePrivate->behind |
| 812 | || ((swipePrivate->left || swipePrivate->right) |
| 813 | && (qFuzzyIsNull(d: swipePrivate->positionBeforePress) |
| 814 | || (swipePrivate->positionBeforePress == -1.0 && distance >= 0.0) |
| 815 | || (swipePrivate->positionBeforePress == 1.0 && distance <= 0.0)))) { |
| 816 | |
| 817 | // We must instantiate the items here so that we can calculate the |
| 818 | // position against the width of the relevant item. |
| 819 | QQuickItem *relevantItem = swipePrivate->createRelevantItemForDistance(distance); |
| 820 | // If there isn't any relevant item, the user may have swiped back to the 0 position, |
| 821 | // or they swiped back to a position that is equal to positionBeforePress. |
| 822 | const qreal normalizedDistance = relevantItem ? distance / relevantItem->width() : 0.0; |
| 823 | qreal position = 0; |
| 824 | |
| 825 | // If the control was exposed before the drag begun, the distance should be inverted. |
| 826 | // For example, if the control had been swiped to the right, the position would be 1.0. |
| 827 | // If the control was then swiped to the left by a distance of -20 pixels, the normalized |
| 828 | // distance might be -0.2, for example, which cannot be used as the position; the swipe |
| 829 | // started from the right, so we account for that by adding the position. |
| 830 | if (qFuzzyIsNull(d: normalizedDistance)) { |
| 831 | // There are two cases when the normalizedDistance can be 0, |
| 832 | // and we must distinguish between them: |
| 833 | // |
| 834 | // a) The swipe returns to the position that it was at before the press event. |
| 835 | // In this case, the distance will be 0. |
| 836 | // There would have been many position changes in the meantime, so we can't just |
| 837 | // ignore the move event; we have to set position to what it was before the press. |
| 838 | // |
| 839 | // b) If the position was at, 1.0, for example, and the control was then swiped |
| 840 | // to the left by the exact width of the left item, there won't be any relevant item |
| 841 | // (because the swipe's position would be at 0.0). In turn, the normalizedDistance |
| 842 | // would be 0 (because of the lack of a relevant item), but the distance will be non-zero. |
| 843 | position = qFuzzyIsNull(d: distance) ? swipePrivate->positionBeforePress : 0; |
| 844 | } else if (!swipePrivate->wasComplete) { |
| 845 | position = normalizedDistance; |
| 846 | } else { |
| 847 | position = distance > 0 ? normalizedDistance - 1.0 : normalizedDistance + 1.0; |
| 848 | } |
| 849 | |
| 850 | if (swipePrivate->isTransitioning()) |
| 851 | swipePrivate->transitionManager->cancel(); |
| 852 | swipe.setPosition(position); |
| 853 | } |
| 854 | } else { |
| 855 | // The swipe wasn't initiated. |
| 856 | if (event->position().toPoint().y() < 0 || event->position().toPoint().y() > height) { |
| 857 | // The mouse went outside the vertical bounds of the control, so |
| 858 | // we should no longer consider it pressed. |
| 859 | q->setPressed(false); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | event->accept(); |
| 864 | |
| 865 | return q->keepMouseGrab(); |
| 866 | } |
| 867 | |
| 868 | static const qreal exposeVelocityThreshold = 300.0; |
| 869 | |
| 870 | bool QQuickSwipeDelegatePrivate::handleMouseReleaseEvent(QQuickItem *item, QMouseEvent *event) |
| 871 | { |
| 872 | Q_Q(QQuickSwipeDelegate); |
| 873 | QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(swipe: &swipe); |
| 874 | swipePrivate->velocityCalculator.stopMeasuring(m_point2: event->position().toPoint(), timestamp: event->timestamp()); |
| 875 | |
| 876 | const bool hadGrabbedMouse = q->keepMouseGrab(); |
| 877 | q->setKeepMouseGrab(false); |
| 878 | |
| 879 | // QQuickSwipe::close() doesn't allow closing while pressed, but now we're releasing. |
| 880 | // So set the pressed state false if this release _could_ result in closing, so that check can be bypassed. |
| 881 | if (!qIsNull(d: swipePrivate->position)) |
| 882 | q->setPressed(false); |
| 883 | |
| 884 | // Animations for the background and contentItem delegates are typically |
| 885 | // only enabled when !control.down, so that the animations aren't running |
| 886 | // when the user is swiping. To ensure that the animations are enabled |
| 887 | // *before* the positions of these delegates change (via the swipe.setPosition() calls below), |
| 888 | // we must cancel the press. QQuickAbstractButton::mouseUngrabEvent() does this |
| 889 | // for us, but by then it's too late. |
| 890 | if (hadGrabbedMouse) { |
| 891 | // TODO: this is copied from QQuickAbstractButton::mouseUngrabEvent(). |
| 892 | // Eventually it should be moved into a private helper so that we don't have to duplicate it. |
| 893 | q->setPressed(false); |
| 894 | stopPressRepeat(); |
| 895 | stopPressAndHold(); |
| 896 | emit q->canceled(); |
| 897 | } |
| 898 | |
| 899 | // Inform the given item that the mouse is released, in case it's an interactive child. |
| 900 | if (item != q && (swipePrivate->complete || swipePrivate->wasComplete)) |
| 901 | forwardMouseEvent(event, destination: item, localPos: item->mapFromScene(point: event->scenePosition())); |
| 902 | |
| 903 | // The control can be exposed by either swiping past the halfway mark, or swiping fast enough. |
| 904 | const qreal swipeVelocity = swipePrivate->velocityCalculator.velocity().x(); |
| 905 | if (swipePrivate->position > 0.5 || |
| 906 | (swipePrivate->position > 0.0 && swipeVelocity > exposeVelocityThreshold)) { |
| 907 | swipePrivate->beginTransition(newPosition: 1.0); |
| 908 | swipePrivate->wasComplete = true; |
| 909 | } else if (swipePrivate->position < -0.5 || |
| 910 | (swipePrivate->position < 0.0 && swipeVelocity < -exposeVelocityThreshold)) { |
| 911 | swipePrivate->beginTransition(newPosition: -1.0); |
| 912 | swipePrivate->wasComplete = true; |
| 913 | } else if (!swipePrivate->isTransitioning()) { |
| 914 | // The position is either <= 0.5 or >= -0.5, so the position should go to 0. |
| 915 | // However, if the position was already 0 or close to it, we were just clicked, |
| 916 | // and we don't need to start a transition. |
| 917 | if (!qFuzzyIsNull(d: swipePrivate->position)) |
| 918 | swipePrivate->beginTransition(newPosition: 0.0); |
| 919 | swipePrivate->wasComplete = false; |
| 920 | } |
| 921 | |
| 922 | // Inform any QQuickSwipeDelegateAttached objects that the mouse is released. |
| 923 | attachedObjectsSetPressed(item, scenePos: event->scenePosition(), pressed: false); |
| 924 | |
| 925 | // Only consume child events if we had grabbed the mouse. |
| 926 | return hadGrabbedMouse; |
| 927 | } |
| 928 | |
| 929 | /*! \internal |
| 930 | Send a localized copy of \a event with \a localPos to the \a destination item. |
| 931 | */ |
| 932 | void QQuickSwipeDelegatePrivate::forwardMouseEvent(QMouseEvent *event, QQuickItem *destination, QPointF localPos) |
| 933 | { |
| 934 | Q_Q(QQuickSwipeDelegate); |
| 935 | QMutableSinglePointEvent localizedEvent(*event); |
| 936 | QMutableEventPoint::setPosition(p&: localizedEvent.point(i: 0), arg: localPos); |
| 937 | QGuiApplication::sendEvent(receiver: destination, event: &localizedEvent); |
| 938 | q->setPressed(!localizedEvent.isAccepted()); |
| 939 | } |
| 940 | |
| 941 | /*! \internal |
| 942 | For each QQuickSwipeDelegateAttached object on children of \a item: |
| 943 | if \a scenePos is in the attachee (the item to which it's attached), then |
| 944 | set its \a pressed state. Unless \a cancel is \c true, when the state |
| 945 | transitions from pressed to released, also emit \l QQuickSwipeDelegateAttached::clicked(). |
| 946 | Returns \c true if at least one relevant attached object was found. |
| 947 | */ |
| 948 | bool QQuickSwipeDelegatePrivate::attachedObjectsSetPressed(QQuickItem *item, QPointF scenePos, bool pressed, bool cancel) |
| 949 | { |
| 950 | bool found = false; |
| 951 | QVarLengthArray<QQuickItem *, 16> itemAndChildren; |
| 952 | itemAndChildren.append(t: item); |
| 953 | for (int i = 0; i < itemAndChildren.size(); ++i) { |
| 954 | auto item = itemAndChildren.at(idx: i); |
| 955 | auto posInItem = item->mapFromScene(point: scenePos); |
| 956 | if (item->contains(point: posInItem)) { |
| 957 | if (Attached *attached = attachedObject(item)) { |
| 958 | const bool wasPressed = attached->isPressed(); |
| 959 | attached->setPressed(pressed); |
| 960 | if (wasPressed && !pressed && !cancel) |
| 961 | emit attached->clicked(); |
| 962 | found = true; |
| 963 | } |
| 964 | } |
| 965 | for (auto child : item->childItems()) |
| 966 | itemAndChildren.append(t: child); |
| 967 | } |
| 968 | return found; |
| 969 | } |
| 970 | |
| 971 | static void warnIfHorizontallyAnchored(QQuickItem *item, const QString &itemName) |
| 972 | { |
| 973 | if (!item) |
| 974 | return; |
| 975 | |
| 976 | QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors; |
| 977 | if (anchors && (anchors->fill() || anchors->centerIn() || anchors->left().item || anchors->right().item) |
| 978 | && !item->property(name: "_q_QQuickSwipeDelegate_warned" ).toBool()) { |
| 979 | qmlWarning(me: item) << QString::fromLatin1(ba: "SwipeDelegate: cannot use horizontal anchors with %1; unable to layout the item." ).arg(a: itemName); |
| 980 | item->setProperty(name: "_q_QQuickSwipeDelegate_warned" , value: true); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | void QQuickSwipeDelegatePrivate::resizeContent() |
| 985 | { |
| 986 | warnIfHorizontallyAnchored(item: background, QStringLiteral("background" )); |
| 987 | warnIfHorizontallyAnchored(item: contentItem, QStringLiteral("contentItem" )); |
| 988 | |
| 989 | // If the background and contentItem are repositioned due to a swipe, |
| 990 | // we don't want to call QQuickControlPrivate's implementation of this function, |
| 991 | // as it repositions the contentItem to be visible. |
| 992 | // However, we still want to position the contentItem vertically |
| 993 | // and resize it (in case the control was resized while open). |
| 994 | QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(swipe: &swipe); |
| 995 | if (!swipePrivate->complete) { |
| 996 | QQuickItemDelegatePrivate::resizeContent(); |
| 997 | } else if (contentItem) { |
| 998 | Q_Q(QQuickSwipeDelegate); |
| 999 | contentItem->setY(q->topPadding()); |
| 1000 | contentItem->setWidth(q->availableWidth()); |
| 1001 | contentItem->setHeight(q->availableHeight()); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | QPalette QQuickSwipeDelegatePrivate::defaultPalette() const |
| 1006 | { |
| 1007 | return QQuickTheme::palette(scope: QQuickTheme::ListView); |
| 1008 | } |
| 1009 | |
| 1010 | /*! \internal |
| 1011 | Recursively search right and/or left item tree of swipe delegate for any item that |
| 1012 | contains the \a event position. |
| 1013 | |
| 1014 | Returns the first such item found, otherwise \c nullptr. |
| 1015 | */ |
| 1016 | QQuickItem *QQuickSwipeDelegatePrivate::getPressedItem(QQuickItem *childItem, QMouseEvent *event) const |
| 1017 | { |
| 1018 | if (!childItem || !event) |
| 1019 | return nullptr; |
| 1020 | |
| 1021 | QQuickItem *item = nullptr; |
| 1022 | |
| 1023 | if (childItem->acceptedMouseButtons() && |
| 1024 | childItem->contains(point: childItem->mapFromScene(point: event->scenePosition()))) { |
| 1025 | item = childItem; |
| 1026 | } else { |
| 1027 | const auto &childItems = childItem->childItems(); |
| 1028 | for (const auto &child: childItems) { |
| 1029 | if ((item = getPressedItem(childItem: child, event))) |
| 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | return item; |
| 1035 | } |
| 1036 | |
| 1037 | QQuickSwipeDelegate::QQuickSwipeDelegate(QQuickItem *parent) |
| 1038 | : QQuickItemDelegate(*(new QQuickSwipeDelegatePrivate(this)), parent) |
| 1039 | { |
| 1040 | // QQuickSwipeDelegate still depends on synthesized mouse events |
| 1041 | setAcceptTouchEvents(false); |
| 1042 | } |
| 1043 | |
| 1044 | /*! |
| 1045 | \since QtQuick.Controls 2.2 (Qt 5.9) |
| 1046 | \qmlmethod void QtQuick.Controls::SwipeDelegate::swipe.open(enumeration side) |
| 1047 | |
| 1048 | This method sets the \c position of the swipe so that it opens |
| 1049 | from the specified \a side. |
| 1050 | |
| 1051 | Available values: |
| 1052 | \value SwipeDelegate.Left The \c position is set to \c 1, which makes the swipe open |
| 1053 | from the left. Either \c swipe.left or \c swipe.behind must |
| 1054 | have been specified; otherwise the call is ignored. |
| 1055 | \value SwipeDelegate.Right The \c position is set to \c -1, which makes the swipe open |
| 1056 | from the right. Either \c swipe.right or \c swipe.behind must |
| 1057 | have been specified; otherwise the call is ignored. |
| 1058 | |
| 1059 | Any animations defined for the \l {Item::}{x} position of \l {Control::}{contentItem} |
| 1060 | and \l {Control::}{background} will be triggered. |
| 1061 | |
| 1062 | \sa swipe, swipe.close() |
| 1063 | */ |
| 1064 | |
| 1065 | /*! |
| 1066 | \since QtQuick.Controls 2.1 (Qt 5.8) |
| 1067 | \qmlmethod void QtQuick.Controls::SwipeDelegate::swipe.close() |
| 1068 | |
| 1069 | This method sets the \c position of the swipe to \c 0. Any animations |
| 1070 | defined for the \l {Item::}{x} position of \l {Control::}{contentItem} |
| 1071 | and \l {Control::}{background} will be triggered. |
| 1072 | |
| 1073 | \sa swipe, swipe.open() |
| 1074 | */ |
| 1075 | |
| 1076 | /*! |
| 1077 | \since QtQuick.Controls 2.2 (Qt 5.9) |
| 1078 | \qmlsignal void QtQuick.Controls::SwipeDelegate::swipe.opened() |
| 1079 | |
| 1080 | This signal is emitted when the delegate has been swiped open |
| 1081 | and the transition has finished. |
| 1082 | |
| 1083 | It is useful for performing some action upon completion of a swipe. |
| 1084 | For example, it can be used to remove the delegate from the list |
| 1085 | that it is in. |
| 1086 | |
| 1087 | \sa swipe, swipe.closed() |
| 1088 | */ |
| 1089 | |
| 1090 | /*! |
| 1091 | \since QtQuick.Controls 2.2 (Qt 5.9) |
| 1092 | \qmlsignal void QtQuick.Controls::SwipeDelegate::swipe.closed() |
| 1093 | |
| 1094 | This signal is emitted when the delegate has been swiped to closed |
| 1095 | and the transition has finished. |
| 1096 | |
| 1097 | It is useful for performing some action upon cancellation of a swipe. |
| 1098 | For example, it can be used to cancel the removal of the delegate from |
| 1099 | the list that it is in. |
| 1100 | |
| 1101 | \sa swipe, swipe.opened() |
| 1102 | */ |
| 1103 | |
| 1104 | /*! |
| 1105 | \since QtQuick.Controls 2.1 (Qt 5.8) |
| 1106 | \qmlsignal void QtQuick.Controls::SwipeDelegate::swipe.completed() |
| 1107 | |
| 1108 | This signal is emitted when \c swipe.complete becomes \c true. |
| 1109 | |
| 1110 | It is useful for performing some action upon completion of a swipe. |
| 1111 | For example, it can be used to remove the delegate from the list |
| 1112 | that it is in. |
| 1113 | |
| 1114 | \sa swipe |
| 1115 | */ |
| 1116 | |
| 1117 | /*! |
| 1118 | \qmlproperty real QtQuick.Controls::SwipeDelegate::swipe.position |
| 1119 | \qmlproperty bool QtQuick.Controls::SwipeDelegate::swipe.complete |
| 1120 | \qmlproperty bool QtQuick.Controls::SwipeDelegate::swipe.enabled |
| 1121 | \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.left |
| 1122 | \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.behind |
| 1123 | \qmlproperty Component QtQuick.Controls::SwipeDelegate::swipe.right |
| 1124 | \qmlproperty Item QtQuick.Controls::SwipeDelegate::swipe.leftItem |
| 1125 | \qmlproperty Item QtQuick.Controls::SwipeDelegate::swipe.behindItem |
| 1126 | \qmlproperty Item QtQuick.Controls::SwipeDelegate::swipe.rightItem |
| 1127 | \qmlproperty Transition QtQuick.Controls::SwipeDelegate::swipe.transition |
| 1128 | |
| 1129 | \table |
| 1130 | \header |
| 1131 | \li Name |
| 1132 | \li Description |
| 1133 | \row |
| 1134 | \li position |
| 1135 | \li This read-only property holds the position of the swipe relative to either |
| 1136 | side of the control. When this value reaches either |
| 1137 | \c -1.0 (left side) or \c 1.0 (right side) and the mouse button is |
| 1138 | released, \c complete will be \c true. |
| 1139 | \row |
| 1140 | \li complete |
| 1141 | \li This read-only property holds whether the control is fully exposed after |
| 1142 | having been swiped to the left or right. |
| 1143 | |
| 1144 | When complete is \c true, any interactive items declared in \c left, |
| 1145 | \c right, or \c behind will receive mouse events. |
| 1146 | \row |
| 1147 | \li enabled |
| 1148 | \li This property determines whether or not the control can be swiped. |
| 1149 | |
| 1150 | This property was added in QtQuick.Controls 2.2. |
| 1151 | \row |
| 1152 | \li left |
| 1153 | \li This property holds the left delegate. |
| 1154 | |
| 1155 | The left delegate sits behind both \l {Control::}{contentItem} and |
| 1156 | \l {Control::}{background}. When the SwipeDelegate is swiped to the right, |
| 1157 | this item will be gradually revealed. |
| 1158 | |
| 1159 | \include qquickswipedelegate-interaction.qdocinc |
| 1160 | \row |
| 1161 | \li behind |
| 1162 | \li This property holds the delegate that is shown when the |
| 1163 | SwipeDelegate is swiped to both the left and right. |
| 1164 | |
| 1165 | As with the \c left and \c right delegates, it sits behind both |
| 1166 | \l {Control::}{contentItem} and \l {Control::}{background}. However, a |
| 1167 | SwipeDelegate whose \c behind has been set can be continuously swiped |
| 1168 | from either side, and will always show the same item. |
| 1169 | |
| 1170 | \include qquickswipedelegate-interaction.qdocinc |
| 1171 | \row |
| 1172 | \li right |
| 1173 | \li This property holds the right delegate. |
| 1174 | |
| 1175 | The right delegate sits behind both \l {Control::}{contentItem} and |
| 1176 | \l {Control::}{background}. When the SwipeDelegate is swiped to the left, |
| 1177 | this item will be gradually revealed. |
| 1178 | |
| 1179 | \include qquickswipedelegate-interaction.qdocinc |
| 1180 | \row |
| 1181 | \li leftItem |
| 1182 | \li This read-only property holds the item instantiated from the \c left component. |
| 1183 | |
| 1184 | If \c left has not been set, or the position hasn't changed since |
| 1185 | creation of the SwipeDelegate, this property will be \c null. |
| 1186 | \row |
| 1187 | \li behindItem |
| 1188 | \li This read-only property holds the item instantiated from the \c behind component. |
| 1189 | |
| 1190 | If \c behind has not been set, or the position hasn't changed since |
| 1191 | creation of the SwipeDelegate, this property will be \c null. |
| 1192 | \row |
| 1193 | \li rightItem |
| 1194 | \li This read-only property holds the item instantiated from the \c right component. |
| 1195 | |
| 1196 | If \c right has not been set, or the position hasn't changed since |
| 1197 | creation of the SwipeDelegate, this property will be \c null. |
| 1198 | \row |
| 1199 | \li transition |
| 1200 | \li This property holds the transition that is applied when a swipe is released, |
| 1201 | or \l swipe.open() or \l swipe.close() is called. |
| 1202 | |
| 1203 | \snippet qtquickcontrols-swipedelegate-transition.qml 1 |
| 1204 | |
| 1205 | This property was added in Qt Quick Controls 2.2. |
| 1206 | \endtable |
| 1207 | |
| 1208 | \sa {Control::}{contentItem}, {Control::}{background}, swipe.open(), swipe.close() |
| 1209 | */ |
| 1210 | QQuickSwipe *QQuickSwipeDelegate::swipe() const |
| 1211 | { |
| 1212 | Q_D(const QQuickSwipeDelegate); |
| 1213 | return const_cast<QQuickSwipe*>(&d->swipe); |
| 1214 | } |
| 1215 | |
| 1216 | QQuickSwipeDelegateAttached *QQuickSwipeDelegate::qmlAttachedProperties(QObject *object) |
| 1217 | { |
| 1218 | return new QQuickSwipeDelegateAttached(object); |
| 1219 | } |
| 1220 | |
| 1221 | static bool isChildOrGrandchildOf(QQuickItem *child, QQuickItem *item) |
| 1222 | { |
| 1223 | return item && (child == item || item->isAncestorOf(child)); |
| 1224 | } |
| 1225 | |
| 1226 | bool QQuickSwipeDelegate::childMouseEventFilter(QQuickItem *child, QEvent *event) |
| 1227 | { |
| 1228 | Q_D(QQuickSwipeDelegate); |
| 1229 | // The contentItem is, by default, usually a non-interactive item like Text, and |
| 1230 | // the same applies to the background. This means that simply stacking the left/right/behind |
| 1231 | // items before these items won't allow us to get mouse events when the control is not currently exposed |
| 1232 | // but has been previously. Therefore, we instead call setFiltersChildMouseEvents(true) in the constructor |
| 1233 | // and filter out child events only when the child is the left/right/behind item. |
| 1234 | const QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(swipe: &d->swipe); |
| 1235 | if (!isChildOrGrandchildOf(child, item: swipePrivate->leftItem) && !isChildOrGrandchildOf(child, item: swipePrivate->behindItem) |
| 1236 | && !isChildOrGrandchildOf(child, item: swipePrivate->rightItem)) { |
| 1237 | return false; |
| 1238 | } |
| 1239 | |
| 1240 | switch (event->type()) { |
| 1241 | case QEvent::MouseButtonPress: { |
| 1242 | return d->handleMousePressEvent(item: child, event: static_cast<QMouseEvent *>(event)); |
| 1243 | } case QEvent::MouseMove: { |
| 1244 | return d->handleMouseMoveEvent(item: child, event: static_cast<QMouseEvent *>(event)); |
| 1245 | } case QEvent::MouseButtonRelease: { |
| 1246 | // Make sure that the control gets release events if it has created child |
| 1247 | // items that are stealing events from it. |
| 1248 | QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); |
| 1249 | QQuickItemDelegate::mouseReleaseEvent(event: mouseEvent); |
| 1250 | return d->handleMouseReleaseEvent(item: child, event: mouseEvent); |
| 1251 | } case QEvent::UngrabMouse: { |
| 1252 | // If the mouse was pressed over e.g. rightItem and then dragged down, |
| 1253 | // the ListView would eventually grab the mouse, at which point we must |
| 1254 | // clear the pressed flag so that it doesn't stay pressed after the release. |
| 1255 | Attached *attached = attachedObject(item: child); |
| 1256 | if (attached) |
| 1257 | attached->setPressed(false); |
| 1258 | return false; |
| 1259 | } default: |
| 1260 | return false; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | // We only override this to set positionBeforePress; |
| 1265 | // otherwise, it's the same as the base class implementation. |
| 1266 | void QQuickSwipeDelegate::mousePressEvent(QMouseEvent *event) |
| 1267 | { |
| 1268 | Q_D(QQuickSwipeDelegate); |
| 1269 | QQuickItemDelegate::mousePressEvent(event); |
| 1270 | |
| 1271 | QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(swipe: &d->swipe); |
| 1272 | if (!swipePrivate->enabled) |
| 1273 | return; |
| 1274 | |
| 1275 | swipePrivate->positionBeforePress = swipePrivate->position; |
| 1276 | swipePrivate->velocityCalculator.startMeasuring(point1: event->position().toPoint(), timestamp: event->timestamp()); |
| 1277 | |
| 1278 | if (swipePrivate->complete) { |
| 1279 | d->pressedItem = d->getPressedItem(childItem: d->swipe.rightItem(), event); |
| 1280 | if (!d->pressedItem) |
| 1281 | d->pressedItem = d->getPressedItem(childItem: d->swipe.leftItem(), event); |
| 1282 | if (d->pressedItem) |
| 1283 | d->handleMousePressEvent(item: d->pressedItem, event); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | void QQuickSwipeDelegate::mouseMoveEvent(QMouseEvent *event) |
| 1288 | { |
| 1289 | Q_D(QQuickSwipeDelegate); |
| 1290 | if (filtersChildMouseEvents()) |
| 1291 | d->handleMouseMoveEvent(item: this, event); |
| 1292 | else |
| 1293 | QQuickItemDelegate::mouseMoveEvent(event); |
| 1294 | if (d->pressedItem) |
| 1295 | d->handleMouseMoveEvent(item: d->pressedItem, event); |
| 1296 | } |
| 1297 | |
| 1298 | void QQuickSwipeDelegate::mouseReleaseEvent(QMouseEvent *event) |
| 1299 | { |
| 1300 | Q_D(QQuickSwipeDelegate); |
| 1301 | if (!filtersChildMouseEvents() || !d->handleMouseReleaseEvent(item: this, event)) |
| 1302 | QQuickItemDelegate::mouseReleaseEvent(event); |
| 1303 | |
| 1304 | if (d->pressedItem) { |
| 1305 | if (d->pressedItem->acceptedMouseButtons()) |
| 1306 | d->handleMouseReleaseEvent(item: d->pressedItem, event); |
| 1307 | d->pressedItem = nullptr; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | void QQuickSwipeDelegate::mouseUngrabEvent() |
| 1312 | { |
| 1313 | Q_D(QQuickSwipeDelegate); |
| 1314 | setPressed(false); |
| 1315 | |
| 1316 | auto item = d->swipe.rightItem(); |
| 1317 | if (item) { |
| 1318 | if (auto control = qmlobject_cast<QQuickControl *>(object: item)) |
| 1319 | QQuickControlPrivate::get(control)->handleUngrab(); |
| 1320 | Attached *attached = attachedObject(item); |
| 1321 | if (attached) |
| 1322 | attached->setPressed(false); |
| 1323 | } else { |
| 1324 | item = d->swipe.leftItem(); |
| 1325 | if (item) { |
| 1326 | if (auto control = qmlobject_cast<QQuickControl *>(object: item)) |
| 1327 | QQuickControlPrivate::get(control)->handleUngrab(); |
| 1328 | Attached *attached = attachedObject(item); |
| 1329 | if (attached) |
| 1330 | attached->setPressed(false); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | d->pressedItem = nullptr; |
| 1335 | } |
| 1336 | |
| 1337 | void QQuickSwipeDelegate::touchEvent(QTouchEvent *event) |
| 1338 | { |
| 1339 | // Don't allow QQuickControl accept the touch event, because QQuickSwipeDelegate |
| 1340 | // is still based on synthesized mouse events |
| 1341 | event->ignore(); |
| 1342 | } |
| 1343 | |
| 1344 | void QQuickSwipeDelegate::componentComplete() |
| 1345 | { |
| 1346 | Q_D(QQuickSwipeDelegate); |
| 1347 | QQuickItemDelegate::componentComplete(); |
| 1348 | QQuickSwipePrivate::get(swipe: &d->swipe)->reposition(animationPolicy: DontAnimatePosition); |
| 1349 | } |
| 1350 | |
| 1351 | void QQuickSwipeDelegate::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) |
| 1352 | { |
| 1353 | Q_D(QQuickSwipeDelegate); |
| 1354 | QQuickControl::geometryChange(newGeometry, oldGeometry); |
| 1355 | |
| 1356 | if (isComponentComplete() && !qFuzzyCompare(p1: newGeometry.width(), p2: oldGeometry.width())) { |
| 1357 | QQuickSwipePrivate *swipePrivate = QQuickSwipePrivate::get(swipe: &d->swipe); |
| 1358 | swipePrivate->reposition(animationPolicy: DontAnimatePosition); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | QFont QQuickSwipeDelegate::defaultFont() const |
| 1363 | { |
| 1364 | return QQuickTheme::font(scope: QQuickTheme::ListView); |
| 1365 | } |
| 1366 | |
| 1367 | #if QT_CONFIG(accessibility) |
| 1368 | QAccessible::Role QQuickSwipeDelegate::accessibleRole() const |
| 1369 | { |
| 1370 | return QAccessible::ListItem; |
| 1371 | } |
| 1372 | #endif |
| 1373 | |
| 1374 | class QQuickSwipeDelegateAttachedPrivate : public QObjectPrivate |
| 1375 | { |
| 1376 | Q_DECLARE_PUBLIC(QQuickSwipeDelegateAttached) |
| 1377 | |
| 1378 | public: |
| 1379 | // True when left/right/behind is non-interactive and is pressed. |
| 1380 | bool pressed = false; |
| 1381 | }; |
| 1382 | |
| 1383 | /*! |
| 1384 | \since QtQuick.Controls 2.1 (Qt 5.8) |
| 1385 | \qmlattachedsignal QtQuick.Controls::SwipeDelegate::clicked() |
| 1386 | |
| 1387 | This signal can be attached to a non-interactive item declared in |
| 1388 | \c swipe.left, \c swipe.right, or \c swipe.behind, in order to react to |
| 1389 | clicks. Items can only be clicked when \c swipe.complete is \c true. |
| 1390 | |
| 1391 | For interactive controls (such as \l Button) declared in these |
| 1392 | items, use their respective \c clicked() signal instead. |
| 1393 | |
| 1394 | To respond to clicks on the SwipeDelegate itself, use its |
| 1395 | \l {AbstractButton::}{clicked()} signal. |
| 1396 | |
| 1397 | \note See the documentation for \l pressed for information on |
| 1398 | how to use the event-related properties correctly. |
| 1399 | |
| 1400 | \sa pressed |
| 1401 | */ |
| 1402 | |
| 1403 | QQuickSwipeDelegateAttached::QQuickSwipeDelegateAttached(QObject *object) |
| 1404 | : QObject(*(new QQuickSwipeDelegateAttachedPrivate), object) |
| 1405 | { |
| 1406 | QQuickItem *item = qobject_cast<QQuickItem *>(o: object); |
| 1407 | if (item) { |
| 1408 | // This allows us to be notified when an otherwise non-interactive item |
| 1409 | // is pressed and clicked. The alternative is much more more complex: |
| 1410 | // iterating through children that contain the event pos and finding |
| 1411 | // the first one with an attached object. |
| 1412 | item->setAcceptedMouseButtons(Qt::AllButtons); |
| 1413 | } else { |
| 1414 | qWarning() << "Attached properties of SwipeDelegate must be accessed through an Item" ; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /*! |
| 1419 | \since QtQuick.Controls 2.1 (Qt 5.8) |
| 1420 | \qmlattachedproperty bool QtQuick.Controls::SwipeDelegate::pressed |
| 1421 | \readonly |
| 1422 | |
| 1423 | This property can be attached to a non-interactive item declared in |
| 1424 | \c swipe.left, \c swipe.right, or \c swipe.behind, in order to detect if it |
| 1425 | is pressed. Items can only be pressed when \c swipe.complete is \c true. |
| 1426 | |
| 1427 | For example: |
| 1428 | |
| 1429 | \code |
| 1430 | swipe.right: Label { |
| 1431 | anchors.right: parent.right |
| 1432 | height: parent.height |
| 1433 | text: "Action" |
| 1434 | color: "white" |
| 1435 | padding: 12 |
| 1436 | background: Rectangle { |
| 1437 | color: SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato" |
| 1438 | } |
| 1439 | } |
| 1440 | \endcode |
| 1441 | |
| 1442 | It is possible to have multiple items which individually receive mouse and |
| 1443 | touch events. For example, to have two actions in the \c swipe.right item, |
| 1444 | use the following code: |
| 1445 | |
| 1446 | \code |
| 1447 | swipe.right: Row { |
| 1448 | anchors.right: parent.right |
| 1449 | height: parent.height |
| 1450 | |
| 1451 | Label { |
| 1452 | id: moveLabel |
| 1453 | text: qsTr("Move") |
| 1454 | color: "white" |
| 1455 | verticalAlignment: Label.AlignVCenter |
| 1456 | padding: 12 |
| 1457 | height: parent.height |
| 1458 | |
| 1459 | SwipeDelegate.onClicked: console.log("Moving...") |
| 1460 | |
| 1461 | background: Rectangle { |
| 1462 | color: moveLabel.SwipeDelegate.pressed ? Qt.darker("#ffbf47", 1.1) : "#ffbf47" |
| 1463 | } |
| 1464 | } |
| 1465 | Label { |
| 1466 | id: deleteLabel |
| 1467 | text: qsTr("Delete") |
| 1468 | color: "white" |
| 1469 | verticalAlignment: Label.AlignVCenter |
| 1470 | padding: 12 |
| 1471 | height: parent.height |
| 1472 | |
| 1473 | SwipeDelegate.onClicked: console.log("Deleting...") |
| 1474 | |
| 1475 | background: Rectangle { |
| 1476 | color: deleteLabel.SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato" |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | \endcode |
| 1481 | |
| 1482 | Note how the \c color assignment in each \l {Control::}{background} item |
| 1483 | qualifies the attached property with the \c id of the label. This |
| 1484 | is important; using the attached properties on an item causes that item |
| 1485 | to accept events. Suppose we had left out the \c id in the previous example: |
| 1486 | |
| 1487 | \code |
| 1488 | color: SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato" |
| 1489 | \endcode |
| 1490 | |
| 1491 | The \l Rectangle background item is a child of the label, so it naturally |
| 1492 | receives events before it. In practice, this means that the background |
| 1493 | color will change, but the \c onClicked handler in the label will never |
| 1494 | get called. |
| 1495 | |
| 1496 | For interactive controls (such as \l Button) declared in these |
| 1497 | items, use their respective \c pressed property instead. |
| 1498 | |
| 1499 | For presses on the SwipeDelegate itself, use its |
| 1500 | \l {AbstractButton::}{pressed} property. |
| 1501 | |
| 1502 | \sa clicked() |
| 1503 | */ |
| 1504 | bool QQuickSwipeDelegateAttached::isPressed() const |
| 1505 | { |
| 1506 | Q_D(const QQuickSwipeDelegateAttached); |
| 1507 | return d->pressed; |
| 1508 | } |
| 1509 | |
| 1510 | void QQuickSwipeDelegateAttached::setPressed(bool pressed) |
| 1511 | { |
| 1512 | Q_D(QQuickSwipeDelegateAttached); |
| 1513 | if (pressed == d->pressed) |
| 1514 | return; |
| 1515 | |
| 1516 | d->pressed = pressed; |
| 1517 | emit pressedChanged(); |
| 1518 | } |
| 1519 | |
| 1520 | QT_END_NAMESPACE |
| 1521 | |
| 1522 | #include "moc_qquickswipe_p.cpp" |
| 1523 | #include "moc_qquickswipedelegate_p.cpp" |
| 1524 | |