Introduce QImage{Reader,Writer}::imageFormatsForMimeType()
It can be used if, for example, you get an image MIME type from the user, and you want to find the appropriate format for loading or saving this image. Task-number: QTBUG-49714 Change-Id: Iae5a7e9d658d6c3d1cd750a8bbc279f1812f99df Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>bb10
parent
32da9ae389
commit
37217c57fa
|
|
@ -1552,4 +1552,21 @@ QList<QByteArray> QImageReader::supportedMimeTypes()
|
|||
return QImageReaderWriterHelpers::supportedMimeTypes(QImageReaderWriterHelpers::CanRead);
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.12
|
||||
|
||||
Returns the list of image formats corresponding to \mimeType.
|
||||
|
||||
Note that the QGuiApplication instance must be created before this function is
|
||||
called.
|
||||
|
||||
\sa supportedImageFormats(), supportedMimeTypes()
|
||||
*/
|
||||
|
||||
QList<QByteArray> QImageReader::imageFormatsForMimeType(const QByteArray &mimeType)
|
||||
{
|
||||
return QImageReaderWriterHelpers::imageFormatsForMimeType(mimeType,
|
||||
QImageReaderWriterHelpers::CanRead);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -144,6 +144,7 @@ public:
|
|||
static QByteArray imageFormat(QIODevice *device);
|
||||
static QList<QByteArray> supportedImageFormats();
|
||||
static QList<QByteArray> supportedMimeTypes();
|
||||
static QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType);
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QImageReader)
|
||||
|
|
|
|||
|
|
@ -78,7 +78,8 @@ static void appendImagePluginFormats(QFactoryLoader *loader,
|
|||
|
||||
static void appendImagePluginMimeTypes(QFactoryLoader *loader,
|
||||
QImageIOPlugin::Capability cap,
|
||||
QList<QByteArray> *result)
|
||||
QList<QByteArray> *result,
|
||||
QList<QByteArray> *resultKeys = nullptr)
|
||||
{
|
||||
QList<QJsonObject> metaDataList = loader->metaData();
|
||||
|
||||
|
|
@ -90,8 +91,12 @@ static void appendImagePluginMimeTypes(QFactoryLoader *loader,
|
|||
QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
|
||||
const int keyCount = keys.size();
|
||||
for (int k = 0; k < keyCount; ++k) {
|
||||
if (plugin && (plugin->capabilities(0, keys.at(k).toString().toLatin1()) & cap) != 0)
|
||||
const QByteArray key = keys.at(k).toString().toLatin1();
|
||||
if (plugin && (plugin->capabilities(0, key) & cap) != 0) {
|
||||
result->append(mimeTypes.at(k).toString().toLatin1());
|
||||
if (resultKeys)
|
||||
resultKeys->append(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -143,6 +148,33 @@ QList<QByteArray> supportedMimeTypes(Capability cap)
|
|||
return mimeTypes;
|
||||
}
|
||||
|
||||
QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType, Capability cap)
|
||||
{
|
||||
QList<QByteArray> formats;
|
||||
if (mimeType.startsWith("image/")) {
|
||||
const QByteArray type = mimeType.mid(sizeof("image/") - 1);
|
||||
for (const auto &fmt : _qt_BuiltInFormats) {
|
||||
if (fmt.mimeType == type && !formats.contains(fmt.extension))
|
||||
formats << fmt.extension;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef QT_NO_IMAGEFORMATPLUGIN
|
||||
QList<QByteArray> mimeTypes;
|
||||
QList<QByteArray> keys;
|
||||
appendImagePluginMimeTypes(loader(), pluginCapability(cap), &mimeTypes, &keys);
|
||||
for (int i = 0; i < mimeTypes.size(); ++i) {
|
||||
if (mimeTypes.at(i) == mimeType) {
|
||||
const auto &key = keys.at(i);
|
||||
if (!formats.contains(key))
|
||||
formats << key;
|
||||
}
|
||||
}
|
||||
#endif // QT_NO_IMAGEFORMATPLUGIN
|
||||
|
||||
return formats;
|
||||
}
|
||||
|
||||
} // QImageReaderWriterHelpers
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -130,6 +130,7 @@ enum Capability {
|
|||
};
|
||||
QList<QByteArray> supportedImageFormats(Capability cap);
|
||||
QList<QByteArray> supportedMimeTypes(Capability cap);
|
||||
QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType, Capability cap);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -863,4 +863,21 @@ QList<QByteArray> QImageWriter::supportedMimeTypes()
|
|||
return QImageReaderWriterHelpers::supportedMimeTypes(QImageReaderWriterHelpers::CanWrite);
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.12
|
||||
|
||||
Returns the list of image formats corresponding to \mimeType.
|
||||
|
||||
Note that the QGuiApplication instance must be created before this function is
|
||||
called.
|
||||
|
||||
\sa supportedImageFormats(), supportedMimeTypes()
|
||||
*/
|
||||
|
||||
QList<QByteArray> QImageWriter::imageFormatsForMimeType(const QByteArray &mimeType)
|
||||
{
|
||||
return QImageReaderWriterHelpers::imageFormatsForMimeType(mimeType,
|
||||
QImageReaderWriterHelpers::CanWrite);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ public:
|
|||
|
||||
static QList<QByteArray> supportedImageFormats();
|
||||
static QList<QByteArray> supportedMimeTypes();
|
||||
static QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType);
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QImageWriter)
|
||||
|
|
|
|||
Loading…
Reference in New Issue