Add image quality handling to QTextImageFormat

This patch enhances QTextImageFormat with a property for image quality.
Default will be quality of 100. The user may set different values with
setQuality(int). QTextODFexport will export images as png if quality is
set to 100 and as jpg with the compression quality set to the given value
if smaller than 100.

[ChangeLog][QtGui][QTextImageFormat] Adds two new functions to the class:
setQuality(int=100) and quality(). Is currently used by QTextODFWriter to
determine the image type and quality when exporting images to ODT files.

Change-Id: Iaa8ec0246aaba004d98c9e8c66609795101519a9
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Daniel Savi 2018-02-28 16:10:32 +01:00 committed by André Hartmann
parent b3bbba7b87
commit 84eb9b5e30
3 changed files with 44 additions and 6 deletions

View File

@ -3077,7 +3077,8 @@ QTextTableFormat::QTextTableFormat(const QTextFormat &fmt)
REPLACEMENT CHARACTER) which has an associated QTextImageFormat. The
image format specifies a name with setName() that is used to
locate the image. The size of the rectangle that the image will
occupy is specified using setWidth() and setHeight().
occupy is specified in pixels using setWidth() and setHeight().
The desired image quality may be set with setQuality().
Images can be supplied in any format for which Qt has an image
reader, so SVG drawings can be included alongside PNG, TIFF and
@ -3166,6 +3167,28 @@ QTextImageFormat::QTextImageFormat(const QTextFormat &fmt)
\sa width(), setHeight()
*/
/*!
\fn void QTextImageFormat::setQuality(int quality = 100)
\since 5.12
Sets the quality that should be used by exporters when exporting the image. QTextDocumentWriter
will export jpg images with the \a quality set here when exporting to ODF files if \a quality is
set to a value between 0 and 100. Or it will export png images if \a quality is set to 100
(default) or greater.
\sa quality()
*/
/*!
\fn qreal QTextImageFormat::quality() const
\since 5.12
Returns the value set by setQuality().
\sa setQuality()
*/
/*!
\fn void QTextCharFormat::setFontCapitalization(QFont::Capitalization capitalization)
\since 4.4

View File

@ -250,6 +250,7 @@ public:
ImageName = 0x5000,
ImageWidth = 0x5010,
ImageHeight = 0x5011,
ImageQuality = 0x5014,
// internal
/*
@ -754,6 +755,10 @@ public:
inline qreal height() const
{ return doubleProperty(ImageHeight); }
inline void setQuality(int quality = 100);
inline int quality() const
{ return intProperty(ImageQuality); }
protected:
explicit QTextImageFormat(const QTextFormat &format);
friend class QTextFormat;
@ -770,6 +775,9 @@ inline void QTextImageFormat::setWidth(qreal awidth)
inline void QTextImageFormat::setHeight(qreal aheight)
{ setProperty(ImageHeight, aheight); }
inline void QTextImageFormat::setQuality(int aquality)
{ setProperty(ImageQuality, aquality); }
class Q_GUI_EXPORT QTextFrameFormat : public QTextFormat
{
public:

View File

@ -420,7 +420,6 @@ void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextF
QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());
// vvv Copy pasted mostly from Qt =================
QImage image;
QString name = imageFormat.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
@ -442,11 +441,19 @@ void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextF
if (! image.isNull()) {
QBuffer imageBytes;
QImageWriter imageWriter(&imageBytes, "png");
imageWriter.write(image);
QString filename = m_strategy->createUniqueImageName();
m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());
int imgQuality = imageFormat.quality();
if (imgQuality >= 100 || imgQuality < 0 || image.hasAlphaChannel()) {
QImageWriter imageWriter(&imageBytes, "png");
imageWriter.write(image);
m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());
} else {
// Write images without alpha channel as jpg with quality set by QTextImageFormat
QImageWriter imageWriter(&imageBytes, "jpg");
imageWriter.setQuality(imgQuality);
imageWriter.write(image);
m_strategy->addFile(filename, QString::fromLatin1("image/jpg"), imageBytes.data());
}
// get the width/height from the format.
qreal width = imageFormat.hasProperty(QTextFormat::ImageWidth)
? imageFormat.width() : image.width();