Quantum GIS API Documentation  1.7.4
src/gui/qgstextannotationitem.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               qgstextannotationitem.cpp
00003                               ------------------------
00004   begin                : February 9, 2010
00005   copyright            : (C) 2010 by Marco Hugentobler
00006   email                : marco dot hugentobler at hugis dot net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgstextannotationitem.h"
00019 #include <QDomDocument>
00020 #include <QPainter>
00021 
00022 QgsTextAnnotationItem::QgsTextAnnotationItem( QgsMapCanvas* canvas ): QgsAnnotationItem( canvas ), mDocument( new QTextDocument( QObject::tr( "QGIS rocks!" ) ) )
00023 {
00024   mDocument->setUseDesignMetrics( true );
00025 }
00026 
00027 QgsTextAnnotationItem::~QgsTextAnnotationItem()
00028 {
00029   delete mDocument;
00030 }
00031 
00032 QTextDocument* QgsTextAnnotationItem::document() const
00033 {
00034   if ( !mDocument )
00035   {
00036     return 0;
00037   }
00038 
00039   return mDocument->clone();
00040 }
00041 
00042 void QgsTextAnnotationItem::setDocument( const QTextDocument* doc )
00043 {
00044   delete mDocument;
00045   mDocument = doc->clone();
00046 }
00047 
00048 void QgsTextAnnotationItem::paint( QPainter * painter )
00049 {
00050   if ( !painter || !mDocument )
00051   {
00052     return;
00053   }
00054 
00055   drawFrame( painter );
00056   if ( mMapPositionFixed )
00057   {
00058     drawMarkerSymbol( painter );
00059   }
00060   double frameWidth = mFrameBorderWidth;
00061   mDocument->setTextWidth( mFrameSize.width() );
00062 
00063   painter->save();
00064   painter->translate( mOffsetFromReferencePoint.x() + frameWidth / 2.0,
00065                       mOffsetFromReferencePoint.y() + frameWidth / 2.0 );
00066 
00067   //draw text document
00068   mDocument->drawContents( painter, QRectF( 0, 0, mFrameSize.width() - frameWidth / 2.0, mFrameSize.height() - frameWidth / 2.0 ) );
00069   painter->restore();
00070   if ( isSelected() )
00071   {
00072     drawSelectionBoxes( painter );
00073   }
00074 }
00075 
00076 void QgsTextAnnotationItem::writeXML( QDomDocument& doc ) const
00077 {
00078   QDomElement documentElem = doc.documentElement();
00079   if ( documentElem.isNull() )
00080   {
00081     return;
00082   }
00083   QDomElement annotationElem = doc.createElement( "TextAnnotationItem" );
00084   if ( mDocument )
00085   {
00086     annotationElem.setAttribute( "document", mDocument->toHtml() );
00087   }
00088   _writeXML( doc, annotationElem );
00089   documentElem.appendChild( annotationElem );
00090 }
00091 
00092 void QgsTextAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& itemElem )
00093 {
00094   delete mDocument;
00095   mDocument = new QTextDocument;
00096   mDocument->setHtml( itemElem.attribute( "document", QObject::tr( "<html>QGIS rocks!</html>" ) ) );
00097   QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
00098   if ( !annotationElem.isNull() )
00099   {
00100     _readXML( doc, annotationElem );
00101   }
00102 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines