Skip to content

Commit

Permalink
[annotations] Support expression-based text items
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and nyalldawson committed Sep 10, 2021
1 parent 922b8ff commit b111f51
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/core/annotations/qgsannotationpointtextitem.cpp
Expand Up @@ -57,9 +57,10 @@ void QgsAnnotationPointTextItem::render( QgsRenderContext &context, QgsFeedback

context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );

const QString displayText = QgsExpression::replaceExpressionText( mText, &context.expressionContext(), &context.distanceArea() );
QgsTextRenderer::drawText( pt, mAngle * M_PI / 180.0,
QgsTextRenderer::convertQtHAlignment( mAlignment ),
mText.split( '\n' ), context, mTextFormat );
displayText.split( '\n' ), context, mTextFormat );
}

bool QgsAnnotationPointTextItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
Expand Down Expand Up @@ -121,8 +122,10 @@ QgsRectangle QgsAnnotationPointTextItem::boundingBox() const

QgsRectangle QgsAnnotationPointTextItem::boundingBox( QgsRenderContext &context ) const
{
const double widthInPixels = QgsTextRenderer::textWidth( context, mTextFormat, mText.split( '\n' ) );
const double heightInPixels = QgsTextRenderer::textHeight( context, mTextFormat, mText.split( '\n' ) );
const QString displayText = QgsExpression::replaceExpressionText( mText, &context.expressionContext(), &context.distanceArea() );

const double widthInPixels = QgsTextRenderer::textWidth( context, mTextFormat, displayText.split( '\n' ) );
const double heightInPixels = QgsTextRenderer::textHeight( context, mTextFormat, displayText.split( '\n' ) );

// text size has already been calculated using any symbology reference scale factor above -- we need
// to temporarily remove the reference scale here or we'll be undoing the scaling
Expand Down
34 changes: 34 additions & 0 deletions tests/src/python/test_qgsannotationpointtextitem.py
Expand Up @@ -217,6 +217,40 @@ def testRenderMarker(self):

self.assertTrue(self.imageCheck('pointtext_item', 'pointtext_item', image))

def testRenderMarkerExpression(self):
item = QgsAnnotationPointTextItem('[% 1 + 1.5 %]', QgsPointXY(12.3, 13.2))

format = QgsTextFormat.fromQFont(getTestFont('Bold'))
format.setColor(QColor(255, 0, 0))
format.setOpacity(150 / 255)
format.setSize(20)
item.setFormat(format)

item.setAngle(30)
item.setAlignment(Qt.AlignRight)

settings = QgsMapSettings()
settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
settings.setExtent(QgsRectangle(10, 10, 16, 16))
settings.setOutputSize(QSize(300, 300))

settings.setFlag(QgsMapSettings.Antialiasing, False)

rc = QgsRenderContext.fromMapSettings(settings)
image = QImage(200, 200, QImage.Format_ARGB32)
image.setDotsPerMeterX(96 / 25.4 * 1000)
image.setDotsPerMeterY(96 / 25.4 * 1000)
image.fill(QColor(255, 255, 255))
painter = QPainter(image)
rc.setPainter(painter)

try:
item.render(rc, None)
finally:
painter.end()

self.assertTrue(self.imageCheck('pointtext_item_expression', 'pointtext_item_expression', image))

def testRenderWithTransform(self):
item = QgsAnnotationPointTextItem('my text', QgsPointXY(12.3, 13.2))

Expand Down
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 b111f51

Please sign in to comment.