QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
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 
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)
QDomNode appendChild(const QDomNode &newChild)
void render(QPainter *painter)
QString attribute(const QString &name, const QString &defValue) const
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
void setFilePath(const QString &file)
QgsSvgAnnotationItem(QgsMapCanvas *canvas)
void writeXML(QDomDocument &doc) const override
void drawMarkerSymbol(QPainter *p) const
Draws the map position marker symbol to a destination painter.
int height() const
QDomElement documentElement() const
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:109
void readXML(const QDomDocument &doc, const QDomElement &itemElem) override
qreal x() const
qreal y() const
bool load(const QString &filename)
void setAttribute(const QString &name, const QString &value)
bool isSelected() const
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
void paint(QPainter *painter) override
function to be implemented by derived classes
bool isValid() const
An annotation item can be either placed either on screen corrdinates or on map coordinates.
bool isNull() const
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
int width() const
QString readPath(QString filename, const QString &relativeBasePath=QString()) const
Turn filename read from the project file to an absolute path.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
QDomElement firstChildElement(const QString &tagName) const
QDomElement createElement(const QString &tagName)
qreal height() const
QSizeF mFrameSize
Size of the frame (without balloon)
qreal width() const
void drawFrame(QPainter *p) const
Draws the annotation frame to a destination painter.