Skip to content

Commit

Permalink
add enabled state for annotation items (qgis#55798)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored Jan 17, 2024
1 parent c93f8e7 commit d296f0e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ Sets the item's z ``index``, which controls the order in which annotation items
are rendered in the layer.

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

bool enabled() const;
%Docstring
Returns ``True`` if the item is enabled and will be rendered in the layer.

.. seealso:: :py:func:`setEnabled`

.. versionadded:: 3.36
%End

void setEnabled( bool enabled );
%Docstring
Sets if the item will be rendered or not in the layer.

.. seealso:: :py:func:`enabled`

.. versionadded:: 3.36
%End

virtual QList< QgsAnnotationItemNode > nodes() const;
Expand Down
18 changes: 18 additions & 0 deletions python/core/auto_generated/annotations/qgsannotationitem.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ Sets the item's z ``index``, which controls the order in which annotation items
are rendered in the layer.

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

bool enabled() const;
%Docstring
Returns ``True`` if the item is enabled and will be rendered in the layer.

.. seealso:: :py:func:`setEnabled`

.. versionadded:: 3.36
%End

void setEnabled( bool enabled );
%Docstring
Sets if the item will be rendered or not in the layer.

.. seealso:: :py:func:`enabled`

.. versionadded:: 3.36
%End

virtual QList< QgsAnnotationItemNode > nodes() const;
Expand Down
3 changes: 3 additions & 0 deletions src/core/annotations/qgsannotationitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ QList<QgsAnnotationItemNode> QgsAnnotationItem::nodes() const

void QgsAnnotationItem::copyCommonProperties( const QgsAnnotationItem *other )
{
setEnabled( other->enabled() );
setZIndex( other->zIndex() );
setUseSymbologyReferenceScale( other->useSymbologyReferenceScale() );
setSymbologyReferenceScale( other->symbologyReferenceScale() );
}

bool QgsAnnotationItem::writeCommonProperties( QDomElement &element, QDomDocument &, const QgsReadWriteContext & ) const
{
element.setAttribute( QStringLiteral( "enabled" ), static_cast<int>( enabled() ) );
element.setAttribute( QStringLiteral( "zIndex" ), zIndex() );
element.setAttribute( QStringLiteral( "useReferenceScale" ), useSymbologyReferenceScale() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
element.setAttribute( QStringLiteral( "referenceScale" ), qgsDoubleToString( symbologyReferenceScale() ) );
Expand All @@ -55,6 +57,7 @@ bool QgsAnnotationItem::writeCommonProperties( QDomElement &element, QDomDocumen

bool QgsAnnotationItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext & )
{
setEnabled( element.attribute( QStringLiteral( "enabled" ) ).toInt() );
setZIndex( element.attribute( QStringLiteral( "zIndex" ) ).toInt() );
setUseSymbologyReferenceScale( element.attribute( QStringLiteral( "useReferenceScale" ), QStringLiteral( "0" ) ).toInt() );
setSymbologyReferenceScale( element.attribute( QStringLiteral( "referenceScale" ) ).toDouble() );
Expand Down
18 changes: 17 additions & 1 deletion src/core/annotations/qgsannotationitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ class CORE_EXPORT QgsAnnotationItem
*/
void setZIndex( int index ) { mZIndex = index; }

/**
* Returns TRUE if the item is enabled and will be rendered in the layer.
*
* \see setEnabled()
* \since QGIS 3.36
*/
bool enabled() const { return mEnabled; }

/**
* Sets if the item will be rendered or not in the layer.
*
* \see enabled()
* \since QGIS 3.36
*/
void setEnabled( bool enabled ) { mEnabled = enabled; }

/**
* Returns the nodes for the item, used for editing the item.
*
Expand Down Expand Up @@ -259,7 +275,7 @@ class CORE_EXPORT QgsAnnotationItem
private:

int mZIndex = 0;

bool mEnabled = true;
bool mUseReferenceScale = false;
double mReferenceScale = 0;

Expand Down
3 changes: 3 additions & 0 deletions src/core/annotations/qgsannotationlayerrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ bool QgsAnnotationLayerRenderer::render()
break;
}

if ( !item.second->enabled() )
continue;

std::optional< QgsScopedRenderContextReferenceScaleOverride > referenceScaleOverride;
if ( item.second->useSymbologyReferenceScale() )
{
Expand Down
49 changes: 49 additions & 0 deletions tests/src/python/test_qgsannotationlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,55 @@ def test_render_via_job_with_transform(self):
self.assertTrue(compareWkt(result, expected, tol=1000), "mismatch Expected:\n{}\nGot:\n{}\n".format(expected,
result))

def testRenderWithDisabledItems(self):
layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
self.assertTrue(layer.isValid())

item = QgsAnnotationPolygonItem(
QgsPolygon(QgsLineString([QgsPoint(11.5, 13), QgsPoint(12, 13), QgsPoint(12, 13.5), QgsPoint(11.5, 13)])))
item.setSymbol(
QgsFillSymbol.createSimple({'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2'}))
item.setZIndex(1)
i1_id = layer.addItem(item)

item = QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)]))
item.setSymbol(QgsLineSymbol.createSimple({'color': '#ffff00', 'line_width': '3'}))
item.setZIndex(2)
item.setEnabled(False)
i2_id = layer.addItem(item)

layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))

settings = QgsMapSettings()
settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
settings.setExtent(QgsRectangle(1250958, 1386945, 1420709, 1532518))
settings.setOutputSize(QSize(300, 300))

settings.setFlag(QgsMapSettings.Antialiasing, False)

rc = QgsRenderContext.fromMapSettings(settings)
rc.setCoordinateTransform(QgsCoordinateTransform(layer.crs(), settings.destinationCrs(), QgsProject.instance()))
rc.setExtent(
rc.coordinateTransform().transformBoundingBox(settings.extent(), QgsCoordinateTransform.ReverseTransform))
image = QImage(200, 200, QImage.Format_ARGB32)
image.setDotsPerMeterX(int(96 / 25.4 * 1000))
image.setDotsPerMeterY(int(96 / 25.4 * 1000))
image.fill(QColor(255, 255, 255))
painter = QPainter(image)
rc.setPainter(painter)

try:
renderer = layer.createMapRenderer(rc)
renderer.render()
finally:
painter.end()

self.assertTrue(self.image_check('layer_render_disabled', 'layer_render_disabled', image))
# also check details of rendered items
item_details = renderer.takeRenderedItemDetails()
self.assertEqual([i.layerId() for i in item_details], [layer.id()] * 1)
self.assertCountEqual([i.itemId() for i in item_details], [i1_id])


if __name__ == '__main__':
unittest.main()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d296f0e

Please sign in to comment.