QGIS API Documentation  2.0.1-Dufour
 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 }