QGIS API Documentation  2.8.2-Wien
 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 }