From d64f08fdc2308073fbd44f70b3ccfddc78d3a5cf Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 28 Feb 2022 01:44:47 +0200 Subject: [PATCH] new QImageWriter::defaultImageFormat() method to get the prefered/best image format Signed-off-by: Ivailo Monev --- src/gui/image/qimage.cpp | 6 +++--- src/gui/image/qimagewriter.cpp | 24 ++++++++++++++++-------- src/gui/image/qimagewriter.h | 1 + src/gui/image/qpixmap.cpp | 2 +- src/gui/kernel/qapplication.cpp | 2 +- src/gui/kernel/qdnd.cpp | 24 ++++++++++++------------ src/gui/painting/qpaintengine_raster.cpp | 4 ++-- src/gui/qguicommon_p.h | 8 ++++++++ src/tools/designer/qdesigner_actions.cpp | 7 +++---- 9 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 06b30ed56..2cf5a5d30 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -295,7 +295,7 @@ bool QImageData::checkForAlphaPixels() const The complete list of supported file formats are available through the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions. New file formats - can be added as plugins. By default, Qt supports the following + can be added as plugins. By default, Katie supports the following formats: \table @@ -2992,8 +2992,8 @@ QImage QImage::fromData(const char *data, int size, const char *format) suffix. The \a quality factor must be in the range 0 to 100 or -1. Specify - 0 to obtain small compressed files, 100 for large uncompressed - files, and -1 (the default) to use the default settings. + 0 to obtain small files, 100 for large and -1 (the default) to use + the default settings. Returns true if the image was successfully saved; otherwise returns false. diff --git a/src/gui/image/qimagewriter.cpp b/src/gui/image/qimagewriter.cpp index e8ae8f3fa..6d6d7eb64 100644 --- a/src/gui/image/qimagewriter.cpp +++ b/src/gui/image/qimagewriter.cpp @@ -236,7 +236,8 @@ QImageWriter::~QImageWriter() \snippet doc/src/snippets/code/src_gui_image_qimagewriter.cpp 0 You can call supportedImageFormats() for the full list of formats - QImageWriter supports. + QImageWriter supports and defaultImageFormat() for the prefered + format. \sa format() */ @@ -494,9 +495,19 @@ bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const } /*! + Returns the default image format to use with QImageWriter. + + \sa supportedImageFormats() +*/ +QByteArray QImageWriter::defaultImageFormat() +{ + return QByteArray(qt_imageformat); +} + +/*! Returns the list of image formats supported by QImageWriter. - By default, Qt can write the following formats: + By default, Katie can write the following formats: \table \header \o Format \o Description @@ -505,13 +516,14 @@ bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const \row \o PPM \o Portable Pixmap \endtable - Reading and writing SVG files is supported through Qt's + Reading and writing SVG files is supported through Katie's \l{QtSvg Module}{SVG Module}. Note that the QApplication instance must be created before this function is called. - \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin + \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin, + defaultImageFormat() */ QList QImageWriter::supportedImageFormats() { @@ -538,7 +550,3 @@ QList QImageWriter::supportedImageFormats() } QT_END_NAMESPACE - - - - diff --git a/src/gui/image/qimagewriter.h b/src/gui/image/qimagewriter.h index 4733cae08..426d272ef 100644 --- a/src/gui/image/qimagewriter.h +++ b/src/gui/image/qimagewriter.h @@ -73,6 +73,7 @@ public: bool supportsOption(QImageIOHandler::ImageOption option) const; + static QByteArray defaultImageFormat(); static QList supportedImageFormats(); private: diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index 033133a29..f6b7d0238 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -1329,7 +1329,7 @@ QPixmap QPixmap::transformed(const QMatrix &matrix, Qt::TransformationMode mode) The complete list of supported file formats are available through the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions. New file formats - can be added as plugins. By default, Qt supports the following + can be added as plugins. By default, Katie supports the following formats: \table diff --git a/src/gui/kernel/qapplication.cpp b/src/gui/kernel/qapplication.cpp index ddc4f7133..bf6563518 100644 --- a/src/gui/kernel/qapplication.cpp +++ b/src/gui/kernel/qapplication.cpp @@ -3878,7 +3878,7 @@ void QApplicationPrivate::emitLastWindowClosed() Returns true if \a effect is enabled; otherwise returns false. - By default, Qt will try to use the desktop settings. To prevent this, call + By default, Katie will try to use the desktop settings. To prevent this, call setDesktopSettingsAware(false). \note All effects are disabled on screens running at less than 16-bit color diff --git a/src/gui/kernel/qdnd.cpp b/src/gui/kernel/qdnd.cpp index 036c02f1f..4cb8cf577 100644 --- a/src/gui/kernel/qdnd.cpp +++ b/src/gui/kernel/qdnd.cpp @@ -38,9 +38,10 @@ #include "qimagereader.h" #include "qimagewriter.h" #include "qdebug.h" -#include - #include "qapplication_p.h" +#include "qguicommon_p.h" + +#include #ifndef QT_NO_DRAGANDDROP @@ -220,10 +221,10 @@ static QStringList imageReadMimeFormats() formats.append(format); } - //put png at the front because it is best - int pngIndex = formats.indexOf(QLatin1String("image/png")); - if (pngIndex != -1 && pngIndex != 0) - formats.move(pngIndex, 0); + // put the best in front + int bestIndex = formats.indexOf(QLatin1String(qt_imagemime)); + if (bestIndex > 0) + formats.move(bestIndex, 0); return formats; } @@ -238,10 +239,10 @@ static QStringList imageWriteMimeFormats() formats.append(format); } - //put png at the front because it is best - int pngIndex = formats.indexOf(QLatin1String("image/png")); - if (pngIndex != -1 && pngIndex != 0) - formats.move(pngIndex, 0); + // put the best in front + int bestIndex = formats.indexOf(QLatin1String(qt_imagemime)); + if (bestIndex > 0) + formats.move(bestIndex, 0); return formats; } @@ -382,8 +383,7 @@ QByteArray QInternalMimeData::renderDataHelper(const QString &mimeType, const QM QImage image = qvariant_cast(data->imageData()); QBuffer buf(&ba); buf.open(QBuffer::WriteOnly); - // would there not be PNG ?? - image.save(&buf, "PNG"); + image.save(&buf, qt_imageformat); } else if (mimeType.startsWith(QLatin1String("image/")) && data->hasImage()) { QImage image = qvariant_cast(data->imageData()); QBuffer buf(&ba); diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index f8090b2e8..54ff557b6 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -311,7 +311,7 @@ bool QRasterPaintEngine::end() void QRasterPaintEngine::saveBuffer(const QString &s) const { Q_D(const QRasterPaintEngine); - d->rasterBuffer->bufferImage().save(s, "PNG"); + d->rasterBuffer->bufferImage().save(s, qt_imageformat); } #endif @@ -2888,7 +2888,7 @@ void dumpClip(int width, int height, const QClipData *clip) Q_ASSERT(x1 >= 0); fprintf(stderr,"clip %d: %d %d - %d %d\n", counter, x0, y0, x1, y1); - clipImg.save(QString::fromLatin1("clip-%0.png").arg(counter++)); + clipImg.save(QString::fromLatin1("clip-%1.%2").arg(counter++).arg(qt_imageformat); } #endif diff --git a/src/gui/qguicommon_p.h b/src/gui/qguicommon_p.h index 7f0c28a5b..6d4858a55 100644 --- a/src/gui/qguicommon_p.h +++ b/src/gui/qguicommon_p.h @@ -11,6 +11,14 @@ QT_BEGIN_NAMESPACE +#ifndef QT_NO_IMAGEFORMAT_KAT +static const char* const qt_imageformat = "kat"; +static const char* const qt_imagemime = "image/katie"; +#else +static const char* const qt_imageformat = "png"; +static const char* const qt_imagemime = "image/png"; +#endif + #ifdef QT_NO_FPU static const qreal qt_epsilon = qreal(1e-12); #else diff --git a/src/tools/designer/qdesigner_actions.cpp b/src/tools/designer/qdesigner_actions.cpp index 1719c3912..bc5e2ed30 100644 --- a/src/tools/designer/qdesigner_actions.cpp +++ b/src/tools/designer/qdesigner_actions.cpp @@ -80,6 +80,7 @@ #include #include #include +#include "qguicommon_p.h" QT_BEGIN_NAMESPACE @@ -1210,14 +1211,12 @@ qdesigner_internal::PreviewConfiguration QDesignerActions::previewConfiguration( void QDesignerActions::savePreviewImage() { - const char *format = "png"; - QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow(); if (!fw) return; QImage image; - const QString extension = QString::fromAscii(format); + const QString extension = QString::fromAscii(qt_imageformat); const QString filter = tr("Image files (*.%1)").arg(extension); QString suggestion = fw->fileName(); @@ -1239,7 +1238,7 @@ void QDesignerActions::savePreviewImage() image = pixmap.toImage(); } - if (image.save(fileName, format)) { + if (image.save(fileName, qt_imageformat)) { showStatusBarMessage(tr("Saved image %1.").arg(QFileInfo(fileName).fileName())); break; } -- 2.11.0