18 #include <QTextDocument>
21 class KDReports::ImageElementPrivate
30 bool m_fitToPage =
false;
36 : d(new ImageElementPrivate)
42 : d(new ImageElementPrivate)
49 , d(new ImageElementPrivate(*other.d))
70 d->m_pixmap = QVariant::fromValue(pixmap.toImage());
71 d->m_pixmapSize = pixmap.size();
76 return d->m_pixmap.value<QPixmap>();
81 d->m_pixmap = QVariant::fromValue(image);
82 d->m_pixmapSize = image.size();
87 return d->m_pixmap.value<QImage>();
95 d->m_fitToPage =
false;
105 d->m_height = height;
108 d->m_fitToPage =
false;
120 d->m_fitToPage =
true;
125 return d->m_fitToPage;
132 const QSize sz( qRound(
mmToPixels( d->m_width.width() ) ),
133 qRound(
mmToPixels( d->m_width.height() ) ) );
134 d->m_pixmap = d->m_pixmap.scaled( sz, Qt::KeepAspectRatio, Qt::SmoothTransformation );
135 qDebug() <<
"ImageElement: m_width (mm) =" << d->m_width <<
" sz (pixels) =" << sz;
139 if (d->m_pixmapSize.isNull())
142 static int imageNumber = 0;
143 const QString name = QStringLiteral(
"image%1.png").arg(++imageNumber);
144 builder.
currentDocument().addResource(QTextDocument::ImageResource, QUrl(name), d->m_pixmap);
147 QTextImageFormat imageFormat;
148 imageFormat.setName(name);
149 imageFormat.setWidth(d->m_pixmapSize.width());
150 imageFormat.setHeight(d->m_pixmapSize.height());
151 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
153 imageFormat.setQuality(100);
158 const qreal pixelWidth =
mmToPixels(d->m_width);
159 const qreal pixelHeight = pixelWidth * imageFormat.height() / imageFormat.width();
160 imageFormat.setWidth(pixelWidth);
161 imageFormat.setHeight(pixelHeight);
167 }
else if (d->m_height) {
169 const qreal pixelHeight = qRound(
mmToPixels(d->m_height));
170 const qreal pixelWidth = pixelHeight * imageFormat.width() / imageFormat.height();
171 imageFormat.setHeight(pixelHeight);
172 imageFormat.setWidth(pixelWidth);
174 imageFormat.setProperty(
ResizableImageProperty, QString(QLatin1Char(
'H') + QString::number(d->m_height)));
178 }
else if (d->m_fitToPage) {
183 QTextCursor &cursor = builder.
cursor();
184 cursor.insertImage(imageFormat);
Element & operator=(const Element &other)
void setHeight(qreal height, Unit unit=Millimeters)
void setImage(const QImage &image)
ImageElement & operator=(const ImageElement &other)
void build(ReportBuilder &) const override
ImageElement(const QPixmap &pixmap)
void setWidth(qreal width, Unit unit=Millimeters)
void setId(const QString &id)
Element * clone() const override
void setPixmap(const QPixmap &pixmap)
TextDocumentData & currentDocumentData()
QTextDocument & currentDocument()
void addResourceName(const QString &resourceName)
static void updatePercentSize(QTextImageFormat &format, QSizeF size)
void setHasResizableImages()
@ Millimeters
Millimeters (the default)
KDREPORTS_EXPORT qreal mmToPixels(qreal mm)
static const int ResizableImageProperty