QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsannotationrectangletextitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationrectangletextitem.cpp
3 ----------------
4 begin : July 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgscalloutsregistry.h"
21#include "qgsgeometry.h"
22#include "qgslinesymbollayer.h"
23#include "qgsrendercontext.h"
24#include "qgssymbollayerutils.h"
25#include "qgstextrenderer.h"
26#include "qgsunittypes.h"
27
35
37
39{
40 return QStringLiteral( "recttext" );
41}
42
43void QgsAnnotationRectangleTextItem::renderInBounds( QgsRenderContext &context, const QRectF &painterBounds, QgsFeedback * )
44{
45 const double marginLeft = context.convertToPainterUnits( mMargins.left(), mMarginUnit );
46 const double marginTop = context.convertToPainterUnits( mMargins.top(), mMarginUnit );
47 const double marginRight = context.convertToPainterUnits( mMargins.right(), mMarginUnit );
48 const double marginBottom = context.convertToPainterUnits( mMargins.bottom(), mMarginUnit );
49
50 const QRectF innerRect(
51 painterBounds.left() + marginLeft,
52 painterBounds.top() + marginTop,
53 painterBounds.width() - marginLeft - marginRight,
54 painterBounds.height() - marginTop - marginBottom );
55
56 const QString displayText = QgsExpression::replaceExpressionText( mText, &context.expressionContext(), &context.distanceArea() );
57
58 const bool prevWorkaroundFlag = context.testFlag( Qgis::RenderContextFlag::ApplyScalingWorkaroundForTextRendering );
60 QgsTextRenderer::drawText( innerRect, 0,
62 mTextFormat.allowHtmlFormatting() ? QStringList{displayText }: displayText.split( '\n' ), context, mTextFormat, true,
66}
67
68bool QgsAnnotationRectangleTextItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
69{
70 element.setAttribute( QStringLiteral( "text" ), mText );
71 element.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
72
73 QDomElement textFormatElem = document.createElement( QStringLiteral( "rectTextFormat" ) );
74 textFormatElem.appendChild( mTextFormat.writeXml( document, context ) );
75 element.appendChild( textFormatElem );
76
77 element.setAttribute( QStringLiteral( "margins" ), mMargins.toString() );
78 element.setAttribute( QStringLiteral( "marginUnit" ), QgsUnitTypes::encodeUnit( mMarginUnit ) );
79
80 writeCommonProperties( element, document, context );
81 return true;
82}
83
88
89bool QgsAnnotationRectangleTextItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
90{
91 mText = element.attribute( QStringLiteral( "text" ) );
92
93 const QDomElement textFormatElem = element.firstChildElement( QStringLiteral( "rectTextFormat" ) );
94 if ( !textFormatElem.isNull() )
95 {
96 const QDomNodeList textFormatNodeList = textFormatElem.elementsByTagName( QStringLiteral( "text-style" ) );
97 const QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
98 mTextFormat.readXml( textFormatElem, context );
99 }
100
101 mMargins = QgsMargins::fromString( element.attribute( QStringLiteral( "margins" ) ) );
102 mMarginUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( QStringLiteral( "marginUnit" ), QgsUnitTypes::encodeUnit( Qgis::RenderUnit::Millimeters ) ) );
103
104 mAlignment = static_cast< Qt::Alignment >( element.attribute( QStringLiteral( "alignment" ) ).toInt() );
105
106 readCommonProperties( element, context );
107 return true;
108}
109
111{
112 auto item = std::make_unique< QgsAnnotationRectangleTextItem >( mText, bounds() );
113
114 item->setFormat( mTextFormat );
115 item->setAlignment( mAlignment );
116
117 item->setMargins( mMargins );
118 item->setMarginsUnit( mMarginUnit );
119
120 item->copyCommonProperties( this );
121 return item.release();
122}
123
139
141{
142 return mTextFormat;
143}
144
146{
147 mTextFormat = format;
148}
149
151{
152 return mAlignment;
153}
154
156{
157 mAlignment = alignment;
158}
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2465
@ SupportsCallouts
Item supports callouts.
Definition qgis.h:2467
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2466
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2470
@ Millimeters
Millimeters.
Definition qgis.h:5184
@ ApplyScalingWorkaroundForTextRendering
Whether a scaling workaround designed to stablise the rendering of small font sizes (or for painters ...
Definition qgis.h:2762
@ WrapLines
Automatically wrap long lines of text.
Definition qgis.h:3408
@ SpatialBounds
Item is rendered inside fixed spatial bounds, and size will depend on map scale.
Definition qgis.h:2481
@ FixedSize
Item is rendered at a fixed size, regardless of map scale. Item's location is georeferenced to a spat...
Definition qgis.h:2482
@ RelativeToMapFrame
Items size and placement is relative to the map's frame, and the item will always be rendered in the ...
Definition qgis.h:2483
QgsRectangle bounds() const
Returns the bounds of the item.
bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context) override
Reads common properties from the base class from the given DOM element.
QgsAnnotationRectItem(const QgsRectangle &bounds)
Constructor for QgsAnnotationRectItem, rendering the annotation within the specified bounds geometry.
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes common properties from the base class into an XML element.
void setBackgroundEnabled(bool enabled)
Sets whether the item's background should be rendered.
void setFrameEnabled(bool enabled)
Sets whether the item's frame should be rendered.
Qgis::AnnotationPlacementMode placementMode() const
Returns the placement mode for the item.
QgsTextFormat format() const
Returns the text format used to render the text.
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
void setFormat(const QgsTextFormat &format)
Sets the text format used to render the text.
QgsAnnotationRectangleTextItem * clone() const override
Returns a clone of the item.
Qt::Alignment alignment() const
Returns the text's alignment relative to the bounds() rectangle.
static QgsAnnotationRectangleTextItem * create()
Creates a new rectangle text annotation item.
QString text() const
Returns the text rendered by the item.
void renderInBounds(QgsRenderContext &context, const QRectF &painterBounds, QgsFeedback *feedback) override
Renders the item to the specified render context.
void setAlignment(Qt::Alignment alignment)
Sets the text's alignment relative to the bounds() rectangle.
QgsAnnotationRectangleTextItem(const QString &text, const QgsRectangle &bounds)
Constructor for QgsAnnotationRectangleTextItem, containing the specified text within the specified bo...
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
~QgsAnnotationRectangleTextItem() override
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
static QgsMargins fromString(const QString &string)
Returns a QgsMargins object decoded from a string, or a null QgsMargins if the string could not be in...
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
const QgsDistanceArea & distanceArea() const
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
QgsExpressionContext & expressionContext()
Gets the expression context.
bool testFlag(Qgis::RenderContextFlag flag) const
Check whether a particular flag is enabled.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
Container for all settings relating to text rendering.
static Qgis::TextVerticalAlignment convertQtVAlignment(Qt::Alignment alignment)
Converts a Qt vertical alignment flag to a Qgis::TextVerticalAlignment value.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static Qgis::TextHorizontalAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a Qgis::TextHorizontalAlignment value.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
#define BUILTIN_UNREACHABLE
Definition qgis.h:7208