Skip to content

Commit

Permalink
[server] WMS GetLegendGr.. cascading size
Browse files Browse the repository at this point in the history
Fix #57422 by adjusting the legend size considering the
cascaded legend imag size.
  • Loading branch information
elpaso committed Sep 18, 2024
1 parent dfe6494 commit 90622be
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,16 @@ Constructor for QgsWmsLegendNode.
virtual void invalidateMapBasedData();


QImage getLegendGraphic( bool synchronous = false ) const;
%Docstring
Lazily initializes mImage

:param synchronous: if ``True``, the image is fetched synchronously, otherwise asynchronously

.. versionadded:: 3.40
%End


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,16 @@ Constructor for QgsWmsLegendNode.
virtual void invalidateMapBasedData();


QImage getLegendGraphic( bool synchronous = false ) const;
%Docstring
Lazily initializes mImage

:param synchronous: if ``True``, the image is fetched synchronously, otherwise asynchronously

.. versionadded:: 3.40
%End


SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );
Expand Down
10 changes: 8 additions & 2 deletions src/core/layertree/qgslayertreemodellegendnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,14 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode

void invalidateMapBasedData() override;

/**
* Lazily initializes mImage
* \param synchronous if TRUE, the image is fetched synchronously, otherwise asynchronously
* \since QGIS 3.40
*/
QImage getLegendGraphic( bool synchronous = false ) const;


#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
Expand All @@ -763,8 +771,6 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode

private:

// Lazily initializes mImage
QImage getLegendGraphic( bool synchronous = false ) const;

QImage renderMessage( const QString &msg ) const;

Expand Down
31 changes: 29 additions & 2 deletions src/server/services/wms/qgswmsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,36 @@ namespace QgsWms
QList<QgsMapLayer *> layers = mContext.layersToRender();
configureLayers( layers );

// init renderer
const qreal dpmm = mContext.dotsPerMm();

QgsLegendSettings settings = legendSettings();

// adjust the size settings if there any WMS cascading layers to renderer
const auto layersToRender = mContext.layersToRender();
for ( const auto &layer : std::as_const( layersToRender ) )
{
// If it is a cascading WMS layer, get legend node image size
if ( layer->dataProvider()->name() == QStringLiteral( "wms" ) )
{
if ( QgsWmsLegendNode *layerNode = qobject_cast<QgsWmsLegendNode *>( model.findLegendNode( layer->id(), QString() ) ) )
{
const auto image { layerNode->getLegendGraphic( true ) };
if ( ! image.isNull() )
{
// Check that we are not exceeding the maximum size
if ( mContext.isValidWidthHeight( image.width(), image.height() ) )
{
const double w = image.width() / dpmm;
const double h = image.height() / dpmm;
const QSizeF newWmsSize { w, h };
settings.setWmsLegendSize( newWmsSize );
}
}
}
}
}

// init renderer
QgsLegendRenderer renderer( &model, settings );

// create context
Expand All @@ -146,7 +174,6 @@ namespace QgsWms

// create image according to context
std::unique_ptr<QImage> image;
const qreal dpmm = mContext.dotsPerMm();
const QSizeF minSize = renderer.minimumSize( &context );
const QSize size( static_cast<int>( minSize.width() * dpmm ), static_cast<int>( minSize.height() * dpmm ) );
if ( !mContext.isValidWidthHeight( size.width(), size.height() ) )
Expand Down

0 comments on commit 90622be

Please sign in to comment.