File indexing completed on 2024-05-19 15:09:21

0001 /*
0002     SPDX-FileCopyrightText: 2010 BetterInbox <contact@betterinbox.com>
0003     SPDX-FileContributor: Gregory Schlomoff <greg@betterinbox.com>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 #ifndef DECLARATIVEDRAGAREA_H
0009 #define DECLARATIVEDRAGAREA_H
0010 
0011 #include "DeclarativeMimeData.h"
0012 
0013 #include <QImage>
0014 #include <QQuickItem>
0015 #include <QSharedPointer>
0016 
0017 class QQmlComponent;
0018 class QQuickItemGrabResult;
0019 
0020 class DeclarativeDragArea : public QQuickItem
0021 {
0022     Q_OBJECT
0023 
0024     /**
0025      * The delegate is the item that will be displayed next to the mouse cursor during the drag and drop operation.
0026      * It usually consists of a large, semi-transparent icon representing the data being dragged.
0027      */
0028     Q_PROPERTY(QQuickItem *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged RESET resetDelegate)
0029 
0030     /**
0031      * The QML element that is the source of the resulting drag and drop operation. This can be defined to any item, and will
0032      * be available to the DropArea as event.data.source
0033      */
0034     Q_PROPERTY(QQuickItem *source READ source WRITE setSource NOTIFY sourceChanged RESET resetSource)
0035 
0036     // TODO: to be implemented
0037     Q_PROPERTY(QQuickItem *target READ source NOTIFY targetChanged)
0038 
0039     /**
0040      * the mime data of the drag operation
0041      * @see DeclarativeMimeData
0042      */
0043     Q_PROPERTY(DeclarativeMimeData *mimeData READ mimeData CONSTANT)
0044 
0045     /**
0046      * If false no drag operation will be generate
0047      */
0048     Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) // TODO: Should call setAcceptDrops()
0049 
0050     /**
0051      * Supported operations, a combination of
0052      *  Qt.CopyAction
0053      *  Qt.MoveAction
0054      *  Qt.LinkAction
0055      *  Qt.ActionMask
0056      *  Qt.IgnoreAction
0057      *  Qt.TargetMoveAction
0058      */
0059     Q_PROPERTY(Qt::DropActions supportedActions READ supportedActions WRITE setSupportedActions NOTIFY supportedActionsChanged)
0060 
0061     /**
0062      * The default action will be performed during a drag when no modificators are pressed.
0063      */
0064     Q_PROPERTY(Qt::DropAction defaultAction READ defaultAction WRITE setDefaultAction NOTIFY defaultActionChanged)
0065 
0066     /**
0067      * distance in pixel after which a drag event will get started
0068      */
0069     Q_PROPERTY(int startDragDistance READ startDragDistance WRITE setStartDragDistance NOTIFY startDragDistanceChanged)
0070 
0071     /**
0072      * an image to be used as delegate. if present overrides the delegate property. in can be either a QImage or a QIcon
0073      */
0074     Q_PROPERTY(QVariant delegateImage READ delegateImage WRITE setDelegateImage NOTIFY delegateImageChanged)
0075 
0076     /**
0077      * Whether a drag currently originates from this drag area.
0078      *
0079      * @since 5.19
0080      */
0081     Q_PROPERTY(bool dragActive READ dragActive NOTIFY dragActiveChanged)
0082 
0083 public:
0084     DeclarativeDragArea(QQuickItem *parent = nullptr);
0085     ~DeclarativeDragArea() override;
0086 
0087     QQuickItem *delegate() const;
0088     void setDelegate(QQuickItem *delegate);
0089     void resetDelegate();
0090 
0091     QVariant delegateImage() const;
0092     void setDelegateImage(const QVariant &image);
0093     QQuickItem *target() const;
0094     QQuickItem *source() const;
0095     void setSource(QQuickItem *source);
0096     void resetSource();
0097 
0098     bool dragActive() const;
0099 
0100     bool isEnabled() const;
0101     void setEnabled(bool enabled);
0102 
0103     int startDragDistance() const;
0104     void setStartDragDistance(int distance);
0105 
0106     // supported actions
0107     Qt::DropActions supportedActions() const;
0108     void setSupportedActions(Qt::DropActions actions);
0109 
0110     // default action
0111     Qt::DropAction defaultAction() const;
0112     void setDefaultAction(Qt::DropAction action);
0113 
0114     DeclarativeMimeData *mimeData() const;
0115 
0116 Q_SIGNALS:
0117     void dragStarted();
0118     void delegateChanged();
0119     void dragActiveChanged();
0120     void sourceChanged();
0121     void targetChanged();
0122     void dataChanged();
0123     void enabledChanged();
0124     void drop(int action);
0125     void supportedActionsChanged();
0126     void defaultActionChanged();
0127     void startDragDistanceChanged();
0128     void delegateImageChanged();
0129 
0130 protected:
0131     void mouseMoveEvent(QMouseEvent *event) override;
0132     void mousePressEvent(QMouseEvent *event) override;
0133     void mouseReleaseEvent(QMouseEvent *) override;
0134     void timerEvent(QTimerEvent *event) override;
0135     bool childMouseEventFilter(QQuickItem *item, QEvent *event) override;
0136 
0137 private:
0138     void startDrag(const QImage &image);
0139 
0140     QQuickItem *m_delegate;
0141     QQuickItem *m_source;
0142     QQuickItem *m_target;
0143     QSharedPointer<QQuickItemGrabResult> m_grabResult;
0144     bool m_enabled;
0145     bool m_draggingJustStarted;
0146     bool m_dragActive;
0147     Qt::DropActions m_supportedActions;
0148     Qt::DropAction m_defaultAction;
0149     DeclarativeMimeData *const m_data;
0150     QImage m_delegateImage;
0151     int m_startDragDistance;
0152     QPointF m_buttonDownPos;
0153     int m_pressAndHoldTimerId;
0154 };
0155 
0156 #endif // DECLARATIVEDRAGAREA_H