| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QQUICKDRAG_P_H |
| 41 | #define QQUICKDRAG_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtQuick/qquickitem.h> |
| 55 | |
| 56 | #include <private/qintrusivelist_p.h> |
| 57 | #include <private/qqmlguard_p.h> |
| 58 | |
| 59 | #include <QtCore/qmimedata.h> |
| 60 | #include <QtCore/qstringlist.h> |
| 61 | #include <QtCore/qurl.h> |
| 62 | |
| 63 | QT_REQUIRE_CONFIG(quick_draganddrop); |
| 64 | |
| 65 | QT_BEGIN_NAMESPACE |
| 66 | |
| 67 | class QQuickItem; |
| 68 | class QQuickDrag; |
| 69 | class QQuickDragPrivate; |
| 70 | |
| 71 | class QQuickDragGrabber |
| 72 | { |
| 73 | class Item : public QQmlGuard<QQuickItem> |
| 74 | { |
| 75 | public: |
| 76 | Item(QQuickItem *item) : QQmlGuard<QQuickItem>(item) {} |
| 77 | |
| 78 | QIntrusiveListNode node; |
| 79 | protected: |
| 80 | void objectDestroyed(QQuickItem *) override { delete this; } |
| 81 | }; |
| 82 | |
| 83 | typedef QIntrusiveList<Item, &Item::node> ItemList; |
| 84 | |
| 85 | public: |
| 86 | QQuickDragGrabber() : m_target(nullptr) {} |
| 87 | ~QQuickDragGrabber() { while (!m_items.isEmpty()) delete m_items.first(); } |
| 88 | |
| 89 | |
| 90 | QObject *target() const |
| 91 | { |
| 92 | if (m_target) |
| 93 | return m_target; |
| 94 | else if (!m_items.isEmpty()) |
| 95 | return *m_items.first(); |
| 96 | else |
| 97 | return nullptr; |
| 98 | } |
| 99 | void setTarget(QObject *target) { m_target = target; } |
| 100 | void resetTarget() { m_target = nullptr; } |
| 101 | |
| 102 | bool isEmpty() const { return m_items.isEmpty(); } |
| 103 | |
| 104 | typedef ItemList::iterator iterator; |
| 105 | iterator begin() { return m_items.begin(); } |
| 106 | iterator end() { return m_items.end(); } |
| 107 | |
| 108 | void grab(QQuickItem *item) { m_items.insert(n: new Item(item)); } |
| 109 | iterator release(iterator at) { Item *item = *at; at = at.erase(); delete item; return at; } |
| 110 | |
| 111 | private: |
| 112 | |
| 113 | ItemList m_items; |
| 114 | QObject *m_target; |
| 115 | }; |
| 116 | |
| 117 | class QQuickDropEventEx : public QDropEvent |
| 118 | { |
| 119 | public: |
| 120 | void setProposedAction(Qt::DropAction action) { default_action = action; drop_action = action; } |
| 121 | |
| 122 | static void setProposedAction(QDropEvent *event, Qt::DropAction action) { |
| 123 | static_cast<QQuickDropEventEx *>(event)->setProposedAction(action); |
| 124 | } |
| 125 | |
| 126 | void copyActions(const QDropEvent &from) { |
| 127 | default_action = from.proposedAction(); drop_action = from.dropAction(); } |
| 128 | |
| 129 | static void copyActions(QDropEvent *to, const QDropEvent &from) { |
| 130 | static_cast<QQuickDropEventEx *>(to)->copyActions(from); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | class QQuickDragMimeData : public QMimeData |
| 135 | { |
| 136 | Q_OBJECT |
| 137 | public: |
| 138 | QQuickDragMimeData() |
| 139 | : m_source(nullptr) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | QStringList keys() const { return m_keys; } |
| 144 | QObject *source() const { return m_source; } |
| 145 | |
| 146 | private: |
| 147 | QObject *m_source; |
| 148 | Qt::DropActions m_supportedActions; |
| 149 | QStringList m_keys; |
| 150 | |
| 151 | friend class QQuickDragAttached; |
| 152 | friend class QQuickDragAttachedPrivate; |
| 153 | }; |
| 154 | |
| 155 | class QQmlV4Function; |
| 156 | class QQuickDragAttached; |
| 157 | class Q_AUTOTEST_EXPORT QQuickDrag : public QObject |
| 158 | { |
| 159 | Q_OBJECT |
| 160 | |
| 161 | Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged RESET resetTarget) |
| 162 | Q_PROPERTY(Axis axis READ axis WRITE setAxis NOTIFY axisChanged) |
| 163 | Q_PROPERTY(qreal minimumX READ xmin WRITE setXmin NOTIFY minimumXChanged) |
| 164 | Q_PROPERTY(qreal maximumX READ xmax WRITE setXmax NOTIFY maximumXChanged) |
| 165 | Q_PROPERTY(qreal minimumY READ ymin WRITE setYmin NOTIFY minimumYChanged) |
| 166 | Q_PROPERTY(qreal maximumY READ ymax WRITE setYmax NOTIFY maximumYChanged) |
| 167 | Q_PROPERTY(bool active READ active NOTIFY activeChanged) |
| 168 | Q_PROPERTY(bool filterChildren READ filterChildren WRITE setFilterChildren NOTIFY filterChildrenChanged) |
| 169 | Q_PROPERTY(bool smoothed READ smoothed WRITE setSmoothed NOTIFY smoothedChanged) |
| 170 | // Note, threshold was added in QtQuick 2.2 but REVISION is not supported (or needed) for grouped |
| 171 | // properties See QTBUG-33179 |
| 172 | Q_PROPERTY(qreal threshold READ threshold WRITE setThreshold NOTIFY thresholdChanged RESET resetThreshold) |
| 173 | //### consider drag and drop |
| 174 | |
| 175 | QML_NAMED_ELEMENT(Drag) |
| 176 | QML_UNCREATABLE("Drag is only available via attached properties." ) |
| 177 | QML_ATTACHED(QQuickDragAttached) |
| 178 | |
| 179 | public: |
| 180 | QQuickDrag(QObject *parent=nullptr); |
| 181 | ~QQuickDrag(); |
| 182 | |
| 183 | enum DragType { None, Automatic, Internal }; |
| 184 | Q_ENUM(DragType) |
| 185 | |
| 186 | QQuickItem *target() const; |
| 187 | void setTarget(QQuickItem *target); |
| 188 | void resetTarget(); |
| 189 | |
| 190 | enum Axis { XAxis=0x01, YAxis=0x02, XAndYAxis=0x03, XandYAxis=XAndYAxis }; |
| 191 | Q_ENUM(Axis) |
| 192 | Axis axis() const; |
| 193 | void setAxis(Axis); |
| 194 | |
| 195 | qreal xmin() const; |
| 196 | void setXmin(qreal); |
| 197 | qreal xmax() const; |
| 198 | void setXmax(qreal); |
| 199 | qreal ymin() const; |
| 200 | void setYmin(qreal); |
| 201 | qreal ymax() const; |
| 202 | void setYmax(qreal); |
| 203 | |
| 204 | bool smoothed() const; |
| 205 | void setSmoothed(bool smooth); |
| 206 | |
| 207 | qreal threshold() const; |
| 208 | void setThreshold(qreal); |
| 209 | void resetThreshold(); |
| 210 | |
| 211 | bool active() const; |
| 212 | void setActive(bool); |
| 213 | |
| 214 | bool filterChildren() const; |
| 215 | void setFilterChildren(bool); |
| 216 | |
| 217 | static QQuickDragAttached *qmlAttachedProperties(QObject *obj); |
| 218 | |
| 219 | Q_SIGNALS: |
| 220 | void targetChanged(); |
| 221 | void axisChanged(); |
| 222 | void minimumXChanged(); |
| 223 | void maximumXChanged(); |
| 224 | void minimumYChanged(); |
| 225 | void maximumYChanged(); |
| 226 | void activeChanged(); |
| 227 | void filterChildrenChanged(); |
| 228 | void smoothedChanged(); |
| 229 | void thresholdChanged(); |
| 230 | |
| 231 | private: |
| 232 | QQuickItem *_target; |
| 233 | Axis _axis; |
| 234 | qreal _xmin; |
| 235 | qreal _xmax; |
| 236 | qreal _ymin; |
| 237 | qreal _ymax; |
| 238 | bool _active : 1; |
| 239 | bool _filterChildren: 1; |
| 240 | bool _smoothed : 1; |
| 241 | qreal _threshold; |
| 242 | Q_DISABLE_COPY(QQuickDrag) |
| 243 | }; |
| 244 | |
| 245 | class QQuickDragAttachedPrivate; |
| 246 | class QQuickDragAttached : public QObject |
| 247 | { |
| 248 | Q_OBJECT |
| 249 | Q_DECLARE_PRIVATE(QQuickDragAttached) |
| 250 | |
| 251 | Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged) |
| 252 | Q_PROPERTY(QObject *source READ source WRITE setSource NOTIFY sourceChanged RESET resetSource) |
| 253 | Q_PROPERTY(QObject *target READ target NOTIFY targetChanged) |
| 254 | Q_PROPERTY(QPointF hotSpot READ hotSpot WRITE setHotSpot NOTIFY hotSpotChanged) |
| 255 | Q_PROPERTY(QUrl imageSource READ imageSource WRITE setImageSource NOTIFY imageSourceChanged) |
| 256 | Q_PROPERTY(QStringList keys READ keys WRITE setKeys NOTIFY keysChanged) |
| 257 | Q_PROPERTY(QVariantMap mimeData READ mimeData WRITE setMimeData NOTIFY mimeDataChanged) |
| 258 | Q_PROPERTY(Qt::DropActions supportedActions READ supportedActions WRITE setSupportedActions NOTIFY supportedActionsChanged) |
| 259 | Q_PROPERTY(Qt::DropAction proposedAction READ proposedAction WRITE setProposedAction NOTIFY proposedActionChanged) |
| 260 | Q_PROPERTY(QQuickDrag::DragType dragType READ dragType WRITE setDragType NOTIFY dragTypeChanged) |
| 261 | |
| 262 | QML_ANONYMOUS |
| 263 | |
| 264 | public: |
| 265 | QQuickDragAttached(QObject *parent); |
| 266 | ~QQuickDragAttached(); |
| 267 | |
| 268 | bool isActive() const; |
| 269 | void setActive(bool active); |
| 270 | |
| 271 | QObject *source() const; |
| 272 | void setSource(QObject *item); |
| 273 | void resetSource(); |
| 274 | |
| 275 | QObject *target() const; |
| 276 | |
| 277 | QPointF hotSpot() const; |
| 278 | void setHotSpot(const QPointF &hotSpot); |
| 279 | |
| 280 | QUrl imageSource() const; |
| 281 | void setImageSource(const QUrl &url); |
| 282 | |
| 283 | QStringList keys() const; |
| 284 | void setKeys(const QStringList &keys); |
| 285 | |
| 286 | QVariantMap mimeData() const; |
| 287 | void setMimeData(const QVariantMap &mimeData); |
| 288 | |
| 289 | Qt::DropActions supportedActions() const; |
| 290 | void setSupportedActions(Qt::DropActions actions); |
| 291 | |
| 292 | Qt::DropAction proposedAction() const; |
| 293 | void setProposedAction(Qt::DropAction action); |
| 294 | |
| 295 | QQuickDrag::DragType dragType() const; |
| 296 | void setDragType(QQuickDrag::DragType dragType); |
| 297 | |
| 298 | Q_INVOKABLE int drop(); |
| 299 | |
| 300 | bool event(QEvent *event) override; |
| 301 | |
| 302 | public Q_SLOTS: |
| 303 | void start(QQmlV4Function *); |
| 304 | void startDrag(QQmlV4Function *); |
| 305 | void cancel(); |
| 306 | |
| 307 | Q_SIGNALS: |
| 308 | void dragStarted(); |
| 309 | void dragFinished(Qt::DropAction dropAction); |
| 310 | |
| 311 | void activeChanged(); |
| 312 | void sourceChanged(); |
| 313 | void targetChanged(); |
| 314 | void hotSpotChanged(); |
| 315 | void imageSourceChanged(); |
| 316 | void keysChanged(); |
| 317 | void mimeDataChanged(); |
| 318 | void supportedActionsChanged(); |
| 319 | void proposedActionChanged(); |
| 320 | void dragTypeChanged(); |
| 321 | }; |
| 322 | |
| 323 | QT_END_NAMESPACE |
| 324 | |
| 325 | QML_DECLARE_TYPE(QQuickDrag) |
| 326 | |
| 327 | #endif |
| 328 | |