QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
22 QgsTextAnnotationItem::QgsTextAnnotationItem( QgsMapCanvas* canvas ): QgsAnnotationItem( canvas ), mDocument( new QTextDocument( QObject::tr( "QGIS rocks!" ) ) )
23 {
24  mDocument->setUseDesignMetrics( true );
25 }
26 
28 {
29  delete mDocument;
30 }
31 
32 QTextDocument* QgsTextAnnotationItem::document() const
33 {
34  if ( !mDocument )
35  {
36  return 0;
37  }
38 
39  return mDocument->clone();
40 }
41 
42 void QgsTextAnnotationItem::setDocument( const QTextDocument* doc )
43 {
44  delete mDocument;
45  mDocument = doc->clone();
46 }
47 
48 void QgsTextAnnotationItem::paint( QPainter * painter )
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 
84 void QgsTextAnnotationItem::writeXML( QDomDocument& doc ) const
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( "<html>QGIS rocks!</html>" ) ) );
105  QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
106  if ( !annotationElem.isNull() )
107  {
108  _readXML( doc, annotationElem );
109  }
110 }
void _readXML(const QDomDocument &doc, const QDomElement &annotationElem)
double mFrameBorderWidth
Width of the frame.
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
void writeXML(QDomDocument &doc) const
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
void drawSelectionBoxes(QPainter *p)
void paint(QPainter *painter)
function to be implemented by derived classes
void drawMarkerSymbol(QPainter *p)
QgsTextAnnotationItem(QgsMapCanvas *canvas)
An annotation item can be either placed either on screen corrdinates or on map coordinates.
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
QTextDocument * document() const
Returns document (caller takes ownership)
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
void drawFrame(QPainter *p)
void readXML(const QDomDocument &doc, const QDomElement &itemElem)
void setDocument(const QTextDocument *doc)
Sets document (does not take ownership)
QSizeF mFrameSize
Size of the frame (without balloon)
#define tr(sourceText)