From 7ffd08aafb5abfe4b20ea4b3d205001d99fa42ad Mon Sep 17 00:00:00 2001 From: Oliver Sander Date: Fri, 12 Mar 2021 21:20:57 +0100 Subject: [PATCH] Use device pixel ratio for the correct screen The method qApp->devicePixelRatio() should only be used under special circumstances. The documentation of QGuiApplication says: "Returns the highest screen device pixel ratio found on the system. [...] Use this function only when you don't know which window you are targeting. If you do know the target window, use QWindow::devicePixelRatio() instead." The Okular code used qApp->devicePixelRatio() a few times without reason. Few people noticed this, because apparently only Windows and Wayland can set per-screen dprs. This patch replaces those calls with the correct QWindow call. --- part/pagepainter.cpp | 6 +++--- part/presentationwidget.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/part/pagepainter.cpp b/part/pagepainter.cpp index 6a8c36f3b..b88760624 100644 --- a/part/pagepainter.cpp +++ b/part/pagepainter.cpp @@ -106,7 +106,7 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter, if (p != nullptr) { pixmap = *p; - pixmap.setDevicePixelRatio(qApp->devicePixelRatio()); + pixmap.setDevicePixelRatio(dpr); } /** 1B - IF NO PIXMAP, DRAW EMPTY PAGE **/ @@ -250,7 +250,7 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter, if (!limitsInTile.isEmpty()) { QPixmap *tilePixmap = tile.pixmap(); - tilePixmap->setDevicePixelRatio(qApp->devicePixelRatio()); + tilePixmap->setDevicePixelRatio(dpr); if (tilePixmap->width() == dTileRect.width() && tilePixmap->height() == dTileRect.height()) { destPainter->drawPixmap(limitsInTile.topLeft(), *tilePixmap, dLimitsInTile.translated(-dTileRect.topLeft())); @@ -290,7 +290,7 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter, if (!limitsInTile.isEmpty()) { QPixmap *tilePixmap = tile.pixmap(); - tilePixmap->setDevicePixelRatio(qApp->devicePixelRatio()); + tilePixmap->setDevicePixelRatio(dpr); if (tilePixmap->width() == dTileRect.width() && tilePixmap->height() == dTileRect.height()) { p.drawPixmap(limitsInTile.translated(-limits.topLeft()).topLeft(), *tilePixmap, dLimitsInTile.translated(-dTileRect.topLeft())); diff --git a/part/presentationwidget.cpp b/part/presentationwidget.cpp index f43865a9b..b7b8b0229 100644 --- a/part/presentationwidget.cpp +++ b/part/presentationwidget.cpp @@ -439,7 +439,7 @@ void PresentationWidget::notifyCurrentPageChanged(int previousPage, int currentP // if pixmap not inside the Okular::Page we request it and wait for // notifyPixmapChanged call or else we can proceed to pixmap generation - if (!frame->page->hasPixmap(this, ceil(pixW * qApp->devicePixelRatio()), ceil(pixH * qApp->devicePixelRatio()))) { + if (!frame->page->hasPixmap(this, ceil(pixW * devicePixelRatioF()), ceil(pixH * devicePixelRatioF()))) { requestPixmaps(); } else { // make the background pixmap @@ -1019,7 +1019,7 @@ void PresentationWidget::changePage(int newPage) void PresentationWidget::generatePage(bool disableTransition) { if (m_lastRenderedPixmap.isNull()) { - qreal dpr = qApp->devicePixelRatio(); + qreal dpr = devicePixelRatioF(); m_lastRenderedPixmap = QPixmap(m_width * dpr, m_height * dpr); m_lastRenderedPixmap.setDevicePixelRatio(dpr); @@ -1069,7 +1069,7 @@ void PresentationWidget::generatePage(bool disableTransition) void PresentationWidget::generateIntroPage(QPainter &p) { - qreal dpr = qApp->devicePixelRatio(); + qreal dpr = devicePixelRatioF(); // use a vertical gray gradient background int blend1 = m_height / 10, blend2 = 9 * m_height / 10; @@ -1148,7 +1148,7 @@ inline int qt_div255(int x) void PresentationWidget::generateOverlay() { #ifdef ENABLE_PROGRESS_OVERLAY - qreal dpr = qApp->devicePixelRatio(); + qreal dpr = devicePixelRatioF(); // calculate overlay geometry and resize pixmap if needed double side = m_width / 16.0; @@ -1483,7 +1483,7 @@ void PresentationWidget::slotTransitionStep() QPainter pixmapPainter; m_currentPixmapOpacity += 1.0 / m_transitionSteps; m_lastRenderedPixmap = QPixmap(m_lastRenderedPixmap.size()); - m_lastRenderedPixmap.setDevicePixelRatio(qApp->devicePixelRatio()); + m_lastRenderedPixmap.setDevicePixelRatio(devicePixelRatioF()); m_lastRenderedPixmap.fill(Qt::transparent); pixmapPainter.begin(&m_lastRenderedPixmap); pixmapPainter.setCompositionMode(QPainter::CompositionMode_Source);