Skip to content
Permalink
Browse files

Address review

  • Loading branch information
nirvn committed May 3, 2021
1 parent 8d1371f commit 8299547682c2bbbb10881b7dd0f7cf7ade1b242e
@@ -199,14 +199,18 @@ Returns the target DPI (dots per inch) to be taken into consideration when rende

The default value is -1, which states no DPI target is provided.

.. seealso:: :py:func:`setDpiTarger`
.. seealso:: :py:func:`setDpiTarget`

.. versionadded:: 3.20
%End

void setDpiTarget( double dpi );
%Docstring
Sets the target ``dpi`` (dots per inch) to be taken into consideration when rendering.

.. seealso:: :py:func:`dpiTarget`

.. versionadded:: 3.20
%End

void setMagnificationFactor( double factor, const QgsPointXY *center = 0 );
@@ -283,7 +283,6 @@ per millimeter.
double dpiTarget() const;
%Docstring
Returns the targeted DPI for rendering.
scale.

.. seealso:: :py:func:`setDpiTarget`

@@ -78,7 +78,7 @@ Base class for raster data providers.
ProviderHintBenefitsFromResampling,
ProviderHintCanPerformProviderResampling,
ReloadData,
PixelDependentRendering,
DpiDependentData,
};

typedef QFlags<QgsRasterDataProvider::ProviderCapability> ProviderCapabilities;
@@ -20,7 +20,14 @@ The drawing pipe for raster layers.
#include "qgsrasterdrawer.h"
%End
public:

QgsRasterDrawer( QgsRasterIterator *iterator, double dpiTarget = -1.0 );
%Docstring
The QgsRasterDrawer constructor.

:param iterator: the raster iterator to fetch data from
:param dpiTarget: the target ``dpi`` (dots per inch) to be taken into consideration when rendering
%End

void draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsMapToPixel *qgsMapToPixel, QgsRasterBlockFeedback *feedback = 0 );
%Docstring
@@ -209,14 +209,16 @@ class CORE_EXPORT QgsMapSettings : public QgsTemporalRangeObject
*
* The default value is -1, which states no DPI target is provided.
*
* \see setDpiTarger()
* \see setDpiTarget()
* \since QGIS 3.20
*/
double dpiTarget() const;

/**
* Sets the target \a dpi (dots per inch) to be taken into consideration when rendering.
*
* \see dpiTarget()
* \since QGIS 3.20
*/
void setDpiTarget( double dpi );

@@ -335,7 +335,6 @@ class CORE_EXPORT QgsRenderContext : public QgsTemporalRangeObject

/**
* Returns the targeted DPI for rendering.
* scale.
*
* \see setDpiTarget()
* \since QGIS 3.20
@@ -103,7 +103,7 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRast
ProviderHintBenefitsFromResampling = 1 << 3, //!< Provider benefits from resampling and should apply user default resampling settings (since QGIS 3.10)
ProviderHintCanPerformProviderResampling = 1 << 4, //!< Provider can perform resampling (to be opposed to post rendering resampling) (since QGIS 3.16)
ReloadData = 1 << 5, //!< Is able to force reload data / clear local caches. Since QGIS 3.18, see QgsDataProvider::reloadProviderData()
PixelDependentRendering = 1 << 6, //! Provider's rendering is dependent on requested pixel size of the viewport
DpiDependentData = 1 << 6, //! Provider's rendering is dependent on requested pixel size of the viewport (since QGIS 3.20)
};

//! Provider capabilities
@@ -117,7 +117,6 @@ void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsM
}
}

#include <QDebug>
void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const QImage &img, int topLeftCol, int topLeftRow, const QgsMapToPixel *qgsMapToPixel ) const
{
if ( !p || !viewPort )
@@ -127,7 +126,7 @@ void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const

const double dpiScaleFactor = mDpiTarget >= 0.0 ? mDpiTarget / p->device()->logicalDpiX() : 1.0;
//top left position in device coords
QPoint tlPoint = QPoint( viewPort->mTopLeftPoint.x() + topLeftCol / dpiScaleFactor, viewPort->mTopLeftPoint.y() + topLeftRow / dpiScaleFactor );
QPoint tlPoint = QPoint( viewPort->mTopLeftPoint.x() + std::floor( topLeftCol / dpiScaleFactor ), viewPort->mTopLeftPoint.y() + std::floor( topLeftRow / dpiScaleFactor ) );

QgsScopedQPainterState painterState( p );
p->setRenderHint( QPainter::Antialiasing, false );
@@ -136,11 +135,11 @@ void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const
// in #7766, it seems to be a bug in Qt, setting a brush with alpha 255 is a workaround
// which should not harm anything
p->setBrush( QBrush( QColor( Qt::white ), Qt::NoBrush ) );
int w = qgsMapToPixel->mapWidth();
int h = qgsMapToPixel->mapHeight();
if ( qgsMapToPixel )
{
double rotation = qgsMapToPixel->mapRotation();
const int w = qgsMapToPixel->mapWidth();
const int h = qgsMapToPixel->mapHeight();
const double rotation = qgsMapToPixel->mapRotation();
if ( rotation )
{
// both viewPort and image sizes are dependent on scale
@@ -152,7 +151,7 @@ void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const
}
}

p->drawImage( tlPoint, dpiScaleFactor != 1.0 ? img.scaledToWidth( img.width() / dpiScaleFactor ) : img );
p->drawImage( tlPoint, dpiScaleFactor != 1.0 ? img.scaledToHeight( std::ceil( img.height() / dpiScaleFactor ) ) : img );

#if 0
// For debugging:
@@ -37,6 +37,12 @@ class QgsRasterIterator;
class CORE_EXPORT QgsRasterDrawer
{
public:

/**
* The QgsRasterDrawer constructor.
* \param iterator the raster iterator to fetch data from
* \param dpiTarget the target \a dpi (dots per inch) to be taken into consideration when rendering
*/
QgsRasterDrawer( QgsRasterIterator *iterator, double dpiTarget = -1.0 );

/**
@@ -202,7 +202,7 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
mRasterViewPort->mHeight = static_cast<qgssize>( std::abs( mRasterViewPort->mBottomRightPoint.y() - mRasterViewPort->mTopLeftPoint.y() ) );


if ( mProviderCapabilities & QgsRasterDataProvider::PixelDependentRendering
if ( mProviderCapabilities & QgsRasterDataProvider::DpiDependentData
&& rendererContext.dpiTarget() >= 0.0 )
{
const double dpiScaleFactor = rendererContext.dpiTarget() / rendererContext.painter()->device()->logicalDpiX();
@@ -227,6 +227,8 @@ void QgsLayoutPropertiesWidget::dpiChanged( int value )
mLayout->undoStack()->beginCommand( mLayout, tr( "Set Default DPI" ), QgsLayout::UndoLayoutDpi );
mLayout->renderContext().setDpi( value );
mLayout->undoStack()->endCommand();

mLayout->refresh();
}

void QgsLayoutPropertiesWidget::worldFileToggled()
@@ -2024,9 +2024,9 @@ int QgsWmsProvider::capabilities() const
// capability |= Capability::Prefetch;
}

if ( mSettings.mTiled && mSettings.mXyz )
if ( mSettings.mTiled || mSettings.mXyz )
{
capability |= PixelDependentRendering;
capability |= DpiDependentData;
}

QgsDebugMsgLevel( QStringLiteral( "capability = %1" ).arg( capability ), 2 );
@@ -14,7 +14,7 @@

import os

from qgis.PyQt.QtCore import QFileInfo, QRectF, QDir, QCoreApplication, QEvent
from qgis.PyQt.QtCore import QFileInfo, QRectF, QDir, QCoreApplication, QEvent, QSizeF
from qgis.PyQt.QtXml import QDomDocument
from qgis.PyQt.QtGui import QPainter, QColor
from qgis.PyQt.QtTest import QSignalSpy
@@ -860,6 +860,17 @@ def testCrsChanged(self):
map.refresh()
self.assertEqual(len(spy), 7)

def testCrsChanged(self):
"""
Test that the CRS changed signal is emitted in the right circumstances
"""
p = QgsProject()
layout = QgsLayout(p)
layout.renderContext().setDpi(111.1)
map = QgsLayoutItemMap(layout)
ms = map.mapSettings(QgsRectangle(0, 0, 1, 1), QSizeF(10, 10), 96, False)
self.assertEqual(ms.dpiTarget(), 111.1)


if __name__ == '__main__':
unittest.main()
@@ -163,6 +163,10 @@ def testFromMapSettings(self):
self.assertEqual(rc.temporalRange(),
QgsDateTimeRange(QDateTime(2020, 1, 1, 0, 0), QDateTime(2010, 12, 31, 23, 59)))

ms.setDpiTarget(111.1)
rc = QgsRenderContext.fromMapSettings(ms)
self.assertEqual(rc.dpiTarget(), 111.1)

def testVectorSimplification(self):
"""
Test vector simplification hints, ensure they are copied correctly from map settings

0 comments on commit 8299547

Please sign in to comment.