Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't refresh single pseudo color band classication when min/max hasn…
…'t really changed

Fixes #36172
  • Loading branch information
troopa81 committed May 19, 2020
1 parent 3fbbc01 commit 8499e09
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -186,9 +186,6 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,
Q_UNUSED( bandNo )
QgsDebugMsg( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );

double oldMin = lineEditValue( mMinLineEdit );
double oldMax = lineEditValue( mMaxLineEdit );

if ( std::isnan( min ) )
{
whileBlocking( mMinLineEdit )->clear();
Expand All @@ -207,7 +204,10 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,
whileBlocking( mMaxLineEdit )->setText( QString::number( max ) );
}

if ( !qgsDoubleNear( oldMin, min ) || !qgsDoubleNear( oldMax, max ) )
// We compare old min and new min as text because QString::number keeps a fixed number of significant
// digits (default 6) and so loaded min/max will always differ from current one, which triggers a
// classification, and wipe out every user modification (see https://github.com/qgis/QGIS/issues/36172)
if ( mMinLineEdit->text() != QString::number( min ) || mMaxLineEdit->text() != QString::number( max ) )
{
whileBlocking( mColorRampShaderWidget )->setRasterBand( bandNo );
whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( min, max );
Expand Down
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Expand Up @@ -166,3 +166,4 @@ ADD_QGIS_TEST(texteditwrapper testqgstexteditwrapper.cpp)
ADD_QGIS_TEST(tableeditorwidget testqgstableeditor.cpp)
ADD_QGIS_TEST(newdatabasetablewidget testqgsnewdatabasetablewidget.cpp)
ADD_QGIS_TEST(ogrproviderguitest testqgsogrprovidergui.cpp)
ADD_QGIS_TEST(singlebandpseudocolorrendererwidget testqgssinglebandpseudocolorrendererwidget.cpp)
120 changes: 120 additions & 0 deletions tests/src/gui/testqgssinglebandpseudocolorrendererwidget.cpp
@@ -0,0 +1,120 @@
/***************************************************************************
testqgssinglebandpsueodcolorrendererwidget
--------------------------------------
Date : May 2020
Copyright : (C) 2020 Julien Cabieces
Email : julien.cabieces@oslandia.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include <QObject>
#include <QString>

#include <qgsapplication.h>
#include <qgsrasterlayer.h>
#include "qgssinglebandpseudocolorrendererwidget.h"
#include "qgssinglebandpseudocolorrenderer.h"
#include "qgsrastershader.h"

/**
* \ingroup UnitTests
* This is a unit test to verify that raster histogram works
*/
class TestQgsSingleBandPseudoColorRendererWidget : public QObject
{
Q_OBJECT

public:

TestQgsSingleBandPseudoColorRendererWidget() {}

private:

QgsRasterLayer *mRasterLayer = nullptr;

private slots:

// init / cleanup
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.

// tests
void testEditLabel();
};


// slots

void TestQgsSingleBandPseudoColorRendererWidget::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

QString mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
QString myLandsatFileName = mTestDataDir + "landsat.tif";

mRasterLayer = new QgsRasterLayer( myLandsatFileName, "landsat" );
QVERIFY( mRasterLayer->isValid() );
}

void TestQgsSingleBandPseudoColorRendererWidget::cleanupTestCase()
{
delete mRasterLayer;
}

void TestQgsSingleBandPseudoColorRendererWidget::testEditLabel()
{
// related to https://github.com/qgis/QGIS/issues/36172

QgsRasterShader *rasterShader = new QgsRasterShader();
QgsColorRampShader *colorRampShader = new QgsColorRampShader();
colorRampShader->setColorRampType( QgsColorRampShader::Interpolated );

const double min = 122.123456;
const double max = 129.123456;

// configure pseudo band color renderer
QList<QgsColorRampShader::ColorRampItem> colorRampItems;
QgsColorRampShader::ColorRampItem firstItem;
firstItem.value = 0.0;
firstItem.label = "zero";
firstItem.color = QColor( 0, 0, 255 );
colorRampItems.append( firstItem );

colorRampShader->setColorRampItemList( colorRampItems );
rasterShader->setRasterShaderFunction( colorRampShader );

QgsSingleBandPseudoColorRenderer *rasterRenderer = new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), 1, rasterShader );
rasterRenderer->setClassificationMin( min );
rasterRenderer->setClassificationMax( max );
mRasterLayer->dataProvider()->setNoDataValue( 1, 126 );
rasterRenderer->setNodataColor( QColor( 255, 0, 255 ) );
mRasterLayer->setRenderer( rasterRenderer );

QgsSingleBandPseudoColorRendererWidget widget( mRasterLayer );

// force loading min/max with the same exact values
// it should not triggers classification and we should get the initial ramp item
widget.loadMinMax( 1, min, max );

QgsSingleBandPseudoColorRenderer *newRasterRenderer = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( widget.renderer() );
QVERIFY( newRasterRenderer );
QVERIFY( rasterRenderer->shader() );

QgsColorRampShader *newColorRampShader = dynamic_cast<QgsColorRampShader *>( newRasterRenderer->shader()->rasterShaderFunction() );
QVERIFY( newColorRampShader );

QList<QgsColorRampShader::ColorRampItem> newColorRampItems = newColorRampShader->colorRampItemList();
QCOMPARE( newColorRampItems.at( 0 ).label, QStringLiteral( "zero" ) );
}



QGSTEST_MAIN( TestQgsSingleBandPseudoColorRendererWidget )
#include "testqgssinglebandpseudocolorrendererwidget.moc"

0 comments on commit 8499e09

Please sign in to comment.