| 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 | /*! |
| 21 | * \qmltype DragArea |
| 22 | * \inqmlmodule org.kde.draganddrop |
| 23 | */ |
| 24 | class DeclarativeDragArea : public QQuickItem |
| 25 | { |
| 26 | Q_OBJECT |
| 27 | QML_NAMED_ELEMENT(DragArea) |
| 28 | |
| 29 | /*! |
| 30 | * \qmlproperty Item DragArea::delegate |
| 31 | * The delegate is the item that will be displayed next to the mouse cursor during the drag and drop operation. |
| 32 | * It usually consists of a large, semi-transparent icon representing the data being dragged. |
| 33 | */ |
| 34 | Q_PROPERTY(QQuickItem *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged RESET resetDelegate) |
| 35 | |
| 36 | /*! |
| 37 | * \qmlproperty Item DragArea::source |
| 38 | * The QML element that is the source of the resulting drag and drop operation. This can be defined to any item, and will |
| 39 | * be available to the DropArea as event.data.source |
| 40 | */ |
| 41 | Q_PROPERTY(QQuickItem *source READ source WRITE setSource NOTIFY sourceChanged RESET resetSource) |
| 42 | |
| 43 | // TODO: to be implemented |
| 44 | /*! |
| 45 | * \qmlproperty Item DragArea::target |
| 46 | */ |
| 47 | Q_PROPERTY(QQuickItem *target READ source NOTIFY targetChanged) |
| 48 | |
| 49 | /*! |
| 50 | * \qmlproperty MimeData DragArea::mimedata |
| 51 | * the mime data of the drag operation |
| 52 | * \sa MimeData |
| 53 | */ |
| 54 | Q_PROPERTY(DeclarativeMimeData *mimeData READ mimeData CONSTANT) |
| 55 | |
| 56 | /*! |
| 57 | * \qmlproperty bool DragArea::enabled |
| 58 | * If false no drag operation will be generate |
| 59 | */ |
| 60 | Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) // TODO: Should call setAcceptDrops() |
| 61 | |
| 62 | /*! |
| 63 | * \qmlproperty Qt::DropActions DragArea::supportedActions |
| 64 | * Supported operations, a combination of: |
| 65 | * \list |
| 66 | * \li Qt.CopyAction |
| 67 | * \li Qt.MoveAction |
| 68 | * \li Qt.LinkAction |
| 69 | * \li Qt.ActionMask |
| 70 | * \li Qt.IgnoreAction |
| 71 | * \li Qt.TargetMoveAction |
| 72 | * \endlist |
| 73 | */ |
| 74 | Q_PROPERTY(Qt::DropActions supportedActions READ supportedActions WRITE setSupportedActions NOTIFY supportedActionsChanged) |
| 75 | |
| 76 | /*! |
| 77 | * \qmlproperty Qt::DropAction DragArea::defaultAction |
| 78 | * |
| 79 | * The default action will be performed during a drag when no modificators are pressed. |
| 80 | */ |
| 81 | Q_PROPERTY(Qt::DropAction defaultAction READ defaultAction WRITE setDefaultAction NOTIFY defaultActionChanged) |
| 82 | |
| 83 | /*! |
| 84 | * \qmlproperty int DragArea::startDragDistance |
| 85 | * distance in pixel after which a drag event will get started |
| 86 | */ |
| 87 | Q_PROPERTY(int startDragDistance READ startDragDistance WRITE setStartDragDistance NOTIFY startDragDistanceChanged) |
| 88 | |
| 89 | /*! |
| 90 | * \qmlproperty var DragArea::delegateImage |
| 91 | * \brief An image to be used as delegate. |
| 92 | * |
| 93 | * If present, it overrides the delegate property. |
| 94 | * |
| 95 | * It can be either a QImage or a QIcon. |
| 96 | */ |
| 97 | Q_PROPERTY(QVariant delegateImage READ delegateImage WRITE setDelegateImage NOTIFY delegateImageChanged) |
| 98 | |
| 99 | /*! |
| 100 | * \qmlproperty bool DragArea::dragActive |
| 101 | * Whether a drag currently originates from this drag area. |
| 102 | * |
| 103 | * \since 5.19 |
| 104 | */ |
| 105 | Q_PROPERTY(bool dragActive READ dragActive NOTIFY dragActiveChanged) |
| 106 | |
| 107 | public: |
| 108 | DeclarativeDragArea(QQuickItem *parent = nullptr); |
| 109 | ~DeclarativeDragArea() override; |
| 110 | |
| 111 | QQuickItem *delegate() const; |
| 112 | void setDelegate(QQuickItem *delegate); |
| 113 | void resetDelegate(); |
| 114 | |
| 115 | QVariant delegateImage() const; |
| 116 | void setDelegateImage(const QVariant &image); |
| 117 | QQuickItem *target() const; |
| 118 | QQuickItem *source() const; |
| 119 | void setSource(QQuickItem *source); |
| 120 | void resetSource(); |
| 121 | |
| 122 | bool dragActive() const; |
| 123 | |
| 124 | bool isEnabled() const; |
| 125 | void setEnabled(bool enabled); |
| 126 | |
| 127 | int startDragDistance() const; |
| 128 | void setStartDragDistance(int distance); |
| 129 | |
| 130 | // supported actions |
| 131 | Qt::DropActions supportedActions() const; |
| 132 | void setSupportedActions(Qt::DropActions actions); |
| 133 | |
| 134 | // default action |
| 135 | Qt::DropAction defaultAction() const; |
| 136 | void setDefaultAction(Qt::DropAction action); |
| 137 | |
| 138 | DeclarativeMimeData *mimeData() const; |
| 139 | |
| 140 | Q_SIGNALS: |
| 141 | /*! \qmlsignal DragArea::dragStarted() */ |
| 142 | void dragStarted(); |
| 143 | /*! \qmlsignal DragArea::delegateChanged() */ |
| 144 | void delegateChanged(); |
| 145 | /*! \qmlsignal DragArea::dragActiveChanged() */ |
| 146 | void dragActiveChanged(); |
| 147 | /*! \qmlsignal DragArea::sourceChanged() */ |
| 148 | void sourceChanged(); |
| 149 | /*! \qmlsignal DragArea::targetChanged() */ |
| 150 | void targetChanged(); |
| 151 | /*! \qmlsignal DragArea::dataChanged() */ |
| 152 | void dataChanged(); |
| 153 | /*! \qmlsignal DragArea::enabledChanged() */ |
| 154 | void enabledChanged(); |
| 155 | /*! \qmlsignal DragArea::drop(int action) */ |
| 156 | void drop(int action); |
| 157 | /*! \qmlsignal DragArea::supportedActionsChanged() */ |
| 158 | void supportedActionsChanged(); |
| 159 | /*! \qmlsignal DragArea::defaultActionChanged() */ |
| 160 | void defaultActionChanged(); |
| 161 | /*! \qmlsignal DragArea::startDragDistanceChanged() */ |
| 162 | void startDragDistanceChanged(); |
| 163 | /*! \qmlsignal DragArea::delegateImageChanged() */ |
| 164 | void delegateImageChanged(); |
| 165 | |
| 166 | protected: |
| 167 | void mouseMoveEvent(QMouseEvent *event) override; |
| 168 | void mousePressEvent(QMouseEvent *event) override; |
| 169 | void mouseReleaseEvent(QMouseEvent *) override; |
| 170 | void timerEvent(QTimerEvent *event) override; |
| 171 | bool childMouseEventFilter(QQuickItem *item, QEvent *event) override; |
| 172 | |
| 173 | private: |
| 174 | void startDrag(const QImage &image); |
| 175 | |
| 176 | QQuickItem *m_delegate; |
| 177 | QQuickItem *m_source; |
| 178 | QQuickItem *m_target; |
| 179 | QSharedPointer<QQuickItemGrabResult> m_grabResult; |
| 180 | bool m_enabled; |
| 181 | bool m_draggingJustStarted; |
| 182 | bool m_dragActive; |
| 183 | Qt::DropActions m_supportedActions; |
| 184 | Qt::DropAction m_defaultAction; |
| 185 | DeclarativeMimeData *const m_data; |
| 186 | QImage m_delegateImage; |
| 187 | int m_startDragDistance; |
| 188 | QPointF m_buttonDownPos; |
| 189 | int m_pressAndHoldTimerId; |
| 190 | }; |
| 191 | |
| 192 | #endif // DECLARATIVEDRAGAREA_H |
| 193 | |