Merge pull request #58790 from elpaso/bugfix-gh57422-server-cascading-legend-size

[server] WMS GetLegendGr.. cascading size
This commit is contained in:
Julien Cabieces 2024-09-23 10:04:26 +02:00 committed by GitHub
commit bcaea01703
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 3 deletions

View File

@ -661,6 +661,14 @@ Constructor for QgsWmsLegendNode.
virtual void invalidateMapBasedData();
QImage getLegendGraphicBlocking( ) const;
%Docstring
Fetches the image from the server and returns it.
.. versionadded:: 3.40
%End
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );

View File

@ -661,6 +661,14 @@ Constructor for QgsWmsLegendNode.
virtual void invalidateMapBasedData();
QImage getLegendGraphicBlocking( ) const;
%Docstring
Fetches the image from the server and returns it.
.. versionadded:: 3.40
%End
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsWmsLegendNode: \"%1\">" ).arg( sipCpp->data( Qt::DisplayRole ).toString() );

View File

@ -1462,6 +1462,11 @@ void QgsWmsLegendNode::invalidateMapBasedData()
emit dataChanged();
}
QImage QgsWmsLegendNode::getLegendGraphicBlocking() const
{
return getLegendGraphic( true );
}
// -------------------------------------------------------------------------
QgsDataDefinedSizeLegendNode::QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent )

View File

@ -747,6 +747,13 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode
void invalidateMapBasedData() override;
/**
* Fetches the image from the server and returns it.
* \since QGIS 3.40
*/
QImage getLegendGraphicBlocking( ) const;
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
@ -763,7 +770,7 @@ class CORE_EXPORT QgsWmsLegendNode : public QgsLayerTreeModelLegendNode
private:
// Lazily initializes mImage
// Lazy loading of the image
QImage getLegendGraphic( bool synchronous = false ) const;
QImage renderMessage( const QString &msg ) const;

View File

@ -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->getLegendGraphicBlocking( ) };
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
@ -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() ) )