QGIS API Documentation 3.99.0-Master (d270888f95f)
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
20#include "qgsproject.h"
21#include "qgsreadwritecontext.h"
22#include "qgssymbollayerutils.h"
23
24#include <QDomDocument>
25#include <QDomElement>
26#include <QString>
27
28#include "moc_qgssvgannotation.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : QgsAnnotation( parent )
34{
35
36}
37
39{
40 auto c = std::make_unique<QgsSvgAnnotation>();
41 copyCommonProperties( c.get() );
42 c->setFilePath( mFilePath );
43 return c.release();
44}
45
46void QgsSvgAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
47{
48 const QString filePath = QgsSymbolLayerUtils::svgSymbolPathToName( mFilePath, context.pathResolver() );
49 QDomElement svgAnnotationElem = doc.createElement( u"SVGAnnotationItem"_s );
50 svgAnnotationElem.setAttribute( u"file"_s, filePath );
51 _writeXml( svgAnnotationElem, doc, context );
52 elem.appendChild( svgAnnotationElem );
53}
54
55void QgsSvgAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
56{
57 const QString filePath = QgsSymbolLayerUtils::svgSymbolNameToPath( itemElem.attribute( u"file"_s ), context.pathResolver() );
59 const QDomElement annotationElem = itemElem.firstChildElement( u"AnnotationItem"_s );
60 if ( !annotationElem.isNull() )
61 {
62 _readXml( annotationElem, context );
63 }
64}
65
66void QgsSvgAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
67{
68 QPainter *painter = context.painter();
69 if ( !painter || ( context.feedback() && context.feedback()->isCanceled() ) )
70 {
71 return;
72 }
73
74 //keep width/height ratio of svg
75 const QRect viewBox = mSvgRenderer.viewBox();
76 if ( viewBox.isValid() )
77 {
78 const double widthRatio = size.width() / viewBox.width();
79 const double heightRatio = size.height() / viewBox.height();
80 double renderWidth = 0;
81 double renderHeight = 0;
82 if ( widthRatio <= heightRatio )
83 {
84 renderWidth = size.width();
85 renderHeight = viewBox.height() * widthRatio;
86 }
87 else
88 {
89 renderHeight = size.height();
90 renderWidth = viewBox.width() * heightRatio;
91 }
92
93 mSvgRenderer.render( painter, QRectF( 0, 0, renderWidth,
94 renderHeight ) );
95 }
96}
97
98void QgsSvgAnnotation::setFilePath( const QString &file )
99{
100 mFilePath = file;
101 mSvgRenderer.load( mFilePath );
102 emit appearanceChanged();
103}
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.
QgsAnnotation(QObject *parent=nullptr)
Constructor for QgsAnnotation.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
A container for the context for various read/write operations on 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...
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