QGIS API Documentation 3.39.0-Master (7b5d8bea57d)
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#include "qgsgeometry.h"
20#include "qgsrendercontext.h"
21#include "qgssymbollayerutils.h"
22#include "qgslinesymbollayer.h"
23#include "qgstextrenderer.h"
24#include "qgsunittypes.h"
25#include "qgscalloutsregistry.h"
26
28 : QgsAnnotationRectItem( bounds )
29 , mText( text )
30{
32 setFrameEnabled( true );
33}
34
36
38{
39 return QStringLiteral( "recttext" );
40}
41
42void QgsAnnotationRectangleTextItem::renderInBounds( QgsRenderContext &context, const QRectF &painterBounds, QgsFeedback * )
43{
44 const double marginLeft = context.convertToPainterUnits( mMargins.left(), mMarginUnit );
45 const double marginTop = context.convertToPainterUnits( mMargins.top(), mMarginUnit );
46 const double marginRight = context.convertToPainterUnits( mMargins.right(), mMarginUnit );
47 const double marginBottom = context.convertToPainterUnits( mMargins.bottom(), mMarginUnit );
48
49 const QRectF innerRect(
50 painterBounds.left() + marginLeft,
51 painterBounds.top() + marginTop,
52 painterBounds.width() - marginLeft - marginRight,
53 painterBounds.height() - marginTop - marginBottom );
54
55 const QString displayText = QgsExpression::replaceExpressionText( mText, &context.expressionContext(), &context.distanceArea() );
56
57 const bool prevWorkaroundFlag = context.testFlag( Qgis::RenderContextFlag::ApplyScalingWorkaroundForTextRendering );
59 QgsTextRenderer::drawText( innerRect, 0,
61 mTextFormat.allowHtmlFormatting() ? QStringList{displayText }: displayText.split( '\n' ), context, mTextFormat, true,
65}
66
67bool QgsAnnotationRectangleTextItem::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
68{
69 element.setAttribute( QStringLiteral( "text" ), mText );
70 element.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
71
72 QDomElement textFormatElem = document.createElement( QStringLiteral( "rectTextFormat" ) );
73 textFormatElem.appendChild( mTextFormat.writeXml( document, context ) );
74 element.appendChild( textFormatElem );
75
76 element.setAttribute( QStringLiteral( "margins" ), mMargins.toString() );
77 element.setAttribute( QStringLiteral( "marginUnit" ), QgsUnitTypes::encodeUnit( mMarginUnit ) );
78
79 writeCommonProperties( element, document, context );
80 return true;
81}
82
87
88bool QgsAnnotationRectangleTextItem::readXml( const QDomElement &element, const QgsReadWriteContext &context )
89{
90 mText = element.attribute( QStringLiteral( "text" ) );
91
92 const QDomElement textFormatElem = element.firstChildElement( QStringLiteral( "rectTextFormat" ) );
93 if ( !textFormatElem.isNull() )
94 {
95 const QDomNodeList textFormatNodeList = textFormatElem.elementsByTagName( QStringLiteral( "text-style" ) );
96 const QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
97 mTextFormat.readXml( textFormatElem, context );
98 }
99
100 mMargins = QgsMargins::fromString( element.attribute( QStringLiteral( "margins" ) ) );
101 mMarginUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( QStringLiteral( "marginUnit" ), QgsUnitTypes::encodeUnit( Qgis::RenderUnit::Millimeters ) ) );
102
103 mAlignment = static_cast< Qt::Alignment >( element.attribute( QStringLiteral( "alignment" ) ).toInt() );
104
105 readCommonProperties( element, context );
106 return true;
107}
108
110{
111 std::unique_ptr< QgsAnnotationRectangleTextItem > item = std::make_unique< QgsAnnotationRectangleTextItem >( mText, bounds() );
112
113 item->setFormat( mTextFormat );
114 item->setAlignment( mAlignment );
115
116 item->setMargins( mMargins );
117 item->setMarginsUnit( mMarginUnit );
118
119 item->copyCommonProperties( this );
120 return item.release();
121}
122
138
140{
141 return mTextFormat;
142}
143
145{
146 mTextFormat = format;
147}
148
150{
151 return mAlignment;
152}
153
154void QgsAnnotationRectangleTextItem::setAlignment( Qt::Alignment alignment )
155{
156 mAlignment = alignment;
157}
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
@ SupportsCallouts
Item supports callouts.
@ SupportsReferenceScale
Item supports reference scale based rendering.
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2302
@ Millimeters
Millimeters.
@ ApplyScalingWorkaroundForTextRendering
Whether a scaling workaround designed to stablise the rendering of small font sizes (or for painters ...
@ WrapLines
Automatically wrap long lines of text.
@ SpatialBounds
Item is rendered inside fixed spatial bounds, and size will depend on map scale.
@ FixedSize
Item is rendered at a fixed size, regardless of map scale. Item's location is georeferenced to a spat...
@ RelativeToMapFrame
Items size and placement is relative to the map's frame, and the item will always be rendered in the ...
Abstract base class for annotation items which render annotations in a rectangular shape.
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.
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.
An annotation item which renders paragraphs of text within a rectangle.
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.
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
double top() const
Returns the top margin.
Definition qgsmargins.h:77
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...
double right() const
Returns the right margin.
Definition qgsmargins.h:83
double bottom() const
Returns the bottom margin.
Definition qgsmargins.h:89
QString toString() const
Returns the margins encoded to a string.
double left() const
Returns the left margin.
Definition qgsmargins.h:71
The class is used as a container of context for various read/write operations on other 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.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
bool allowHtmlFormatting() const
Returns true if text should be treated as a HTML document and HTML tags should be used for formatting...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
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:6472