Skip to content
Permalink
Browse files
Make it easier for annotation item subclasses to correctly
copy/read/write common base class properties
  • Loading branch information
nyalldawson committed Sep 7, 2021
1 parent 046a2aa commit 837dd24858bef897c9f5fb213615ad55b1eda97c
@@ -63,6 +63,10 @@ Returns item flags.
virtual QgsAnnotationItem *clone() = 0 /Factory/;
%Docstring
Returns a clone of the item. Ownership is transferred to the caller.

Implementations should include a call to :py:func:`~QgsAnnotationItem.copyCommonProperties` to copy the base class properties.

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

virtual QString type() const = 0;
@@ -91,11 +95,23 @@ render operations.
virtual bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const = 0;
%Docstring
Writes the item's state into an XML ``element``.

Implementations should include a call to :py:func:`~QgsAnnotationItem.writeCommonProperties` to store the base class properties.

.. seealso:: :py:func:`readXml`

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

virtual bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) = 0;
%Docstring
Reads the item's state from the given DOM ``element``.

Implementations should include a call to :py:func:`~QgsAnnotationItem.readCommonProperties` to read the base class properties.

.. seealso:: :py:func:`writeXml`

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

int zIndex() const;
@@ -118,6 +134,33 @@ are rendered in the layer.
%Docstring
Returns the nodes for the item, used for editing the item.

.. versionadded:: 3.22
%End

protected:

void copyCommonProperties( const QgsAnnotationItem *other );
%Docstring
Copies common properties from the base class from an ``other`` item.

.. versionadded:: 3.22
%End

bool writeCommonProperties( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
%Docstring
Writes common properties from the base class into an XML ``element``.

.. seealso:: :py:func:`writeXml`

.. versionadded:: 3.22
%End

bool readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context );
%Docstring
Reads common properties from the base class from the given DOM ``element``.

.. seealso:: :py:func:`readXml`

.. versionadded:: 3.22
%End

@@ -27,3 +27,20 @@ QList<QgsAnnotationItemNode> QgsAnnotationItem::nodes() const
{
return {};
}

void QgsAnnotationItem::copyCommonProperties( const QgsAnnotationItem *other )
{
setZIndex( other->zIndex() );
}

bool QgsAnnotationItem::writeCommonProperties( QDomElement &element, QDomDocument &, const QgsReadWriteContext & ) const
{
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
return true;
}

bool QgsAnnotationItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext & )
{
setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
return true;
}
@@ -89,6 +89,10 @@ class CORE_EXPORT QgsAnnotationItem

/**
* Returns a clone of the item. Ownership is transferred to the caller.
*
* Implementations should include a call to copyCommonProperties() to copy the base class properties.
*
* \see copyCommonProperties()
*/
virtual QgsAnnotationItem *clone() = 0 SIP_FACTORY;

@@ -117,11 +121,21 @@ class CORE_EXPORT QgsAnnotationItem

/**
* Writes the item's state into an XML \a element.
*
* Implementations should include a call to writeCommonProperties() to store the base class properties.
*
* \see readXml()
* \see writeCommonProperties()
*/
virtual bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const = 0;

/**
* Reads the item's state from the given DOM \a element.
*
* Implementations should include a call to readCommonProperties() to read the base class properties.
*
* \see writeXml()
* \see readCommonProperties()
*/
virtual bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) = 0;

@@ -148,6 +162,31 @@ class CORE_EXPORT QgsAnnotationItem
*/
virtual QList< QgsAnnotationItemNode > nodes() const;

protected:

/**
* Copies common properties from the base class from an \a other item.
*
* \since QGIS 3.22
*/
void copyCommonProperties( const QgsAnnotationItem *other );

/**
* Writes common properties from the base class into an XML \a element.
*
* \see writeXml()
* \since QGIS 3.22
*/
bool writeCommonProperties( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;

/**
* Reads common properties from the base class from the given DOM \a element.
*
* \see readXml()
* \since QGIS 3.22
*/
bool readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context );

private:

int mZIndex = 0;
@@ -74,9 +74,8 @@ void QgsAnnotationLineItem::render( QgsRenderContext &context, QgsFeedback * )
bool QgsAnnotationLineItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "wkt" ), mCurve->asWkt() );
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );

element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );
writeCommonProperties( element, document, context );

return true;
}
@@ -103,12 +102,12 @@ bool QgsAnnotationLineItem::readXml( const QDomElement &element, const QgsReadWr
if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
mCurve.reset( curve->clone() );

setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );

const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
if ( !symbolElem.isNull() )
setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( symbolElem, context ) );

readCommonProperties( element, context );

return true;
}

@@ -121,7 +120,7 @@ QgsAnnotationLineItem *QgsAnnotationLineItem::clone()
{
std::unique_ptr< QgsAnnotationLineItem > item = std::make_unique< QgsAnnotationLineItem >( mCurve->clone() );
item->setSymbol( mSymbol->clone() );
item->setZIndex( zIndex() );
item->copyCommonProperties( this );
return item.release();
}

@@ -61,10 +61,10 @@ bool QgsAnnotationMarkerItem::writeXml( QDomElement &element, QDomDocument &docu
{
element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );

element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "markerSymbol" ), mSymbol.get(), document, context ) );

writeCommonProperties( element, document, context );

return true;
}

@@ -90,20 +90,19 @@ bool QgsAnnotationMarkerItem::readXml( const QDomElement &element, const QgsRead
const double y = element.attribute( QStringLiteral( "y" ) ).toDouble();
mPoint = QgsPoint( x, y );

setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );

const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
if ( !symbolElem.isNull() )
setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsMarkerSymbol >( symbolElem, context ) );

readCommonProperties( element, context );
return true;
}

QgsAnnotationMarkerItem *QgsAnnotationMarkerItem::clone()
{
std::unique_ptr< QgsAnnotationMarkerItem > item = std::make_unique< QgsAnnotationMarkerItem >( mPoint );
item->setSymbol( mSymbol->clone() );
item->setZIndex( zIndex() );
item->copyCommonProperties( this );
return item.release();
}

@@ -66,14 +66,14 @@ bool QgsAnnotationPointTextItem::writeXml( QDomElement &element, QDomDocument &d
element.setAttribute( QStringLiteral( "x" ), qgsDoubleToString( mPoint.x() ) );
element.setAttribute( QStringLiteral( "y" ), qgsDoubleToString( mPoint.y() ) );
element.setAttribute( QStringLiteral( "text" ), mText );
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
element.setAttribute( QStringLiteral( "angle" ), qgsDoubleToString( mAngle ) );
element.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );

QDomElement textFormatElem = document.createElement( QStringLiteral( "pointTextFormat" ) );
textFormatElem.appendChild( mTextFormat.writeXml( document, context ) );
element.appendChild( textFormatElem );

writeCommonProperties( element, document, context );
return true;
}

@@ -90,7 +90,6 @@ bool QgsAnnotationPointTextItem::readXml( const QDomElement &element, const QgsR
mText = element.attribute( QStringLiteral( "text" ) );
mAngle = element.attribute( QStringLiteral( "angle" ) ).toDouble();
mAlignment = static_cast< Qt::Alignment >( element.attribute( QStringLiteral( "alignment" ) ).toInt() );
setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );

const QDomElement textFormatElem = element.firstChildElement( QStringLiteral( "pointTextFormat" ) );
if ( !textFormatElem.isNull() )
@@ -100,6 +99,7 @@ bool QgsAnnotationPointTextItem::readXml( const QDomElement &element, const QgsR
mTextFormat.readXml( textFormatElem, context );
}

readCommonProperties( element, context );
return true;
}

@@ -109,7 +109,7 @@ QgsAnnotationPointTextItem *QgsAnnotationPointTextItem::clone()
item->setFormat( mTextFormat );
item->setAngle( mAngle );
item->setAlignment( mAlignment );
item->setZIndex( zIndex() );
item->copyCommonProperties( this );
return item.release();
}

@@ -88,10 +88,9 @@ void QgsAnnotationPolygonItem::render( QgsRenderContext &context, QgsFeedback *
bool QgsAnnotationPolygonItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "wkt" ), mPolygon->asWkt() );

element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
element.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "lineSymbol" ), mSymbol.get(), document, context ) );

writeCommonProperties( element, document, context );
return true;
}

@@ -134,20 +133,19 @@ bool QgsAnnotationPolygonItem::readXml( const QDomElement &element, const QgsRea
if ( const QgsCurvePolygon *polygon = qgsgeometry_cast< const QgsCurvePolygon * >( geometry.constGet() ) )
mPolygon.reset( polygon->clone() );

setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );

const QDomElement symbolElem = element.firstChildElement( QStringLiteral( "symbol" ) );
if ( !symbolElem.isNull() )
setSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( symbolElem, context ) );

readCommonProperties( element, context );
return true;
}

QgsAnnotationPolygonItem *QgsAnnotationPolygonItem::clone()
{
std::unique_ptr< QgsAnnotationPolygonItem > item = std::make_unique< QgsAnnotationPolygonItem >( mPolygon->clone() );
item->setSymbol( mSymbol->clone() );
item->setZIndex( zIndex() );
item->copyCommonProperties( this );;
return item.release();
}

0 comments on commit 837dd24

Please sign in to comment.