QGIS API Documentation  2.3.0-Master
 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  //draw text document
68  mDocument->drawContents( painter, QRectF( 0, 0, mFrameSize.width() - frameWidth / 2.0, mFrameSize.height() - frameWidth / 2.0 ) );
69  painter->restore();
70  if ( isSelected() )
71  {
72  drawSelectionBoxes( painter );
73  }
74 }
75 
76 void QgsTextAnnotationItem::writeXML( QDomDocument& doc ) const
77 {
78  QDomElement documentElem = doc.documentElement();
79  if ( documentElem.isNull() )
80  {
81  return;
82  }
83  QDomElement annotationElem = doc.createElement( "TextAnnotationItem" );
84  if ( mDocument )
85  {
86  annotationElem.setAttribute( "document", mDocument->toHtml() );
87  }
88  _writeXML( doc, annotationElem );
89  documentElem.appendChild( annotationElem );
90 }
91 
92 void QgsTextAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& itemElem )
93 {
94  delete mDocument;
95  mDocument = new QTextDocument;
96  mDocument->setHtml( itemElem.attribute( "document", QObject::tr( "<html>QGIS rocks!</html>" ) ) );
97  QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
98  if ( !annotationElem.isNull() )
99  {
100  _readXML( doc, annotationElem );
101  }
102 }
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:105
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)