Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qmimedata.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qmimedata.h" | - | ||||||||||||
35 | - | |||||||||||||
36 | #include "private/qobject_p.h" | - | ||||||||||||
37 | #include "qurl.h" | - | ||||||||||||
38 | #include "qstringlist.h" | - | ||||||||||||
39 | #include "qtextcodec.h" | - | ||||||||||||
40 | - | |||||||||||||
41 | QT_BEGIN_NAMESPACE | - | ||||||||||||
42 | - | |||||||||||||
43 | struct QMimeDataStruct | - | ||||||||||||
44 | { | - | ||||||||||||
45 | QString format; | - | ||||||||||||
46 | QVariant data; | - | ||||||||||||
47 | }; | - | ||||||||||||
48 | Q_DECLARE_TYPEINFO(QMimeDataStruct, Q_MOVABLE_TYPE); | - | ||||||||||||
49 | - | |||||||||||||
50 | class QMimeDataPrivate : public QObjectPrivate | - | ||||||||||||
51 | { | - | ||||||||||||
52 | Q_DECLARE_PUBLIC(QMimeData) | - | ||||||||||||
53 | public: | - | ||||||||||||
54 | void removeData(const QString &format); | - | ||||||||||||
55 | void setData(const QString &format, const QVariant &data); | - | ||||||||||||
56 | QVariant getData(const QString &format) const; | - | ||||||||||||
57 | - | |||||||||||||
58 | QVariant retrieveTypedData(const QString &format, QVariant::Type type) const; | - | ||||||||||||
59 | - | |||||||||||||
60 | QVector<QMimeDataStruct> dataList; | - | ||||||||||||
61 | }; | - | ||||||||||||
62 | - | |||||||||||||
63 | void QMimeDataPrivate::removeData(const QString &format) | - | ||||||||||||
64 | { | - | ||||||||||||
65 | for (int i=0; i<dataList.size(); i++) {
| 43-118 | ||||||||||||
66 | if (dataList.at(i).format == format) {
| 8-35 | ||||||||||||
67 | dataList.removeAt(i); | - | ||||||||||||
68 | return; executed 8 times by 2 tests: return; Executed by:
| 8 | ||||||||||||
69 | } | - | ||||||||||||
70 | } executed 35 times by 4 tests: end of block Executed by:
| 35 | ||||||||||||
71 | } executed 118 times by 12 tests: end of block Executed by:
| 118 | ||||||||||||
72 | - | |||||||||||||
73 | void QMimeDataPrivate::setData(const QString &format, const QVariant &data) | - | ||||||||||||
74 | { | - | ||||||||||||
75 | // remove it first if the format is already here. | - | ||||||||||||
76 | removeData(format); | - | ||||||||||||
77 | QMimeDataStruct mimeData; | - | ||||||||||||
78 | mimeData.format = format; | - | ||||||||||||
79 | mimeData.data = data; | - | ||||||||||||
80 | dataList += mimeData; | - | ||||||||||||
81 | } executed 124 times by 12 tests: end of block Executed by:
| 124 | ||||||||||||
82 | - | |||||||||||||
83 | - | |||||||||||||
84 | QVariant QMimeDataPrivate::getData(const QString &format) const | - | ||||||||||||
85 | { | - | ||||||||||||
86 | QVariant data; | - | ||||||||||||
87 | for (int i=0; i<dataList.size(); i++) {
| 9-173 | ||||||||||||
88 | if (dataList.at(i).format == format) {
| 45-128 | ||||||||||||
89 | data = dataList.at(i).data; | - | ||||||||||||
90 | break; executed 128 times by 16 tests: break; Executed by:
| 128 | ||||||||||||
91 | } | - | ||||||||||||
92 | } executed 45 times by 6 tests: end of block Executed by:
| 45 | ||||||||||||
93 | return data; executed 137 times by 16 tests: return data; Executed by:
| 137 | ||||||||||||
94 | } | - | ||||||||||||
95 | - | |||||||||||||
96 | QVariant QMimeDataPrivate::retrieveTypedData(const QString &format, QVariant::Type type) const | - | ||||||||||||
97 | { | - | ||||||||||||
98 | Q_Q(const QMimeData); | - | ||||||||||||
99 | - | |||||||||||||
100 | QVariant data = q->retrieveData(format, type); | - | ||||||||||||
101 | - | |||||||||||||
102 | // Text data requested: fallback to URL data if available | - | ||||||||||||
103 | if (format == QLatin1String("text/plain") && !data.isValid()) {
| 4-90 | ||||||||||||
104 | data = retrieveTypedData(QLatin1String("text/uri-list"), QVariant::List); | - | ||||||||||||
105 | if (data.type() == QVariant::Url) {
| 0-4 | ||||||||||||
106 | data = QVariant(data.toUrl().toDisplayString()); | - | ||||||||||||
107 | } else if (data.type() == QVariant::List) { never executed: end of block
| 0-2 | ||||||||||||
108 | QString text; | - | ||||||||||||
109 | int numUrls = 0; | - | ||||||||||||
110 | const QList<QVariant> list = data.toList(); | - | ||||||||||||
111 | for (int i = 0; i < list.size(); ++i) {
| 2-3 | ||||||||||||
112 | if (list.at(i).type() == QVariant::Url) {
| 0-3 | ||||||||||||
113 | text.append(list.at(i).toUrl().toDisplayString() + QLatin1Char('\n')); | - | ||||||||||||
114 | ++numUrls; | - | ||||||||||||
115 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
116 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
117 | if (numUrls == 1)
| 1 | ||||||||||||
118 | text.chop(1); // no final '\n' if there's only one URL executed 1 time by 1 test: text.chop(1); Executed by:
| 1 | ||||||||||||
119 | data = QVariant(text); | - | ||||||||||||
120 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
121 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
122 | - | |||||||||||||
123 | if (data.type() == type || !data.isValid())
| 7-99 | ||||||||||||
124 | return data; executed 106 times by 15 tests: return data; Executed by:
| 106 | ||||||||||||
125 | - | |||||||||||||
126 | // provide more conversion possiblities than just what QVariant provides | - | ||||||||||||
127 | - | |||||||||||||
128 | // URLs can be lists as well... | - | ||||||||||||
129 | if ((type == QVariant::Url && data.type() == QVariant::List)
| 0-31 | ||||||||||||
130 | || (type == QVariant::List && data.type() == QVariant::Url))
| 0-31 | ||||||||||||
131 | return data; never executed: return data; | 0 | ||||||||||||
132 | - | |||||||||||||
133 | // images and pixmaps are interchangeable | - | ||||||||||||
134 | if ((type == QVariant::Pixmap && data.type() == QVariant::Image)
| 0-31 | ||||||||||||
135 | || (type == QVariant::Image && data.type() == QVariant::Pixmap))
| 0-31 | ||||||||||||
136 | return data; never executed: return data; | 0 | ||||||||||||
137 | - | |||||||||||||
138 | if (data.type() == QVariant::ByteArray) {
| 8-23 | ||||||||||||
139 | // see if we can convert to the requested type | - | ||||||||||||
140 | switch(type) { | - | ||||||||||||
141 | #ifndef QT_NO_TEXTCODEC | - | ||||||||||||
142 | case QVariant::String: { executed 8 times by 3 tests: case QVariant::String: Executed by:
| 8 | ||||||||||||
143 | const QByteArray ba = data.toByteArray(); | - | ||||||||||||
144 | QTextCodec *codec = QTextCodec::codecForName("utf-8"); | - | ||||||||||||
145 | if (format == QLatin1String("text/html"))
| 3-5 | ||||||||||||
146 | codec = QTextCodec::codecForHtml(ba, codec); executed 5 times by 2 tests: codec = QTextCodec::codecForHtml(ba, codec); Executed by:
| 5 | ||||||||||||
147 | return codec->toUnicode(ba); executed 8 times by 3 tests: return codec->toUnicode(ba); Executed by:
| 8 | ||||||||||||
148 | } | - | ||||||||||||
149 | #endif // QT_NO_TEXTCODEC | - | ||||||||||||
150 | case QVariant::Color: { never executed: case QVariant::Color: | 0 | ||||||||||||
151 | QVariant newData = data; | - | ||||||||||||
152 | newData.convert(QVariant::Color); | - | ||||||||||||
153 | return newData; never executed: return newData; | 0 | ||||||||||||
154 | } | - | ||||||||||||
155 | case QVariant::List: { never executed: case QVariant::List: | 0 | ||||||||||||
156 | if (format != QLatin1String("text/uri-list"))
| 0 | ||||||||||||
157 | break; never executed: break; | 0 | ||||||||||||
158 | // fall through | - | ||||||||||||
159 | } | - | ||||||||||||
160 | case QVariant::Url: { code before this statement never executed: case QVariant::Url: never executed: case QVariant::Url: | 0 | ||||||||||||
161 | QByteArray ba = data.toByteArray(); | - | ||||||||||||
162 | // Qt 3.x will send text/uri-list with a trailing | - | ||||||||||||
163 | // null-terminator (that is *not* sent for any other | - | ||||||||||||
164 | // text/* mime-type), so chop it off | - | ||||||||||||
165 | if (ba.endsWith('\0'))
| 0 | ||||||||||||
166 | ba.chop(1); never executed: ba.chop(1); | 0 | ||||||||||||
167 | - | |||||||||||||
168 | QList<QByteArray> urls = ba.split('\n'); | - | ||||||||||||
169 | QList<QVariant> list; | - | ||||||||||||
170 | for (int i = 0; i < urls.size(); ++i) {
| 0 | ||||||||||||
171 | QByteArray ba = urls.at(i).trimmed(); | - | ||||||||||||
172 | if (!ba.isEmpty())
| 0 | ||||||||||||
173 | list.append(QUrl::fromEncoded(ba)); never executed: list.append(QUrl::fromEncoded(ba)); | 0 | ||||||||||||
174 | } never executed: end of block | 0 | ||||||||||||
175 | return list; never executed: return list; | 0 | ||||||||||||
176 | } | - | ||||||||||||
177 | default: never executed: default: | 0 | ||||||||||||
178 | break; never executed: break; | 0 | ||||||||||||
179 | } | - | ||||||||||||
180 | - | |||||||||||||
181 | } else if (type == QVariant::ByteArray) {
| 0-23 | ||||||||||||
182 | - | |||||||||||||
183 | // try to convert to bytearray | - | ||||||||||||
184 | switch(data.type()) { | - | ||||||||||||
185 | case QVariant::ByteArray: never executed: case QVariant::ByteArray: | 0 | ||||||||||||
186 | case QVariant::Color: never executed: case QVariant::Color: | 0 | ||||||||||||
187 | return data.toByteArray(); never executed: return data.toByteArray(); | 0 | ||||||||||||
188 | case QVariant::String: executed 22 times by 6 tests: case QVariant::String: Executed by:
| 22 | ||||||||||||
189 | return data.toString().toUtf8(); executed 22 times by 6 tests: return data.toString().toUtf8(); Executed by:
| 22 | ||||||||||||
190 | case QVariant::Url: never executed: case QVariant::Url: | 0 | ||||||||||||
191 | return data.toUrl().toEncoded(); never executed: return data.toUrl().toEncoded(); | 0 | ||||||||||||
192 | case QVariant::List: { executed 1 time by 1 test: case QVariant::List: Executed by:
| 1 | ||||||||||||
193 | // has to be list of URLs | - | ||||||||||||
194 | QByteArray result; | - | ||||||||||||
195 | QList<QVariant> list = data.toList(); | - | ||||||||||||
196 | for (int i = 0; i < list.size(); ++i) {
| 1-2 | ||||||||||||
197 | if (list.at(i).type() == QVariant::Url) {
| 0-2 | ||||||||||||
198 | result += list.at(i).toUrl().toEncoded(); | - | ||||||||||||
199 | result += "\r\n"; | - | ||||||||||||
200 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
201 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
202 | if (!result.isEmpty())
| 0-1 | ||||||||||||
203 | return result; executed 1 time by 1 test: return result; Executed by:
| 1 | ||||||||||||
204 | break; never executed: break; | 0 | ||||||||||||
205 | } | - | ||||||||||||
206 | default: never executed: default: | 0 | ||||||||||||
207 | break; never executed: break; | 0 | ||||||||||||
208 | } | - | ||||||||||||
209 | } | - | ||||||||||||
210 | return data; never executed: return data; | 0 | ||||||||||||
211 | } | - | ||||||||||||
212 | - | |||||||||||||
213 | /*! | - | ||||||||||||
214 | \class QMimeData | - | ||||||||||||
215 | \inmodule QtCore | - | ||||||||||||
216 | \brief The QMimeData class provides a container for data that records information | - | ||||||||||||
217 | about its MIME type. | - | ||||||||||||
218 | - | |||||||||||||
219 | QMimeData is used to describe information that can be stored in | - | ||||||||||||
220 | the \l{QClipboard}{clipboard}, and transferred via the \l{drag | - | ||||||||||||
221 | and drop} mechanism. QMimeData objects associate the data that | - | ||||||||||||
222 | they hold with the corresponding MIME types to ensure that | - | ||||||||||||
223 | information can be safely transferred between applications, and | - | ||||||||||||
224 | copied around within the same application. | - | ||||||||||||
225 | - | |||||||||||||
226 | QMimeData objects are usually created using \c new and supplied | - | ||||||||||||
227 | to QDrag or QClipboard objects. This is to enable Qt to manage | - | ||||||||||||
228 | the memory that they use. | - | ||||||||||||
229 | - | |||||||||||||
230 | A single QMimeData object can store the same data using several | - | ||||||||||||
231 | different formats at the same time. The formats() function | - | ||||||||||||
232 | returns a list of the available formats in order of preference. | - | ||||||||||||
233 | The data() function returns the raw data associated with a MIME | - | ||||||||||||
234 | type, and setData() allows you to set the data for a MIME type. | - | ||||||||||||
235 | - | |||||||||||||
236 | For the most common MIME types, QMimeData provides convenience | - | ||||||||||||
237 | functions to access the data: | - | ||||||||||||
238 | - | |||||||||||||
239 | \table | - | ||||||||||||
240 | \header \li Tester \li Getter \li Setter \li MIME Types | - | ||||||||||||
241 | \row \li hasText() \li text() \li setText() \li \c text/plain | - | ||||||||||||
242 | \row \li hasHtml() \li html() \li setHtml() \li \c text/html | - | ||||||||||||
243 | \row \li hasUrls() \li urls() \li setUrls() \li \c text/uri-list | - | ||||||||||||
244 | \row \li hasImage() \li imageData() \li setImageData() \li \c image/ * | - | ||||||||||||
245 | \row \li hasColor() \li colorData() \li setColorData() \li \c application/x-color | - | ||||||||||||
246 | \endtable | - | ||||||||||||
247 | - | |||||||||||||
248 | For example, if your write a widget that accepts URL drags, you | - | ||||||||||||
249 | would end up writing code like this: | - | ||||||||||||
250 | - | |||||||||||||
251 | \snippet code/src_corelib_kernel_qmimedata.cpp 0 | - | ||||||||||||
252 | - | |||||||||||||
253 | There are three approaches for storing custom data in a QMimeData | - | ||||||||||||
254 | object: | - | ||||||||||||
255 | - | |||||||||||||
256 | \list 1 | - | ||||||||||||
257 | \li Custom data can be stored directly in a QMimeData object as a | - | ||||||||||||
258 | QByteArray using setData(). For example: | - | ||||||||||||
259 | - | |||||||||||||
260 | \snippet code/src_corelib_kernel_qmimedata.cpp 1 | - | ||||||||||||
261 | - | |||||||||||||
262 | \li We can subclass QMimeData and reimplement hasFormat(), | - | ||||||||||||
263 | formats(), and retrieveData(). | - | ||||||||||||
264 | - | |||||||||||||
265 | \li If the drag and drop operation occurs within a single | - | ||||||||||||
266 | application, we can subclass QMimeData and add extra data in | - | ||||||||||||
267 | it, and use a qobject_cast() in the receiver's drop event | - | ||||||||||||
268 | handler. For example: | - | ||||||||||||
269 | - | |||||||||||||
270 | \snippet code/src_corelib_kernel_qmimedata.cpp 2 | - | ||||||||||||
271 | \endlist | - | ||||||||||||
272 | - | |||||||||||||
273 | \section1 Platform-Specific MIME Types | - | ||||||||||||
274 | - | |||||||||||||
275 | On Windows, formats() will also return custom formats available | - | ||||||||||||
276 | in the MIME data, using the \c{x-qt-windows-mime} subtype to | - | ||||||||||||
277 | indicate that they represent data in non-standard formats. | - | ||||||||||||
278 | The formats will take the following form: | - | ||||||||||||
279 | - | |||||||||||||
280 | \snippet code/src_corelib_kernel_qmimedata.cpp 3 | - | ||||||||||||
281 | - | |||||||||||||
282 | The following are examples of custom MIME types: | - | ||||||||||||
283 | - | |||||||||||||
284 | \snippet code/src_corelib_kernel_qmimedata.cpp 4 | - | ||||||||||||
285 | - | |||||||||||||
286 | The \c value declaration of each format describes the way in which the | - | ||||||||||||
287 | data is encoded. | - | ||||||||||||
288 | - | |||||||||||||
289 | In some cases (e.g. dropping multiple email attachments), multiple data | - | ||||||||||||
290 | values are available. They can be accessed by adding an \c index value: | - | ||||||||||||
291 | - | |||||||||||||
292 | \snippet code/src_corelib_kernel_qmimedata.cpp 8 | - | ||||||||||||
293 | - | |||||||||||||
294 | On Windows, the MIME format does not always map directly to the | - | ||||||||||||
295 | clipboard formats. Qt provides QWinMime to map clipboard | - | ||||||||||||
296 | formats to open-standard MIME formats. Similarly, the | - | ||||||||||||
297 | QMacPasteboardMime maps MIME to Mac flavors. | - | ||||||||||||
298 | - | |||||||||||||
299 | \sa QClipboard, QDragEnterEvent, QDragMoveEvent, QDropEvent, QDrag, | - | ||||||||||||
300 | QMacPasteboardMime, {Drag and Drop} | - | ||||||||||||
301 | */ | - | ||||||||||||
302 | - | |||||||||||||
303 | /*! | - | ||||||||||||
304 | Constructs a new MIME data object with no data in it. | - | ||||||||||||
305 | */ | - | ||||||||||||
306 | QMimeData::QMimeData() | - | ||||||||||||
307 | : QObject(*new QMimeDataPrivate, 0) | - | ||||||||||||
308 | { | - | ||||||||||||
309 | } executed 231 times by 19 tests: end of block Executed by:
| 231 | ||||||||||||
310 | - | |||||||||||||
311 | /*! | - | ||||||||||||
312 | Destroys the MIME data object. | - | ||||||||||||
313 | */ | - | ||||||||||||
314 | QMimeData::~QMimeData() | - | ||||||||||||
315 | { | - | ||||||||||||
316 | } | - | ||||||||||||
317 | - | |||||||||||||
318 | /*! | - | ||||||||||||
319 | Returns a list of URLs contained within the MIME data object. | - | ||||||||||||
320 | - | |||||||||||||
321 | URLs correspond to the MIME type \c text/uri-list. | - | ||||||||||||
322 | - | |||||||||||||
323 | \sa hasUrls(), data() | - | ||||||||||||
324 | */ | - | ||||||||||||
325 | QList<QUrl> QMimeData::urls() const | - | ||||||||||||
326 | { | - | ||||||||||||
327 | Q_D(const QMimeData); | - | ||||||||||||
328 | QVariant data = d->retrieveTypedData(QLatin1String("text/uri-list"), QVariant::List); | - | ||||||||||||
329 | QList<QUrl> urls; | - | ||||||||||||
330 | if (data.type() == QVariant::Url)
| 0-2 | ||||||||||||
331 | urls.append(data.toUrl()); never executed: urls.append(data.toUrl()); | 0 | ||||||||||||
332 | else if (data.type() == QVariant::List) {
| 0-2 | ||||||||||||
333 | QList<QVariant> list = data.toList(); | - | ||||||||||||
334 | for (int i = 0; i < list.size(); ++i) {
| 2-3 | ||||||||||||
335 | if (list.at(i).type() == QVariant::Url)
| 0-3 | ||||||||||||
336 | urls.append(list.at(i).toUrl()); executed 3 times by 1 test: urls.append(list.at(i).toUrl()); Executed by:
| 3 | ||||||||||||
337 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
338 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
339 | return urls; executed 2 times by 1 test: return urls; Executed by:
| 2 | ||||||||||||
340 | } | - | ||||||||||||
341 | - | |||||||||||||
342 | /*! | - | ||||||||||||
343 | Sets the URLs stored in the MIME data object to those specified by \a urls. | - | ||||||||||||
344 | - | |||||||||||||
345 | URLs correspond to the MIME type \c text/uri-list. | - | ||||||||||||
346 | - | |||||||||||||
347 | Since Qt 5.0, setUrls also exports the urls as plain text, if setText | - | ||||||||||||
348 | was not called before, to make it possible to drop them into any lineedit | - | ||||||||||||
349 | and text editor. | - | ||||||||||||
350 | - | |||||||||||||
351 | \sa hasUrls(), setData() | - | ||||||||||||
352 | */ | - | ||||||||||||
353 | void QMimeData::setUrls(const QList<QUrl> &urls) | - | ||||||||||||
354 | { | - | ||||||||||||
355 | Q_D(QMimeData); | - | ||||||||||||
356 | QList<QVariant> list; | - | ||||||||||||
357 | const int numUrls = urls.size(); | - | ||||||||||||
358 | list.reserve(numUrls); | - | ||||||||||||
359 | for (int i = 0; i < numUrls; ++i)
| 2-3 | ||||||||||||
360 | list.append(urls.at(i)); executed 3 times by 1 test: list.append(urls.at(i)); Executed by:
| 3 | ||||||||||||
361 | - | |||||||||||||
362 | d->setData(QLatin1String("text/uri-list"), list); | - | ||||||||||||
363 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
364 | - | |||||||||||||
365 | /*! | - | ||||||||||||
366 | Returns \c true if the object can return a list of urls; otherwise | - | ||||||||||||
367 | returns \c false. | - | ||||||||||||
368 | - | |||||||||||||
369 | URLs correspond to the MIME type \c text/uri-list. | - | ||||||||||||
370 | - | |||||||||||||
371 | \sa setUrls(), urls(), hasFormat() | - | ||||||||||||
372 | */ | - | ||||||||||||
373 | bool QMimeData::hasUrls() const | - | ||||||||||||
374 | { | - | ||||||||||||
375 | return hasFormat(QLatin1String("text/uri-list")); executed 20 times by 3 tests: return hasFormat(QLatin1String("text/uri-list")); Executed by:
| 20 | ||||||||||||
376 | } | - | ||||||||||||
377 | - | |||||||||||||
378 | - | |||||||||||||
379 | /*! | - | ||||||||||||
380 | Returns a plain text (MIME type \c text/plain) representation of | - | ||||||||||||
381 | the data. | - | ||||||||||||
382 | - | |||||||||||||
383 | \sa hasText(), html(), data() | - | ||||||||||||
384 | */ | - | ||||||||||||
385 | QString QMimeData::text() const | - | ||||||||||||
386 | { | - | ||||||||||||
387 | Q_D(const QMimeData); | - | ||||||||||||
388 | QVariant data = d->retrieveTypedData(QLatin1String("text/plain"), QVariant::String); | - | ||||||||||||
389 | return data.toString(); executed 66 times by 9 tests: return data.toString(); Executed by:
| 66 | ||||||||||||
390 | } | - | ||||||||||||
391 | - | |||||||||||||
392 | /*! | - | ||||||||||||
393 | Sets \a text as the plain text (MIME type \c text/plain) used to | - | ||||||||||||
394 | represent the data. | - | ||||||||||||
395 | - | |||||||||||||
396 | \sa hasText(), setHtml(), setData() | - | ||||||||||||
397 | */ | - | ||||||||||||
398 | void QMimeData::setText(const QString &text) | - | ||||||||||||
399 | { | - | ||||||||||||
400 | Q_D(QMimeData); | - | ||||||||||||
401 | d->setData(QLatin1String("text/plain"), text); | - | ||||||||||||
402 | } executed 45 times by 8 tests: end of block Executed by:
| 45 | ||||||||||||
403 | - | |||||||||||||
404 | /*! | - | ||||||||||||
405 | Returns \c true if the object can return plain text (MIME type \c | - | ||||||||||||
406 | text/plain); otherwise returns \c false. | - | ||||||||||||
407 | - | |||||||||||||
408 | \sa setText(), text(), hasHtml(), hasFormat() | - | ||||||||||||
409 | */ | - | ||||||||||||
410 | bool QMimeData::hasText() const | - | ||||||||||||
411 | { | - | ||||||||||||
412 | return hasFormat(QLatin1String("text/plain")) || hasUrls(); executed 30 times by 3 tests: return hasFormat(QLatin1String("text/plain")) || hasUrls(); Executed by:
| 0-30 | ||||||||||||
413 | } | - | ||||||||||||
414 | - | |||||||||||||
415 | /*! | - | ||||||||||||
416 | Returns a string if the data stored in the object is HTML (MIME | - | ||||||||||||
417 | type \c text/html); otherwise returns an empty string. | - | ||||||||||||
418 | - | |||||||||||||
419 | \sa hasHtml(), setData() | - | ||||||||||||
420 | */ | - | ||||||||||||
421 | QString QMimeData::html() const | - | ||||||||||||
422 | { | - | ||||||||||||
423 | Q_D(const QMimeData); | - | ||||||||||||
424 | QVariant data = d->retrieveTypedData(QLatin1String("text/html"), QVariant::String); | - | ||||||||||||
425 | return data.toString(); executed 7 times by 2 tests: return data.toString(); Executed by:
| 7 | ||||||||||||
426 | } | - | ||||||||||||
427 | - | |||||||||||||
428 | /*! | - | ||||||||||||
429 | Sets \a html as the HTML (MIME type \c text/html) used to | - | ||||||||||||
430 | represent the data. | - | ||||||||||||
431 | - | |||||||||||||
432 | \sa hasHtml(), setText(), setData() | - | ||||||||||||
433 | */ | - | ||||||||||||
434 | void QMimeData::setHtml(const QString &html) | - | ||||||||||||
435 | { | - | ||||||||||||
436 | Q_D(QMimeData); | - | ||||||||||||
437 | d->setData(QLatin1String("text/html"), html); | - | ||||||||||||
438 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
439 | - | |||||||||||||
440 | /*! | - | ||||||||||||
441 | Returns \c true if the object can return HTML (MIME type \c | - | ||||||||||||
442 | text/html); otherwise returns \c false. | - | ||||||||||||
443 | - | |||||||||||||
444 | \sa setHtml(), html(), hasFormat() | - | ||||||||||||
445 | */ | - | ||||||||||||
446 | bool QMimeData::hasHtml() const | - | ||||||||||||
447 | { | - | ||||||||||||
448 | return hasFormat(QLatin1String("text/html")); executed 25 times by 3 tests: return hasFormat(QLatin1String("text/html")); Executed by:
| 25 | ||||||||||||
449 | } | - | ||||||||||||
450 | - | |||||||||||||
451 | /*! | - | ||||||||||||
452 | Returns a QVariant storing a QImage if the object can return an | - | ||||||||||||
453 | image; otherwise returns a null variant. | - | ||||||||||||
454 | - | |||||||||||||
455 | A QVariant is used because QMimeData belongs to the Qt Core | - | ||||||||||||
456 | module, whereas QImage belongs to Qt GUI. To convert the | - | ||||||||||||
457 | QVariant to a QImage, simply use qvariant_cast(). For example: | - | ||||||||||||
458 | - | |||||||||||||
459 | \snippet code/src_corelib_kernel_qmimedata.cpp 5 | - | ||||||||||||
460 | - | |||||||||||||
461 | \sa hasImage() | - | ||||||||||||
462 | */ | - | ||||||||||||
463 | QVariant QMimeData::imageData() const | - | ||||||||||||
464 | { | - | ||||||||||||
465 | Q_D(const QMimeData); | - | ||||||||||||
466 | return d->retrieveTypedData(QLatin1String("application/x-qt-image"), QVariant::Image); executed 3 times by 1 test: return d->retrieveTypedData(QLatin1String("application/x-qt-image"), QVariant::Image); Executed by:
| 3 | ||||||||||||
467 | } | - | ||||||||||||
468 | - | |||||||||||||
469 | /*! | - | ||||||||||||
470 | Sets the data in the object to the given \a image. | - | ||||||||||||
471 | - | |||||||||||||
472 | A QVariant is used because QMimeData belongs to the Qt Core | - | ||||||||||||
473 | module, whereas QImage belongs to Qt GUI. The conversion | - | ||||||||||||
474 | from QImage to QVariant is implicit. For example: | - | ||||||||||||
475 | - | |||||||||||||
476 | \snippet code/src_corelib_kernel_qmimedata.cpp 6 | - | ||||||||||||
477 | - | |||||||||||||
478 | \sa hasImage(), setData() | - | ||||||||||||
479 | */ | - | ||||||||||||
480 | void QMimeData::setImageData(const QVariant &image) | - | ||||||||||||
481 | { | - | ||||||||||||
482 | Q_D(QMimeData); | - | ||||||||||||
483 | d->setData(QLatin1String("application/x-qt-image"), image); | - | ||||||||||||
484 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
485 | - | |||||||||||||
486 | /*! | - | ||||||||||||
487 | Returns \c true if the object can return an image; otherwise returns | - | ||||||||||||
488 | false. | - | ||||||||||||
489 | - | |||||||||||||
490 | \sa setImageData(), imageData(), hasFormat() | - | ||||||||||||
491 | */ | - | ||||||||||||
492 | bool QMimeData::hasImage() const | - | ||||||||||||
493 | { | - | ||||||||||||
494 | return hasFormat(QLatin1String("application/x-qt-image")); executed 5 times by 1 test: return hasFormat(QLatin1String("application/x-qt-image")); Executed by:
| 5 | ||||||||||||
495 | } | - | ||||||||||||
496 | - | |||||||||||||
497 | /*! | - | ||||||||||||
498 | Returns a color if the data stored in the object represents a | - | ||||||||||||
499 | color (MIME type \c application/x-color); otherwise returns a | - | ||||||||||||
500 | null variant. | - | ||||||||||||
501 | - | |||||||||||||
502 | A QVariant is used because QMimeData belongs to the Qt Core | - | ||||||||||||
503 | module, whereas QColor belongs to Qt GUI. To convert the | - | ||||||||||||
504 | QVariant to a QColor, simply use qvariant_cast(). For example: | - | ||||||||||||
505 | - | |||||||||||||
506 | \snippet code/src_corelib_kernel_qmimedata.cpp 7 | - | ||||||||||||
507 | - | |||||||||||||
508 | \sa hasColor(), setColorData(), data() | - | ||||||||||||
509 | */ | - | ||||||||||||
510 | QVariant QMimeData::colorData() const | - | ||||||||||||
511 | { | - | ||||||||||||
512 | Q_D(const QMimeData); | - | ||||||||||||
513 | return d->retrieveTypedData(QLatin1String("application/x-color"), QVariant::Color); executed 2 times by 1 test: return d->retrieveTypedData(QLatin1String("application/x-color"), QVariant::Color); Executed by:
| 2 | ||||||||||||
514 | } | - | ||||||||||||
515 | - | |||||||||||||
516 | /*! | - | ||||||||||||
517 | Sets the color data in the object to the given \a color. | - | ||||||||||||
518 | - | |||||||||||||
519 | Colors correspond to the MIME type \c application/x-color. | - | ||||||||||||
520 | - | |||||||||||||
521 | \sa hasColor(), setData() | - | ||||||||||||
522 | */ | - | ||||||||||||
523 | void QMimeData::setColorData(const QVariant &color) | - | ||||||||||||
524 | { | - | ||||||||||||
525 | Q_D(QMimeData); | - | ||||||||||||
526 | d->setData(QLatin1String("application/x-color"), color); | - | ||||||||||||
527 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
528 | - | |||||||||||||
529 | - | |||||||||||||
530 | /*! | - | ||||||||||||
531 | Returns \c true if the object can return a color (MIME type \c | - | ||||||||||||
532 | application/x-color); otherwise returns \c false. | - | ||||||||||||
533 | - | |||||||||||||
534 | \sa setColorData(), colorData(), hasFormat() | - | ||||||||||||
535 | */ | - | ||||||||||||
536 | bool QMimeData::hasColor() const | - | ||||||||||||
537 | { | - | ||||||||||||
538 | return hasFormat(QLatin1String("application/x-color")); executed 6 times by 1 test: return hasFormat(QLatin1String("application/x-color")); Executed by:
| 6 | ||||||||||||
539 | } | - | ||||||||||||
540 | - | |||||||||||||
541 | /*! | - | ||||||||||||
542 | Returns the data stored in the object in the format described by | - | ||||||||||||
543 | the MIME type specified by \a mimeType. | - | ||||||||||||
544 | */ | - | ||||||||||||
545 | QByteArray QMimeData::data(const QString &mimeType) const | - | ||||||||||||
546 | { | - | ||||||||||||
547 | Q_D(const QMimeData); | - | ||||||||||||
548 | QVariant data = d->retrieveTypedData(mimeType, QVariant::ByteArray); | - | ||||||||||||
549 | return data.toByteArray(); executed 53 times by 11 tests: return data.toByteArray(); Executed by:
| 53 | ||||||||||||
550 | } | - | ||||||||||||
551 | - | |||||||||||||
552 | /*! | - | ||||||||||||
553 | Sets the data associated with the MIME type given by \a mimeType | - | ||||||||||||
554 | to the specified \a data. | - | ||||||||||||
555 | - | |||||||||||||
556 | For the most common types of data, you can call the higher-level | - | ||||||||||||
557 | functions setText(), setHtml(), setUrls(), setImageData(), and | - | ||||||||||||
558 | setColorData() instead. | - | ||||||||||||
559 | - | |||||||||||||
560 | Note that if you want to use a custom data type in an item view drag and drop | - | ||||||||||||
561 | operation, you must register it as a Qt \l{QMetaType}{meta type}, using the | - | ||||||||||||
562 | Q_DECLARE_METATYPE() macro, and implement stream operators for it. The stream | - | ||||||||||||
563 | operators must then be registered with the qRegisterMetaTypeStreamOperators() | - | ||||||||||||
564 | function. | - | ||||||||||||
565 | - | |||||||||||||
566 | \sa hasFormat(), QMetaType, {QMetaType::}{qRegisterMetaTypeStreamOperators()} | - | ||||||||||||
567 | */ | - | ||||||||||||
568 | void QMimeData::setData(const QString &mimeType, const QByteArray &data) | - | ||||||||||||
569 | { | - | ||||||||||||
570 | Q_D(QMimeData); | - | ||||||||||||
571 | - | |||||||||||||
572 | if (mimeType == QLatin1String("text/uri-list")) {
| 1-69 | ||||||||||||
573 | QByteArray ba = data; | - | ||||||||||||
574 | if (ba.endsWith('\0'))
| 0-1 | ||||||||||||
575 | ba.chop(1); never executed: ba.chop(1); | 0 | ||||||||||||
576 | QList<QByteArray> urls = ba.split('\n'); | - | ||||||||||||
577 | QList<QVariant> list; | - | ||||||||||||
578 | for (int i = 0; i < urls.size(); ++i) {
| 1-3 | ||||||||||||
579 | QByteArray ba = urls.at(i).trimmed(); | - | ||||||||||||
580 | if (!ba.isEmpty())
| 1-2 | ||||||||||||
581 | list.append(QUrl::fromEncoded(ba)); executed 2 times by 1 test: list.append(QUrl::fromEncoded(ba)); Executed by:
| 2 | ||||||||||||
582 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
583 | d->setData(mimeType, list); | - | ||||||||||||
584 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
585 | d->setData(mimeType, QVariant(data)); | - | ||||||||||||
586 | } executed 69 times by 8 tests: end of block Executed by:
| 69 | ||||||||||||
587 | } | - | ||||||||||||
588 | - | |||||||||||||
589 | /*! | - | ||||||||||||
590 | Returns \c true if the object can return data for the MIME type | - | ||||||||||||
591 | specified by \a mimeType; otherwise returns \c false. | - | ||||||||||||
592 | - | |||||||||||||
593 | For the most common types of data, you can call the higher-level | - | ||||||||||||
594 | functions hasText(), hasHtml(), hasUrls(), hasImage(), and | - | ||||||||||||
595 | hasColor() instead. | - | ||||||||||||
596 | - | |||||||||||||
597 | \sa formats(), setData(), data() | - | ||||||||||||
598 | */ | - | ||||||||||||
599 | bool QMimeData::hasFormat(const QString &mimeType) const | - | ||||||||||||
600 | { | - | ||||||||||||
601 | return formats().contains(mimeType); executed 126 times by 13 tests: return formats().contains(mimeType); Executed by:
| 126 | ||||||||||||
602 | } | - | ||||||||||||
603 | - | |||||||||||||
604 | /*! | - | ||||||||||||
605 | Returns a list of formats supported by the object. This is a list | - | ||||||||||||
606 | of MIME types for which the object can return suitable data. The | - | ||||||||||||
607 | formats in the list are in a priority order. | - | ||||||||||||
608 | - | |||||||||||||
609 | For the most common types of data, you can call the higher-level | - | ||||||||||||
610 | functions hasText(), hasHtml(), hasUrls(), hasImage(), and | - | ||||||||||||
611 | hasColor() instead. | - | ||||||||||||
612 | - | |||||||||||||
613 | \sa hasFormat(), setData(), data() | - | ||||||||||||
614 | */ | - | ||||||||||||
615 | QStringList QMimeData::formats() const | - | ||||||||||||
616 | { | - | ||||||||||||
617 | Q_D(const QMimeData); | - | ||||||||||||
618 | QStringList list; | - | ||||||||||||
619 | for (int i=0; i<d->dataList.size(); i++)
| 124-152 | ||||||||||||
620 | list += d->dataList.at(i).format; executed 152 times by 13 tests: list += d->dataList.at(i).format; Executed by:
| 152 | ||||||||||||
621 | return list; executed 124 times by 13 tests: return list; Executed by:
| 124 | ||||||||||||
622 | } | - | ||||||||||||
623 | - | |||||||||||||
624 | /*! | - | ||||||||||||
625 | Returns a variant with the given \a type containing data for the | - | ||||||||||||
626 | MIME type specified by \a mimeType. If the object does not | - | ||||||||||||
627 | support the MIME type or variant type given, a null variant is | - | ||||||||||||
628 | returned instead. | - | ||||||||||||
629 | - | |||||||||||||
630 | This function is called by the general data() getter and by the | - | ||||||||||||
631 | convenience getters (text(), html(), urls(), imageData(), and | - | ||||||||||||
632 | colorData()). You can reimplement it if you want to store your | - | ||||||||||||
633 | data using a custom data structure (instead of a QByteArray, | - | ||||||||||||
634 | which is what setData() provides). You would then also need | - | ||||||||||||
635 | to reimplement hasFormat() and formats(). | - | ||||||||||||
636 | - | |||||||||||||
637 | \sa data() | - | ||||||||||||
638 | */ | - | ||||||||||||
639 | QVariant QMimeData::retrieveData(const QString &mimeType, QVariant::Type type) const | - | ||||||||||||
640 | { | - | ||||||||||||
641 | Q_UNUSED(type); | - | ||||||||||||
642 | Q_D(const QMimeData); | - | ||||||||||||
643 | return d->getData(mimeType); executed 137 times by 16 tests: return d->getData(mimeType); Executed by:
| 137 | ||||||||||||
644 | } | - | ||||||||||||
645 | - | |||||||||||||
646 | /*! | - | ||||||||||||
647 | Removes all the MIME type and data entries in the object. | - | ||||||||||||
648 | */ | - | ||||||||||||
649 | void QMimeData::clear() | - | ||||||||||||
650 | { | - | ||||||||||||
651 | Q_D(QMimeData); | - | ||||||||||||
652 | d->dataList.clear(); | - | ||||||||||||
653 | } executed 12 times by 2 tests: end of block Executed by:
| 12 | ||||||||||||
654 | - | |||||||||||||
655 | /*! | - | ||||||||||||
656 | \since 4.4 | - | ||||||||||||
657 | - | |||||||||||||
658 | Removes the data entry for \a mimeType in the object. | - | ||||||||||||
659 | */ | - | ||||||||||||
660 | void QMimeData::removeFormat(const QString &mimeType) | - | ||||||||||||
661 | { | - | ||||||||||||
662 | Q_D(QMimeData); | - | ||||||||||||
663 | d->removeData(mimeType); | - | ||||||||||||
664 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
665 | - | |||||||||||||
666 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |