1 | // Copyright (C) 2018 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 | #ifndef QQUICKDRAGHANDLER_H |
5 | #define QQUICKDRAGHANDLER_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include "qquickmultipointhandler_p.h" |
19 | #include "qquickdragaxis_p.h" |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | using namespace Qt::StringLiterals; |
24 | |
25 | class Q_QUICK_PRIVATE_EXPORT QQuickDragHandler : public QQuickMultiPointHandler |
26 | { |
27 | Q_OBJECT |
28 | Q_PROPERTY(QQuickDragAxis * xAxis READ xAxis CONSTANT FINAL) |
29 | Q_PROPERTY(QQuickDragAxis * yAxis READ yAxis CONSTANT FINAL) |
30 | #if QT_DEPRECATED_SINCE(6, 2) |
31 | Q_PROPERTY(QVector2D translation READ translation NOTIFY translationChanged FINAL) |
32 | #endif |
33 | Q_PROPERTY(QVector2D activeTranslation READ activeTranslation NOTIFY translationChanged REVISION(6, 2) FINAL) |
34 | Q_PROPERTY(QVector2D persistentTranslation READ persistentTranslation WRITE setPersistentTranslation NOTIFY translationChanged REVISION(6, 2) FINAL) |
35 | Q_PROPERTY(SnapMode snapMode READ snapMode WRITE setSnapMode NOTIFY snapModeChanged REVISION(2, 14) FINAL) |
36 | QML_NAMED_ELEMENT(DragHandler) |
37 | QML_ADDED_IN_VERSION(2, 12) |
38 | |
39 | public: |
40 | enum SnapMode { |
41 | NoSnap = 0, |
42 | SnapAuto, |
43 | SnapIfPressedOutsideTarget, |
44 | SnapAlways |
45 | }; |
46 | Q_ENUM(SnapMode) |
47 | |
48 | explicit QQuickDragHandler(QQuickItem *parent = nullptr); |
49 | |
50 | void handlePointerEventImpl(QPointerEvent *event) override; |
51 | |
52 | QQuickDragAxis *xAxis() { return &m_xAxis; } |
53 | QQuickDragAxis *yAxis() { return &m_yAxis; } |
54 | |
55 | #if QT_DEPRECATED_SINCE(6, 2) |
56 | QVector2D translation() const { return activeTranslation(); } |
57 | #endif |
58 | QVector2D activeTranslation() const { return QVector2D(QPointF(m_xAxis.activeValue(), m_yAxis.activeValue())); } |
59 | void setActiveTranslation(const QVector2D &trans); |
60 | QVector2D persistentTranslation() const { return QVector2D(QPointF(m_xAxis.persistentValue(), m_yAxis.persistentValue())); } |
61 | void setPersistentTranslation(const QVector2D &trans); |
62 | QQuickDragHandler::SnapMode snapMode() const; |
63 | void setSnapMode(QQuickDragHandler::SnapMode mode); |
64 | |
65 | Q_SIGNALS: |
66 | void translationChanged(QVector2D delta); |
67 | Q_REVISION(2, 14) void snapModeChanged(); |
68 | |
69 | protected: |
70 | bool wantsPointerEvent(QPointerEvent *event) override; |
71 | void onActiveChanged() override; |
72 | void onGrabChanged(QQuickPointerHandler *grabber, QPointingDevice::GrabTransition transition, QPointerEvent *event, QEventPoint &point) override; |
73 | |
74 | private: |
75 | void ungrab(); |
76 | void enforceAxisConstraints(QPointF *localPos); |
77 | QPointF targetCentroidPosition(); |
78 | |
79 | private: |
80 | QPointF m_pressTargetPos; // We must also store the local targetPos, because we cannot deduce |
81 | // the press target pos from the scene pos in case there was e.g a |
82 | // flick in one of the ancestors during the drag. |
83 | |
84 | QQuickDragAxis m_xAxis = {this, u"x"_s }; |
85 | QQuickDragAxis m_yAxis = {this, u"y"_s }; |
86 | QQuickDragHandler::SnapMode m_snapMode = SnapAuto; |
87 | bool m_pressedInsideTarget = false; |
88 | |
89 | friend class QQuickDragAxis; |
90 | }; |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif // QQUICKDRAGHANDLER_H |
95 | |