[api] Add virtual QgsRasterRenderer::setInputBand method

Attempts to set the input band for the renderer.
Returns TRUE if the band was successfully set, or FALSE if the
band could not be set.

This was implemented in various raster renderer subclasses,
but it was necessary to down cast and then call the individual
methods (which don't have consistent names!).

Instead, add a top level virtual method so that it's easy to
change the input band for the renderers.
This commit is contained in:
Nyall Dawson 2024-03-14 12:45:26 +10:00
parent 844f0ccc74
commit 5d29d49869
28 changed files with 132 additions and 21 deletions

View File

@ -71,6 +71,8 @@ Sets the band used by the renderer.
.. seealso:: :py:func:`band`
%End
virtual bool setInputBand( int band );
double azimuth() const;
%Docstring

View File

@ -127,6 +127,9 @@ Set category label
Returns the raster band used for rendering the raster.
%End
virtual bool setInputBand( int band );
virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const;
virtual QList< QPair< QString, QColor > > legendSymbologyItems() const;

View File

@ -56,10 +56,8 @@ Creates an instance of the renderer based on definition from XML (used by render
%Docstring
Returns the number of the input raster band
%End
void setInputBand( int band );
%Docstring
Sets the number of the input raster band
%End
virtual bool setInputBand( int band );
double contourInterval() const;
%Docstring

View File

@ -67,6 +67,19 @@ The default implementation returns ``False``.
virtual bool setInput( QgsRasterInterface *input );
virtual bool setInputBand( int band );
%Docstring
Attempts to set the input ``band`` for the renderer.
Returns ``True`` if the band was successfully set, or ``False`` if the band could not be set.
.. note::
Not all renderers support setting the input band.
.. versionadded:: 3.38
%End
virtual QgsRasterBlock *block( int bandNo,
const QgsRectangle &extent,
int width,

View File

@ -35,6 +35,8 @@ QgsSingleBandColorDataRenderer cannot be copied. Use :py:func:`~QgsSingleBandCol
virtual bool setInput( QgsRasterInterface *input );
virtual bool setInputBand( int band );
virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = 0 ) /Factory/;

View File

@ -45,6 +45,9 @@ QgsSingleBandGrayRenderer cannot be copied. Use :py:func:`~QgsSingleBandGrayRend
int grayBand() const;
void setGrayBand( int band );
virtual bool setInputBand( int band );
const QgsContrastEnhancement *contrastEnhancement() const;
void setContrastEnhancement( QgsContrastEnhancement *ce /Transfer/ );
%Docstring

View File

@ -101,6 +101,8 @@ Sets the band used by the renderer.
.. seealso:: :py:func:`band`
%End
virtual bool setInputBand( int band );
double classificationMin() const;
double classificationMax() const;

View File

@ -71,6 +71,8 @@ Sets the band used by the renderer.
.. seealso:: :py:func:`band`
%End
virtual bool setInputBand( int band );
double azimuth() const;
%Docstring

View File

@ -127,6 +127,9 @@ Set category label
Returns the raster band used for rendering the raster.
%End
virtual bool setInputBand( int band );
virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const;
virtual QList< QPair< QString, QColor > > legendSymbologyItems() const;

View File

@ -56,10 +56,8 @@ Creates an instance of the renderer based on definition from XML (used by render
%Docstring
Returns the number of the input raster band
%End
void setInputBand( int band );
%Docstring
Sets the number of the input raster band
%End
virtual bool setInputBand( int band );
double contourInterval() const;
%Docstring

View File

@ -67,6 +67,19 @@ The default implementation returns ``False``.
virtual bool setInput( QgsRasterInterface *input );
virtual bool setInputBand( int band );
%Docstring
Attempts to set the input ``band`` for the renderer.
Returns ``True`` if the band was successfully set, or ``False`` if the band could not be set.
.. note::
Not all renderers support setting the input band.
.. versionadded:: 3.38
%End
virtual QgsRasterBlock *block( int bandNo,
const QgsRectangle &extent,
int width,

View File

@ -35,6 +35,8 @@ QgsSingleBandColorDataRenderer cannot be copied. Use :py:func:`~QgsSingleBandCol
virtual bool setInput( QgsRasterInterface *input );
virtual bool setInputBand( int band );
virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = 0 ) /Factory/;

View File

@ -45,6 +45,9 @@ QgsSingleBandGrayRenderer cannot be copied. Use :py:func:`~QgsSingleBandGrayRend
int grayBand() const;
void setGrayBand( int band );
virtual bool setInputBand( int band );
const QgsContrastEnhancement *contrastEnhancement() const;
void setContrastEnhancement( QgsContrastEnhancement *ce /Transfer/ );
%Docstring

View File

@ -101,6 +101,8 @@ Sets the band used by the renderer.
.. seealso:: :py:func:`band`
%End
virtual bool setInputBand( int band );
double classificationMin() const;
double classificationMax() const;

View File

@ -571,11 +571,17 @@ QList<int> QgsHillshadeRenderer::usesBands() const
void QgsHillshadeRenderer::setBand( int bandNo )
{
if ( bandNo > mInput->bandCount() || bandNo <= 0 )
setInputBand( bandNo );
}
bool QgsHillshadeRenderer::setInputBand( int band )
{
if ( band > mInput->bandCount() || band <= 0 )
{
return;
return false;
}
mBand = bandNo;
mBand = band;
return true;
}
void QgsHillshadeRenderer::toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const

View File

@ -74,6 +74,7 @@ class CORE_EXPORT QgsHillshadeRenderer : public QgsRasterRenderer
* \see band
*/
void setBand( int bandNo );
bool setInputBand( int band ) override;
/**
* Returns the direction of the light over the raster between 0-360.

View File

@ -175,6 +175,12 @@ void QgsPalettedRasterRenderer::setLabel( double idx, const QString &label )
}
}
bool QgsPalettedRasterRenderer::setInputBand( int band )
{
mBand = band;
return true;
}
QgsRasterBlock *QgsPalettedRasterRenderer::block( int, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
{
std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock() );

View File

@ -146,6 +146,8 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
*/
int band() const { return mBand; }
bool setInputBand( int band ) override;
void writeXml( QDomDocument &doc, QDomElement &parentElem ) const override;
QList< QPair< QString, QColor > > legendSymbologyItems() const override;
QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY override;

View File

@ -232,6 +232,12 @@ QList<QgsLayerTreeModelLegendNode *> QgsRasterContourRenderer::createLegendNodes
return nodes;
}
bool QgsRasterContourRenderer::setInputBand( int band )
{
mInputBand = band;
return true;
}
void QgsRasterContourRenderer::setContourSymbol( QgsLineSymbol *symbol )
{
mContourSymbol.reset( symbol );

View File

@ -55,8 +55,7 @@ class CORE_EXPORT QgsRasterContourRenderer : public QgsRasterRenderer
//! Returns the number of the input raster band
int inputBand() const { return mInputBand; }
//! Sets the number of the input raster band
void setInputBand( int band ) { mInputBand = band; }
bool setInputBand( int band ) override;
//! Returns the interval of contour lines generation
double contourInterval() const { return mContourInterval; }

View File

@ -97,6 +97,11 @@ bool QgsRasterRenderer::setInput( QgsRasterInterface *input )
return true;
}
bool QgsRasterRenderer::setInputBand( int )
{
return false;
}
bool QgsRasterRenderer::usesTransparency() const
{
if ( !mInput )

View File

@ -85,6 +85,17 @@ class CORE_EXPORT QgsRasterRenderer : public QgsRasterInterface
bool setInput( QgsRasterInterface *input ) override;
/**
* Attempts to set the input \a band for the renderer.
*
* Returns TRUE if the band was successfully set, or FALSE if the band could not be set.
*
* \note Not all renderers support setting the input band.
*
* \since QGIS 3.38
*/
virtual bool setInputBand( int band );
QgsRasterBlock *block( int bandNo,
const QgsRectangle &extent,
int width,

View File

@ -23,8 +23,9 @@
#include <QImage>
#include <memory>
QgsSingleBandColorDataRenderer::QgsSingleBandColorDataRenderer( QgsRasterInterface *input, int band ):
QgsRasterRenderer( input, QStringLiteral( "singlebandcolordata" ) ), mBand( band )
QgsSingleBandColorDataRenderer::QgsSingleBandColorDataRenderer( QgsRasterInterface *input, int band )
: QgsRasterRenderer( input, QStringLiteral( "singlebandcolordata" ) )
, mBand( band )
{
}
@ -139,3 +140,9 @@ bool QgsSingleBandColorDataRenderer::setInput( QgsRasterInterface *input )
}
return false;
}
bool QgsSingleBandColorDataRenderer::setInputBand( int band )
{
mBand = band;
return true;
}

View File

@ -44,6 +44,7 @@ class CORE_EXPORT QgsSingleBandColorDataRenderer: public QgsRasterRenderer
static QgsRasterRenderer *create( const QDomElement &elem, QgsRasterInterface *input ) SIP_FACTORY;
bool setInput( QgsRasterInterface *input ) override;
bool setInputBand( int band ) override;
QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override SIP_FACTORY;

View File

@ -193,6 +193,17 @@ QgsRasterBlock *QgsSingleBandGrayRenderer::block( int bandNo, const QgsRectangle
return outputBlock.release();
}
void QgsSingleBandGrayRenderer::setGrayBand( int band )
{
setInputBand( band );
}
bool QgsSingleBandGrayRenderer::setInputBand( int band )
{
mGrayBand = band;
return true;
}
void QgsSingleBandGrayRenderer::writeXml( QDomDocument &doc, QDomElement &parentElem ) const
{
if ( parentElem.isNull() )

View File

@ -56,7 +56,9 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer
QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override SIP_FACTORY;
int grayBand() const { return mGrayBand; }
void setGrayBand( int band ) { mGrayBand = band; }
void setGrayBand( int band );
bool setInputBand( int band ) override;
const QgsContrastEnhancement *contrastEnhancement() const { return mContrastEnhancement.get(); }
//! Takes ownership
void setContrastEnhancement( QgsContrastEnhancement *ce SIP_TRANSFER );

View File

@ -37,17 +37,23 @@ QgsSingleBandPseudoColorRenderer::QgsSingleBandPseudoColorRenderer( QgsRasterInt
}
void QgsSingleBandPseudoColorRenderer::setBand( int bandNo )
{
setInputBand( bandNo );
}
bool QgsSingleBandPseudoColorRenderer::setInputBand( int band )
{
if ( !mInput )
{
mBand = bandNo;
return;
mBand = band;
return true;
}
if ( bandNo <= mInput->bandCount() || bandNo > 0 )
else if ( band <= mInput->bandCount() || band > 0 )
{
mBand = bandNo;
mBand = band;
return true;
}
return false;
}
void QgsSingleBandPseudoColorRenderer::setClassificationMin( double min )
@ -481,3 +487,4 @@ bool QgsSingleBandPseudoColorRenderer::canCreateRasterAttributeTable() const
{
return true;
}

View File

@ -95,6 +95,7 @@ class CORE_EXPORT QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
* \see band
*/
void setBand( int bandNo );
bool setInputBand( int band ) override;
double classificationMin() const { return mClassificationMin; }
double classificationMax() const { return mClassificationMax; }