KDeclarative

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

KDE's Doxygen guidelines are available online.