diff --git a/python/core/layout/qgslayoutcontext.sip b/python/core/layout/qgslayoutcontext.sip index 203cebb7a9f..c3e987e77ff 100644 --- a/python/core/layout/qgslayoutcontext.sip +++ b/python/core/layout/qgslayoutcontext.sip @@ -235,6 +235,11 @@ Emitted when the context's ``layer`` is changed. %Docstring Emitted certain settings in the context is changed, e.g. by setting a new feature or vector layer for the context. +%End + + void dpiChanged(); +%Docstring +Emitted when the context's DPI is changed. %End }; diff --git a/src/core/layout/qgslayoutcontext.cpp b/src/core/layout/qgslayoutcontext.cpp index 6fe6d928c15..dc70e54b056 100644 --- a/src/core/layout/qgslayoutcontext.cpp +++ b/src/core/layout/qgslayoutcontext.cpp @@ -89,7 +89,11 @@ void QgsLayoutContext::setLayer( QgsVectorLayer *layer ) void QgsLayoutContext::setDpi( double dpi ) { + if ( dpi == mMeasurementConverter.dpi() ) + return; + mMeasurementConverter.setDpi( dpi ); + emit dpiChanged(); } double QgsLayoutContext::dpi() const diff --git a/src/core/layout/qgslayoutcontext.h b/src/core/layout/qgslayoutcontext.h index 339522359a2..0bb53b10ffb 100644 --- a/src/core/layout/qgslayoutcontext.h +++ b/src/core/layout/qgslayoutcontext.h @@ -236,6 +236,11 @@ class CORE_EXPORT QgsLayoutContext : public QObject */ void changed(); + /** + * Emitted when the context's DPI is changed. + */ + void dpiChanged(); + private: Flags mFlags = nullptr; diff --git a/src/core/layout/qgslayoutitempicture.cpp b/src/core/layout/qgslayoutitempicture.cpp index 4ccb50e8966..2d2a86e7900 100644 --- a/src/core/layout/qgslayoutitempicture.cpp +++ b/src/core/layout/qgslayoutitempicture.cpp @@ -54,14 +54,12 @@ QgsLayoutItemPicture::QgsLayoutItemPicture( QgsLayout *layout ) //connect some signals -#if 0 //TODO //connect to atlas feature changing //to update the picture source expression - connect( &mComposition->atlasComposition(), &QgsAtlasComposition::featureChanged, this, [ = ] { refreshPicture(); } ); + connect( &layout->context(), &QgsLayoutContext::changed, this, [ = ] { refreshPicture(); } ); //connect to layout print resolution changing - connect( layout->context(), &QgsLayoutContext::printResolutionChanged, this, &QgsLayoutItemPicture::recalculateSize ); -#endif + connect( &layout->context(), &QgsLayoutContext::dpiChanged, this, &QgsLayoutItemPicture::recalculateSize ); connect( this, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItemPicture::shapeChanged ); } diff --git a/tests/src/core/testqgslayoutcontext.cpp b/tests/src/core/testqgslayoutcontext.cpp index 986931a4fa5..ed9bd23eacf 100644 --- a/tests/src/core/testqgslayoutcontext.cpp +++ b/tests/src/core/testqgslayoutcontext.cpp @@ -145,9 +145,17 @@ void TestQgsLayoutContext::layer() void TestQgsLayoutContext::dpi() { QgsLayoutContext context; + + QSignalSpy spyDpiChanged( &context, &QgsLayoutContext::dpiChanged ); context.setDpi( 600 ); QCOMPARE( context.dpi(), 600.0 ); QCOMPARE( context.measurementConverter().dpi(), 600.0 ); + QCOMPARE( spyDpiChanged.count(), 1 ); + + context.setDpi( 600 ); + QCOMPARE( spyDpiChanged.count(), 1 ); + context.setDpi( 6000 ); + QCOMPARE( spyDpiChanged.count(), 2 ); } void TestQgsLayoutContext::renderContextFlags()