QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgstextannotation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextannotation.cpp
3 ------------------------
4begin : February 9, 2010
5copyright : (C) 2010 by Marco Hugentobler
6email : marco dot hugentobler at hugis dot net
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
18#include "qgstextannotation.h"
19
20#include <memory>
21
22#include "qgsrendercontext.h"
23
24#include <QDomDocument>
25#include <QPainter>
26#include <QString>
27
28#include "moc_qgstextannotation.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : QgsAnnotation( parent )
34 , mDocument( new QTextDocument( QString() ) )
35{
36 mDocument->setUseDesignMetrics( true );
37}
38
40{
41 auto c = std::make_unique<QgsTextAnnotation>();
42 copyCommonProperties( c.get() );
43 c->setDocument( mDocument.get() );
44 return c.release();
45}
46
47const QTextDocument *QgsTextAnnotation::document() const
48{
49 return mDocument.get();
50}
51
52void QgsTextAnnotation::setDocument( const QTextDocument *doc )
53{
54 if ( doc )
55 mDocument.reset( doc->clone() );
56 else
57 mDocument.reset();
58 emit appearanceChanged();
59}
60
61void QgsTextAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
62{
63 QPainter *painter = context.painter();
64 if ( !mDocument || ! painter || ( context.feedback() && context.feedback()->isCanceled() ) )
65 {
66 return;
67 }
68
69 // scale painter back to 96 dpi, so layout prints match screen rendering
70 const QgsScopedQPainterState painterState( context.painter() );
71 const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
72 context.painter()->scale( scaleFactor, scaleFactor );
73 size /= scaleFactor;
74
75 mDocument->setTextWidth( size.width() );
76
77 QRectF clipRect = QRectF( 0, 0, size.width(), size.height() );
78 if ( painter->hasClipping() )
79 {
80 //QTextDocument::drawContents will draw text outside of the painter's clip region
81 //when it is passed a clip rectangle. So, we need to intersect it with the
82 //painter's clip region to prevent text drawn outside clipped region (e.g., outside layout maps, see #10400)
83 clipRect = clipRect.intersected( painter->clipRegion().boundingRect() );
84 }
85 //draw text document
86 mDocument->drawContents( painter, clipRect );
87}
88
89void QgsTextAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
90{
91 QDomElement annotationElem = doc.createElement( u"TextAnnotationItem"_s );
92 if ( mDocument )
93 {
94 annotationElem.setAttribute( u"document"_s, mDocument->toHtml() );
95 }
96 _writeXml( annotationElem, doc, context );
97 elem.appendChild( annotationElem );
98}
99
100void QgsTextAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
101{
102 mDocument = std::make_unique<QTextDocument>( );
103 mDocument->setHtml( itemElem.attribute( u"document"_s, QString() ) );
104 const QDomElement annotationElem = itemElem.firstChildElement( u"AnnotationItem"_s );
105 if ( !annotationElem.isNull() )
106 {
107 _readXml( annotationElem, context );
108 }
109}
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
QgsAnnotation(QObject *parent=nullptr)
Constructor for QgsAnnotation.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Scoped object for saving and restoring a QPainter object's state.
QgsTextAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
const QTextDocument * document() const
Returns the text document which will be rendered within the annotation.
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
void setDocument(const QTextDocument *doc)
Sets the text document which will be rendered within the annotation.
QgsTextAnnotation(QObject *parent=nullptr)
Constructor for QgsTextAnnotation.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c