From 381c8819fa4f10d210cc48ed502652a12776649e Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Tue, 30 Jan 2024 14:10:11 +0700 Subject: [PATCH] [layouts] Fix rendering of map preview on high DPI screens --- src/core/layout/qgslayoutitemmap.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/core/layout/qgslayoutitemmap.cpp b/src/core/layout/qgslayoutitemmap.cpp index 16314c597342..55527059157d 100644 --- a/src/core/layout/qgslayoutitemmap.cpp +++ b/src/core/layout/qgslayoutitemmap.cpp @@ -40,7 +40,9 @@ #include "qgsvectortileutils.h" #include "qgsunittypes.h" +#include #include +#include #include #include @@ -1112,7 +1114,7 @@ void QgsLayoutItemMap::paint( QPainter *painter, const QStyleOptionGraphicsItem //Background color is already included in cached image, so no need to draw double imagePixelWidth = mCacheFinalImage->width(); //how many pixels of the image are for the map extent? - double scale = rect().width() / imagePixelWidth; + double scale = rect().width() / imagePixelWidth * mCacheFinalImage->devicePixelRatio(); QgsScopedQPainterState rotatedPainterState( painter ); @@ -1592,11 +1594,18 @@ void QgsLayoutItemMap::recreateCachedImageInBackground() if ( w <= 0 || h <= 0 ) return; - mCacheRenderingImage.reset( new QImage( w, h, QImage::Format_ARGB32 ) ); + double devicePixelRatio = 1.0; + if ( QWidget *activeWindow = QApplication::activeWindow() ) + { + devicePixelRatio = ( activeWindow->screen() ? QApplication::activeWindow()->screen()->devicePixelRatio() : 1.0 ); + } + + mCacheRenderingImage.reset( new QImage( w * devicePixelRatio, h * devicePixelRatio, QImage::Format_ARGB32 ) ); // set DPI of the image mCacheRenderingImage->setDotsPerMeterX( static_cast< int >( std::round( 1000 * w / widthLayoutUnits ) ) ); mCacheRenderingImage->setDotsPerMeterY( static_cast< int >( std::round( 1000 * h / heightLayoutUnits ) ) ); + mCacheRenderingImage->setDevicePixelRatio( devicePixelRatio ); //start with empty fill to avoid artifacts mCacheRenderingImage->fill( QColor( 255, 255, 255, 0 ).rgba() ); @@ -1667,7 +1676,15 @@ QgsMapSettings QgsLayoutItemMap::mapSettings( const QgsRectangle &extent, QSizeF jobMapSettings.setOutputSize( size.toSize() ); jobMapSettings.setOutputDpi( dpi ); if ( layout()->renderContext().isPreviewRender() ) + { jobMapSettings.setDpiTarget( layout()->renderContext().dpi() ); + double devicePixelRatio = 1.0; + if ( QWidget *activeWindow = QApplication::activeWindow() ) + { + devicePixelRatio = ( activeWindow->screen() ? QApplication::activeWindow()->screen()->devicePixelRatio() : 1.0 ); + } + jobMapSettings.setDevicePixelRatio( devicePixelRatio ); + } jobMapSettings.setBackgroundColor( Qt::transparent ); jobMapSettings.setRotation( mEvaluatedMapRotation ); if ( mLayout )