Skip to content

Commit

Permalink
Add QgsColorRampLegendNodeSettings to QgsSingleBandGrayRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 18, 2020
1 parent f96ba98 commit a0accab
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions python/core/auto_generated/raster/qgscolorrampshader.sip.in
Expand Up @@ -212,6 +212,8 @@ Returns whether the shader will clip values which are out of range.
Returns the color ramp shader legend settings. Returns the color ramp shader legend settings.


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

.. versionadded:: 3.18
%End %End


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


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

.. versionadded:: 3.18
%End %End


protected: protected:
Expand Down
20 changes: 20 additions & 0 deletions python/core/auto_generated/raster/qgssinglebandgrayrenderer.sip.in
Expand Up @@ -65,6 +65,26 @@ Takes ownership
virtual void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const; 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: private:
QgsSingleBandGrayRenderer( const QgsSingleBandGrayRenderer & ); QgsSingleBandGrayRenderer( const QgsSingleBandGrayRenderer & );
const QgsSingleBandGrayRenderer &operator=( const QgsSingleBandGrayRenderer & ); const QgsSingleBandGrayRenderer &operator=( const QgsSingleBandGrayRenderer & );
Expand Down
1 change: 1 addition & 0 deletions src/core/raster/qgscolorrampshader.cpp
Expand Up @@ -37,6 +37,7 @@ QgsColorRampShader::QgsColorRampShader( double minimumValue, double maximumValue
: QgsRasterShaderFunction( minimumValue, maximumValue ) : QgsRasterShaderFunction( minimumValue, maximumValue )
, mColorRampType( type ) , mColorRampType( type )
, mClassificationMode( classificationMode ) , mClassificationMode( classificationMode )
, mLegendSettings( qgis::make_unique< QgsColorRampLegendNodeSettings >() )
{ {
QgsDebugMsgLevel( QStringLiteral( "called." ), 4 ); QgsDebugMsgLevel( QStringLiteral( "called." ), 4 );


Expand Down
2 changes: 2 additions & 0 deletions src/core/raster/qgscolorrampshader.h
Expand Up @@ -230,6 +230,7 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
* Returns the color ramp shader legend settings. * Returns the color ramp shader legend settings.
* *
* \see setLegendSettings() * \see setLegendSettings()
* \since QGIS 3.18
*/ */
const QgsColorRampLegendNodeSettings *legendSettings() const; const QgsColorRampLegendNodeSettings *legendSettings() const;


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


Expand Down
30 changes: 28 additions & 2 deletions src/core/raster/qgssinglebandgrayrenderer.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgscontrastenhancement.h" #include "qgscontrastenhancement.h"
#include "qgsrastertransparency.h" #include "qgsrastertransparency.h"
#include "qgscolorramplegendnode.h" #include "qgscolorramplegendnode.h"
#include "qgscolorramplegendnodesettings.h"
#include "qgsreadwritecontext.h"
#include <QDomDocument> #include <QDomDocument>
#include <QDomElement> #include <QDomElement>
#include <QImage> #include <QImage>
Expand All @@ -30,6 +32,7 @@ QgsSingleBandGrayRenderer::QgsSingleBandGrayRenderer( QgsRasterInterface *input,
, mGrayBand( grayBand ) , mGrayBand( grayBand )
, mGradient( BlackToWhite ) , mGradient( BlackToWhite )
, mContrastEnhancement( nullptr ) , mContrastEnhancement( nullptr )
, mLegendSettings( qgis::make_unique< QgsColorRampLegendNodeSettings >() )
{ {
} }


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


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

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

return r; return r;
} }


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

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

parentElem.appendChild( rasterRendererElem ); parentElem.appendChild( rasterRendererElem );
} }


Expand Down Expand Up @@ -226,8 +239,9 @@ QList<QgsLayerTreeModelLegendNode *> QgsSingleBandGrayRenderer::createLegendNode
const QColor minColor = ( mGradient == BlackToWhite ) ? Qt::black : Qt::white; const QColor minColor = ( mGradient == BlackToWhite ) ? Qt::black : Qt::white;
const QColor maxColor = ( mGradient == BlackToWhite ) ? Qt::white : Qt::black; const QColor maxColor = ( mGradient == BlackToWhite ) ? Qt::white : Qt::black;
res << new QgsColorRampLegendNode( nodeLayer, new QgsGradientColorRamp( minColor, maxColor ), res << new QgsColorRampLegendNode( nodeLayer, new QgsGradientColorRamp( minColor, maxColor ),
QString::number( mContrastEnhancement->minimumValue() ), mLegendSettings ? *mLegendSettings : QgsColorRampLegendNodeSettings(),
QString::number( mContrastEnhancement->maximumValue() ) ); mContrastEnhancement->minimumValue(),
mContrastEnhancement->maximumValue() );
} }
return res; return res;
} }
Expand Down Expand Up @@ -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 );
}
21 changes: 20 additions & 1 deletion src/core/raster/qgssinglebandgrayrenderer.h
Expand Up @@ -25,6 +25,7 @@


class QgsContrastEnhancement; class QgsContrastEnhancement;
class QDomElement; class QDomElement;
class QgsColorRampLegendNodeSettings;


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


void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override; 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: private:
#ifdef SIP_RUN #ifdef SIP_RUN
QgsSingleBandGrayRenderer( const QgsSingleBandGrayRenderer & ); QgsSingleBandGrayRenderer( const QgsSingleBandGrayRenderer & );
Expand All @@ -79,7 +98,7 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer
int mGrayBand; int mGrayBand;
Gradient mGradient; Gradient mGradient;
std::unique_ptr< QgsContrastEnhancement > mContrastEnhancement; std::unique_ptr< QgsContrastEnhancement > mContrastEnhancement;

std::unique_ptr< QgsColorRampLegendNodeSettings > mLegendSettings;
}; };


#endif // QGSSINGLEBANDGRAYRENDERER_H #endif // QGSSINGLEBANDGRAYRENDERER_H

0 comments on commit a0accab

Please sign in to comment.