Skip to content
Permalink
Browse files
[annotations] Support expression-based text items
  • Loading branch information
nirvn authored and nyalldawson committed Sep 10, 2021
1 parent 922b8ff commit b111f518d7e807ca91bcd0cc4f1ce8422f72eb4d
@@ -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
@@ -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
@@ -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))

Binary file not shown.

0 comments on commit b111f51

Please sign in to comment.