Skip to content

Commit

Permalink
[FEATURE] save and restore color ramp used for singleband pseudocolor…
Browse files Browse the repository at this point in the history
… rendering
  • Loading branch information
alexbruy committed Nov 2, 2016
1 parent 1c44d74 commit 2497375
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/core/raster/qgscolorrampshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ QgsColorRampShader::QgsColorRampShader( double theMinimumValue, double theMaximu
, mLUTFactor( 1.0 )
, mLUTInitialized( false )
, mClip( false )
, mColorRampName( "" )
{
QgsDebugMsgLevel( "called.", 4 );
}
Expand Down Expand Up @@ -82,6 +83,12 @@ void QgsColorRampShader::setColorRampType( const QString& theType )
}
}

void QgsColorRampShader::setColorRampName( const QString& theName )
{
mColorRampName = theName;
}


bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int *theReturnAlphaValue )
{
if ( mColorRampItemList.isEmpty() )
Expand Down
9 changes: 9 additions & 0 deletions src/core/raster/qgscolorrampshader.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
//! \brief Get the color ramp type as a string
QString colorRampTypeAsQString();

//! \brief Get the original color ramp name
QString colorRampName() const {return mColorRampName;}

//! \brief Set custom colormap
void setColorRampItemList( const QList<QgsColorRampShader::ColorRampItem>& theList ); //TODO: sort on set

Expand All @@ -84,6 +87,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
//! \brief Set the color ramp type
void setColorRampType( const QString& theType );

//! \brief Set the source color ramp name
void setColorRampName( const QString& theName );

//! \brief Generates and new RGB value based on one input value
bool shade( double, int*, int*, int*, int* ) override;

Expand Down Expand Up @@ -121,6 +127,9 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
double mLUTFactor;
bool mLUTInitialized;

/** Colorramp name*/
QString mColorRampName;

//! Do not render values out of range
bool mClip;
};
Expand Down
4 changes: 3 additions & 1 deletion src/core/raster/qgsrastershader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void QgsRasterShader::writeXml( QDomDocument& doc, QDomElement& parent ) const
if ( colorRampShader )
{
QDomElement colorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
colorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), colorRampShader->colorRampTypeAsQString() );
colorRampShaderElem.setAttribute( "colorRampName", colorRampShader->colorRampName() );
colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
colorRampShaderElem.setAttribute( QStringLiteral( "clip" ), colorRampShader->clip() );
//items
QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
Expand All @@ -169,6 +170,7 @@ void QgsRasterShader::readXml( const QDomElement& elem )
if ( !colorRampShaderElem.isNull() )
{
QgsColorRampShader* colorRampShader = new QgsColorRampShader();
colorRampShader->setColorRampName( colorRampShaderElem.attribute( "colorRampName", "" ) );
colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );

Expand Down
1 change: 1 addition & 0 deletions src/core/raster/qgssinglebandpseudocolorrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ QgsSingleBandPseudoColorRenderer* QgsSingleBandPseudoColorRenderer::clone() cons
{
QgsColorRampShader * colorRampShader = new QgsColorRampShader( mShader->minimumValue(), mShader->maximumValue() );

colorRampShader->setColorRampName( origColorRampShader->colorRampName() );
colorRampShader->setColorRampType( origColorRampShader->colorRampType() );
colorRampShader->setClip( origColorRampShader->clip() );
colorRampShader->setColorRampItemList( origColorRampShader->colorRampItemList() );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()

QgsColorRampShader::ColorRamp_TYPE interpolation = static_cast< QgsColorRampShader::ColorRamp_TYPE >( mColorInterpolationComboBox->currentData().toInt() );
colorRampShader->setColorRampType( interpolation );
colorRampShader->setColorRampName( mColorRampComboBox->currentText() );
rasterShader->setRasterShaderFunction( colorRampShader );

int bandNumber = mBandComboBox->currentData().toInt();
Expand Down Expand Up @@ -792,6 +793,14 @@ void QgsSingleBandPseudoColorRendererWidget::setFromRenderer( const QgsRasterRen
const QgsColorRampShader* colorRampShader = dynamic_cast<const QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
if ( colorRampShader )
{
int idx = mColorRampComboBox->findText( colorRampShader->colorRampName() );
if ( idx == -1 )
{
QSettings settings;
QString defaultPalette = settings.value( "/Raster/defaultPalette", "Spectral" ).toString();
idx = mColorRampComboBox->findText( defaultPalette );
}
mColorRampComboBox->setCurrentIndex( idx );
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findData( colorRampShader->colorRampType() ) );

const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
Expand Down

0 comments on commit 2497375

Please sign in to comment.