| 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 DECLARATIVEMIMEDATA_H |
| 9 | #define DECLARATIVEMIMEDATA_H |
| 10 | |
| 11 | #include <QColor> |
| 12 | #include <QJsonArray> |
| 13 | #include <QMimeData> |
| 14 | #include <QQuickItem> |
| 15 | #include <QUrl> |
| 16 | |
| 17 | struct MimeDataForign { |
| 18 | Q_GADGET |
| 19 | QML_ANONYMOUS |
| 20 | QML_FOREIGN(QMimeData) |
| 21 | }; |
| 22 | |
| 23 | /*! |
| 24 | * \qmltype MimeData |
| 25 | * \inqmlmodule org.kde.draganddrop |
| 26 | * |
| 27 | * \brief This is a wrapper class around QMimeData with a few extensions |
| 28 | * to provide better support for in-qml drag & drops. |
| 29 | */ |
| 30 | class DeclarativeMimeData : public QMimeData |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | QML_NAMED_ELEMENT(MimeData) |
| 34 | QML_UNCREATABLE("MimeData cannot be created from QML." ) |
| 35 | |
| 36 | /*! |
| 37 | * \qmlproperty string MimeData::text |
| 38 | * A plain text (MIME type text/plain) representation of the data. |
| 39 | */ |
| 40 | Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) |
| 41 | |
| 42 | /*! |
| 43 | * \qmlproperty string MimeData::html |
| 44 | * A string if the data stored in the object is HTML (MIME type text/html); otherwise returns an empty string. |
| 45 | */ |
| 46 | Q_PROPERTY(QString html READ html WRITE setHtml NOTIFY htmlChanged) |
| 47 | |
| 48 | /// TODO: We should use QDeclarativeListProperty<QUrls> to return the whole list instead of only the first element. |
| 49 | /*! |
| 50 | * \qmlproperty url MimeData::url |
| 51 | * The first URL from the urls property of QMimeData. |
| 52 | */ |
| 53 | Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) |
| 54 | |
| 55 | /*! |
| 56 | * \qmlproperty QJsonArray MimeData::urls |
| 57 | * A list of URLs contained within the MIME data object. |
| 58 | * URLs correspond to the MIME type text/uri-list. |
| 59 | */ |
| 60 | Q_PROPERTY(QJsonArray urls READ urls WRITE setUrls NOTIFY urlsChanged) |
| 61 | |
| 62 | /*! |
| 63 | * \qmlproperty color MimeData::color |
| 64 | * A color if the data stored in the object represents a color (MIME type application/x-color); otherwise QColor(). |
| 65 | */ |
| 66 | Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) |
| 67 | |
| 68 | /*! |
| 69 | * \qmlproperty Item MimeData::source |
| 70 | * The graphical item on the scene that started the drag event. It may be null. |
| 71 | * |
| 72 | * Setting source to any existing qml item will enable the receiver of the drag and drop operation to know in which item |
| 73 | * the operation originated. |
| 74 | * |
| 75 | * In the case of inter-application drag and drop operations, the source will not be available, and will be 0. |
| 76 | * Be sure to test it in your QML code, before using it, or it will generate errors in the console. |
| 77 | * |
| 78 | */ |
| 79 | Q_PROPERTY(QQuickItem *source READ source WRITE setSource NOTIFY sourceChanged) |
| 80 | |
| 81 | /*! |
| 82 | * \qmlproperty bool MimeData::hasUrls |
| 83 | * \sa QMimeData::hasUrls |
| 84 | */ |
| 85 | Q_PROPERTY(bool hasUrls READ hasUrls NOTIFY urlsChanged) |
| 86 | // TODO: Image property |
| 87 | |
| 88 | /*! |
| 89 | * \qmlproperty list<string> MimeData::formats |
| 90 | * \sa QMimeData::formats |
| 91 | */ |
| 92 | Q_PROPERTY(QStringList formats READ formats) |
| 93 | public: |
| 94 | DeclarativeMimeData(); |
| 95 | /*! |
| 96 | * \brief Creates a new MimeData by cloning the QMimeData |
| 97 | * passed as parameter. |
| 98 | * |
| 99 | * This is useful for two reasons: |
| 100 | * \list |
| 101 | * \li In DragArea, we want to clone our "working copy" of the MimeData instance, as Qt will automatically delete it after the drag and drop operation. |
| 102 | * \li In the drop events, the QMimeData is const, and we have troubles passing const to QML. So we clone it to remove the "constness" |
| 103 | * \endlist |
| 104 | * |
| 105 | * This method will try to cast the QMimeData to MimeData, and will clone our extensions to QMimeData as well |
| 106 | */ |
| 107 | DeclarativeMimeData(const QMimeData *copy); |
| 108 | |
| 109 | QUrl url() const; |
| 110 | void setUrl(const QUrl &url); |
| 111 | |
| 112 | QJsonArray urls() const; |
| 113 | void setUrls(const QJsonArray &urls); |
| 114 | |
| 115 | QColor color() const; |
| 116 | void setColor(const QColor &color); |
| 117 | |
| 118 | /*! |
| 119 | * \qmlmethod MimeData::hasColor() |
| 120 | */ |
| 121 | Q_INVOKABLE bool hasColor() const; |
| 122 | |
| 123 | /*! |
| 124 | * \qmlmethod MimeData::setData(string mimeType, variant data) |
| 125 | */ |
| 126 | Q_INVOKABLE void setData(const QString &mimeType, const QVariant &data); |
| 127 | |
| 128 | QQuickItem *source() const; |
| 129 | void setSource(QQuickItem *source); |
| 130 | |
| 131 | /*! |
| 132 | * \qmlmethod MimeData::getDataAsByteArray(string format) |
| 133 | */ |
| 134 | Q_INVOKABLE QByteArray getDataAsByteArray(const QString &format); |
| 135 | |
| 136 | /* |
| 137 | QString text() const; //TODO: Reimplement this to issue the onChanged signals |
| 138 | void setText(const QString &text); |
| 139 | QString html() const; |
| 140 | void setHtml(const QString &html); |
| 141 | */ |
| 142 | |
| 143 | Q_SIGNALS: |
| 144 | void textChanged(); // FIXME not being used |
| 145 | void htmlChanged(); // FIXME not being used |
| 146 | void urlChanged(); |
| 147 | void urlsChanged(); |
| 148 | void colorChanged(); |
| 149 | void sourceChanged(); |
| 150 | |
| 151 | private: |
| 152 | QQuickItem *m_source; |
| 153 | }; |
| 154 | |
| 155 | #endif // DECLARATIVEMIMEDATA_H |
| 156 | |