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