11#include <QQuickWindow>
17#include "managedtexturenode.h"
19#include <Kirigami/Platform/PlatformTheme>
26 , m_textureChanged(false)
31 connect(m_svg, &Svg::repaintNeeded,
this, &SvgItem::updateNeeded);
32 connect(m_svg, &Svg::repaintNeeded,
this, &SvgItem::naturalSizeChanged);
33 connect(m_svg, &Svg::sizeChanged,
this, &SvgItem::naturalSizeChanged);
34 connect(m_svg, &Svg::repaintNeeded,
this, &SvgItem::elementRectChanged);
35 connect(m_svg, &Svg::sizeChanged,
this, &SvgItem::elementRectChanged);
42void SvgItem::componentComplete()
44 m_kirigamiTheme = qobject_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(
this,
true));
45 if (!m_kirigamiTheme) {
46 qCWarning(LOG_KSVGQML) <<
"No theme!" << qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(
this,
true) <<
this;
50 auto checkApplyTheme = [
this]() {
51 if (!m_svg->imageSet()->filePath(QStringLiteral(
"colors")).isEmpty()) {
52 m_svg->clearColorOverrides();
55 auto applyTheme = [
this]() {
59 if (!m_svg->imageSet()->filePath(QStringLiteral(
"colors")).isEmpty()) {
60 m_svg->clearColorOverrides();
63 m_svg->setColor(Svg::Text, m_kirigamiTheme->textColor());
64 m_svg->setColor(Svg::Background, m_kirigamiTheme->backgroundColor());
65 m_svg->setColor(Svg::Highlight, m_kirigamiTheme->highlightColor());
66 m_svg->setColor(Svg::HighlightedText, m_kirigamiTheme->highlightedTextColor());
67 m_svg->setColor(Svg::PositiveText, m_kirigamiTheme->positiveTextColor());
68 m_svg->setColor(Svg::NeutralText, m_kirigamiTheme->neutralTextColor());
69 m_svg->setColor(Svg::NegativeText, m_kirigamiTheme->negativeTextColor());
72 connect(m_kirigamiTheme, &Kirigami::Platform::PlatformTheme::colorsChanged,
this, applyTheme);
73 connect(m_svg->imageSet(), &ImageSet::imageSetChanged,
this, checkApplyTheme);
74 connect(m_svg, &Svg::imageSetChanged,
this, checkApplyTheme);
79void SvgItem::setImagePath(
const QString &path)
81 if (!m_svg || m_svg->imagePath() == path) {
85 updateDevicePixelRatio();
86 m_svg->setImagePath(path);
88 Q_EMIT imagePathChanged();
90 if (isComponentComplete()) {
95QString SvgItem::imagePath()
const
97 return m_svg->imagePath();
100void SvgItem::setElementId(
const QString &elementID)
102 if (elementID == m_elementID) {
106 if (implicitWidth() <= 0) {
107 setImplicitWidth(naturalSize().width());
109 if (implicitHeight() <= 0) {
110 setImplicitHeight(naturalSize().height());
113 m_elementID = elementID;
114 Q_EMIT elementIdChanged();
115 Q_EMIT naturalSizeChanged();
116 Q_EMIT elementRectChanged();
118 scheduleImageUpdate();
121QString SvgItem::elementId()
const
129 disconnect(m_svg.data(),
nullptr,
this,
nullptr);
134 connect(svg, &Svg::repaintNeeded,
this, &SvgItem::updateNeeded);
135 connect(svg, &Svg::repaintNeeded,
this, &SvgItem::naturalSizeChanged);
136 connect(svg, &Svg::repaintNeeded,
this, &SvgItem::elementRectChanged);
137 connect(svg, &Svg::sizeChanged,
this, &SvgItem::naturalSizeChanged);
138 connect(svg, &Svg::sizeChanged,
this, &SvgItem::elementRectChanged);
141 if (implicitWidth() <= 0) {
142 setImplicitWidth(naturalSize().width());
144 if (implicitHeight() <= 0) {
145 setImplicitHeight(naturalSize().height());
148 scheduleImageUpdate();
151 Q_EMIT naturalSizeChanged();
152 Q_EMIT elementRectChanged();
153 Q_EMIT imagePathChanged();
161QSizeF SvgItem::naturalSize()
const
165 }
else if (!m_elementID.isEmpty()) {
166 return m_svg->elementSize(m_elementID);
169 return m_svg->size();
172QRectF SvgItem::elementRect()
const
176 }
else if (!m_elementID.isEmpty()) {
177 return m_svg->elementRect(m_elementID);
183QSGNode *SvgItem::updatePaintNode(
QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
185 Q_UNUSED(updatePaintNodeData);
186 if (!
window() || !m_svg) {
192 if (width() == 0.0 || height() == 0.0) {
197 ManagedTextureNode *textureNode =
static_cast<ManagedTextureNode *
>(oldNode);
199 textureNode =
new ManagedTextureNode;
200 m_textureChanged =
true;
211 if (m_image.isNull()) {
217 textureNode->setTexture(texture);
218 m_textureChanged =
false;
220 textureNode->
setRect(0, 0, width(), height());
228void SvgItem::updateNeeded()
230 if (implicitWidth() <= 0) {
231 setImplicitWidth(naturalSize().width());
233 if (implicitHeight() <= 0) {
234 setImplicitHeight(naturalSize().height());
236 scheduleImageUpdate();
239void SvgItem::scheduleImageUpdate()
245void SvgItem::updatePolish()
251 m_textureChanged =
true;
252 m_svg->setContainsMultipleImages(!m_elementID.isEmpty());
253 m_image = m_svg->image(
QSize(width(), height()), m_elementID);
257void SvgItem::geometryChange(
const QRectF &newGeometry,
const QRectF &oldGeometry)
259 if (newGeometry.
size() != oldGeometry.
size() && newGeometry.
isValid()) {
260 scheduleImageUpdate();
266void SvgItem::updateDevicePixelRatio()
269 if (newDevicePixelRatio != m_svg->devicePixelRatio()) {
270 m_svg->setDevicePixelRatio(newDevicePixelRatio);
271 m_textureChanged =
true;
277 if (change == ItemSceneChange && value.window) {
278 updateDevicePixelRatio();
280 updateDevicePixelRatio();
288#include "moc_svgitem.cpp"
A theme aware image-centric SVG class.
void update(Part *part, const QByteArray &data, qint64 dataSize)
qreal devicePixelRatio() const const
virtual void componentComplete() override
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
virtual void itemChange(ItemChange change, const ItemChangeData &value)
virtual void updatePolish()
bool isValid() const const
QSizeF size() const const
void setFiltering(QSGTexture::Filtering filtering)
void setRect(const QRectF &r)
QSGTexture * texture() const const
virtual QSize textureSize() const const=0
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)