QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgstextannotation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextannotation.cpp
3  ------------------------
4 begin : February 9, 2010
5 copyright : (C) 2010 by Marco Hugentobler
6 email : 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 #include "qgsrendercontext.h"
20 #include <QDomDocument>
21 #include <QPainter>
22 
24  : QgsAnnotation( parent )
25  , mDocument( new QTextDocument( QString() ) )
26 {
27  mDocument->setUseDesignMetrics( true );
28 }
29 
31 {
32  std::unique_ptr< QgsTextAnnotation > c( new QgsTextAnnotation() );
33  copyCommonProperties( c.get() );
34  c->setDocument( mDocument.get() );
35  return c.release();
36 }
37 
38 const QTextDocument *QgsTextAnnotation::document() const
39 {
40  return mDocument.get();
41 }
42 
43 void QgsTextAnnotation::setDocument( const QTextDocument *doc )
44 {
45  if ( doc )
46  mDocument.reset( doc->clone() );
47  else
48  mDocument.reset();
49  emit appearanceChanged();
50 }
51 
52 void QgsTextAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
53 {
54  QPainter *painter = context.painter();
55  if ( !mDocument )
56  {
57  return;
58  }
59 
60  // scale painter back to 96 dpi, so layout prints match screen rendering
61  const QgsScopedQPainterState painterState( context.painter() );
62  const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
63  context.painter()->scale( scaleFactor, scaleFactor );
64  size /= scaleFactor;
65 
66  mDocument->setTextWidth( size.width() );
67 
68  QRectF clipRect = QRectF( 0, 0, size.width(), size.height() );
69  if ( painter->hasClipping() )
70  {
71  //QTextDocument::drawContents will draw text outside of the painter's clip region
72  //when it is passed a clip rectangle. So, we need to intersect it with the
73  //painter's clip region to prevent text drawn outside clipped region (e.g., outside layout maps, see #10400)
74  clipRect = clipRect.intersected( painter->clipRegion().boundingRect() );
75  }
76  //draw text document
77  mDocument->drawContents( painter, clipRect );
78 }
79 
80 void QgsTextAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
81 {
82  QDomElement annotationElem = doc.createElement( QStringLiteral( "TextAnnotationItem" ) );
83  if ( mDocument )
84  {
85  annotationElem.setAttribute( QStringLiteral( "document" ), mDocument->toHtml() );
86  }
87  _writeXml( annotationElem, doc, context );
88  elem.appendChild( annotationElem );
89 }
90 
91 void QgsTextAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
92 {
93  mDocument.reset( new QTextDocument );
94  mDocument->setHtml( itemElem.attribute( QStringLiteral( "document" ), QString() ) );
95  const QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
96  if ( !annotationElem.isNull() )
97  {
98  _readXml( annotationElem, context );
99  }
100 }
QgsAnnotation::_readXml
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
Definition: qgsannotation.cpp:309
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsTextAnnotation::QgsTextAnnotation
QgsTextAnnotation(QObject *parent=nullptr)
Constructor for QgsTextAnnotation.
Definition: qgstextannotation.cpp:23
QgsTextAnnotation::renderAnnotation
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
Definition: qgstextannotation.cpp:52
QgsTextAnnotation::setDocument
void setDocument(const QTextDocument *doc)
Sets the text document which will be rendered within the annotation.
Definition: qgstextannotation.cpp:43
QgsTextAnnotation::clone
QgsTextAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
Definition: qgstextannotation.cpp:30
QgsTextAnnotation::readXml
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
Definition: qgstextannotation.cpp:91
QgsAnnotation
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:53
qgsrendercontext.h
QgsAnnotation::_writeXml
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
Definition: qgsannotation.cpp:264
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1336
QgsTextAnnotation::writeXml
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
Definition: qgstextannotation.cpp:80
QgsTextAnnotation::document
const QTextDocument * document() const
Returns the text document which will be rendered within the annotation.
Definition: qgstextannotation.cpp:38
c
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
Definition: porting_processing.dox:1
qgstextannotation.h
QgsTextAnnotation
An annotation item that displays formatted text from a QTextDocument document.
Definition: qgstextannotation.h:31
QgsAnnotation::appearanceChanged
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsAnnotation::copyCommonProperties
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
Definition: qgsannotation.cpp:410