diff options
Diffstat (limited to 'src/quick/handlers')
-rw-r--r-- | src/quick/handlers/qquickdraghandler.cpp | 37 | ||||
-rw-r--r-- | src/quick/handlers/qquickdraghandler_p.h | 13 |
2 files changed, 48 insertions, 2 deletions
diff --git a/src/quick/handlers/qquickdraghandler.cpp b/src/quick/handlers/qquickdraghandler.cpp index 22fe3df4d0..61b66beff4 100644 --- a/src/quick/handlers/qquickdraghandler.cpp +++ b/src/quick/handlers/qquickdraghandler.cpp @@ -114,8 +114,14 @@ void QQuickDragHandler::onGrabChanged(QQuickPointerHandler *grabber, QQuickEvent QQuickMultiPointHandler::onGrabChanged(grabber, transition, point); if (grabber == this && transition == QQuickEventPoint::GrabExclusive && target()) { // In case the grab got handed over from another grabber, we might not get the Press. - if (!m_pressedInsideTarget) { - if (target() != parentItem()) + + auto isDescendant = [](QQuickItem *parent, QQuickItem *target) { + return (target != parent) && !target->isAncestorOf(parent); + }; + if (m_snapMode == SnapAlways + || (m_snapMode == SnapIfPressedOutsideTarget && !m_pressedInsideTarget) + || (m_snapMode == SnapAuto && !m_pressedInsideTarget && isDescendant(parentItem(), target())) + ) { m_pressTargetPos = QPointF(target()->width(), target()->height()) / 2; } else if (m_pressTargetPos.isNull()) { m_pressTargetPos = targetCentroidPosition(); @@ -123,6 +129,33 @@ void QQuickDragHandler::onGrabChanged(QQuickPointerHandler *grabber, QQuickEvent } } +/*! + \qmlproperty enumeration QtQuick.DragHandler::snapMode + + This property holds the snap mode. + + The snap mode configures snapping of the \l target item's center to the event point. + + Possible values: + \value DragHandler.SnapNever Never snap + \value DragHandler.SnapAuto The \l target snaps if the event point was pressed outside of the \l target + item \e and the \l target is a descendant of \l parentItem (default) + \value DragHandler.SnapWhenPressedOutsideTarget The \l target snaps if the event point was pressed outside of the \l target + \value DragHandler.SnapAlways Always snap +*/ +QQuickDragHandler::SnapMode QQuickDragHandler::snapMode() const +{ + return m_snapMode; +} + +void QQuickDragHandler::setSnapMode(QQuickDragHandler::SnapMode mode) +{ + if (mode == m_snapMode) + return; + m_snapMode = mode; + emit snapModeChanged(); +} + void QQuickDragHandler::onActiveChanged() { QQuickMultiPointHandler::onActiveChanged(); diff --git a/src/quick/handlers/qquickdraghandler_p.h b/src/quick/handlers/qquickdraghandler_p.h index 748026488a..18c1fd841d 100644 --- a/src/quick/handlers/qquickdraghandler_p.h +++ b/src/quick/handlers/qquickdraghandler_p.h @@ -62,8 +62,17 @@ class Q_QUICK_PRIVATE_EXPORT QQuickDragHandler : public QQuickMultiPointHandler Q_PROPERTY(QQuickDragAxis * xAxis READ xAxis CONSTANT) Q_PROPERTY(QQuickDragAxis * yAxis READ yAxis CONSTANT) Q_PROPERTY(QVector2D translation READ translation NOTIFY translationChanged) + Q_PROPERTY(SnapMode snapMode READ snapMode WRITE setSnapMode NOTIFY snapModeChanged) public: + enum SnapMode { + NoSnap = 0, + SnapAuto, + SnapIfPressedOutsideTarget, + SnapAlways + }; + Q_ENUM(SnapMode) + explicit QQuickDragHandler(QQuickItem *parent = nullptr); void handlePointerEventImpl(QQuickPointerEvent *event) override; @@ -73,11 +82,14 @@ public: QVector2D translation() const { return m_translation; } void setTranslation(const QVector2D &trans); + QQuickDragHandler::SnapMode snapMode() const; + void setSnapMode(QQuickDragHandler::SnapMode mode); void enforceConstraints(); Q_SIGNALS: void translationChanged(); + void snapModeChanged(); protected: void onActiveChanged() override; @@ -97,6 +109,7 @@ private: QQuickDragAxis m_xAxis; QQuickDragAxis m_yAxis; + QQuickDragHandler::SnapMode m_snapMode = SnapAuto; bool m_pressedInsideTarget = false; friend class QQuickDragAxis; |