QGIS API Documentation  2.99.0-Master (7fe5405)
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 
29 void QgsSvgAnnotationItem::writeXml( QDomDocument& doc ) const
30 {
31  QDomElement documentElem = doc.documentElement();
32  if ( documentElem.isNull() )
33  {
34  return;
35  }
36 
37  QDomElement svgAnnotationElem = doc.createElement( QStringLiteral( "SVGAnnotationItem" ) );
38  svgAnnotationElem.setAttribute( QStringLiteral( "file" ), QgsProject::instance()->writePath( mFilePath ) );
39  _writeXml( doc, svgAnnotationElem );
40  documentElem.appendChild( svgAnnotationElem );
41 }
42 
43 void QgsSvgAnnotationItem::readXml( const QDomDocument& doc, const QDomElement& itemElem )
44 {
45  QString filePath = QgsProject::instance()->readPath( itemElem.attribute( QStringLiteral( "file" ) ) );
46  setFilePath( filePath );
47  QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
48  if ( !annotationElem.isNull() )
49  {
50  _readXml( doc, annotationElem );
51  }
52 }
53 
54 void QgsSvgAnnotationItem::paint( QPainter* painter )
55 {
56  if ( !painter )
57  {
58  return;
59  }
60 
61  drawFrame( painter );
62  if ( mMapPositionFixed )
63  {
64  drawMarkerSymbol( painter );
65  }
66 
67  //keep width/height ratio of svg
68  QRect viewBox = mSvgRenderer.viewBox();
69  if ( viewBox.isValid() )
70  {
71  double widthRatio = mFrameSize.width() / viewBox.width();
72  double heightRatio = mFrameSize.height() / viewBox.height();
73  double renderWidth = 0;
74  double renderHeight = 0;
75  if ( widthRatio <= heightRatio )
76  {
77  renderWidth = mFrameSize.width();
78  renderHeight = viewBox.height() * mFrameSize.width() / viewBox.width();
79  }
80  else
81  {
82  renderHeight = mFrameSize.height();
83  renderWidth = viewBox.width() * mFrameSize.height() / viewBox.height();
84  }
85 
86  mSvgRenderer.render( painter, QRectF( mOffsetFromReferencePoint.x(), mOffsetFromReferencePoint.y(), renderWidth,
87  renderHeight ) );
88  }
89  if ( isSelected() )
90  {
91  drawSelectionBoxes( painter );
92  }
93 }
94 
95 void QgsSvgAnnotationItem::setFilePath( const QString& file )
96 {
97  mFilePath = file;
98  mSvgRenderer.load( mFilePath );
99 }
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:74
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:350
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.