QGIS API Documentation  2.99.0-Master (8ec3eaf)
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( QStringLiteral( "SVGAnnotationItem" ) );
43  svgAnnotationElem.setAttribute( QStringLiteral( "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( QStringLiteral( "file" ) ) );
51  setFilePath( filePath );
52  QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "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 
100 void QgsSvgAnnotationItem::setFilePath( const QString& file )
101 {
102  mFilePath = file;
103  mSvgRenderer.load( mFilePath );
104 }
void readXml(const QDomDocument &doc, const QDomElement &itemElem) override
Deserialize from XML.
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
void setFilePath(const QString &file)
QgsSvgAnnotationItem(QgsMapCanvas *canvas)
void drawMarkerSymbol(QPainter *p) const
Draws the map position marker symbol to a destination painter.
void drawSelectionBoxes(QPainter *p) const
Draws selection handles around the item.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:106
void writeXml(QDomDocument &doc) const override
Serialize to XML.
void paint(QPainter *painter) override
function to be implemented by derived classes
An annotation item can be either placed either on screen corrdinates or on map coordinates.
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
Serialize to XML.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:348
QString readPath(QString filename) const
Turn filename read from the project file to an absolute path.
QSizeF mFrameSize
Size of the frame (without balloon)
void drawFrame(QPainter *p) const
Draws the annotation frame to a destination painter.
void _readXml(const QDomDocument &doc, const QDomElement &annotationElem)
Deserialize from XML.