Save and restore patch shapes for symbol nodes

This commit is contained in:
Nyall Dawson 2020-04-09 11:28:31 +10:00
parent 8c94c9d9bf
commit c4049c6439
3 changed files with 65 additions and 1 deletions

View File

@ -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)

View File

@ -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 )
{
@ -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

View File

@ -29,6 +29,7 @@ class QgsPluginLayer;
class QgsRasterLayer;
class QgsReadWriteContext;
class QgsVectorLayer;
class QgsLegendPatchShape;
#include "qgis_core.h"
@ -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 );
};