QGIS API Documentation  2.2.0-Valmiera
 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 }