Skip to content
Permalink
Browse files

Add QgsColorRampLegendNodeSettings to QgsSingleBandGrayRenderer

  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent f96ba98 commit a0accab0c25e1ceef922621a5f9c8fe7a8ebf713
@@ -212,6 +212,8 @@ Returns whether the shader will clip values which are out of range.
Returns the color ramp shader legend settings.

.. seealso:: :py:func:`setLegendSettings`

.. versionadded:: 3.18
%End

void setLegendSettings( QgsColorRampLegendNodeSettings *settings /Transfer/ );
@@ -221,6 +223,8 @@ Sets the color ramp shader legend ``settings``.
Ownership of ``settings`` is transferred.

.. seealso:: :py:func:`legendSettings`

.. versionadded:: 3.18
%End

protected:
@@ -65,6 +65,26 @@ Takes ownership
virtual void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const;


const QgsColorRampLegendNodeSettings *legendSettings() const;
%Docstring
Returns the color ramp shader legend settings.

.. seealso:: :py:func:`setLegendSettings`

.. versionadded:: 3.18
%End

void setLegendSettings( QgsColorRampLegendNodeSettings *settings /Transfer/ );
%Docstring
Sets the color ramp shader legend ``settings``.

Ownership of ``settings`` is transferred.

.. seealso:: :py:func:`legendSettings`

.. versionadded:: 3.18
%End

private:
QgsSingleBandGrayRenderer( const QgsSingleBandGrayRenderer & );
const QgsSingleBandGrayRenderer &operator=( const QgsSingleBandGrayRenderer & );
@@ -37,6 +37,7 @@ QgsColorRampShader::QgsColorRampShader( double minimumValue, double maximumValue
: QgsRasterShaderFunction( minimumValue, maximumValue )
, mColorRampType( type )
, mClassificationMode( classificationMode )
, mLegendSettings( qgis::make_unique< QgsColorRampLegendNodeSettings >() )
{
QgsDebugMsgLevel( QStringLiteral( "called." ), 4 );

@@ -230,6 +230,7 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
* Returns the color ramp shader legend settings.
*
* \see setLegendSettings()
* \since QGIS 3.18
*/
const QgsColorRampLegendNodeSettings *legendSettings() const;

@@ -239,6 +240,7 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
* Ownership of \a settings is transferred.
*
* \see legendSettings()
* \since QGIS 3.18
*/
void setLegendSettings( QgsColorRampLegendNodeSettings *settings SIP_TRANSFER );

@@ -19,6 +19,8 @@
#include "qgscontrastenhancement.h"
#include "qgsrastertransparency.h"
#include "qgscolorramplegendnode.h"
#include "qgscolorramplegendnodesettings.h"
#include "qgsreadwritecontext.h"
#include <QDomDocument>
#include <QDomElement>
#include <QImage>
@@ -30,6 +32,7 @@ QgsSingleBandGrayRenderer::QgsSingleBandGrayRenderer( QgsRasterInterface *input,
, mGrayBand( grayBand )
, mGradient( BlackToWhite )
, mContrastEnhancement( nullptr )
, mLegendSettings( qgis::make_unique< QgsColorRampLegendNodeSettings >() )
{
}

@@ -43,6 +46,7 @@ QgsSingleBandGrayRenderer *QgsSingleBandGrayRenderer::clone() const
{
renderer->setContrastEnhancement( new QgsContrastEnhancement( *mContrastEnhancement ) );
}
renderer->setLegendSettings( mLegendSettings ? new QgsColorRampLegendNodeSettings( *mLegendSettings.get() ) : new QgsColorRampLegendNodeSettings() );
return renderer;
}

@@ -70,6 +74,11 @@ QgsRasterRenderer *QgsSingleBandGrayRenderer::create( const QDomElement &elem, Q
ce->readXml( contrastEnhancementElem );
r->setContrastEnhancement( ce );
}

std::unique_ptr< QgsColorRampLegendNodeSettings > legendSettings = qgis::make_unique< QgsColorRampLegendNodeSettings >();
legendSettings->readXml( elem, QgsReadWriteContext() );
r->setLegendSettings( legendSettings.release() );

return r;
}

@@ -196,6 +205,10 @@ void QgsSingleBandGrayRenderer::writeXml( QDomDocument &doc, QDomElement &parent
mContrastEnhancement->writeXml( doc, contrastElem );
rasterRendererElem.appendChild( contrastElem );
}

if ( mLegendSettings )
mLegendSettings->writeXml( doc, rasterRendererElem, QgsReadWriteContext() );

parentElem.appendChild( rasterRendererElem );
}

@@ -226,8 +239,9 @@ QList<QgsLayerTreeModelLegendNode *> QgsSingleBandGrayRenderer::createLegendNode
const QColor minColor = ( mGradient == BlackToWhite ) ? Qt::black : Qt::white;
const QColor maxColor = ( mGradient == BlackToWhite ) ? Qt::white : Qt::black;
res << new QgsColorRampLegendNode( nodeLayer, new QgsGradientColorRamp( minColor, maxColor ),
QString::number( mContrastEnhancement->minimumValue() ),
QString::number( mContrastEnhancement->maximumValue() ) );
mLegendSettings ? *mLegendSettings : QgsColorRampLegendNodeSettings(),
mContrastEnhancement->minimumValue(),
mContrastEnhancement->maximumValue() );
}
return res;
}
@@ -391,3 +405,15 @@ void QgsSingleBandGrayRenderer::toSld( QDomDocument &doc, QDomElement &element,
}
}
}

const QgsColorRampLegendNodeSettings *QgsSingleBandGrayRenderer::legendSettings() const
{
return mLegendSettings.get();
}

void QgsSingleBandGrayRenderer::setLegendSettings( QgsColorRampLegendNodeSettings *settings )
{
if ( settings == mLegendSettings.get() )
return;
mLegendSettings.reset( settings );
}
@@ -25,6 +25,7 @@

class QgsContrastEnhancement;
class QDomElement;
class QgsColorRampLegendNodeSettings;

/**
* \ingroup core
@@ -70,6 +71,24 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer

void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;

/**
* Returns the color ramp shader legend settings.
*
* \see setLegendSettings()
* \since QGIS 3.18
*/
const QgsColorRampLegendNodeSettings *legendSettings() const;

/**
* Sets the color ramp shader legend \a settings.
*
* Ownership of \a settings is transferred.
*
* \see legendSettings()
* \since QGIS 3.18
*/
void setLegendSettings( QgsColorRampLegendNodeSettings *settings SIP_TRANSFER );

private:
#ifdef SIP_RUN
QgsSingleBandGrayRenderer( const QgsSingleBandGrayRenderer & );
@@ -79,7 +98,7 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer
int mGrayBand;
Gradient mGradient;
std::unique_ptr< QgsContrastEnhancement > mContrastEnhancement;

std::unique_ptr< QgsColorRampLegendNodeSettings > mLegendSettings;
};

#endif // QGSSINGLEBANDGRAYRENDERER_H

0 comments on commit a0accab

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