Skip to content

Commit

Permalink
Fix missing min/max values for classification when changing datasets
Browse files Browse the repository at this point in the history
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.
wonder-sk committed Aug 17, 2018
1 parent 2f5ef8e commit d91e12d
Showing 7 changed files with 41 additions and 6 deletions.
13 changes: 13 additions & 0 deletions python/core/auto_generated/mesh/qgsmeshrenderersettings.sip.in
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions src/app/mesh/qgsmeshrendererscalarsettingswidget.cpp
Original file line number Diff line number Diff line change
@@ -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 );
}

1 change: 1 addition & 0 deletions src/core/mesh/qgsmeshlayer.cpp
Original file line number Diff line number Diff line change
@@ -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 );
}
7 changes: 4 additions & 3 deletions src/core/mesh/qgsmeshlayerrenderer.cpp
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 10 additions & 0 deletions src/core/mesh/qgsmeshrenderersettings.cpp
Original file line number Diff line number Diff line change
@@ -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 );
}
9 changes: 9 additions & 0 deletions src/core/mesh/qgsmeshrenderersettings.h
Original file line number Diff line number Diff line change
@@ -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;
};

/**
2 changes: 1 addition & 1 deletion src/ui/mesh/qgsmeshrendererscalarsettingswidgetbase.ui
Original file line number Diff line number Diff line change
@@ -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.