KSvg::Svg Class
class KSvg::SvgA theme aware image-centric SVG class. More...
Header: | #include <Svg> |
CMake: | find_package(KF6 REQUIRED COMPONENTS Svg) target_link_libraries(mytarget PRIVATE KF6::Svg) |
Inherits: | QObject |
Public Types
enum | ColorSet { View, Window, Button, Selection, Tooltip, …, Header } |
enum | Status { Normal, Selected, Inactive } |
enum | StyleSheetColor { Text, Background, Highlight, HighlightedText, PositiveText, …, HeaderNegativeText } |
Public Functions
KSvg::Svg::ColorSet | colorSet() const |
bool | containsMultipleImages() const |
qreal | devicePixelRatio() const |
QRectF | elementRect(const QString &elementId) const |
QSizeF | elementSize(const QString &elementId) const |
bool | fromCurrentImageSet() const |
bool | hasElement(const QString &elementId) const |
QImage | image(const QSize &size, const QString &elementID = QString()) |
QString | imagePath() const |
KSvg::ImageSet * | imageSet() const |
(since KSvg 4.3) bool | isUsingRenderingCache() const |
bool | isValid() const |
void | paint(QPainter *painter, const QPointF &point, const QString &elementID = QString()) |
void | paint(QPainter *painter, const QRectF &rect, const QString &elementID = QString()) |
void | paint(QPainter *painter, int x, int y, const QString &elementID = QString()) |
void | paint(QPainter *painter, int x, int y, int width, int height, const QString &elementID = QString()) |
QPixmap | pixmap(const QString &elementID = QString()) |
void | resize() |
void | resize(const QSizeF &size) |
void | resize(qreal width, qreal height) |
void | setColorSet(KSvg::Svg::ColorSet colorSet) |
void | setContainsMultipleImages(bool multiple) |
void | setDevicePixelRatio(qreal factor) |
virtual void | setImagePath(const QString &svgFilePath) |
(since KSvg 4.3) void | setImageSet(KSvg::ImageSet *theme) |
(since KSvg 5.23) void | setStatus(KSvg::Svg::Status status) |
(since KSvg 4.3) void | setUsingRenderingCache(bool useCache) |
QSizeF | size() const |
(since KSvg 5.23) KSvg::Svg::Status | status() const |
Signals
void | colorHintChanged() |
void | colorSetChanged(KSvg::Svg::ColorSet colorSet) |
void | fromCurrentImageSetChanged(bool fromCurrentImageSet) |
void | imagePathChanged() |
void | imageSetChanged(KSvg::ImageSet *imageSet) |
void | repaintNeeded() |
void | sizeChanged() |
void | statusChanged(KSvg::Svg::Status status) |
Related Non-Members
Svg(QObject *parent = nullptr) |
Detailed Description
KSvg::Svg provides a class for rendering SVG images to a QPainter in a convenient manner. Unless an absolute path to a file is provided, it loads the SVG document using KSvg::ImageSet. It also provides a number of internal optimizations to help lower the cost of painting SVGs, such as caching.
See also KSvg::FrameSvg.
Member Type Documentation
enum Svg::ColorSet
enum Svg::Status
enum Svg::StyleSheetColor
Member Function Documentation
[signal]
void Svg::colorHintChanged()
This signal is emitted whenever the color hint has changed.
KSvg::Svg::ColorSet Svg::colorSet() const
Returns the color set for this Svg
Note: Getter function for property colorSet.
See also setColorSet().
[signal]
void Svg::colorSetChanged(KSvg::Svg::ColorSet colorSet)
This signal is emitted when the color set has changed.
Note: Notifier signal for property colorSet.
bool Svg::containsMultipleImages() const
This method returns whether the SVG contains multiple images.
If this is true
, the SVG will be treated as a collection of related images, rather than a consistent drawing.
Returns true
if the SVG will be treated as containing multiple images, false
if it will be treated as a coherent image.
Note: Getter function for property multipleImages.
See also setContainsMultipleImages().
qreal Svg::devicePixelRatio() const
This method returns the device pixel ratio for this Svg.
See also setDevicePixelRatio().
[invokable]
QRectF Svg::elementRect(const QString &elementId) const
This method returns the bounding rect of a given element.
This is the bounding rect of the element with ID elementId after the SVG has been scaled (see resize()). Note that this is unaffected by the containsMultipleImages property.
elementId the id of the element to check Returns the current rect of a given element, given the current size of the SVG
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
QSizeF Svg::elementSize(const QString &elementId) const
This method returns the size of a given element.
This is the size of the element with ID elementId after the SVG has been scaled (see resize()). Note that this is unaffected by the containsMultipleImages property.
elementId the id of the element to check Returns the size of a given element, given the current size of the SVG
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
bool Svg::fromCurrentImageSet() const
This method returns whether the current theme has this SVG, without having to fall back to the default theme.
Returns true if the svg is loaded from the current theme
Note: Getter function for property fromCurrentImageSet.
See also ImageSet::currentImageSetHasImage.
[signal]
void Svg::fromCurrentImageSetChanged(bool fromCurrentImageSet)
This signal is emitted when the value of fromCurrentImageSet() has changed.
Note: Notifier signal for property fromCurrentImageSet.
[invokable]
bool Svg::hasElement(const QString &elementId) const
This method checks whether an element exists in the loaded SVG.
elementId the id of the element to check for Returns true
if the element is defined in the SVG, otherwise false
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
QImage Svg::image(const QSize &size, const QString &elementID = QString())
This method returns an image of the SVG represented by this object.
The size of the image will be the size of this Svg object (size()) if containsMultipleImages is true
; otherwise, it will be the size of the requested element after the whole SVG has been scaled to size().
elementId the ID string of the element to render, or an empty string for the whole SVG (the default) Returns a QPixmap of the rendered SVG
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
QString Svg::imagePath() const
This method returns the SVG file to render.
If this SVG is themed, this will be a relative path, and will not include a file extension.
Returns either an absolute path to an SVG file, or an image name
Note: Getter function for property imagePath.
See also setImagePath() and ImageSet::imagePath().
[signal]
void Svg::imagePathChanged()
This signal is emitted whenever the image path has changed.
Note: Notifier signal for property imagePath.
KSvg::ImageSet *Svg::imageSet() const
This method returns the KSvg::ImageSet used by this Svg object.
This determines how relative image paths are interpreted.
Returns the theme used by this Svg
See also setImageSet().
[signal]
void Svg::imageSetChanged(KSvg::ImageSet *imageSet)
This signal is emitted when the image set has changed.
[since KSvg 4.3]
bool Svg::isUsingRenderingCache() const
Whether the rendering cache is being used.
This method returns whether the Svg object is using caching for rendering results.
Note: Getter function for property usingRenderingCache.
This function was introduced in KSvg 4.3.
[invokable]
bool Svg::isValid() const
This method checks whether this object is backed by a valid SVG file.
This method can be expensive as it causes disk access.
Returns true
if the SVG file exists and the document is valid, otherwise false
.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
void Svg::paint(QPainter *painter, const QPointF &point, const QString &elementID = QString())
This method paints all or part of the SVG represented by this object.
The size of the painted area will be the size of this Svg object (size()) if containsMultipleImages is true
; otherwise, it will be the size of the requested element after the whole SVG has been scaled to size().
painter the QPainter to use point the position to start drawing; the entire svg will be drawn starting at this point. elementId the ID string of the element to render, or an empty string for the whole SVG (the default)
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
void Svg::paint(QPainter *painter, const QRectF &rect, const QString &elementID = QString())
This method paints all or part of the SVG represented by this object.
painter the QPainter to use rect the rect to draw into; if smaller than the current size the drawing is starting at this point. elementId the ID string of the element to render, or an empty string for the whole SVG (the default)
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
void Svg::paint(QPainter *painter, int x, int y, const QString &elementID = QString())
This method paints all or part of the SVG represented by this object.
The size of the painted area will be the size of this Svg object (size()) if containsMultipleImages is true
; otherwise, it will be the size of the requested element after the whole SVG has been scaled to size().
painter the QPainter to use x the horizontal coordinate to start painting from y the vertical coordinate to start painting from elementId the ID string of the element to render, or an empty string for the whole SVG (the default)
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
void Svg::paint(QPainter *painter, int x, int y, int width, int height, const QString &elementID = QString())
This method paints all or part of the SVG represented by this object.
painter the QPainter to use x the horizontal coordinate to start painting from y the vertical coordinate to start painting from width the width of the element to draw height the height of the element do draw elementId the ID string of the element to render, or an empty string for the whole SVG (the default)
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable]
QPixmap Svg::pixmap(const QString &elementID = QString())
This method returns a pixmap of the SVG represented by this object.
The size of the pixmap will be the size of this Svg object (size()) if containsMultipleImages is true
; otherwise, it will be the size of the requested element after the whole SVG has been scaled to size().
elementId the ID string of the element to render, or an empty string for the whole SVG (the default) Returns a QPixmap of the rendered SVG
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[signal]
void Svg::repaintNeeded()
This signal is emitted whenever the SVG data has changed in such a way that a repaint is required.
Any usage of an SVG object that does the painting itself must connect to this signal and respond by updating the painting. Note that connecting to ImageSet::imageSetChanged is incorrect in such a use case as the SVG itself may not be updated yet nor may theme change be the only case when a repaint is needed. Also note that classes or QML code which take Svg objects as parameters for their own painting all respond to this signal so that in those cases manually responding to the signal is unnecessary; ONLY when direct, manual painting with an Svg object is done in application code is this signal used.
[invokable]
void Svg::resize()
This method resizes the rendered image to the natural size of the SVG.
Rendering will actually take place on the next call to paint.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Setter function for property size.
[invokable]
void Svg::resize(const QSizeF &size)
This method resizes the rendered image.
Rendering will actually take place on the next call to paint.
If containsMultipleImages is true
, each element of the SVG will be rendered at this size by default; otherwise, the entire image will be scaled to this size and each element will be scaled appropriately.
size the new size of the image
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Setter function for property size.
[invokable]
void Svg::resize(qreal width, qreal height)
This method resizes the rendered image.
Rendering will actually take place on the next call to paint.
If containsMultipleImages is true
, each element of the SVG will be rendered at this size by default; otherwise, the entire image will be scaled to this size and each element will be scaled appropriately.
width the new width height the new height
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Setter function for property size.
void Svg::setColorSet(KSvg::Svg::ColorSet colorSet)
This method sets a color set for the SVG. Set a color set for the Svg. if the Svg uses stylesheets and has elements that are either TextColor
or BackgroundColor
class, make them use ButtonTextColor
/ ButtonBackgroundColor
or ViewTextColor
/ ViewBackgroundColor
Note: Setter function for property colorSet.
See also colorSet().
void Svg::setContainsMultipleImages(bool multiple)
This method sets whether the SVG contains a single image or multiple ones.
If this is set to true
, the SVG will be treated as a collection of related images, rather than a consistent drawing.
In particular, when individual elements are rendered, this affects whether the elements are resized to size() by default. See paint() and pixmap().
multiple true if the svg contains multiple images
Note: Setter function for property multipleImages.
See also containsMultipleImages(), ::paint(), and ::pixmap().
void Svg::setDevicePixelRatio(qreal factor)
This method sets the device pixel ratio for the Svg.
This is the ratio between image pixels and device-independent pixels. The SVG will produce pixmaps scaled by devicePixelRatio, but all the sizes and element rects will not be altered. The default value is 1.0 and the scale will be done rounded to the floor integer.
Setting it to something higher will make all the elements of this SVG appear bigger.
See also devicePixelRatio().
[virtual]
void Svg::setImagePath(const QString &svgFilePath)
This method sets the SVG file to render.
Relative paths are looked for in the current Svg theme, and should not include the file extension (.svg and .svgz files will be searched for). include the file extension; files with the .svg and .svgz extensions will be found automatically.
If the parent object of this Svg is a KSvg::Applet, relative paths will be searched for in the applet's package first.
svgFilePath either an absolute path to an SVG file, or an image name.
Note: Setter function for property imagePath.
See also ImageSet::imagePath().
[since KSvg 4.3]
void Svg::setImageSet(KSvg::ImageSet *theme)
This method sets the KSvg::ImageSet to use with this Svg object.
By default, Svg objects use KSvg::ImageSet::default().
This determines how relative image paths are interpreted.
theme the theme object to use
This function was introduced in KSvg 4.3.
See also imageSet().
[since KSvg 5.23]
void Svg::setStatus(KSvg::Svg::Status status)
This method sets the image in a selected status.
SVGs can be colored with system color themes. If status is selected, TextColor
will become HighlightedText
color, and BackgroundColor
will become HighlightColor
. This can be used to make SVG-based graphics such as symbolic icons look correct together. Supported statuses are Normal
and Selected
.
Note: Setter function for property status.
This function was introduced in KSvg 5.23.
See also status().
[since KSvg 4.3]
void Svg::setUsingRenderingCache(bool useCache)
This method sets whether or not to cache the results of rendering to pixmaps.
If the SVG is resized and re-rendered often (and does not keep using the same small set of pixmap dimensions), then it may be less efficient to do disk caching. A good example might be a progress meter that uses an Svg object to paint itself: the meter will be changing often enough, with enough unpredictability and without re-use of the previous pixmaps to not get a gain from caching.
Most Svg objects should use the caching feature, however. Therefore, the default is to use the render cache.
useCache true to cache rendered pixmaps
Note: Setter function for property usingRenderingCache.
This function was introduced in KSvg 4.3.
See also isUsingRenderingCache().
QSizeF Svg::size() const
This method returns the size of the SVG.
If the SVG has been resized with resize(), that size will be returned; otherwise, the natural size of the SVG will be returned.
If containsMultipleImages is true
, each element of the SVG will be rendered at this size by default.
Returns the current size of the SVG
Note: Getter function for property size.
[signal]
void Svg::sizeChanged()
This signal is emitted whenever the size has changed.
Note: Notifier signal for property size.
See also ::resize().
[since KSvg 5.23]
KSvg::Svg::Status Svg::status() const
This method returns the Svg object's status.
Note: Getter function for property status.
This function was introduced in KSvg 5.23.
See also setStatus().
[signal]
void Svg::statusChanged(KSvg::Svg::Status status)
This signal is emitted when the status has changed.
Note: Notifier signal for property status.
Related Non-Members
[explicit]
Svg(QObject *parent = nullptr)
This method constructs an SVG object that implicitly shares and caches rendering.
Unlike QSvgRenderer, which this class uses internally, KSvg::Svg represents an image generated from an SVG. As such, it has a related size and transform matrix (the latter being provided by the painter used to paint the image).
The size is initialized to be the SVG's native size.
parent options QObject to parent this to