Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qinternalmimedata.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
5
6#include <QtCore/qbuffer.h>
7#include <QtGui/qimage.h>
8#include <QtGui/qimagereader.h>
9#include <QtGui/qimagewriter.h>
10
12
13using namespace Qt::StringLiterals;
14
15static QStringList imageMimeFormats(const QList<QByteArray> &imageFormats)
16{
18 formats.reserve(imageFormats.size());
19 for (const auto &format : imageFormats)
20 formats.append("image/"_L1 + QLatin1StringView(format.toLower()));
21
22 //put png at the front because it is best
23 const qsizetype pngIndex = formats.indexOf("image/png"_L1);
24 if (pngIndex != -1 && pngIndex != 0)
25 formats.move(pngIndex, 0);
26
27 return formats;
28}
29
34
39
44
48
50{
51 bool foundFormat = hasFormat_sys(mimeType);
52 if (!foundFormat && mimeType == "application/x-qt-image"_L1) {
53 QStringList imageFormats = imageReadMimeFormats();
54 for (int i = 0; i < imageFormats.size(); ++i) {
55 if ((foundFormat = hasFormat_sys(imageFormats.at(i))))
56 break;
57 }
58 }
59 return foundFormat;
60}
61
63{
64 QStringList realFormats = formats_sys();
65 if (!realFormats.contains("application/x-qt-image"_L1)) {
66 QStringList imageFormats = imageReadMimeFormats();
67 for (int i = 0; i < imageFormats.size(); ++i) {
68 if (realFormats.contains(imageFormats.at(i))) {
69 realFormats += "application/x-qt-image"_L1;
70 break;
71 }
72 }
73 }
74 return realFormats;
75}
76
78{
80 if (mimeType == "application/x-qt-image"_L1) {
81 if (data.isNull() || (data.metaType().id() == QMetaType::QByteArray && data.toByteArray().isEmpty())) {
82 // try to find an image
83 QStringList imageFormats = imageReadMimeFormats();
84 for (int i = 0; i < imageFormats.size(); ++i) {
85 data = retrieveData_sys(imageFormats.at(i), type);
86 if (data.isNull() || (data.metaType().id() == QMetaType::QByteArray && data.toByteArray().isEmpty()))
87 continue;
88 break;
89 }
90 }
91 int typeId = type.id();
92 // we wanted some image type, but all we got was a byte array. Convert it to an image.
93 if (data.metaType().id() == QMetaType::QByteArray
94 && (typeId == QMetaType::QImage || typeId == QMetaType::QPixmap || typeId == QMetaType::QBitmap))
95 data = QImage::fromData(data.toByteArray());
96
97 } else if (mimeType == "application/x-color"_L1 && data.metaType().id() == QMetaType::QByteArray) {
98 QColor c;
99 QByteArray ba = data.toByteArray();
100 if (ba.size() == 8) {
101 ushort * colBuf = (ushort *)ba.data();
102 c.setRgbF(qreal(colBuf[0]) / qreal(0xFFFF),
103 qreal(colBuf[1]) / qreal(0xFFFF),
104 qreal(colBuf[2]) / qreal(0xFFFF),
105 qreal(colBuf[3]) / qreal(0xFFFF));
106 data = c;
107 } else {
108 qWarning("Qt: Invalid color format");
109 }
110 } else if (data.metaType() != type && data.metaType().id() == QMetaType::QByteArray) {
111 // try to use mime data's internal conversion stuf.
112 QInternalMimeData *that = const_cast<QInternalMimeData *>(this);
113 that->setData(mimeType, data.toByteArray());
115 that->clear();
116 }
117 return data;
118}
119
121{
122 return imageReadMimeFormats().contains(mimeType);
123}
124
125// helper functions for rendering mimedata to the system, this is needed because QMimeData is in core.
127{
128 QStringList realFormats = data->formats();
129 if (realFormats.contains("application/x-qt-image"_L1)) {
130 // add all supported image formats
131 QStringList imageFormats = imageWriteMimeFormats();
132 for (int i = 0; i < imageFormats.size(); ++i) {
133 if (!realFormats.contains(imageFormats.at(i)))
134 realFormats.append(imageFormats.at(i));
135 }
136 }
137 return realFormats;
138}
139
141{
142
143 bool foundFormat = data->hasFormat(mimeType);
144 if (!foundFormat) {
145 if (mimeType == "application/x-qt-image"_L1) {
146 // check all supported image formats
147 QStringList imageFormats = imageWriteMimeFormats();
148 for (int i = 0; i < imageFormats.size(); ++i) {
149 if ((foundFormat = data->hasFormat(imageFormats.at(i))))
150 break;
151 }
152 } else if (mimeType.startsWith("image/"_L1)) {
153 return data->hasImage() && imageWriteMimeFormats().contains(mimeType);
154 }
155 }
156 return foundFormat;
157}
158
160{
162 if (mimeType == "application/x-color"_L1) {
163 /* QMimeData can only provide colors as QColor or the name
164 of a color as a QByteArray or a QString. So we need to do
165 the conversion to application/x-color here.
166 The application/x-color format is :
167 type: application/x-color
168 format: 16
169 data[0]: red
170 data[1]: green
171 data[2]: blue
172 data[3]: opacity
173 */
174 ba.resize(8);
175 ushort * colBuf = (ushort *)ba.data();
176 QColor c = qvariant_cast<QColor>(data->colorData());
177 colBuf[0] = ushort(c.redF() * 0xFFFF);
178 colBuf[1] = ushort(c.greenF() * 0xFFFF);
179 colBuf[2] = ushort(c.blueF() * 0xFFFF);
180 colBuf[3] = ushort(c.alphaF() * 0xFFFF);
181 } else {
182 ba = data->data(mimeType);
183 if (ba.isEmpty()) {
184 if (mimeType == "application/x-qt-image"_L1 && data->hasImage()) {
185 QImage image = qvariant_cast<QImage>(data->imageData());
186 QBuffer buf(&ba);
188 // would there not be PNG ??
189 image.save(&buf, "PNG");
190 } else if (mimeType.startsWith("image/"_L1) && data->hasImage()) {
191 QImage image = qvariant_cast<QImage>(data->imageData());
192 QBuffer buf(&ba);
194 image.save(&buf, mimeType.mid(mimeType.indexOf(u'/') + 1).toLatin1().toUpper());
195 }
196 }
197 }
198 return ba;
199}
200
202
203#include "moc_qinternalmimedata_p.cpp"
\inmodule QtCore \reentrant
Definition qbuffer.h:16
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
void resize(qsizetype size)
Sets the size of the byte array to size bytes.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QList< QByteArray > supportedImageFormats()
Returns the list of image formats supported by QImageReader.
static QList< QByteArray > supportedImageFormats()
Returns the list of image formats supported by QImageWriter.
\inmodule QtGui
Definition qimage.h:37
static QImage fromData(QByteArrayView data, const char *format=nullptr)
Definition qimage.cpp:3841
static bool hasFormatHelper(const QString &mimeType, const QMimeData *data)
virtual bool hasFormat_sys(const QString &mimeType) const =0
static QByteArray renderDataHelper(const QString &mimeType, const QMimeData *data)
QStringList formats() const override
Returns a list of formats supported by the object.
QVariant retrieveData(const QString &mimeType, QMetaType type) const override
Returns a variant with the given type containing data for the MIME type specified by mimeType.
virtual QStringList formats_sys() const =0
bool hasFormat(const QString &mimeType) const override
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
virtual QVariant retrieveData_sys(const QString &mimeType, QMetaType type) const =0
static bool canReadData(const QString &mimeType)
static QStringList formatsHelper(const QMimeData *data)
\inmodule QtCore
Definition qmetatype.h:341
\inmodule QtCore
Definition qmimedata.h:16
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
virtual QVariant retrieveData(const QString &mimetype, QMetaType preferredType) const
Returns a variant with the given type containing data for the MIME type specified by mimeType.
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
EGLint EGLint * formats
Combined button and popup list for selecting options.
Definition image.cpp:4
const char * mimeType
static QStringList imageReadMimeFormats()
static QStringList imageMimeFormats(const QList< QByteArray > &imageFormats)
static QStringList imageWriteMimeFormats()
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLenum GLuint GLenum GLsizei const GLchar * buf
GLint GLsizei GLsizei GLenum format
const GLubyte * c
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned short ushort
Definition qtypes.h:33
double qreal
Definition qtypes.h:187
QByteArray ba
[0]