1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qmimedata.h"
31
#include "private/qobject_p.h"
33
#include "qstringlist.h"
41
class QMimeDataPrivate : public QObjectPrivate
44
void setData(const QString &format, const QVariant &data);
45
QVariant getData(const QString &format) const;
46
QList<MimeData> dataList;
49
void QMimeDataPrivate::setData(const QString &format, const QVariant &data)
51
// remove it first if the format is already here.
52
for (int i=0; i<dataList.size(); i++) {
53
if (dataList.at(i).format == format) {
59
mimeData.format = format;
65
QVariant QMimeDataPrivate::getData(const QString &format) const
68
for (int i=0; i<dataList.size(); i++) {
69
if (dataList.at(i).format == format) {
70
data = dataList.at(i).data;
79
\brief The QMimeData class provides a container for data that records information
82
QMimeData is used to describe information that can be stored in the clipboard,
83
and transferred via the drag and drop mechanism. QMimeData objects associate the
84
data that they hold with the corresponding MIME types to ensure that information
85
can be safely transferred between applications, and copied around within the
88
QMimeData objects are usually created on the heap and supplied to QDrag
89
or QClipboard objects. This is to enable Qt to manage the memory that they
92
The class provides a number of convenience functions to allow data in common
93
formats to be stored and retrieved, and QMimeData objects can be queried to
94
determine which kind of data they contain.
96
Textual data types are stored with setText() and setHtml(); they can be retrieved
97
with text() and html(). Visual data types are stored with setColorData() and
98
setImageData(); they can be retrieved with colorData() and imageData().
99
The contents of the QMimeData object can be cleared with the clear() function.
101
Use the hasText() and hasHtml() functions to determine whether a given QMimeData
102
object contains textual information; use hasColor() and hasImage()
103
to determine whether it contains standard visual types.
105
Custom data can be stored in a QMimeData object: Use the setData() function
106
with a standard MIME description of the data, and a QByteArray containing the
107
data itself. For example, although we could store an image using
108
setImage(), we can take a Portable Network Graphics (PNG) image
109
from a QByteArray and explicitly store it in a QMimeData object using the
114
QMimeData *mimeData = new QMimeData;
115
mimeData->setData("image/png", pngImage);
118
Usually, it is easier to rely on QMimeData's support for QImage and QPixmap when
121
\sa QClipboard, QDragEnterEvent, QDragMoveEvent, QDropEvent, QDrag,
126
Constructs a new MIME data object.
128
QMimeData::QMimeData()
129
: QObject(*new QMimeDataPrivate, 0)
134
Destroys the MIME data object.
136
QMimeData::~QMimeData()
141
Returns a list of URLs contained within the MIME data object.
143
QList<QUrl> QMimeData::urls() const
145
QVariant data = retrieveData("text/uri-list", QVariant::Url);
147
if (data.type() == QVariant::Url)
148
urls.append(data.toUrl());
149
else if (data.type() == QVariant::List) {
150
QList<QVariant> list = data.toList();
151
for (int i = 0; i < list.size(); ++i) {
152
if (list.at(i).type() == QVariant::Url)
153
urls.append(list.at(i).toUrl());
160
Sets the URLs stored in the MIME data object to those specified by \a urls.
162
void QMimeData::setUrls(const QList<QUrl> &urls)
165
QList<QVariant> list;
166
for (int i = 0; i < urls.size(); ++i)
167
list.append(urls.at(i));
169
d->setData("text/uri-list", list);
173
Returns true if the object can return a list of urls otherwise returns false.
175
bool QMimeData::hasUrls() const
177
return hasFormat("text/uri-list");
182
Returns a plain text representation of the data.
184
QString QMimeData::text() const
186
QVariant data = retrieveData("text/plain", QVariant::String);
187
if (data.type() == QVariant::ByteArray)
188
return QString::fromUtf8(data.toByteArray());
189
else if (data.type() == QVariant::String)
190
return data.toString();
195
Sets \a text as the plain text used to represent the data.
197
void QMimeData::setText(const QString &text)
200
d->setData("text/plain", text);
204
Returns true if the object can return text otherwise returns false.
206
bool QMimeData::hasText() const
208
return hasFormat("text/plain");
212
Returns a string if the data stored in the object is HTML;
213
otherwise returns an empty string.
215
QString QMimeData::html() const
217
QVariant data = retrieveData("text/html", QVariant::String);
218
if (data.type() == QVariant::ByteArray)
219
return QString::fromUtf8(data.toByteArray());
220
else if (data.type() == QVariant::String)
221
return data.toString();
226
Sets the data in the object to the HTML in the \a html string.
228
void QMimeData::setHtml(const QString &html)
231
d->setData("text/html", html);
235
Returns true if the object can return HTML otherwise returns false.
237
bool QMimeData::hasHtml() const
239
return hasFormat("text/html");
243
Returns an image variant if the data stored in the object is in the correct
244
form; otherwise returns an invalid variant.
246
QVariant QMimeData::imageData() const
248
return retrieveData("application/x-qt-image", QVariant::Image);
252
Sets the data in the object to the given \a image.
254
void QMimeData::setImageData(const QVariant &image)
257
d->setData("application/x-qt-image", image);
261
Returns true if the object can return a image otherwise returns false.
263
bool QMimeData::hasImage() const
265
return hasFormat("application/x-qt-image");
269
Returns a color if the data stored in the object represents a color;
270
otherwise returns an invalid variant.
272
QVariant QMimeData::colorData() const
274
QVariant data = retrieveData("application/x-color", QVariant::Color);
275
if (data.type() == QVariant::Color)
282
Sets the data in the object to the given \a color.
284
void QMimeData::setColorData(const QVariant &color)
287
d->setData("application/x-color", color);
292
Returns true if the object can return a color otherwise returns false.
294
bool QMimeData::hasColor() const
296
return hasFormat("application/x-color");
300
Returns the data stored in the object in the format described by the
301
MIME type specified by \a mimetype.
303
QByteArray QMimeData::data(const QString &mimetype) const
305
QVariant data = retrieveData(mimetype, QVariant::ByteArray);
306
return data.toByteArray();
310
Sets the data associated with the MIME type given by \a mimetype to the
313
void QMimeData::setData(const QString &mimetype, const QByteArray &data)
316
d->setData(mimetype, QVariant(data));
320
Returns true if the object can return data for the MIME type specified by
321
\a mimetype; otherwise returns false.
323
bool QMimeData::hasFormat(const QString &mimetype) const
325
return formats().contains(mimetype);
329
Returns a list of formats supported by the object. This is a list of
330
MIME types for which the object can return suitable data. The formats in
331
the list are in a priority order.
333
QStringList QMimeData::formats() const
335
Q_D(const QMimeData);
337
for (int i=0; i<d->dataList.size(); i++)
338
list += d->dataList.at(i).format;
343
Returns a variant with the given \a type containing data for the MIME
344
type specified by \a mimetype. If the object does not support the
345
MIME type or variant type given, a null variant is returned instead.
347
QVariant QMimeData::retrieveData(const QString &mimetype, QVariant::Type type) const
349
Q_D(const QMimeData);
350
QVariant data = d->getData(mimetype);
351
if (data.type() == type || type == QVariant::Invalid)
354
// URLs can be lists as well...
355
if (type == QVariant::Url && data.type() == QVariant::List)
358
// images and pixmaps are interchangeable
359
if (type == QVariant::Pixmap && data.type() == QVariant::Image
360
|| type == QVariant::Image && data.type() == QVariant::Pixmap)
363
if (data.type() == QVariant::ByteArray) {
364
QByteArray ba = data.toByteArray();
365
// see if we can convert to the requested type
367
case QVariant::String:
368
return QString::fromUtf8(ba);
369
case QVariant::Color:
370
data.convert(QVariant::Color);
372
case QVariant::Url: {
373
QList<QVariant> list;
374
QList<QByteArray> urls = data.toByteArray().split('\n');
375
for (int i = 0; i < urls.size(); ++i) {
376
QByteArray ba = urls.at(i).trimmed();
377
list.append(QUrl::fromEncoded(ba));
386
// try to convert to bytearray
388
switch(data.type()) {
389
case QVariant::ByteArray:
390
case QVariant::Color:
391
result = data.toByteArray();
393
case QVariant::String:
394
result = data.toString().toUtf8();
397
result = data.toUrl().toEncoded();
399
case QVariant::List: {
400
// has to be list of URLs
401
QList<QVariant> list = data.toList();
402
for (int i = 0; i < list.size(); ++i) {
403
if (list.at(i).type() == QVariant::Url) {
404
result += list.at(i).toUrl().toEncoded();
417
Removes all the MIME type and data entries in the object.
419
void QMimeData::clear()