Skip to content

Commit

Permalink
[layouts] Fix rendering of map preview on high DPI screens
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jan 31, 2024
1 parent 310b917 commit 381c881
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/core/layout/qgslayoutitemmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
#include "qgsvectortileutils.h"
#include "qgsunittypes.h"

#include <QApplication>
#include <QPainter>
#include <QScreen>
#include <QStyleOptionGraphicsItem>
#include <QTimer>

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit 381c881

Please sign in to comment.