1 | /* |
2 | SPDX-FileCopyrightText: 2010 BetterInbox <contact@betterinbox.com> |
3 | SPDX-FileContributor: Gregory Schlomoff <greg@betterinbox.com> |
4 | |
5 | SPDX-License-Identifier: MIT |
6 | */ |
7 | |
8 | #ifndef DECLARATIVEDRAGAREA_H |
9 | #define DECLARATIVEDRAGAREA_H |
10 | |
11 | #include "DeclarativeMimeData.h" |
12 | |
13 | #include <QImage> |
14 | #include <QQuickItem> |
15 | #include <QSharedPointer> |
16 | |
17 | class QQmlComponent; |
18 | class QQuickItemGrabResult; |
19 | |
20 | class DeclarativeDragArea : public QQuickItem |
21 | { |
22 | Q_OBJECT |
23 | |
24 | /** |
25 | * The delegate is the item that will be displayed next to the mouse cursor during the drag and drop operation. |
26 | * It usually consists of a large, semi-transparent icon representing the data being dragged. |
27 | */ |
28 | Q_PROPERTY(QQuickItem *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged RESET resetDelegate) |
29 | |
30 | /** |
31 | * The QML element that is the source of the resulting drag and drop operation. This can be defined to any item, and will |
32 | * be available to the DropArea as event.data.source |
33 | */ |
34 | Q_PROPERTY(QQuickItem *source READ source WRITE setSource NOTIFY sourceChanged RESET resetSource) |
35 | |
36 | // TODO: to be implemented |
37 | Q_PROPERTY(QQuickItem *target READ source NOTIFY targetChanged) |
38 | |
39 | /** |
40 | * the mime data of the drag operation |
41 | * @see DeclarativeMimeData |
42 | */ |
43 | Q_PROPERTY(DeclarativeMimeData *mimeData READ mimeData CONSTANT) |
44 | |
45 | /** |
46 | * If false no drag operation will be generate |
47 | */ |
48 | Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) // TODO: Should call setAcceptDrops() |
49 | |
50 | /** |
51 | * Supported operations, a combination of |
52 | * Qt.CopyAction |
53 | * Qt.MoveAction |
54 | * Qt.LinkAction |
55 | * Qt.ActionMask |
56 | * Qt.IgnoreAction |
57 | * Qt.TargetMoveAction |
58 | */ |
59 | Q_PROPERTY(Qt::DropActions supportedActions READ supportedActions WRITE setSupportedActions NOTIFY supportedActionsChanged) |
60 | |
61 | /** |
62 | * The default action will be performed during a drag when no modificators are pressed. |
63 | */ |
64 | Q_PROPERTY(Qt::DropAction defaultAction READ defaultAction WRITE setDefaultAction NOTIFY defaultActionChanged) |
65 | |
66 | /** |
67 | * distance in pixel after which a drag event will get started |
68 | */ |
69 | Q_PROPERTY(int startDragDistance READ startDragDistance WRITE setStartDragDistance NOTIFY startDragDistanceChanged) |
70 | |
71 | /** |
72 | * an image to be used as delegate. if present overrides the delegate property. in can be either a QImage or a QIcon |
73 | */ |
74 | Q_PROPERTY(QVariant delegateImage READ delegateImage WRITE setDelegateImage NOTIFY delegateImageChanged) |
75 | |
76 | /** |
77 | * Whether a drag currently originates from this drag area. |
78 | * |
79 | * @since 5.19 |
80 | */ |
81 | Q_PROPERTY(bool dragActive READ dragActive NOTIFY dragActiveChanged) |
82 | |
83 | public: |
84 | DeclarativeDragArea(QQuickItem *parent = nullptr); |
85 | ~DeclarativeDragArea() override; |
86 | |
87 | QQuickItem *delegate() const; |
88 | void setDelegate(QQuickItem *delegate); |
89 | void resetDelegate(); |
90 | |
91 | QVariant delegateImage() const; |
92 | void setDelegateImage(const QVariant &image); |
93 | QQuickItem *target() const; |
94 | QQuickItem *source() const; |
95 | void setSource(QQuickItem *source); |
96 | void resetSource(); |
97 | |
98 | bool dragActive() const; |
99 | |
100 | bool isEnabled() const; |
101 | void setEnabled(bool enabled); |
102 | |
103 | int startDragDistance() const; |
104 | void setStartDragDistance(int distance); |
105 | |
106 | // supported actions |
107 | Qt::DropActions supportedActions() const; |
108 | void setSupportedActions(Qt::DropActions actions); |
109 | |
110 | // default action |
111 | Qt::DropAction defaultAction() const; |
112 | void setDefaultAction(Qt::DropAction action); |
113 | |
114 | DeclarativeMimeData *mimeData() const; |
115 | |
116 | Q_SIGNALS: |
117 | void dragStarted(); |
118 | void delegateChanged(); |
119 | void dragActiveChanged(); |
120 | void sourceChanged(); |
121 | void targetChanged(); |
122 | void dataChanged(); |
123 | void enabledChanged(); |
124 | void drop(int action); |
125 | void supportedActionsChanged(); |
126 | void defaultActionChanged(); |
127 | void startDragDistanceChanged(); |
128 | void delegateImageChanged(); |
129 | |
130 | protected: |
131 | void mouseMoveEvent(QMouseEvent *event) override; |
132 | void mousePressEvent(QMouseEvent *event) override; |
133 | void mouseReleaseEvent(QMouseEvent *) override; |
134 | void timerEvent(QTimerEvent *event) override; |
135 | bool childMouseEventFilter(QQuickItem *item, QEvent *event) override; |
136 | |
137 | private: |
138 | void startDrag(const QImage &image); |
139 | |
140 | QQuickItem *m_delegate; |
141 | QQuickItem *m_source; |
142 | QQuickItem *m_target; |
143 | QSharedPointer<QQuickItemGrabResult> m_grabResult; |
144 | bool m_enabled; |
145 | bool m_draggingJustStarted; |
146 | bool m_dragActive; |
147 | Qt::DropActions m_supportedActions; |
148 | Qt::DropAction m_defaultAction; |
149 | DeclarativeMimeData *const m_data; |
150 | QImage m_delegateImage; |
151 | int m_startDragDistance; |
152 | QPointF m_buttonDownPos; |
153 | int m_pressAndHoldTimerId; |
154 | }; |
155 | |
156 | #endif // DECLARATIVEDRAGAREA_H |
157 | |