QgsSvgCache class

A cache for images / pictures derived from svg files.

This class supports parameter replacement in svg files according to the svg params specification (http://www.w3.org/TR/2009/WD-SVGParamPrimer-20090616/). Supported are the parameters 'fill-color', 'pen-color', 'outline-width', 'stroke-width'. E.g. <circle fill="param(fill-color red)" stroke="param(pen-color black)" stroke-width="param(outline-width 1)"

QgsSvgCache is not usually directly created, but rather accessed through QgsApplication::svgCache().

Base classes

template<class T>
class QgsAbstractContentCache
Abstract base class for file content caches, such as SVG or raster image caches.

Constructors, destructors, conversion operators

QgsSvgCache(QObject* parent = nullptr)
Constructor for QgsSvgCache.

Public functions

void containsParams(const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasStrokeParam, QColor& defaultStrokeColor, bool& hasStrokeWidthParam, double& defaultStrokeWidth) const
Tests if an svg file contains parameters for fill, stroke color, stroke width.
void containsParams(const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor, bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity, bool& hasStrokeParam, bool& hasDefaultStrokeColor, QColor& defaultStrokeColor, bool& hasStrokeWidthParam, bool& hasDefaultStrokeWidth, double& defaultStrokeWidth, bool& hasStrokeOpacityParam, bool& hasDefaultStrokeOpacity, double& defaultStrokeOpacity) const
Tests if an svg file contains parameters for fill, stroke color, stroke width.
auto getImageData(const QString& path) const -> QByteArray
Gets image data.
auto svgAsImage(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, bool& fitsInCache, double fixedAspectRatio = 0) -> QImage
Gets SVG as QImage.
auto svgAsPicture(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0) -> QPicture
Gets SVG as QPicture&.
auto svgContent(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio = 0) -> QByteArray
Gets SVG content.
auto svgViewboxSize(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio = 0) -> QSizeF
Calculates the viewbox size of a (possibly cached) SVG file.

Signals

void remoteSvgFetched(const QString& url)
Emitted when the cache has finished retrieving an SVG file from a remote url.
auto statusChanged(const QString& statusQString) -> Q_DECL_DEPRECATED void deprecated
Emit a signal to be caught by qgisapp and display a msg on status bar.

Protected functions

auto checkReply(QNetworkReply* reply, const QString& path) const -> bool override
Runs additional checks on a network reply to ensure that the reply content is consistent with that required by the cache.

Function documentation

void QgsSvgCache::containsParams(const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasStrokeParam, QColor& defaultStrokeColor, bool& hasStrokeWidthParam, double& defaultStrokeWidth) const

Tests if an svg file contains parameters for fill, stroke color, stroke width.

If yes, possible default values are returned. If there are several default values in the svg file, only the first one is considered

void QgsSvgCache::containsParams(const QString& path, bool& hasFillParam, bool& hasDefaultFillParam, QColor& defaultFillColor, bool& hasFillOpacityParam, bool& hasDefaultFillOpacity, double& defaultFillOpacity, bool& hasStrokeParam, bool& hasDefaultStrokeColor, QColor& defaultStrokeColor, bool& hasStrokeWidthParam, bool& hasDefaultStrokeWidth, double& defaultStrokeWidth, bool& hasStrokeOpacityParam, bool& hasDefaultStrokeOpacity, double& defaultStrokeOpacity) const

Tests if an svg file contains parameters for fill, stroke color, stroke width.

Parameters
path path to SVG file
hasFillParam will be true if fill param present in SVG
hasDefaultFillParam will be true if fill param has a default value specified
defaultFillColor will be set to default fill color specified in SVG, if present
hasFillOpacityParam will be true if fill opacity param present in SVG
hasDefaultFillOpacity will be true if fill opacity param has a default value specified
defaultFillOpacity will be set to default fill opacity specified in SVG, if present
hasStrokeParam will be true if stroke param present in SVG
hasDefaultStrokeColor will be true if stroke param has a default value specified
defaultStrokeColor will be set to default stroke color specified in SVG, if present
hasStrokeWidthParam will be true if stroke width param present in SVG
hasDefaultStrokeWidth will be true if stroke width param has a default value specified
defaultStrokeWidth will be set to default stroke width specified in SVG, if present
hasStrokeOpacityParam will be true if stroke opacity param present in SVG
hasDefaultStrokeOpacity will be true if stroke opacity param has a default value specified
defaultStrokeOpacity will be set to default stroke opacity specified in SVG, if present

If yes, possible default values are returned. If there are several default values in the svg file, only the first one is considered.

QImage QgsSvgCache::svgAsImage(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, bool& fitsInCache, double fixedAspectRatio = 0)

Gets SVG as QImage.

Parameters
path Absolute path to SVG file.
size size of cached image
fill color of fill
stroke color of stroke
strokeWidth width of stroke
widthScaleFactor width scale factor
fitsInCache
fixedAspectRatio fixed aspect ratio (optional)

QPicture QgsSvgCache::svgAsPicture(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0)

Gets SVG as QPicture&.

Parameters
path Absolute path to SVG file.
size size of cached image
fill color of fill
stroke color of stroke
strokeWidth width of stroke
widthScaleFactor width scale factor
forceVectorOutput
fixedAspectRatio fixed aspect ratio (optional)

QSizeF QgsSvgCache::svgViewboxSize(const QString& path, double size, const QColor& fill, const QColor& stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio = 0)

Calculates the viewbox size of a (possibly cached) SVG file.

Parameters
path Absolute path to SVG file.
size size of cached image
fill color of fill
stroke color of stroke
strokeWidth width of stroke
widthScaleFactor width scale factor
fixedAspectRatio fixed aspect ratio (optional)
Returns viewbox size set in SVG file

void QgsSvgCache::remoteSvgFetched(const QString& url) signal

Emitted when the cache has finished retrieving an SVG file from a remote url.

Q_DECL_DEPRECATED void QgsSvgCache::statusChanged(const QString& statusQString) signal

Emit a signal to be caught by qgisapp and display a msg on status bar.