Class: QgsSvgCache

class qgis.core.QgsSvgCache

Bases: QgsAbstractContentCacheBase

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 parameters are:

  • ``param``(fill): fill color (with no opacity value)

  • ``param``(fill-opacity): fill color opacity

  • ``param``(outline): outline color (with no opacity value)

  • ``param``(outline-opacity): outline color opacity

  • ``param``(outline-width): width of outline strokes

E.g:

<circle fill=”param(fill-color red)” stroke=”param(pen-color black)” stroke-width=”param(outline-width 1)”

Note

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

QgsSvgCache(parent: QObject = None) Constructor for QgsSvgCache.

Methods

checkReply

param reply

childEvent

connectNotify

containsParams

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

containsParamsV3

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

customEvent

disconnectNotify

getImageData

Gets the SVG content corresponding to the given path.

isSignalConnected

onRemoteContentFetched

Triggered after remote content (i.e.

receivers

sender

senderSignalIndex

svgAsImage

Returns an SVG drawing as a QImage.

svgAsPicture

Returns an SVG drawing as a QPicture.

svgContent

Gets the SVG content corresponding to the given path.

svgViewboxSize

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

timerEvent

Signals

remoteSvgFetched

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

statusChanged

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

checkReply(self, reply: QNetworkReply, path: str)bool
Parameters
  • reply (QNetworkReply) –

  • path (str) –

Return type

bool

childEvent(self, QChildEvent)
connectNotify(self, QMetaMethod)
containsParams(self, path: str, defaultFillColor: Union[QColor, Qt.GlobalColor], defaultStrokeColor: Union[QColor, Qt.GlobalColor], blocking: bool = False)Tuple[bool, bool, bool, float]

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. Blocking forces to wait for loading before returning image (optional). WARNING: the blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

Parameters
  • path (str) –

  • defaultFillColor (Union[QColor) –

  • defaultStrokeColor (Union[QColor) –

  • blocking (bool = False) –

Return type

Tuple[bool, bool, bool, float]

containsParamsV3(self, path: str, defaultFillColor: Union[QColor, Qt.GlobalColor], defaultStrokeColor: Union[QColor, Qt.GlobalColor], blocking: bool = False)Tuple[bool, bool, bool, bool, float, bool, bool, bool, bool, float, bool, bool, float]

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.

Parameters
  • path (str) – 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 (Union[QColor) – 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 (Union[QColor) – 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

  • blocking (bool = False) – forces to wait for loading before returning image (optional).

Note

Available in Python bindings as containsParamsV3

Warning

The blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

New in version 2.14.

Return type

Tuple[bool, bool, bool, bool, float, bool, bool, bool, bool, float, bool, bool, float]

customEvent(self, QEvent)
disconnectNotify(self, QMetaMethod)
getImageData(self, path: str, blocking: bool = False)QByteArray

Gets the SVG content corresponding to the given path.

path may be a local file, remote (HTTP) url, or a base 64 encoded string (with a “base64:” prefix).

The class default missingContent byte array is returned if the path could not be resolved or is broken. If the path corresponds to a remote URL, then class default fetchingContent will be returned while the content is in the process of being fetched. The blocking boolean forces to wait for loading before returning result. The content is loaded in the same thread to ensure provided the remote content.

Warning

The blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

Parameters
  • path (str) –

  • blocking (bool = False) –

Return type

QByteArray

isSignalConnected(self, QMetaMethod)bool
onRemoteContentFetched(self, url: str, success: bool)

Triggered after remote content (i.e. HTTP linked content at the given url) has been fetched.

The success argument will be True if the content was successfully fetched, or False if it was not fetched successfully.

receivers(self, PYQT_SIGNAL)int
remoteSvgFetched

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

New in version 3.2: [signal]

Parameters

url (str) –

sender(self)QObject
senderSignalIndex(self)int
statusChanged

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

Deprecated since version Deprecated: since QGIS 3.6 – no longer emitted. [signal]

Parameters

statusQString (str) –

svgAsImage(self, path: str, size: float, fill: Union[QColor, Qt.GlobalColor], stroke: Union[QColor, Qt.GlobalColor], strokeWidth: float, widthScaleFactor: float, fixedAspectRatio: float = 0, blocking: bool = False, parameters: Dict[str, str] = {})Tuple[QImage, bool]

Returns an SVG drawing as a QImage.

Parameters
  • path (str) – Absolute path to SVG file.

  • size (float) – size of cached image

  • fill (Union[QColor) – color of fill

  • stroke (Union[QColor) – color of stroke

  • strokeWidth (float) – width of stroke

  • widthScaleFactor (float) – width scale factor

  • fitsInCache

  • fixedAspectRatio (float = 0) – fixed aspect ratio (optional)

  • blocking (bool = False) – forces to wait for loading before returning image (optional).

  • parameters (Dict[str) – is a map of parameters to dynamically replace content in SVG.

Warning

The blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

Return type

Tuple[QImage, bool]

svgAsPicture(self, path: str, size: float, fill: Union[QColor, Qt.GlobalColor], stroke: Union[QColor, Qt.GlobalColor], strokeWidth: float, widthScaleFactor: float, forceVectorOutput: bool = False, fixedAspectRatio: float = 0, blocking: bool = False, parameters: Dict[str, str] = {})QPicture

Returns an SVG drawing as a QPicture.

Parameters
  • path (str) – Absolute path to SVG file.

  • size (float) – size of cached image

  • fill (Union[QColor) – color of fill

  • stroke (Union[QColor) – color of stroke

  • strokeWidth (float) – width of stroke

  • widthScaleFactor (float) – width scale factor

  • forceVectorOutput (bool = False) –

  • fixedAspectRatio (float = 0) – fixed aspect ratio (optional)

  • blocking (bool = False) – forces to wait for loading before returning image (optional)

  • parameters (Dict[str) – is a map of parameters to dynamically replace content in SVG.

Note

The returned QPicture contains the SVG file centered over the picture origin. I.e. if it is rendered using QPainter.drawPicture( QPointF( 5, 10 ), picture ) it will be drawn centered over the point (5, 10). Appropriate translation to the destination painter based on the picture’s boundingRect may need to be applied if rendering the SVG using the top-left or other reference point is desired.

Warning

The blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

Return type

QPicture

svgContent(self, path: str, size: float, fill: Union[QColor, Qt.GlobalColor], stroke: Union[QColor, Qt.GlobalColor], strokeWidth: float, widthScaleFactor: float, fixedAspectRatio: float = 0, blocking: bool = False, parameters: Dict[str, str] = {})QByteArray

Gets the SVG content corresponding to the given path.

path may be a local file, remote (HTTP) url, or a base 64 encoded string (with a “base64:” prefix).

The parameters size, strokeWidth for width of stroke, widthScaleFactor for width scale factor, fill for color of fill, stroke for color of stroke and fixedAspectRatio for fixed aspect ratio (optional) are needed to get the entry from cache or creates a new entry if it does not exist already.

The blocking boolean forces to wait for loading before returning image. The content is loaded in the same thread to ensure provided the image.

Warning

The blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

Parameters
  • path (str) –

  • size (float) –

  • fill (Union[QColor) –

  • stroke (Union[QColor) –

  • strokeWidth (float) –

  • widthScaleFactor (float) –

  • fixedAspectRatio (float = 0) –

  • blocking (bool = False) –

  • parameters (Dict[str) –

Return type

QByteArray

svgViewboxSize(self, path: str, size: float, fill: Union[QColor, Qt.GlobalColor], stroke: Union[QColor, Qt.GlobalColor], strokeWidth: float, widthScaleFactor: float, fixedAspectRatio: float = 0, blocking: bool = False, parameters: Dict[str, str] = {})QSizeF

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

Parameters
  • path (str) – Absolute path to SVG file.

  • size (float) – size of cached image

  • fill (Union[QColor) – color of fill

  • stroke (Union[QColor) – color of stroke

  • strokeWidth (float) – width of stroke

  • widthScaleFactor (float) – width scale factor

  • fixedAspectRatio (float = 0) – fixed aspect ratio (optional)

  • blocking (bool = False) – forces to wait for loading before returning image (optional).

  • parameters (Dict[str) – is a map of parameters to dynamically replace content in SVG.

Return type

QSizeF

Returns

viewbox size set in SVG file

Warning

The blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

New in version 2.14.

timerEvent(self, QTimerEvent)