From 9d372b601949438794b8c49c42e7fc614b4aee48 Mon Sep 17 00:00:00 2001 From: Jean Felder Date: Wed, 20 Nov 2024 16:32:32 +0100 Subject: [PATCH] qgsrasterlayer: Remove refresh logic This is not used anymore. --- .../raster/qgsrasterlayer.sip.in | 1 - .../raster/qgsrasterlayer.sip.in | 1 - src/core/raster/qgsrasterlayer.cpp | 109 ------------------ src/core/raster/qgsrasterlayer.h | 12 -- tests/src/core/testqgsrasterlayer.cpp | 50 ++++++-- 5 files changed, 42 insertions(+), 131 deletions(-) diff --git a/python/PyQt6/core/auto_generated/raster/qgsrasterlayer.sip.in b/python/PyQt6/core/auto_generated/raster/qgsrasterlayer.sip.in index b46899817b1c..86c4c9c7cf3f 100644 --- a/python/PyQt6/core/auto_generated/raster/qgsrasterlayer.sip.in +++ b/python/PyQt6/core/auto_generated/raster/qgsrasterlayer.sip.in @@ -354,7 +354,6 @@ Set contrast enhancement algorithm %End - virtual QString subsetString() const; %Docstring Returns the string (typically sql) used to define a subset of the layer. diff --git a/python/core/auto_generated/raster/qgsrasterlayer.sip.in b/python/core/auto_generated/raster/qgsrasterlayer.sip.in index b46899817b1c..86c4c9c7cf3f 100644 --- a/python/core/auto_generated/raster/qgsrasterlayer.sip.in +++ b/python/core/auto_generated/raster/qgsrasterlayer.sip.in @@ -354,7 +354,6 @@ Set contrast enhancement algorithm %End - virtual QString subsetString() const; %Docstring Returns the string (typically sql) used to define a subset of the layer. diff --git a/src/core/raster/qgsrasterlayer.cpp b/src/core/raster/qgsrasterlayer.cpp index 6979ac0297f7..3cf6aa4a9889 100644 --- a/src/core/raster/qgsrasterlayer.cpp +++ b/src/core/raster/qgsrasterlayer.cpp @@ -1543,115 +1543,6 @@ void QgsRasterLayer::refreshContrastEnhancement( const QgsRectangle &extent ) } } -void QgsRasterLayer::refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer, - const QgsRectangle &extent ) -{ - QGIS_PROTECT_QOBJECT_THREAD_ACCESS - - if ( mDataProvider && - mLastRectangleUsedByRefreshContrastEnhancementIfNeeded != extent && - rasterRenderer->minMaxOrigin().limits() != QgsRasterMinMaxOrigin::None && - rasterRenderer->minMaxOrigin().extent() == QgsRasterMinMaxOrigin::UpdatedCanvas ) - { - refreshRenderer( rasterRenderer, extent ); - } -} - -void QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ) -{ - QGIS_PROTECT_QOBJECT_THREAD_ACCESS - - if ( mDataProvider ) - { - QgsSingleBandGrayRenderer *singleBandRenderer = nullptr; - QgsMultiBandColorRenderer *multiBandRenderer = nullptr; - QgsSingleBandPseudoColorRenderer *sbpcr = nullptr; - const QgsContrastEnhancement *ce = nullptr; - if ( ( singleBandRenderer = dynamic_cast( rasterRenderer ) ) ) - { - ce = singleBandRenderer->contrastEnhancement(); - } - else if ( ( multiBandRenderer = dynamic_cast( rasterRenderer ) ) ) - { - ce = multiBandRenderer->redContrastEnhancement(); - } - else if ( ( sbpcr = dynamic_cast( rasterRenderer ) ) ) - { - mLastRectangleUsedByRefreshContrastEnhancementIfNeeded = extent; - double min; - double max; - computeMinMax( sbpcr->inputBand(), - rasterRenderer->minMaxOrigin(), - rasterRenderer->minMaxOrigin().limits(), extent, - static_cast( SAMPLE_SIZE ), min, max ); - sbpcr->setClassificationMin( min ); - sbpcr->setClassificationMax( max ); - - if ( sbpcr->shader() ) - { - QgsColorRampShader *colorRampShader = dynamic_cast( sbpcr->shader()->rasterShaderFunction() ); - if ( colorRampShader ) - { - colorRampShader->classifyColorRamp( sbpcr->inputBand(), extent, rasterRenderer->input() ); - } - } - - QgsSingleBandPseudoColorRenderer *r = dynamic_cast( renderer() ); - r->setClassificationMin( min ); - r->setClassificationMax( max ); - - if ( r->shader() ) - { - QgsColorRampShader *colorRampShader = dynamic_cast( r->shader()->rasterShaderFunction() ); - if ( colorRampShader ) - { - colorRampShader->classifyColorRamp( sbpcr->inputBand(), extent, rasterRenderer->input() ); - } - } - - emit repaintRequested(); - emitStyleChanged(); - emit rendererChanged(); - return; - } - - if ( ce && - ce->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement ) - { - mLastRectangleUsedByRefreshContrastEnhancementIfNeeded = extent; - - setContrastEnhancement( ce->contrastEnhancementAlgorithm(), - rasterRenderer->minMaxOrigin().limits(), - extent, - static_cast( SAMPLE_SIZE ), - true, - rasterRenderer ); - - // Update main renderer so that the legends get updated - if ( singleBandRenderer ) - static_cast( renderer() )->setContrastEnhancement( new QgsContrastEnhancement( * singleBandRenderer->contrastEnhancement() ) ); - else if ( multiBandRenderer ) - { - if ( multiBandRenderer->redContrastEnhancement() ) - { - static_cast( renderer() )->setRedContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->redContrastEnhancement() ) ); - } - if ( multiBandRenderer->greenContrastEnhancement() ) - { - static_cast( renderer() )->setGreenContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->greenContrastEnhancement() ) ); - } - if ( multiBandRenderer->blueContrastEnhancement() ) - { - static_cast( renderer() )->setBlueContrastEnhancement( new QgsContrastEnhancement( *multiBandRenderer->blueContrastEnhancement() ) ); - } - } - - emitStyleChanged(); - emit rendererChanged(); - } - } -} - QString QgsRasterLayer::subsetString() const { QGIS_PROTECT_QOBJECT_THREAD_ACCESS diff --git a/src/core/raster/qgsrasterlayer.h b/src/core/raster/qgsrasterlayer.h index ade9752b1980..df827cf7625a 100644 --- a/src/core/raster/qgsrasterlayer.h +++ b/src/core/raster/qgsrasterlayer.h @@ -403,12 +403,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile */ void refreshContrastEnhancement( const QgsRectangle &extent ) SIP_SKIP; - /** - * \brief Refresh renderer with new extent, if needed - * \note not available in Python bindings - */ - void refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ) SIP_SKIP; - /** * Returns the string (typically sql) used to define a subset of the layer. * \returns The subset string or null QString if not implemented by the provider @@ -538,9 +532,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile bool generateLookupTableFlag, QgsRasterRenderer *rasterRenderer ); - //! Refresh renderer - void refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ); - /** * Updates the data source of the layer. The layer's renderer and legend will be preserved only * if the geometry type of the new data source matches the current geometry type of the layer. @@ -592,9 +583,6 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfile std::unique_ptr< QgsRasterPipe > mPipe; - //! To save computations and possible infinite cycle of notifications - QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded; - QDomDocument mOriginalStyleDocument; QDomElement mOriginalStyleElement; diff --git a/tests/src/core/testqgsrasterlayer.cpp b/tests/src/core/testqgsrasterlayer.cpp index 08e7c74dbd2b..3f768b1971eb 100644 --- a/tests/src/core/testqgsrasterlayer.cpp +++ b/tests/src/core/testqgsrasterlayer.cpp @@ -1008,15 +1008,16 @@ void TestQgsRasterLayer::testRefreshRendererIfNeeded() QVERIFY( dynamic_cast( mpLandsatRasterLayer->renderer() ) ); mMapSettings->setLayers( QList() << mpLandsatRasterLayer ); mMapSettings->setExtent( mpLandsatRasterLayer->extent() ); - const double initMinVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue(); + const double initRedMinVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue(); + const double initRedMaxVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->maximumValue(); + const double initGreenMinVal = static_cast( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->minimumValue(); + const double initGreenMaxVal = static_cast( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->maximumValue(); + const double initBlueMinVal = static_cast( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->minimumValue(); + const double initBlueMaxVal = static_cast( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->maximumValue(); // Should do nothing const QgsRectangle newExtent = QgsRectangle( 785000, 3340000, 785100, 3340100 ); QVERIFY( !mpLandsatRasterLayer->renderer()->needsRefresh( newExtent ) ); - mpLandsatRasterLayer->refreshRendererIfNeeded( mpLandsatRasterLayer->renderer(), newExtent ); - QCOMPARE( mpLandsatRasterLayer->renderer()->minMaxOrigin().limits(), QgsRasterMinMaxOrigin::MinMax ); - const double minVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue(); - QGSCOMPARENEAR( initMinVal, minVal, 1e-5 ); // Change to UpdatedCanvas QgsRasterMinMaxOrigin mmo = mpLandsatRasterLayer->renderer()->minMaxOrigin(); @@ -1025,9 +1026,42 @@ void TestQgsRasterLayer::testRefreshRendererIfNeeded() mpLandsatRasterLayer->renderer()->setMinMaxOrigin( mmo ); QCOMPARE( mpLandsatRasterLayer->renderer()->minMaxOrigin().extent(), QgsRasterMinMaxOrigin::UpdatedCanvas ); QVERIFY( mpLandsatRasterLayer->renderer()->needsRefresh( newExtent ) ); - mpLandsatRasterLayer->refreshRendererIfNeeded( mpLandsatRasterLayer->renderer(), newExtent ); - const double newMinVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue(); - QGSCOMPARENOTNEAR( initMinVal, newMinVal, 1e-5 ); + + QList minValues; + QList maxValues; + for ( const int bandIdx : mpLandsatRasterLayer->renderer()->usesBands() ) + { + double min; + double max; + mpLandsatRasterLayer->computeMinMax( bandIdx, mmo, mmo.limits(), + newExtent, static_cast( QgsRasterLayer::SAMPLE_SIZE ), + min, max ); + minValues.append( min ); + maxValues.append( max ); + } + + const bool refreshed = mpLandsatRasterLayer->renderer()->refresh( newExtent, minValues, maxValues ); + QVERIFY( refreshed ); + QVERIFY( !mpLandsatRasterLayer->renderer()->needsRefresh( newExtent ) ); + + const double newRedMinVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->minimumValue(); + const double newRedMaxVal = static_cast( mpLandsatRasterLayer->renderer() )->redContrastEnhancement()->maximumValue(); + const double newGreenMinVal = static_cast( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->minimumValue(); + const double newGreenMaxVal = static_cast( mpLandsatRasterLayer->renderer() )->greenContrastEnhancement()->maximumValue(); + const double newBlueMinVal = static_cast( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->minimumValue(); + const double newBlueMaxVal = static_cast( mpLandsatRasterLayer->renderer() )->blueContrastEnhancement()->maximumValue(); + QGSCOMPARENOTNEAR( initRedMinVal, newRedMinVal, 1e-5 ); + QGSCOMPARENOTNEAR( initRedMaxVal, newRedMaxVal, 1e-5 ); + QGSCOMPARENOTNEAR( initGreenMinVal, newGreenMinVal, 1e-5 ); + QGSCOMPARENOTNEAR( initGreenMaxVal, newGreenMaxVal, 1e-5 ); + QGSCOMPARENOTNEAR( initBlueMinVal, newBlueMinVal, 1e-5 ); + QGSCOMPARENOTNEAR( initBlueMaxVal, newBlueMaxVal, 1e-5 ); + QGSCOMPARENEAR( newRedMinVal, 208.0, 1e-5 ); + QGSCOMPARENEAR( newRedMaxVal, 225.0, 1e-5 ); + QGSCOMPARENEAR( newGreenMinVal, 162.0, 1e-5 ); + QGSCOMPARENEAR( newGreenMaxVal, 172.0, 1e-5 ); + QGSCOMPARENEAR( newBlueMinVal, 90.0, 1e-5 ); + QGSCOMPARENEAR( newBlueMaxVal, 94.0, 1e-5 ); } void TestQgsRasterLayer::sample()