Skip to content
Permalink
Browse files

Fix missing min/max values for classification when changing datasets

The min/max values were stored in the color ramp shader, but to my surprise
these values were never persisted or copied: pseudo-color raster renderer
has a separate storage for the min/max values, so we need that for mesh renderer
as well.

The QgsRasterShaderFunction is not really useful, and QgsColorRamp should not
be derived from it, making things just more confusing.
  • Loading branch information
wonder-sk committed Aug 16, 2018
1 parent 2f5ef8e commit d91e12d41706749b5a74a507863e35f6596501b0
@@ -90,6 +90,19 @@ Returns color ramp shader function
void setColorRampShader( const QgsColorRampShader &shader );
%Docstring
Sets color ramp shader function
%End

double classificationMin() const;
%Docstring
Returns min value used for creation of the color ramp shader
%End
double classificationMax() const;
%Docstring
Returns max value used for creation of the color ramp shader
%End
void setClassificationMinMax( double vMin, double vMax );
%Docstring
Sets min/max values used for creation of the color ramp shader
%End

QDomElement writeXml( QDomDocument &doc ) const;
@@ -45,6 +45,7 @@ QgsMeshRendererScalarSettings QgsMeshRendererScalarSettingsWidget::settings() co
{
QgsMeshRendererScalarSettings settings;
settings.setColorRampShader( mScalarColorRampShaderWidget->shader() );
settings.setClassificationMinMax( lineEditValue( mScalarMinLineEdit ), lineEditValue( mScalarMaxLineEdit ) );
return settings;
}

@@ -59,8 +60,8 @@ void QgsMeshRendererScalarSettingsWidget::syncToLayer( )
const QgsMeshRendererSettings rendererSettings = mMeshLayer->rendererSettings();
const QgsMeshRendererScalarSettings settings = rendererSettings.scalarSettings( mActiveDatasetGroup );
const QgsColorRampShader shader = settings.colorRampShader();
whileBlocking( mScalarMinLineEdit )->setText( QString::number( shader.minimumValue() ) );
whileBlocking( mScalarMaxLineEdit )->setText( QString::number( shader.maximumValue() ) );
whileBlocking( mScalarMinLineEdit )->setText( QString::number( settings.classificationMin() ) );
whileBlocking( mScalarMaxLineEdit )->setText( QString::number( settings.classificationMax() ) );
whileBlocking( mScalarColorRampShaderWidget )->setFromShader( shader );
}

@@ -216,6 +216,7 @@ void QgsMeshLayer::assignDefaultStyleToDatasetGroup( int groupIndex )
fcn.classifyColorRamp( 5, -1, QgsRectangle(), nullptr );

QgsMeshRendererScalarSettings scalarSettings;
scalarSettings.setClassificationMinMax( groupMin, groupMax );
scalarSettings.setColorRampShader( fcn );
mRendererSettings.setScalarSettings( groupIndex, scalarSettings );
}
@@ -205,13 +205,14 @@ void QgsMeshLayerRenderer::renderScalarDataset()
if ( !index.isValid() )
return; // no shader

QgsColorRampShader *fcn = new QgsColorRampShader( mRendererSettings.scalarSettings( index.group() ).colorRampShader() );
const QgsMeshRendererScalarSettings scalarSettings = mRendererSettings.scalarSettings( index.group() );
QgsColorRampShader *fcn = new QgsColorRampShader( scalarSettings.colorRampShader() );
QgsRasterShader *sh = new QgsRasterShader();
sh->setRasterShaderFunction( fcn ); // takes ownership of fcn
QgsMeshLayerInterpolator interpolator( mTriangularMesh, mScalarDatasetValues, mScalarDataOnVertices, mContext, mOutputSize );
QgsSingleBandPseudoColorRenderer renderer( &interpolator, 0, sh ); // takes ownership of sh
renderer.setClassificationMin( fcn->minimumValue() );
renderer.setClassificationMax( fcn->maximumValue() );
renderer.setClassificationMin( scalarSettings.classificationMin() );
renderer.setClassificationMax( scalarSettings.classificationMax() );

std::unique_ptr<QgsRasterBlock> bl( renderer.block( 0, mContext.extent(), mOutputSize.width(), mOutputSize.height(), mFeedback.get() ) );
QImage img = bl->image();
@@ -78,16 +78,26 @@ void QgsMeshRendererScalarSettings::setColorRampShader( const QgsColorRampShader
mColorRampShader = shader;
}

void QgsMeshRendererScalarSettings::setClassificationMinMax( double vMin, double vMax )
{
mClassificationMin = vMin;
mClassificationMax = vMax;
}

QDomElement QgsMeshRendererScalarSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "scalar-settings" );
elem.setAttribute( "min-val", mClassificationMin );
elem.setAttribute( "max-val", mClassificationMax );
QDomElement elemShader = mColorRampShader.writeXml( doc );
elem.appendChild( elemShader );
return elem;
}

void QgsMeshRendererScalarSettings::readXml( const QDomElement &elem )
{
mClassificationMin = elem.attribute( "min-val" ).toDouble();
mClassificationMax = elem.attribute( "max-val" ).toDouble();
QDomElement elemShader = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
mColorRampShader.readXml( elemShader );
}
@@ -82,13 +82,22 @@ class CORE_EXPORT QgsMeshRendererScalarSettings
//! Sets color ramp shader function
void setColorRampShader( const QgsColorRampShader &shader );

//! Returns min value used for creation of the color ramp shader
double classificationMin() const { return mClassificationMin; }
//! Returns max value used for creation of the color ramp shader
double classificationMax() const { return mClassificationMax; }
//! Sets min/max values used for creation of the color ramp shader
void setClassificationMinMax( double vMin, double vMax );

//! Writes configuration to a new DOM element
QDomElement writeXml( QDomDocument &doc ) const;
//! Reads configuration from the given DOM element
void readXml( const QDomElement &elem );

private:
QgsColorRampShader mColorRampShader;
double mClassificationMin = 0;
double mClassificationMax = 0;
};

/**
@@ -47,7 +47,7 @@
<item>
<widget class="QPushButton" name="mScalarRecalculateMinMaxButton">
<property name="text">
<string>Recalculate</string>
<string>Load</string>
</property>
</widget>
</item>

0 comments on commit d91e12d

Please sign in to comment.
You can’t perform that action at this time.