Add methods to serialize QgsLegendPatchShape

This commit is contained in:
Nyall Dawson 2020-04-09 10:59:18 +10:00
parent 07b64a3f2d
commit b920e358c7
4 changed files with 59 additions and 1 deletions

View File

@ -121,6 +121,20 @@ geometry parts and rings).
static QList< QList< QPolygonF > > defaultPatch( QgsSymbol::SymbolType type, QSizeF size );
%Docstring
Returns the default patch geometry for the given symbol ``type`` and ``size`` as a set of QPolygonF objects (parts and rings).
%End
void readXml( const QDomElement &element, const QgsReadWriteContext &context );
%Docstring
Read settings from a DOM ``element``.
.. seealso:: :py:func:`writeXml`
%End
void writeXml( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) const;
%Docstring
Write settings into a DOM ``element``.
.. seealso:: :py:func:`readXml`
%End
};

View File

@ -195,6 +195,20 @@ QList<QList<QPolygonF> > QgsLegendPatchShape::defaultPatch( QgsSymbol::SymbolTyp
return QList< QList<QPolygonF> >();
}
void QgsLegendPatchShape::readXml( const QDomElement &element, const QgsReadWriteContext & )
{
mGeometry = QgsGeometry::fromWkt( element.attribute( QStringLiteral( "wkt" ) ) );
mPreserveAspectRatio = element.attribute( QStringLiteral( "preserveAspect" ) ).toInt();
mSymbolType = static_cast< QgsSymbol::SymbolType >( element.attribute( QStringLiteral( "type" ) ).toInt() );
}
void QgsLegendPatchShape::writeXml( QDomElement &element, QDomDocument &, const QgsReadWriteContext & ) const
{
element.setAttribute( QStringLiteral( "wkt" ), mGeometry.isNull() ? QString() : mGeometry.asWkt( ) );
element.setAttribute( QStringLiteral( "preserveAspect" ), mPreserveAspectRatio ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
element.setAttribute( QStringLiteral( "type" ), QString::number( mSymbolType ) );
}
QgsSymbol::SymbolType QgsLegendPatchShape::symbolType() const
{
return mSymbolType;

View File

@ -131,6 +131,18 @@ class CORE_EXPORT QgsLegendPatchShape
*/
static QList< QList< QPolygonF > > defaultPatch( QgsSymbol::SymbolType type, QSizeF size );
/**
* Read settings from a DOM \a element.
* \see writeXml()
*/
void readXml( const QDomElement &element, const QgsReadWriteContext &context );
/**
* Write settings into a DOM \a element.
* \see readXml()
*/
void writeXml( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) const;
private:
QgsSymbol::SymbolType mSymbolType = QgsSymbol::Fill;
QgsGeometry mGeometry;

View File

@ -26,8 +26,11 @@ from qgis.core import (QgsLegendPatchShape,
QgsFillSymbol,
QgsLineSymbol,
QgsMarkerSymbol,
QgsRenderChecker
QgsRenderChecker,
QgsReadWriteContext
)
from qgis.PyQt.QtXml import QDomDocument, QDomElement
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
@ -208,6 +211,21 @@ class TestQgsLegendPatchShape(unittest.TestCase):
rendered_image = self.renderPatch(shape)
self.assertTrue(self.imageCheck('MultiPolygon', 'multipolygon', rendered_image))
def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')
shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('MultiLineString((5 5, 3 4, 1 2), ( 6 6, 6 0))'), False)
shape.writeXml(elem, doc, QgsReadWriteContext())
s2 = QgsLegendPatchShape()
s2.readXml(elem, QgsReadWriteContext())
self.assertFalse(s2.isNull())
self.assertEqual(s2.geometry().asWkt(), 'MultiLineString ((5 5, 3 4, 1 2),(6 6, 6 0))')
self.assertFalse(s2.preserveAspectRatio())
self.assertEqual(s2.symbolType(), QgsSymbol.Line)
def renderPatch(self, patch):
image = QImage(200, 200, QImage.Format_RGB32)