QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgssvgannotation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssvgannotation.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 "qgssvgannotation.h"
19 
20 #include "qgsreadwritecontext.h"
21 #include "qgsproject.h"
22 #include "qgssymbollayerutils.h"
23 
24 #include <QDomDocument>
25 #include <QDomElement>
26 
27 
29  : QgsAnnotation( parent )
30 {
31 
32 }
33 
35 {
36  std::unique_ptr< QgsSvgAnnotation > c( new QgsSvgAnnotation() );
37  copyCommonProperties( c.get() );
38  c->setFilePath( mFilePath );
39  return c.release();
40 }
41 
42 void QgsSvgAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
43 {
44  QString filePath = QgsSymbolLayerUtils::svgSymbolPathToName( mFilePath, context.pathResolver() );
45  QDomElement svgAnnotationElem = doc.createElement( QStringLiteral( "SVGAnnotationItem" ) );
46  svgAnnotationElem.setAttribute( QStringLiteral( "file" ), filePath );
47  _writeXml( svgAnnotationElem, doc, context );
48  elem.appendChild( svgAnnotationElem );
49 }
50 
51 void QgsSvgAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
52 {
53  QString filePath = QgsSymbolLayerUtils::svgSymbolNameToPath( itemElem.attribute( QStringLiteral( "file" ) ), context.pathResolver() );
55  QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
56  if ( !annotationElem.isNull() )
57  {
58  _readXml( annotationElem, context );
59  }
60 }
61 
62 void QgsSvgAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
63 {
64  QPainter *painter = context.painter();
65  if ( !painter )
66  {
67  return;
68  }
69 
70  //keep width/height ratio of svg
71  QRect viewBox = mSvgRenderer.viewBox();
72  if ( viewBox.isValid() )
73  {
74  double widthRatio = size.width() / viewBox.width();
75  double heightRatio = size.height() / viewBox.height();
76  double renderWidth = 0;
77  double renderHeight = 0;
78  if ( widthRatio <= heightRatio )
79  {
80  renderWidth = size.width();
81  renderHeight = viewBox.height() * widthRatio;
82  }
83  else
84  {
85  renderHeight = size.height();
86  renderWidth = viewBox.width() * heightRatio;
87  }
88 
89  mSvgRenderer.render( painter, QRectF( 0, 0, renderWidth,
90  renderHeight ) );
91  }
92 }
93 
94 void QgsSvgAnnotation::setFilePath( const QString &file )
95 {
96  mFilePath = file;
97  mSvgRenderer.load( mFilePath );
98  emit appearanceChanged();
99 }
QgsAnnotation::_readXml
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
Definition: qgsannotation.cpp:444
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgsreadwritecontext.h
QgsSvgAnnotation::clone
QgsSvgAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
Definition: qgssvgannotation.cpp:34
QgsSvgAnnotation::renderAnnotation
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
Definition: qgssvgannotation.cpp:62
QgsSvgAnnotation::setFilePath
void setFilePath(const QString &file)
Sets the file path for the source SVG file.
Definition: qgssvgannotation.cpp:94
qgssymbollayerutils.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsSymbolLayerUtils::svgSymbolPathToName
static QString svgSymbolPathToName(const QString &path, const QgsPathResolver &pathResolver)
Determines an SVG symbol's name from its path.
Definition: qgssymbollayerutils.cpp:4007
QgsSvgAnnotation
An annotation which renders the contents of an SVG file.
Definition: qgssvgannotation.h:32
QgsAnnotation
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:50
QgsSvgAnnotation::QgsSvgAnnotation
QgsSvgAnnotation(QObject *parent=nullptr)
Constructor for QgsSvgAnnotation.
Definition: qgssvgannotation.cpp:28
QgsAnnotation::_writeXml
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
Definition: qgsannotation.cpp:399
qgssvgannotation.h
QgsSvgAnnotation::writeXml
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
Definition: qgssvgannotation.cpp:42
QgsSvgAnnotation::filePath
QString filePath() const
Returns the file path for the source SVG file.
Definition: qgssvgannotation.h:57
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsSymbolLayerUtils::svgSymbolNameToPath
static QString svgSymbolNameToPath(const QString &name, const QgsPathResolver &pathResolver)
Determines an SVG symbol's path from its name.
Definition: qgssymbollayerutils.cpp:3941
QgsAnnotation::appearanceChanged
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
QgsSvgAnnotation::readXml
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
Definition: qgssvgannotation.cpp:51
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:179
qgsproject.h
QgsAnnotation::copyCommonProperties
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
Definition: qgsannotation.cpp:546
QgsReadWriteContext::pathResolver
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
Definition: qgsreadwritecontext.cpp:47