Skip to content

Commit

Permalink
Save and restore patch shapes for symbol nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 10, 2020
1 parent 8c94c9d commit c4049c6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
18 changes: 18 additions & 0 deletions python/core/auto_generated/qgsmaplayerlegend.sip.in
Expand Up @@ -96,6 +96,24 @@ Miscellaneous utility functions for handling of map layer legend
static QString legendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex );
static bool hasLegendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex );

static void setLegendNodePatchShape( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsLegendPatchShape &shape );
%Docstring
Sets the legend patch ``shape`` for the legend node belonging to ``nodeLayer`` at the specified ``originalIndex``.

.. seealso:: :py:func:`legendNodePatchShape`

.. versionadded:: 3.14
%End

static QgsLegendPatchShape legendNodePatchShape( QgsLayerTreeLayer *nodeLayer, int originalIndex );
%Docstring
Returns the legend patch shape for the legend node belonging to ``nodeLayer`` at the specified ``originalIndex``.

.. seealso:: :py:func:`setLegendNodePatchShape`

.. versionadded:: 3.14
%End

static void applyLayerNodeProperties( QgsLayerTreeLayer *nodeLayer, QList<QgsLayerTreeModelLegendNode *> &nodes );
%Docstring
update according to layer node's custom properties (order of items, user labels for items)
Expand Down
31 changes: 30 additions & 1 deletion src/core/qgsmaplayerlegend.cpp
Expand Up @@ -146,6 +146,27 @@ bool QgsMapLayerLegendUtils::hasLegendNodeUserLabel( QgsLayerTreeLayer *nodeLaye
return nodeLayer->customProperties().contains( "legend/label-" + QString::number( originalIndex ) );
}

void QgsMapLayerLegendUtils::setLegendNodePatchShape( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsLegendPatchShape &shape )
{
QDomDocument patchDoc;
QDomElement patchElem = patchDoc.createElement( QStringLiteral( "patch" ) );
shape.writeXml( patchElem, patchDoc, QgsReadWriteContext() );
patchDoc.appendChild( patchElem );
nodeLayer->setCustomProperty( "legend/patch-shape-" + QString::number( originalIndex ), patchDoc.toString() );
}

QgsLegendPatchShape QgsMapLayerLegendUtils::legendNodePatchShape( QgsLayerTreeLayer *nodeLayer, int originalIndex )
{
QString patchDef = nodeLayer->customProperty( "legend/patch-shape-" + QString::number( originalIndex ) ).toString();
if ( patchDef.isEmpty() )
return QgsLegendPatchShape();

QDomDocument doc( QStringLiteral( "patch" ) );
doc.setContent( patchDef );
QgsLegendPatchShape shape;
shape.readXml( doc.documentElement(), QgsReadWriteContext() );
return shape;
}

void QgsMapLayerLegendUtils::applyLayerNodeProperties( QgsLayerTreeLayer *nodeLayer, QList<QgsLayerTreeModelLegendNode *> &nodes )
{
Expand All @@ -154,9 +175,17 @@ void QgsMapLayerLegendUtils::applyLayerNodeProperties( QgsLayerTreeLayer *nodeLa
const auto constNodes = nodes;
for ( QgsLayerTreeModelLegendNode *legendNode : constNodes )
{
QString userLabel = QgsMapLayerLegendUtils::legendNodeUserLabel( nodeLayer, i++ );
QString userLabel = QgsMapLayerLegendUtils::legendNodeUserLabel( nodeLayer, i );
if ( !userLabel.isNull() )
legendNode->setUserLabel( userLabel );

if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( legendNode ) )
{
const QgsLegendPatchShape shape = QgsMapLayerLegendUtils::legendNodePatchShape( nodeLayer, i );
symbolNode->setPatchShape( shape );
}

i++;
}

// handle user order of nodes
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsmaplayerlegend.h
Expand Up @@ -29,6 +29,7 @@ class QgsPluginLayer;
class QgsRasterLayer;
class QgsReadWriteContext;
class QgsVectorLayer;
class QgsLegendPatchShape;

#include "qgis_core.h"

Expand Down Expand Up @@ -102,6 +103,22 @@ class CORE_EXPORT QgsMapLayerLegendUtils
static QString legendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex );
static bool hasLegendNodeUserLabel( QgsLayerTreeLayer *nodeLayer, int originalIndex );

/**
* Sets the legend patch \a shape for the legend node belonging to \a nodeLayer at the specified \a originalIndex.
*
* \see legendNodePatchShape()
* \since QGIS 3.14
*/
static void setLegendNodePatchShape( QgsLayerTreeLayer *nodeLayer, int originalIndex, const QgsLegendPatchShape &shape );

/**
* Returns the legend patch shape for the legend node belonging to \a nodeLayer at the specified \a originalIndex.
*
* \see setLegendNodePatchShape()
* \since QGIS 3.14
*/
static QgsLegendPatchShape legendNodePatchShape( QgsLayerTreeLayer *nodeLayer, int originalIndex );

//! update according to layer node's custom properties (order of items, user labels for items)
static void applyLayerNodeProperties( QgsLayerTreeLayer *nodeLayer, QList<QgsLayerTreeModelLegendNode *> &nodes );
};
Expand Down

0 comments on commit c4049c6

Please sign in to comment.