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.
This commit is contained in:
Martin Dobias 2018-08-16 17:50:19 +02:00
parent 2f5ef8e0f8
commit d91e12d417
7 changed files with 41 additions and 6 deletions

View File

@ -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;

View File

@ -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 );
}

View File

@ -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 );
}

View File

@ -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();

View File

@ -78,9 +78,17 @@ 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;
@ -88,6 +96,8 @@ QDomElement QgsMeshRendererScalarSettings::writeXml( QDomDocument &doc ) const
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 );
}

View File

@ -82,6 +82,13 @@ 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
@ -89,6 +96,8 @@ class CORE_EXPORT QgsMeshRendererScalarSettings
private:
QgsColorRampShader mColorRampShader;
double mClassificationMin = 0;
double mClassificationMax = 0;
};
/**

View File

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