1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qwaylandmimehelper_p.h"
5#include <QImage>
6#include <QColor>
7#include <QUrl>
8#include <QBuffer>
9#include <QImageWriter>
10
11QT_BEGIN_NAMESPACE
12
13QByteArray QWaylandMimeHelper::getByteArray(QMimeData *mimeData, const QString &mimeType)
14{
15 QByteArray content;
16 if (mimeType == QLatin1String("text/plain")) {
17 content = mimeData->text().toUtf8();
18 } else if (mimeData->hasImage()
19 && (mimeType == QLatin1String("application/x-qt-image")
20 || mimeType.startsWith(s: QLatin1String("image/")))) {
21 QImage image = qvariant_cast<QImage>(v: mimeData->imageData());
22 if (!image.isNull()) {
23 QBuffer buf;
24 buf.open(openMode: QIODevice::ReadWrite);
25 QByteArray fmt = "BMP";
26 if (mimeType.startsWith(s: QLatin1String("image/"))) {
27 QByteArray imgFmt = mimeType.mid(position: 6).toLower().toLatin1();
28 if (QImageWriter::supportedImageFormats().contains(t: imgFmt))
29 fmt = imgFmt;
30 }
31 QImageWriter wr(&buf, fmt);
32 wr.write(image);
33 content = buf.buffer();
34 }
35 } else if (mimeType == QLatin1String("application/x-color")) {
36 content = qvariant_cast<QColor>(v: mimeData->colorData()).name().toLatin1();
37 } else if (mimeType == QLatin1String("text/uri-list")) {
38 QList<QUrl> urls = mimeData->urls();
39 for (int i = 0; i < urls.size(); ++i) {
40 content.append(a: urls.at(i).toEncoded());
41 content.append(s: "\r\n");
42 }
43 } else {
44 content = mimeData->data(mimetype: mimeType);
45 }
46 return content;
47}
48
49QT_END_NAMESPACE
50

source code of qtwayland/src/shared/qwaylandmimehelper.cpp