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 | #include "DeclarativeMimeData.h" |
9 | |
10 | /*! |
11 | \qmlclass MimeData DeclarativeMimeData |
12 | |
13 | This is a wrapper class around QMimeData, with a few extensions to provide better support for in-qml drag & drops. |
14 | */ |
15 | |
16 | DeclarativeMimeData::DeclarativeMimeData() |
17 | : QMimeData() |
18 | , m_source(nullptr) |
19 | { |
20 | } |
21 | |
22 | /*! |
23 | \internal |
24 | \class DeclarativeMimeData |
25 | |
26 | Creates a new DeclarativeMimeData by cloning the QMimeData passed as parameter. |
27 | This is useful for two reasons : |
28 | - In DragArea, we want to clone our "working copy" of the DeclarativeMimeData instance, as Qt will automatically |
29 | delete it after the drag and drop operation. |
30 | - In the drop events, the QMimeData is const, and we have troubles passing const to QML. So we clone it to |
31 | remove the "constness" |
32 | |
33 | This method will try to cast the QMimeData to DeclarativeMimeData, and will clone our extensions to QMimeData as well |
34 | */ |
35 | DeclarativeMimeData::DeclarativeMimeData(const QMimeData *copy) |
36 | : QMimeData() |
37 | , m_source(nullptr) |
38 | { |
39 | // Copy the standard MIME data |
40 | const auto formats = copy->formats(); |
41 | for (const QString &format : formats) { |
42 | QMimeData::setData(mimetype: format, data: copy->data(mimetype: format)); |
43 | } |
44 | |
45 | // If the object we are copying actually is a DeclarativeMimeData, copy our extended properties as well |
46 | const DeclarativeMimeData *declarativeMimeData = qobject_cast<const DeclarativeMimeData *>(object: copy); |
47 | if (declarativeMimeData) { |
48 | this->setSource(declarativeMimeData->source()); |
49 | } |
50 | } |
51 | |
52 | /*! |
53 | \qmlproperty url MimeData::url |
54 | |
55 | Returns the first URL from the urls property of QMimeData |
56 | TODO: We should use QDeclarativeListProperty<QUrls> to return the whole list instead of only the first element. |
57 | */ |
58 | QUrl DeclarativeMimeData::url() const |
59 | { |
60 | if (this->hasUrls() && !this->urls().isEmpty()) { |
61 | return QMimeData::urls().constFirst(); |
62 | } |
63 | return QUrl(); |
64 | } |
65 | void DeclarativeMimeData::setUrl(const QUrl &url) |
66 | { |
67 | if (this->url() == url) { |
68 | return; |
69 | } |
70 | |
71 | QList<QUrl> urlList; |
72 | urlList.append(t: url); |
73 | QMimeData::setUrls(urlList); |
74 | Q_EMIT urlChanged(); |
75 | } |
76 | |
77 | QJsonArray DeclarativeMimeData::urls() const |
78 | { |
79 | QJsonArray varUrls; |
80 | const auto lstUrls = QMimeData::urls(); |
81 | for (const QUrl &url : lstUrls) { |
82 | varUrls.append(value: url.toString()); |
83 | } |
84 | return varUrls; |
85 | } |
86 | |
87 | void DeclarativeMimeData::setUrls(const QJsonArray &urls) |
88 | { |
89 | QList<QUrl> urlList; |
90 | urlList.reserve(asize: urls.size()); |
91 | for (const auto &varUrl : urls) { |
92 | urlList << QUrl(varUrl.toString()); |
93 | } |
94 | QMimeData::setUrls(urlList); |
95 | Q_EMIT urlsChanged(); |
96 | } |
97 | |
98 | // color |
99 | QColor DeclarativeMimeData::color() const |
100 | { |
101 | if (this->hasColor()) { |
102 | return qvariant_cast<QColor>(v: this->colorData()); |
103 | } |
104 | return QColor(); |
105 | } |
106 | |
107 | bool DeclarativeMimeData::hasColor() const |
108 | { |
109 | // qDebug() << " hasColor " << (QMimeData::hasColor() ? color().name() : "false"); |
110 | return QMimeData::hasColor(); |
111 | } |
112 | |
113 | void DeclarativeMimeData::setColor(const QColor &color) |
114 | { |
115 | if (this->color() != color) { |
116 | this->setColorData(color); |
117 | Q_EMIT colorChanged(); |
118 | } |
119 | } |
120 | |
121 | void DeclarativeMimeData::setData(const QString &mimeType, const QVariant &data) |
122 | { |
123 | if (data.userType() == QMetaType::QByteArray) { |
124 | QMimeData::setData(mimetype: mimeType, data: data.toByteArray()); |
125 | } else if (data.canConvert<QString>()) { |
126 | QMimeData::setData(mimetype: mimeType, data: data.toString().toLatin1()); |
127 | } |
128 | } |
129 | |
130 | /*! |
131 | \qmlproperty item MimeData::source |
132 | |
133 | Setting source to any existing qml item will enable the receiver of the drag and drop operation to know in which item |
134 | the operation originated. |
135 | |
136 | In the case of inter-application drag and drop operations, the source will not be available, and will be 0. |
137 | Be sure to test it in your QML code, before using it, or it will generate errors in the console. |
138 | */ |
139 | QQuickItem *DeclarativeMimeData::source() const |
140 | { |
141 | return m_source; |
142 | } |
143 | void DeclarativeMimeData::setSource(QQuickItem *source) |
144 | { |
145 | if (m_source != source) { |
146 | m_source = source; |
147 | Q_EMIT sourceChanged(); |
148 | } |
149 | } |
150 | |
151 | QByteArray DeclarativeMimeData::getDataAsByteArray(const QString &format) |
152 | { |
153 | return data(mimetype: format); |
154 | } |
155 | |
156 | #include "moc_DeclarativeMimeData.cpp" |
157 | |