QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgstextannotationitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextannotationitem.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 "qgstextannotationitem.h"
19 #include <QDomDocument>
20 #include <QPainter>
21 
23 {
24  mDocument->setUseDesignMetrics( true );
25 }
26 
28 {
29  delete mDocument;
30 }
31 
33 {
34  if ( !mDocument )
35  {
36  return nullptr;
37  }
38 
39  return mDocument->clone();
40 }
41 
43 {
44  delete mDocument;
45  mDocument = doc->clone();
46 }
47 
49 {
50  if ( !painter || !mDocument )
51  {
52  return;
53  }
54 
55  drawFrame( painter );
56  if ( mMapPositionFixed )
57  {
58  drawMarkerSymbol( painter );
59  }
60  double frameWidth = mFrameBorderWidth;
61  mDocument->setTextWidth( mFrameSize.width() );
62 
63  painter->save();
64  painter->translate( mOffsetFromReferencePoint.x() + frameWidth / 2.0,
65  mOffsetFromReferencePoint.y() + frameWidth / 2.0 );
66 
67  QRectF clipRect = QRectF( 0, 0, mFrameSize.width() - frameWidth / 2.0, mFrameSize.height() - frameWidth / 2.0 );
68  if ( painter->hasClipping() )
69  {
70  //QTextDocument::drawContents will draw text outside of the painter's clip region
71  //when it is passed a clip rectangle. So, we need to intersect it with the
72  //painter's clip region to prevent text drawn outside clipped region (eg, outside composer maps, see #10400)
73  clipRect = clipRect.intersected( painter->clipRegion().boundingRect() );
74  }
75  //draw text document
76  mDocument->drawContents( painter, clipRect );
77  painter->restore();
78  if ( isSelected() )
79  {
80  drawSelectionBoxes( painter );
81  }
82 }
83 
85 {
86  QDomElement documentElem = doc.documentElement();
87  if ( documentElem.isNull() )
88  {
89  return;
90  }
91  QDomElement annotationElem = doc.createElement( "TextAnnotationItem" );
92  if ( mDocument )
93  {
94  annotationElem.setAttribute( "document", mDocument->toHtml() );
95  }
96  _writeXML( doc, annotationElem );
97  documentElem.appendChild( annotationElem );
98 }
99 
100 void QgsTextAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& itemElem )
101 {
102  delete mDocument;
103  mDocument = new QTextDocument;
104  mDocument->setHtml( itemElem.attribute( "document", QObject::tr( "" ) ) );
105  QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
106  if ( !annotationElem.isNull() )
107  {
108  _readXML( doc, annotationElem );
109  }
110 }
void drawContents(QPainter *p, const QRectF &rect)
bool hasClipping() const
void _readXML(const QDomDocument &doc, const QDomElement &annotationElem)
double mFrameBorderWidth
Width of the frame.
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
void drawMarkerSymbol(QPainter *p) const
Draws the map position marker symbol to a destination painter.
void save()
QRect boundingRect() const
QDomElement documentElement() const
void drawSelectionBoxes(QPainter *p) const
Draws selection handles around the item.
QRectF intersected(const QRectF &rectangle) const
void paint(QPainter *painter) override
function to be implemented by derived classes
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
void readXML(const QDomDocument &doc, const QDomElement &itemElem) override
qreal x() const
qreal y() const
QRegion clipRegion() const
void setAttribute(const QString &name, const QString &value)
bool isSelected() const
QTextDocument * document() const
Returns document (caller takes ownership)
void setUseDesignMetrics(bool b)
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
QgsTextAnnotationItem(QgsMapCanvas *canvas)
An annotation item can be either placed either on screen corrdinates or on map coordinates.
bool isNull() const
void restore()
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
void setTextWidth(qreal width)
QTextDocument * clone(QObject *parent) const
QDomElement firstChildElement(const QString &tagName) const
void translate(const QPointF &offset)
void setHtml(const QString &html)
void writeXML(QDomDocument &doc) const override
void setDocument(const QTextDocument *doc)
Sets document (does not take ownership)
QDomElement createElement(const QString &tagName)
qreal height() const
QString toHtml(const QByteArray &encoding) const
QSizeF mFrameSize
Size of the frame (without balloon)
qreal width() const
void drawFrame(QPainter *p) const
Draws the annotation frame to a destination painter.