Skip to content

Commit

Permalink
Fix data-defined overrides ignored in legend text format
Browse files Browse the repository at this point in the history
Fixes #60628
  • Loading branch information
nyalldawson committed Feb 19, 2025
1 parent b743393 commit 2ffb8c0
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/PyQt6/core/auto_generated/qgslegendsettings.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ The content of the legend is driven by the :py:class:`QgsLegendModel` class.
public:
QgsLegendSettings();

void updateDataDefinedProperties( QgsRenderContext &context );
%Docstring
Updates any data-defined properties in the settings, using the specified
render ``context``.

.. versionadded:: 3.42
%End

void setTitle( const QString &t );
%Docstring
Sets the title for the legend, which will be rendered above all legend items.
Expand Down
8 changes: 8 additions & 0 deletions python/PyQt6/core/auto_generated/qgslegendstyle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ Writes the component's style definition to an XML element.
Reads the component's style definition from an XML element.

.. seealso:: :py:func:`writeXml`
%End

void updateDataDefinedProperties( QgsRenderContext &context );
%Docstring
Updates any data-defined properties in the style, using the specified
render ``context``.

.. versionadded:: 3.42
%End

static QString styleName( Style s );
Expand Down
8 changes: 8 additions & 0 deletions python/core/auto_generated/qgslegendsettings.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ The content of the legend is driven by the :py:class:`QgsLegendModel` class.
public:
QgsLegendSettings();

void updateDataDefinedProperties( QgsRenderContext &context );
%Docstring
Updates any data-defined properties in the settings, using the specified
render ``context``.

.. versionadded:: 3.42
%End

void setTitle( const QString &t );
%Docstring
Sets the title for the legend, which will be rendered above all legend items.
Expand Down
8 changes: 8 additions & 0 deletions python/core/auto_generated/qgslegendstyle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ Writes the component's style definition to an XML element.
Reads the component's style definition from an XML element.

.. seealso:: :py:func:`writeXml`
%End

void updateDataDefinedProperties( QgsRenderContext &context );
%Docstring
Updates any data-defined properties in the style, using the specified
render ``context``.

.. versionadded:: 3.42
%End

static QString styleName( Style s );
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgslegendrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ QSizeF QgsLegendRenderer::paintAndDetermineSize( QgsRenderContext &context )
if ( !rootGroup )
return size;

mSettings.updateDataDefinedProperties( context );

// temporarily remove painter from context -- we don't need to actually draw anything yet. But we DO need
// to send the full render context so that an expression context is available during the size calculation
QgsScopedRenderContextPainterSwap noPainter( context, nullptr );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgslegendsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ QgsLegendSettings::QgsLegendSettings()
rstyle( QgsLegendStyle::SymbolLabel ).setTextFormat( f );
}

void QgsLegendSettings::updateDataDefinedProperties( QgsRenderContext &context )
{
rstyle( QgsLegendStyle::Title ).updateDataDefinedProperties( context );
rstyle( QgsLegendStyle::Group ).updateDataDefinedProperties( context );
rstyle( QgsLegendStyle::Subgroup ).updateDataDefinedProperties( context );
rstyle( QgsLegendStyle::SymbolLabel ).updateDataDefinedProperties( context );
}

QColor QgsLegendSettings::fontColor() const
{
return style( QgsLegendStyle::SymbolLabel ).textFormat().color();
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgslegendsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class CORE_EXPORT QgsLegendSettings
public:
QgsLegendSettings();

/**
* Updates any data-defined properties in the settings, using the specified
* render \a context.
*
* \since QGIS 3.42
*/
void updateDataDefinedProperties( QgsRenderContext &context );

/**
* Sets the title for the legend, which will be rendered above all legend items.
*
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgslegendstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgsfontutils.h"
#include "qgis.h"
#include "qgsreadwritecontext.h"
#include "qgspropertycollection.h"

#include <QFont>
#include <QMap>
Expand Down Expand Up @@ -110,6 +111,13 @@ void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc,
mIndent = elem.attribute( QStringLiteral( "indent" ), QStringLiteral( "0" ) ).toDouble();
}

void QgsLegendStyle::updateDataDefinedProperties( QgsRenderContext &context )
{
if ( mTextFormat.dataDefinedProperties().hasActiveProperties() ) // note, we use format instead of tmpFormat here, it's const and potentially avoids a detach
mTextFormat.updateDataDefinedProperties( context );

}

QString QgsLegendStyle::styleName( Style s )
{
switch ( s )
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgslegendstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ class CORE_EXPORT QgsLegendStyle
*/
void readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context = QgsReadWriteContext() );

/**
* Updates any data-defined properties in the style, using the specified
* render \a context.
*
* \since QGIS 3.42
*/
void updateDataDefinedProperties( QgsRenderContext &context );

/**
* Returns the name for a style component as a string.
*
Expand Down

0 comments on commit 2ffb8c0

Please sign in to comment.