Skip to content

Commit 8499e09

Browse files
authoredMay 19, 2020
Don't refresh single pseudo color band classication when min/max hasn't really changed
Fixes #36172
1 parent 3fbbc01 commit 8499e09

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed
 

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,
186186
Q_UNUSED( bandNo )
187187
QgsDebugMsg( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
188188

189-
double oldMin = lineEditValue( mMinLineEdit );
190-
double oldMax = lineEditValue( mMaxLineEdit );
191-
192189
if ( std::isnan( min ) )
193190
{
194191
whileBlocking( mMinLineEdit )->clear();
@@ -207,7 +204,10 @@ void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min,
207204
whileBlocking( mMaxLineEdit )->setText( QString::number( max ) );
208205
}
209206

210-
if ( !qgsDoubleNear( oldMin, min ) || !qgsDoubleNear( oldMax, max ) )
207+
// We compare old min and new min as text because QString::number keeps a fixed number of significant
208+
// digits (default 6) and so loaded min/max will always differ from current one, which triggers a
209+
// classification, and wipe out every user modification (see https://github.com/qgis/QGIS/issues/36172)
210+
if ( mMinLineEdit->text() != QString::number( min ) || mMaxLineEdit->text() != QString::number( max ) )
211211
{
212212
whileBlocking( mColorRampShaderWidget )->setRasterBand( bandNo );
213213
whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( min, max );

‎tests/src/gui/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@ ADD_QGIS_TEST(texteditwrapper testqgstexteditwrapper.cpp)
166166
ADD_QGIS_TEST(tableeditorwidget testqgstableeditor.cpp)
167167
ADD_QGIS_TEST(newdatabasetablewidget testqgsnewdatabasetablewidget.cpp)
168168
ADD_QGIS_TEST(ogrproviderguitest testqgsogrprovidergui.cpp)
169+
ADD_QGIS_TEST(singlebandpseudocolorrendererwidget testqgssinglebandpseudocolorrendererwidget.cpp)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/***************************************************************************
2+
testqgssinglebandpsueodcolorrendererwidget
3+
--------------------------------------
4+
Date : May 2020
5+
Copyright : (C) 2020 Julien Cabieces
6+
Email : julien.cabieces@oslandia.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include "qgstest.h"
16+
#include <QObject>
17+
#include <QString>
18+
19+
#include <qgsapplication.h>
20+
#include <qgsrasterlayer.h>
21+
#include "qgssinglebandpseudocolorrendererwidget.h"
22+
#include "qgssinglebandpseudocolorrenderer.h"
23+
#include "qgsrastershader.h"
24+
25+
/**
26+
* \ingroup UnitTests
27+
* This is a unit test to verify that raster histogram works
28+
*/
29+
class TestQgsSingleBandPseudoColorRendererWidget : public QObject
30+
{
31+
Q_OBJECT
32+
33+
public:
34+
35+
TestQgsSingleBandPseudoColorRendererWidget() {}
36+
37+
private:
38+
39+
QgsRasterLayer *mRasterLayer = nullptr;
40+
41+
private slots:
42+
43+
// init / cleanup
44+
void initTestCase();// will be called before the first testfunction is executed.
45+
void cleanupTestCase();// will be called after the last testfunction was executed.
46+
47+
// tests
48+
void testEditLabel();
49+
};
50+
51+
52+
// slots
53+
54+
void TestQgsSingleBandPseudoColorRendererWidget::initTestCase()
55+
{
56+
QgsApplication::init();
57+
QgsApplication::initQgis();
58+
59+
QString mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
60+
QString myLandsatFileName = mTestDataDir + "landsat.tif";
61+
62+
mRasterLayer = new QgsRasterLayer( myLandsatFileName, "landsat" );
63+
QVERIFY( mRasterLayer->isValid() );
64+
}
65+
66+
void TestQgsSingleBandPseudoColorRendererWidget::cleanupTestCase()
67+
{
68+
delete mRasterLayer;
69+
}
70+
71+
void TestQgsSingleBandPseudoColorRendererWidget::testEditLabel()
72+
{
73+
// related to https://github.com/qgis/QGIS/issues/36172
74+
75+
QgsRasterShader *rasterShader = new QgsRasterShader();
76+
QgsColorRampShader *colorRampShader = new QgsColorRampShader();
77+
colorRampShader->setColorRampType( QgsColorRampShader::Interpolated );
78+
79+
const double min = 122.123456;
80+
const double max = 129.123456;
81+
82+
// configure pseudo band color renderer
83+
QList<QgsColorRampShader::ColorRampItem> colorRampItems;
84+
QgsColorRampShader::ColorRampItem firstItem;
85+
firstItem.value = 0.0;
86+
firstItem.label = "zero";
87+
firstItem.color = QColor( 0, 0, 255 );
88+
colorRampItems.append( firstItem );
89+
90+
colorRampShader->setColorRampItemList( colorRampItems );
91+
rasterShader->setRasterShaderFunction( colorRampShader );
92+
93+
QgsSingleBandPseudoColorRenderer *rasterRenderer = new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), 1, rasterShader );
94+
rasterRenderer->setClassificationMin( min );
95+
rasterRenderer->setClassificationMax( max );
96+
mRasterLayer->dataProvider()->setNoDataValue( 1, 126 );
97+
rasterRenderer->setNodataColor( QColor( 255, 0, 255 ) );
98+
mRasterLayer->setRenderer( rasterRenderer );
99+
100+
QgsSingleBandPseudoColorRendererWidget widget( mRasterLayer );
101+
102+
// force loading min/max with the same exact values
103+
// it should not triggers classification and we should get the initial ramp item
104+
widget.loadMinMax( 1, min, max );
105+
106+
QgsSingleBandPseudoColorRenderer *newRasterRenderer = dynamic_cast<QgsSingleBandPseudoColorRenderer *>( widget.renderer() );
107+
QVERIFY( newRasterRenderer );
108+
QVERIFY( rasterRenderer->shader() );
109+
110+
QgsColorRampShader *newColorRampShader = dynamic_cast<QgsColorRampShader *>( newRasterRenderer->shader()->rasterShaderFunction() );
111+
QVERIFY( newColorRampShader );
112+
113+
QList<QgsColorRampShader::ColorRampItem> newColorRampItems = newColorRampShader->colorRampItemList();
114+
QCOMPARE( newColorRampItems.at( 0 ).label, QStringLiteral( "zero" ) );
115+
}
116+
117+
118+
119+
QGSTEST_MAIN( TestQgsSingleBandPseudoColorRendererWidget )
120+
#include "testqgssinglebandpseudocolorrendererwidget.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.