QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgssvgannotationitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssvgannotationitem.cpp
3  ------------------------
4  begin : November, 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 "qgssvgannotationitem.h"
19 #include "qgsproject.h"
20 #include <QDomDocument>
21 #include <QDomElement>
22 
23 
25 {
26 
27 }
28 
30 {
31 
32 }
33 
34 void QgsSvgAnnotationItem::writeXML( QDomDocument& doc ) const
35 {
36  QDomElement documentElem = doc.documentElement();
37  if ( documentElem.isNull() )
38  {
39  return;
40  }
41 
42  QDomElement svgAnnotationElem = doc.createElement( "SVGAnnotationItem" );
43  svgAnnotationElem.setAttribute( "file", QgsProject::instance()->writePath( mFilePath ) );
44  _writeXML( doc, svgAnnotationElem );
45  documentElem.appendChild( svgAnnotationElem );
46 }
47 
48 void QgsSvgAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& itemElem )
49 {
50  QString filePath = QgsProject::instance()->readPath( itemElem.attribute( "file" ) );
51  setFilePath( filePath );
52  QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
53  if ( !annotationElem.isNull() )
54  {
55  _readXML( doc, annotationElem );
56  }
57 }
58 
59 void QgsSvgAnnotationItem::paint( QPainter* painter )
60 {
61  if ( !painter )
62  {
63  return;
64  }
65 
66  drawFrame( painter );
67  if ( mMapPositionFixed )
68  {
69  drawMarkerSymbol( painter );
70  }
71 
72  //keep width/height ratio of svg
73  QRect viewBox = mSvgRenderer.viewBox();
74  if ( viewBox.isValid() )
75  {
76  double widthRatio = mFrameSize.width() / viewBox.width();
77  double heightRatio = mFrameSize.height() / viewBox.height();
78  double renderWidth = 0;
79  double renderHeight = 0;
80  if ( widthRatio <= heightRatio )
81  {
82  renderWidth = mFrameSize.width();
83  renderHeight = viewBox.height() * mFrameSize.width() / viewBox.width() ;
84  }
85  else
86  {
87  renderHeight = mFrameSize.height();
88  renderWidth = viewBox.width() * mFrameSize.height() / viewBox.height() ;
89  }
90 
91  mSvgRenderer.render( painter, QRectF( mOffsetFromReferencePoint.x(), mOffsetFromReferencePoint.y(), renderWidth,
92  renderHeight ) );
93  }
94  if ( isSelected() )
95  {
96  drawSelectionBoxes( painter );
97  }
98 }
99 
101 {
102  mFilePath = file;
103  mSvgRenderer.load( mFilePath );
104 }
void _readXML(const QDomDocument &doc, const QDomElement &annotationElem)
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
void setFilePath(const QString &file)
QgsSvgAnnotationItem(QgsMapCanvas *canvas)
QString readPath(QString filename) const
turn filename read from the project file to an absolute path
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
void drawSelectionBoxes(QPainter *p)
void readXML(const QDomDocument &doc, const QDomElement &itemElem)
void paint(QPainter *painter)
function to be implemented by derived classes
QString filePath() const
void writeXML(QDomDocument &doc) const
void drawMarkerSymbol(QPainter *p)
An annotation item can be either placed either on screen corrdinates or on map coordinates.
QString file
Definition: qgssvgcache.cpp:76
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:362
void drawFrame(QPainter *p)
QSizeF mFrameSize
Size of the frame (without balloon)