Skip to content

Commit

Permalink
Fix blurry rendering in hi-dpi (DPR != 1) situations
Browse files Browse the repository at this point in the history
Fixes #58

(cherry picked from commit b84421f)
  • Loading branch information
dfaure-kdab committed May 30, 2024
1 parent 1451f38 commit da31a26
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/CHANGES_2_3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Bugfixes:
* Fix infinite loop and other issues with Qt 6.7 due to QTextFormat behavior change
* Preserve resolution in watermark image (issue #42)
* Fix right-aligned tabs not working when setting paragraphs margins
* Fix blurry rendering in hi-dpi (DPR != 1) situations (issue #58)

New features:
-------------
Expand Down
32 changes: 19 additions & 13 deletions src/KDReports/KDReportsPreviewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,24 @@ void KDReports::PreviewWidgetPrivate::paintItem(QListWidgetItem *item, int index

// Use a QImage so that the raster paint engine is used.
// Gives a 7.7 times speedup (!) compared to X11.
QImage img(PreviewSize, PreviewSize, QImage::Format_ARGB32_Premultiplied);
const qreal dpr = q->devicePixelRatioF();
QImage img(PreviewSize * dpr, PreviewSize * dpr, QImage::Format_ARGB32_Premultiplied);
img.setDevicePixelRatio(dpr);
img.fill(Qt::transparent);
const QSizeF paperSize = m_report->paperSize();
const qreal longestSide = qMax(paperSize.width(), paperSize.height());
qreal width = img.width() * paperSize.width() / longestSide;
qreal height = img.height() * paperSize.height() / longestSide;
// img.fill( QColor( Qt::white ).rgb() );
img.fill(qRgba(0, 0, 0, 0)); // transparent
const qreal paperWidth = paperSize.width();
const qreal paperHeight = paperSize.height();
const qreal longestSide = qMax(paperWidth, paperHeight);
const qreal width = PreviewSize * paperWidth / longestSide;
const qreal height = PreviewSize * paperHeight / longestSide;
QPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate((img.width() - width) / 2, (img.height() - height) / 2);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true);
painter.translate((PreviewSize - width) / 2, (PreviewSize - height) / 2);
painter.fillRect(QRectF(0, 0, width, height), QBrush(Qt::white));
painter.scale(img.width() / longestSide, img.height() / longestSide);
painter.scale(PreviewSize / longestSide, PreviewSize / longestSide);
m_report->paintPage(index, painter);
painter.setPen(QPen(1));
painter.drawRect(QRectF(0, 0, paperSize.width(), paperSize.height()));
painter.drawRect(QRectF(0, 0, paperWidth, paperHeight));

item->setIcon(QIcon(QPixmap::fromImage(img)));
}
Expand All @@ -231,15 +234,18 @@ QPixmap KDReports::PreviewWidgetPrivate::paintPreview(int index) const
const QSizeF paperSize = m_report->paperSize();
const int width = qCeil(paperSize.width() * m_zoomFactor);
const int height = qCeil(paperSize.height() * m_zoomFactor);
const qreal dpr = q->devicePixelRatioF();

QPixmap pixmap(width, height);
QPixmap pixmap(width * dpr, height * dpr);
pixmap.setDevicePixelRatio(dpr);
pixmap.fill(Qt::transparent);
// qDebug() << width << "," << height;

// qDebug() << "paintPreview " << index;

QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true);

painter.fillRect(QRectF(0, 0, width, height), QBrush(Qt::white));
painter.scale(m_zoomFactor, m_zoomFactor);
m_report->paintPage(index, painter);
Expand Down

0 comments on commit da31a26

Please sign in to comment.