Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a color ramp legend for vector heatmap layers #57327

Merged
merged 7 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Creates a new heatmap renderer instance from XML
static QgsHeatmapRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) /Factory/;
virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;

virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) const /Factory/;


virtual void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context );

Expand All @@ -81,6 +83,24 @@ Sets the color ramp to use for shading the heatmap.
:param ramp: color ramp for heatmap. Ownership of ramp is transferred to the renderer.

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

const QgsColorRampLegendNodeSettings &legendSettings() const;
%Docstring
Returns the color ramp legend settings.

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

.. versionadded:: 3.38
%End

void setLegendSettings( const QgsColorRampLegendNodeSettings &settings );
%Docstring
Sets the color ramp legend ``settings``.

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

.. versionadded:: 3.38
%End

double radius() const;
Expand Down
15 changes: 15 additions & 0 deletions python/PyQt6/core/auto_generated/symbology/qgsrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,22 @@ the features displayed using that key.
%Docstring
Returns a list of symbology items for the legend

.. seealso:: :py:func:`createLegendNodes`

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

virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) const /Factory/;
%Docstring
Returns a list of legend nodes to be used for the legend for the renderer.

Ownership is transferred to the caller.

The default implementation creates a legend node for each symbol item returned by :py:func:`~QgsFeatureRenderer.legendSymbolItems`

.. seealso:: :py:func:`legendSymbolItems`

.. versionadded:: 3.38
%End

virtual QString legendClassificationAttribute() const;
Expand Down
14 changes: 14 additions & 0 deletions python/PyQt6/gui/auto_additions/qgscolorramplegendnodewidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# The following has been generated automatically from src/gui/qgscolorramplegendnodewidget.h
# monkey patching scoped based enum
QgsColorRampLegendNodeWidget.Capability.Prefix.__doc__ = "Allow editing legend prefix"
QgsColorRampLegendNodeWidget.Capability.Suffix.__doc__ = "Allow editing legend suffix"
QgsColorRampLegendNodeWidget.Capability.NumberFormat.__doc__ = "Allow editing number format"
QgsColorRampLegendNodeWidget.Capability.DefaultMinimum.__doc__ = "Allow resetting minimum label to default"
QgsColorRampLegendNodeWidget.Capability.DefaultMaximum.__doc__ = "Allow resetting maximum label to default"
QgsColorRampLegendNodeWidget.Capability.AllCapabilities.__doc__ = "All capabilities"
QgsColorRampLegendNodeWidget.Capability.__doc__ = "Capabilities to expose in the widget.\n\n.. versionadded:: 3.38\n\n" + '* ``Prefix``: ' + QgsColorRampLegendNodeWidget.Capability.Prefix.__doc__ + '\n' + '* ``Suffix``: ' + QgsColorRampLegendNodeWidget.Capability.Suffix.__doc__ + '\n' + '* ``NumberFormat``: ' + QgsColorRampLegendNodeWidget.Capability.NumberFormat.__doc__ + '\n' + '* ``DefaultMinimum``: ' + QgsColorRampLegendNodeWidget.Capability.DefaultMinimum.__doc__ + '\n' + '* ``DefaultMaximum``: ' + QgsColorRampLegendNodeWidget.Capability.DefaultMaximum.__doc__ + '\n' + '* ``AllCapabilities``: ' + QgsColorRampLegendNodeWidget.Capability.AllCapabilities.__doc__
# --
QgsColorRampLegendNodeWidget.Capability.baseClass = QgsColorRampLegendNodeWidget
QgsColorRampLegendNodeWidget.Capabilities = lambda flags=0: QgsColorRampLegendNodeWidget.Capability(flags)
QgsColorRampLegendNodeWidget.Capabilities.baseClass = QgsColorRampLegendNodeWidget
Capabilities = QgsColorRampLegendNodeWidget # dirty hack since SIP seems to introduce the flags in module
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@




class QgsColorRampLegendNodeWidget: QgsPanelWidget
{
%Docstring(signature="appended")
Expand All @@ -30,9 +29,24 @@ When changes are made the to settings by a user the :py:func:`~widgetChanged` si
%End
public:

QgsColorRampLegendNodeWidget( QWidget *parent = 0 );
enum class Capability /BaseType=IntFlag/
{
Prefix,
Suffix,
NumberFormat,
DefaultMinimum,
DefaultMaximum,
AllCapabilities,
};

typedef QFlags<QgsColorRampLegendNodeWidget::Capability> Capabilities;


QgsColorRampLegendNodeWidget( QWidget *parent = 0, QgsColorRampLegendNodeWidget::Capabilities capabilities = QgsColorRampLegendNodeWidget::Capability::AllCapabilities );
%Docstring
Constructor for QgsColorRampLegendNodeWidget, with the specified ``parent`` widget.

Since QGIS 3.38, the ``capabilities`` argument can be used to fine-tune settings exposed in the widget.
%End

QgsColorRampLegendNodeSettings settings() const;
Expand All @@ -59,6 +73,8 @@ when using single band gray renderer).
%End

};
QFlags<QgsColorRampLegendNodeWidget::Capability> operator|(QgsColorRampLegendNodeWidget::Capability f1, QFlags<QgsColorRampLegendNodeWidget::Capability> f2);


class QgsColorRampLegendNodeDialog : QDialog
{
Expand All @@ -73,9 +89,11 @@ A dialog for configuring a :py:class:`QgsColorRampLegendNode` (:py:class:`QgsCol
%End
public:

QgsColorRampLegendNodeDialog( const QgsColorRampLegendNodeSettings &settings, QWidget *parent /TransferThis/ = 0 );
QgsColorRampLegendNodeDialog( const QgsColorRampLegendNodeSettings &settings, QWidget *parent /TransferThis/ = 0, QgsColorRampLegendNodeWidget::Capabilities capabilities = QgsColorRampLegendNodeWidget::Capability::AllCapabilities );
%Docstring
Constructor for QgsColorRampLegendNodeDialog, initially showing the specified ``settings``.

Since QGIS 3.38, the ``capabilities`` argument can be used to fine-tune settings exposed in the dialog.
%End

QgsColorRampLegendNodeSettings settings() const;
Expand Down
20 changes: 20 additions & 0 deletions python/core/auto_generated/symbology/qgsheatmaprenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Creates a new heatmap renderer instance from XML
static QgsHeatmapRenderer *convertFromRenderer( const QgsFeatureRenderer *renderer ) /Factory/;
virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;

virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) const /Factory/;


virtual void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context );

Expand All @@ -81,6 +83,24 @@ Sets the color ramp to use for shading the heatmap.
:param ramp: color ramp for heatmap. Ownership of ramp is transferred to the renderer.

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

const QgsColorRampLegendNodeSettings &legendSettings() const;
%Docstring
Returns the color ramp legend settings.

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

.. versionadded:: 3.38
%End

void setLegendSettings( const QgsColorRampLegendNodeSettings &settings );
%Docstring
Sets the color ramp legend ``settings``.

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

.. versionadded:: 3.38
%End

double radius() const;
Expand Down
15 changes: 15 additions & 0 deletions python/core/auto_generated/symbology/qgsrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,22 @@ the features displayed using that key.
%Docstring
Returns a list of symbology items for the legend

.. seealso:: :py:func:`createLegendNodes`

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

virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) const /Factory/;
%Docstring
Returns a list of legend nodes to be used for the legend for the renderer.

Ownership is transferred to the caller.

The default implementation creates a legend node for each symbol item returned by :py:func:`~QgsFeatureRenderer.legendSymbolItems`

.. seealso:: :py:func:`legendSymbolItems`

.. versionadded:: 3.38
%End

virtual QString legendClassificationAttribute() const;
Expand Down
13 changes: 13 additions & 0 deletions python/gui/auto_additions/qgscolorramplegendnodewidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# The following has been generated automatically from src/gui/qgscolorramplegendnodewidget.h
# monkey patching scoped based enum
QgsColorRampLegendNodeWidget.Capability.Prefix.__doc__ = "Allow editing legend prefix"
QgsColorRampLegendNodeWidget.Capability.Suffix.__doc__ = "Allow editing legend suffix"
QgsColorRampLegendNodeWidget.Capability.NumberFormat.__doc__ = "Allow editing number format"
QgsColorRampLegendNodeWidget.Capability.DefaultMinimum.__doc__ = "Allow resetting minimum label to default"
QgsColorRampLegendNodeWidget.Capability.DefaultMaximum.__doc__ = "Allow resetting maximum label to default"
QgsColorRampLegendNodeWidget.Capability.AllCapabilities.__doc__ = "All capabilities"
QgsColorRampLegendNodeWidget.Capability.__doc__ = "Capabilities to expose in the widget.\n\n.. versionadded:: 3.38\n\n" + '* ``Prefix``: ' + QgsColorRampLegendNodeWidget.Capability.Prefix.__doc__ + '\n' + '* ``Suffix``: ' + QgsColorRampLegendNodeWidget.Capability.Suffix.__doc__ + '\n' + '* ``NumberFormat``: ' + QgsColorRampLegendNodeWidget.Capability.NumberFormat.__doc__ + '\n' + '* ``DefaultMinimum``: ' + QgsColorRampLegendNodeWidget.Capability.DefaultMinimum.__doc__ + '\n' + '* ``DefaultMaximum``: ' + QgsColorRampLegendNodeWidget.Capability.DefaultMaximum.__doc__ + '\n' + '* ``AllCapabilities``: ' + QgsColorRampLegendNodeWidget.Capability.AllCapabilities.__doc__
# --
QgsColorRampLegendNodeWidget.Capability.baseClass = QgsColorRampLegendNodeWidget
QgsColorRampLegendNodeWidget.Capabilities.baseClass = QgsColorRampLegendNodeWidget
Capabilities = QgsColorRampLegendNodeWidget # dirty hack since SIP seems to introduce the flags in module
24 changes: 21 additions & 3 deletions python/gui/auto_generated/qgscolorramplegendnodewidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@




class QgsColorRampLegendNodeWidget: QgsPanelWidget
{
%Docstring(signature="appended")
Expand All @@ -30,9 +29,24 @@ When changes are made the to settings by a user the :py:func:`~widgetChanged` si
%End
public:

QgsColorRampLegendNodeWidget( QWidget *parent = 0 );
enum class Capability
{
Prefix,
Suffix,
NumberFormat,
DefaultMinimum,
DefaultMaximum,
AllCapabilities,
};

typedef QFlags<QgsColorRampLegendNodeWidget::Capability> Capabilities;


QgsColorRampLegendNodeWidget( QWidget *parent = 0, QgsColorRampLegendNodeWidget::Capabilities capabilities = QgsColorRampLegendNodeWidget::Capability::AllCapabilities );
%Docstring
Constructor for QgsColorRampLegendNodeWidget, with the specified ``parent`` widget.

Since QGIS 3.38, the ``capabilities`` argument can be used to fine-tune settings exposed in the widget.
%End

QgsColorRampLegendNodeSettings settings() const;
Expand All @@ -59,6 +73,8 @@ when using single band gray renderer).
%End

};
QFlags<QgsColorRampLegendNodeWidget::Capability> operator|(QgsColorRampLegendNodeWidget::Capability f1, QFlags<QgsColorRampLegendNodeWidget::Capability> f2);


class QgsColorRampLegendNodeDialog : QDialog
{
Expand All @@ -73,9 +89,11 @@ A dialog for configuring a :py:class:`QgsColorRampLegendNode` (:py:class:`QgsCol
%End
public:

QgsColorRampLegendNodeDialog( const QgsColorRampLegendNodeSettings &settings, QWidget *parent /TransferThis/ = 0 );
QgsColorRampLegendNodeDialog( const QgsColorRampLegendNodeSettings &settings, QWidget *parent /TransferThis/ = 0, QgsColorRampLegendNodeWidget::Capabilities capabilities = QgsColorRampLegendNodeWidget::Capability::AllCapabilities );
%Docstring
Constructor for QgsColorRampLegendNodeDialog, initially showing the specified ``settings``.

Since QGIS 3.38, the ``capabilities`` argument can be used to fine-tune settings exposed in the dialog.
%End

QgsColorRampLegendNodeSettings settings() const;
Expand Down
20 changes: 8 additions & 12 deletions src/core/qgsmaplayerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,27 +384,24 @@ QList<QgsLayerTreeModelLegendNode *> QgsDefaultVectorLayerLegend::createLayerTre
nodes.append( new QgsSimpleLegendNode( nodeLayer, r->legendClassificationAttribute() ) );
}

const auto constLegendSymbolItems = r->legendSymbolItems();
for ( const QgsLegendSymbolItem &i : constLegendSymbolItems )
const QList<QgsLayerTreeModelLegendNode *> rendererNodes = r->createLegendNodes( nodeLayer );
for ( QgsLayerTreeModelLegendNode *node : rendererNodes )
{
if ( auto *lDataDefinedSizeLegendSettings = i.dataDefinedSizeLegendSettings() )
nodes << new QgsDataDefinedSizeLegendNode( nodeLayer, *lDataDefinedSizeLegendSettings );
else
if ( QgsSymbolLegendNode *legendNode = qobject_cast< QgsSymbolLegendNode *>( node ) )
{
QgsSymbolLegendNode *legendNode = new QgsSymbolLegendNode( nodeLayer, i );
if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( i.ruleKey() ) )
const QString ruleKey = legendNode->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString();
if ( mTextOnSymbolEnabled && mTextOnSymbolContent.contains( ruleKey ) )
{
legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( i.ruleKey() ) );
legendNode->setTextOnSymbolLabel( mTextOnSymbolContent.value( ruleKey ) );
legendNode->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat );
}
nodes << legendNode;
}
nodes << node;
}

if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() )
if ( nodes.count() == 1 && nodes[0]->data( Qt::EditRole ).toString().isEmpty() && qobject_cast< QgsSymbolLegendNode * >( nodes[0] ) )
nodes[0]->setEmbeddedInParent( true );


if ( mLayer->diagramsEnabled() )
{
const auto constLegendItems = mLayer->diagramRenderer()->legendItems( nodeLayer );
Expand Down Expand Up @@ -440,7 +437,6 @@ QList<QgsLayerTreeModelLegendNode *> QgsDefaultVectorLayerLegend::createLayerTre
}
}


return nodes;
}

Expand Down
28 changes: 27 additions & 1 deletion src/core/symbology/qgsheatmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgscolorrampimpl.h"
#include "qgsrendercontext.h"
#include "qgsstyleentityvisitor.h"
#include "qgscolorramplegendnode.h"

#include <QDomDocument>
#include <QDomElement>
Expand All @@ -31,6 +32,8 @@ QgsHeatmapRenderer::QgsHeatmapRenderer()
: QgsFeatureRenderer( QStringLiteral( "heatmapRenderer" ) )
{
mGradientRamp = new QgsGradientColorRamp( QColor( 255, 255, 255 ), QColor( 0, 0, 0 ) );
mLegendSettings.setMinimumLabel( QObject::tr( "Minimum" ) );
mLegendSettings.setMaximumLabel( QObject::tr( "Maximum" ) );
}

QgsHeatmapRenderer::~QgsHeatmapRenderer()
Expand Down Expand Up @@ -284,6 +287,7 @@ QgsHeatmapRenderer *QgsHeatmapRenderer::clone() const
newRenderer->setMaximumValue( mExplicitMax );
newRenderer->setRenderQuality( mRenderQuality );
newRenderer->setWeightExpression( mWeightExpressionString );
newRenderer->setLegendSettings( mLegendSettings );
copyRendererData( newRenderer );

return newRenderer;
Expand Down Expand Up @@ -316,12 +320,16 @@ QgsFeatureRenderer *QgsHeatmapRenderer::create( QDomElement &element, const QgsR
{
r->setColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
}

QgsColorRampLegendNodeSettings legendSettings;
legendSettings.readXml( element, context );
r->setLegendSettings( legendSettings );

return r;
}

QDomElement QgsHeatmapRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
{
Q_UNUSED( context )
QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "heatmapRenderer" ) );
rendererElem.setAttribute( QStringLiteral( "radius" ), QString::number( mRadius ) );
Expand All @@ -336,6 +344,7 @@ QDomElement QgsHeatmapRenderer::save( QDomDocument &doc, const QgsReadWriteConte
const QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), mGradientRamp, doc );
rendererElem.appendChild( colorRampElem );
}
mLegendSettings.writeXml( doc, rendererElem, context );

saveRendererData( doc, rendererElem, context );

Expand Down Expand Up @@ -395,8 +404,25 @@ bool QgsHeatmapRenderer::accept( QgsStyleEntityVisitorInterface *visitor ) const
return true;
}

QList<QgsLayerTreeModelLegendNode *> QgsHeatmapRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer ) const
{
return
{
new QgsColorRampLegendNode( nodeLayer,
mGradientRamp->clone(),
mLegendSettings,
0,
1 )
};
}

void QgsHeatmapRenderer::setColorRamp( QgsColorRamp *ramp )
{
delete mGradientRamp;
mGradientRamp = ramp;
}

void QgsHeatmapRenderer::setLegendSettings( const QgsColorRampLegendNodeSettings &settings )
{
mLegendSettings = settings;
}
Loading
Loading