QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgssvgannotation.cpp"
20
21#include "qgsreadwritecontext.h"
22#include "qgsproject.h"
23#include "qgssymbollayerutils.h"
24
25#include <QDomDocument>
26#include <QDomElement>
27
28
30 : QgsAnnotation( parent )
31{
32
33}
34
36{
37 std::unique_ptr< QgsSvgAnnotation > c( new QgsSvgAnnotation() );
38 copyCommonProperties( c.get() );
39 c->setFilePath( mFilePath );
40 return c.release();
41}
42
43void QgsSvgAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
44{
45 const QString filePath = QgsSymbolLayerUtils::svgSymbolPathToName( mFilePath, context.pathResolver() );
46 QDomElement svgAnnotationElem = doc.createElement( QStringLiteral( "SVGAnnotationItem" ) );
47 svgAnnotationElem.setAttribute( QStringLiteral( "file" ), filePath );
48 _writeXml( svgAnnotationElem, doc, context );
49 elem.appendChild( svgAnnotationElem );
50}
51
52void QgsSvgAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
53{
54 const QString filePath = QgsSymbolLayerUtils::svgSymbolNameToPath( itemElem.attribute( QStringLiteral( "file" ) ), context.pathResolver() );
56 const QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
57 if ( !annotationElem.isNull() )
58 {
59 _readXml( annotationElem, context );
60 }
61}
62
63void QgsSvgAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
64{
65 QPainter *painter = context.painter();
66 if ( !painter || ( context.feedback() && context.feedback()->isCanceled() ) )
67 {
68 return;
69 }
70
71 //keep width/height ratio of svg
72 const QRect viewBox = mSvgRenderer.viewBox();
73 if ( viewBox.isValid() )
74 {
75 const double widthRatio = size.width() / viewBox.width();
76 const double heightRatio = size.height() / viewBox.height();
77 double renderWidth = 0;
78 double renderHeight = 0;
79 if ( widthRatio <= heightRatio )
80 {
81 renderWidth = size.width();
82 renderHeight = viewBox.height() * widthRatio;
83 }
84 else
85 {
86 renderHeight = size.height();
87 renderWidth = viewBox.width() * heightRatio;
88 }
89
90 mSvgRenderer.render( painter, QRectF( 0, 0, renderWidth,
91 renderHeight ) );
92 }
93}
94
95void QgsSvgAnnotation::setFilePath( const QString &file )
96{
97 mFilePath = file;
98 mSvgRenderer.load( mFilePath );
99 emit appearanceChanged();
100}
Abstract base class for annotation items which are drawn over a map.
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
The class is used as a container of context for various read/write operations on other objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
An annotation which renders the contents of an SVG file.
QgsSvgAnnotation(QObject *parent=nullptr)
Constructor for QgsSvgAnnotation.
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
void setFilePath(const QString &file)
Sets the file path for the source SVG file.
QString filePath() const
Returns the file path for the source SVG file.
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
QgsSvgAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
static QString svgSymbolPathToName(const QString &path, const QgsPathResolver &pathResolver)
Determines an SVG symbol's name from its path.
static QString svgSymbolNameToPath(const QString &name, const QgsPathResolver &pathResolver)
Determines an SVG symbol's path from its name.
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