~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qmimedata.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 **
 
3
 ** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
 **
 
5
 ** This file is part of the core module of the Qt Toolkit.
 
6
 **
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
 **
 
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.
 
26
 **
 
27
 ****************************************************************************/
 
28
 
 
29
#include "qmimedata.h"
 
30
 
 
31
#include "private/qobject_p.h"
 
32
#include "qurl.h"
 
33
#include "qstringlist.h"
 
34
 
 
35
struct MimeData
 
36
{
 
37
    QString format;
 
38
    QVariant data;
 
39
};
 
40
 
 
41
class QMimeDataPrivate : public QObjectPrivate
 
42
{
 
43
public:
 
44
    void setData(const QString &format, const QVariant &data);
 
45
    QVariant getData(const QString &format) const;
 
46
    QList<MimeData> dataList;
 
47
};
 
48
 
 
49
void QMimeDataPrivate::setData(const QString &format, const QVariant &data)
 
50
{
 
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) {
 
54
            dataList.removeAt(i);
 
55
            break;
 
56
        }
 
57
    }
 
58
    MimeData mimeData;
 
59
    mimeData.format = format;
 
60
    mimeData.data = data;
 
61
    dataList += mimeData;
 
62
}
 
63
 
 
64
 
 
65
QVariant QMimeDataPrivate::getData(const QString &format) const
 
66
{
 
67
    QVariant data;
 
68
    for (int i=0; i<dataList.size(); i++) {
 
69
        if (dataList.at(i).format == format) {
 
70
            data = dataList.at(i).data;
 
71
            break;
 
72
        }
 
73
    }
 
74
    return data;
 
75
}
 
76
 
 
77
/*!
 
78
    \class QMimeData
 
79
    \brief The QMimeData class provides a container for data that records information
 
80
    about its MIME type.
 
81
 
 
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
 
86
    same application.
 
87
 
 
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
 
90
    use.
 
91
 
 
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.
 
95
 
 
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.
 
100
 
 
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.
 
104
 
 
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
 
110
    following code:
 
111
 
 
112
    \code
 
113
        QByteArray pngImage;
 
114
        QMimeData *mimeData = new QMimeData;
 
115
        mimeData->setData("image/png", pngImage);
 
116
    \endcode
 
117
 
 
118
    Usually, it is easier to rely on QMimeData's support for QImage and QPixmap when
 
119
    handling images.
 
120
 
 
121
    \sa QClipboard, QDragEnterEvent, QDragMoveEvent, QDropEvent, QDrag,
 
122
        {Drag and Drop}
 
123
*/
 
124
 
 
125
/*!
 
126
    Constructs a new MIME data object.
 
127
*/
 
128
QMimeData::QMimeData()
 
129
    : QObject(*new QMimeDataPrivate, 0)
 
130
{
 
131
}
 
132
 
 
133
/*!
 
134
    Destroys the MIME data object.
 
135
*/
 
136
QMimeData::~QMimeData()
 
137
{
 
138
}
 
139
 
 
140
/*!
 
141
    Returns a list of URLs contained within the MIME data object.
 
142
*/
 
143
QList<QUrl> QMimeData::urls() const
 
144
{
 
145
    QVariant data = retrieveData("text/uri-list", QVariant::Url);
 
146
    QList<QUrl> urls;
 
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());
 
154
        }
 
155
    }
 
156
    return urls;
 
157
}
 
158
 
 
159
/*!
 
160
    Sets the URLs stored in the MIME data object to those specified by \a urls.
 
161
*/
 
162
void QMimeData::setUrls(const QList<QUrl> &urls)
 
163
{
 
164
    Q_D(QMimeData);
 
165
    QList<QVariant> list;
 
166
    for (int i = 0; i < urls.size(); ++i)
 
167
        list.append(urls.at(i));
 
168
 
 
169
    d->setData("text/uri-list", list);
 
170
}
 
171
 
 
172
/*!
 
173
    Returns true if the object can return a list of urls otherwise returns false.
 
174
*/
 
175
bool QMimeData::hasUrls() const
 
176
{
 
177
    return hasFormat("text/uri-list");
 
178
}
 
179
 
 
180
 
 
181
/*!
 
182
    Returns a plain text representation of the data.
 
183
*/
 
184
QString QMimeData::text() const
 
185
{
 
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();
 
191
    return QString();
 
192
}
 
193
 
 
194
/*!
 
195
    Sets \a text as the plain text used to represent the data.
 
196
*/
 
197
void QMimeData::setText(const QString &text)
 
198
{
 
199
    Q_D(QMimeData);
 
200
    d->setData("text/plain", text);
 
201
}
 
202
 
 
203
/*!
 
204
    Returns true if the object can return text otherwise returns false.
 
205
*/
 
206
bool QMimeData::hasText() const
 
207
{
 
208
    return hasFormat("text/plain");
 
209
}
 
210
 
 
211
/*!
 
212
    Returns a string if the data stored in the object is HTML;
 
213
    otherwise returns an empty string.
 
214
*/
 
215
QString QMimeData::html() const
 
216
{
 
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();
 
222
    return QString();
 
223
}
 
224
 
 
225
/*!
 
226
    Sets the data in the object to the HTML in the \a html string.
 
227
*/
 
228
void QMimeData::setHtml(const QString &html)
 
229
{
 
230
    Q_D(QMimeData);
 
231
    d->setData("text/html", html);
 
232
}
 
233
 
 
234
/*!
 
235
    Returns true if the object can return HTML otherwise returns false.
 
236
*/
 
237
bool QMimeData::hasHtml() const
 
238
{
 
239
    return hasFormat("text/html");
 
240
}
 
241
 
 
242
/*!
 
243
    Returns an image variant if the data stored in the object is in the correct
 
244
    form; otherwise returns an invalid variant.
 
245
*/
 
246
QVariant QMimeData::imageData() const
 
247
{
 
248
    return retrieveData("application/x-qt-image", QVariant::Image);
 
249
}
 
250
 
 
251
/*!
 
252
    Sets the data in the object to the given \a image.
 
253
*/
 
254
void QMimeData::setImageData(const QVariant &image)
 
255
{
 
256
    Q_D(QMimeData);
 
257
    d->setData("application/x-qt-image", image);
 
258
}
 
259
 
 
260
/*!
 
261
    Returns true if the object can return a image otherwise returns false.
 
262
*/
 
263
bool QMimeData::hasImage() const
 
264
{
 
265
    return hasFormat("application/x-qt-image");
 
266
}
 
267
 
 
268
/*!
 
269
    Returns a color if the data stored in the object represents a color;
 
270
    otherwise returns an invalid variant.
 
271
*/
 
272
QVariant QMimeData::colorData() const
 
273
{
 
274
    QVariant data = retrieveData("application/x-color", QVariant::Color);
 
275
    if (data.type() == QVariant::Color)
 
276
        return data;
 
277
    // ### try to decode
 
278
    return QVariant();
 
279
}
 
280
 
 
281
/*!
 
282
    Sets the data in the object to the given \a color.
 
283
*/
 
284
void QMimeData::setColorData(const QVariant &color)
 
285
{
 
286
    Q_D(QMimeData);
 
287
    d->setData("application/x-color", color);
 
288
}
 
289
 
 
290
 
 
291
/*!
 
292
    Returns true if the object can return a color otherwise returns false.
 
293
*/
 
294
bool QMimeData::hasColor() const
 
295
{
 
296
    return hasFormat("application/x-color");
 
297
}
 
298
 
 
299
/*!
 
300
    Returns the data stored in the object in the format described by the
 
301
    MIME type specified by \a mimetype.
 
302
*/
 
303
QByteArray QMimeData::data(const QString &mimetype) const
 
304
{
 
305
    QVariant data = retrieveData(mimetype, QVariant::ByteArray);
 
306
    return data.toByteArray();
 
307
}
 
308
 
 
309
/*!
 
310
    Sets the data associated with the MIME type given by \a mimetype to the
 
311
    specified \a data.
 
312
*/
 
313
void QMimeData::setData(const QString &mimetype, const QByteArray &data)
 
314
{
 
315
    Q_D(QMimeData);
 
316
    d->setData(mimetype, QVariant(data));
 
317
}
 
318
 
 
319
/*!
 
320
    Returns true if the object can return data for the MIME type specified by
 
321
    \a mimetype; otherwise returns false.
 
322
*/
 
323
bool QMimeData::hasFormat(const QString &mimetype) const
 
324
{
 
325
    return formats().contains(mimetype);
 
326
}
 
327
 
 
328
/*!
 
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.
 
332
*/
 
333
QStringList QMimeData::formats() const
 
334
{
 
335
    Q_D(const QMimeData);
 
336
    QStringList list;
 
337
    for (int i=0; i<d->dataList.size(); i++)
 
338
        list += d->dataList.at(i).format;
 
339
    return list;
 
340
}
 
341
 
 
342
/*!
 
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.
 
346
*/
 
347
QVariant QMimeData::retrieveData(const QString &mimetype, QVariant::Type type) const
 
348
{
 
349
    Q_D(const QMimeData);
 
350
    QVariant data = d->getData(mimetype);
 
351
    if (data.type() == type || type == QVariant::Invalid)
 
352
        return data;
 
353
 
 
354
    // URLs can be lists as well...
 
355
    if (type == QVariant::Url && data.type() == QVariant::List)
 
356
        return data;
 
357
 
 
358
    // images and pixmaps are interchangeable
 
359
    if (type == QVariant::Pixmap && data.type() == QVariant::Image
 
360
        || type == QVariant::Image && data.type() == QVariant::Pixmap)
 
361
        return data;
 
362
 
 
363
    if (data.type() == QVariant::ByteArray) {
 
364
        QByteArray ba = data.toByteArray();
 
365
        // see if we can convert to the requested type
 
366
        switch(type) {
 
367
        case QVariant::String:
 
368
            return QString::fromUtf8(ba);
 
369
        case QVariant::Color:
 
370
            data.convert(QVariant::Color);
 
371
            return data;
 
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));
 
378
            }
 
379
            return list;
 
380
        }
 
381
        default:
 
382
            break;
 
383
        }
 
384
    }
 
385
 
 
386
    // try to convert to bytearray
 
387
    QByteArray result;
 
388
    switch(data.type()) {
 
389
    case QVariant::ByteArray:
 
390
    case QVariant::Color:
 
391
        result = data.toByteArray();
 
392
        break;
 
393
    case QVariant::String:
 
394
        result = data.toString().toUtf8();
 
395
        break;
 
396
    case QVariant::Url:
 
397
        result = data.toUrl().toEncoded();
 
398
        break;
 
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();
 
405
                result += "\r\n";
 
406
            }
 
407
        }
 
408
        break;
 
409
    }
 
410
    default:
 
411
        break;
 
412
    }
 
413
    return result;
 
414
}
 
415
 
 
416
/*!
 
417
    Removes all the MIME type and data entries in the object.
 
418
*/
 
419
void QMimeData::clear()
 
420
{
 
421
    Q_D(QMimeData);
 
422
    d->dataList.clear();
 
423
}
 
424
 
 
425