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);
43 disconnect(m_kirigamiTheme,
nullptr,
this,
nullptr);
46void SvgItem::componentComplete()
48 m_kirigamiTheme = qobject_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(
this,
true));
49 if (!m_kirigamiTheme) {
50 qCWarning(LOG_KSVGQML) <<
"No theme!" << qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(
this,
true) <<
this;
54 auto checkApplyTheme = [
this]() {
55 if (!m_svg->imageSet()->filePath(QStringLiteral(
"colors")).isEmpty()) {
56 m_svg->clearColorOverrides();
59 auto applyTheme = [
this]() {
63 if (!m_svg->imageSet()->filePath(QStringLiteral(
"colors")).isEmpty()) {
64 m_svg->clearColorOverrides();
67 m_svg->setColor(Svg::Text, m_kirigamiTheme->textColor());
68 m_svg->setColor(Svg::Background, m_kirigamiTheme->backgroundColor());
69 m_svg->setColor(Svg::Highlight, m_kirigamiTheme->highlightColor());
70 m_svg->setColor(Svg::HighlightedText, m_kirigamiTheme->highlightedTextColor());
71 m_svg->setColor(Svg::PositiveText, m_kirigamiTheme->positiveTextColor());
72 m_svg->setColor(Svg::NeutralText, m_kirigamiTheme->neutralTextColor());
73 m_svg->setColor(Svg::NegativeText, m_kirigamiTheme->negativeTextColor());
76 connect(m_kirigamiTheme, &Kirigami::Platform::PlatformTheme::colorsChanged,
this, applyTheme);
77 connect(m_svg->imageSet(), &ImageSet::imageSetChanged,
this, checkApplyTheme);
78 connect(m_svg, &Svg::imageSetChanged,
this, checkApplyTheme);
83void SvgItem::setImagePath(
const QString &path)
85 if (!m_svg || m_svg->imagePath() == path) {
89 updateDevicePixelRatio();
90 m_svg->setImagePath(path);
92 Q_EMIT imagePathChanged();
94 if (isComponentComplete()) {
99QString SvgItem::imagePath()
const
101 return m_svg->imagePath();
104void SvgItem::setElementId(
const QString &elementID)
106 if (elementID == m_elementID) {
110 if (implicitWidth() <= 0) {
111 setImplicitWidth(naturalSize().width());
113 if (implicitHeight() <= 0) {
114 setImplicitHeight(naturalSize().height());
117 m_elementID = elementID;
118 Q_EMIT elementIdChanged();
119 Q_EMIT naturalSizeChanged();
120 Q_EMIT elementRectChanged();
122 scheduleImageUpdate();
125QString SvgItem::elementId()
const
133 disconnect(m_svg.data(),
nullptr,
this,
nullptr);
138 connect(svg, &Svg::repaintNeeded,
this, &SvgItem::updateNeeded);
139 connect(svg, &Svg::repaintNeeded,
this, &SvgItem::naturalSizeChanged);
140 connect(svg, &Svg::repaintNeeded,
this, &SvgItem::elementRectChanged);
141 connect(svg, &Svg::sizeChanged,
this, &SvgItem::naturalSizeChanged);
142 connect(svg, &Svg::sizeChanged,
this, &SvgItem::elementRectChanged);
145 if (implicitWidth() <= 0) {
146 setImplicitWidth(naturalSize().width());
148 if (implicitHeight() <= 0) {
149 setImplicitHeight(naturalSize().height());
152 scheduleImageUpdate();
155 Q_EMIT naturalSizeChanged();
156 Q_EMIT elementRectChanged();
157 Q_EMIT imagePathChanged();
165QSizeF SvgItem::naturalSize()
const
169 }
else if (!m_elementID.isEmpty()) {
170 return m_svg->elementSize(m_elementID);
173 return m_svg->size();
176QRectF SvgItem::elementRect()
const
180 }
else if (!m_elementID.isEmpty()) {
181 return m_svg->elementRect(m_elementID);
187QSGNode *SvgItem::updatePaintNode(
QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
189 Q_UNUSED(updatePaintNodeData);
190 if (!
window() || !m_svg) {
196 if (width() == 0.0 || height() == 0.0) {
201 ManagedTextureNode *textureNode =
static_cast<ManagedTextureNode *
>(oldNode);
203 textureNode =
new ManagedTextureNode;
204 m_textureChanged =
true;
215 if (m_image.isNull()) {
221 textureNode->setTexture(texture);
222 m_textureChanged =
false;
224 textureNode->
setRect(0, 0, width(), height());
232void SvgItem::updateNeeded()
234 if (implicitWidth() <= 0) {
235 setImplicitWidth(naturalSize().width());
237 if (implicitHeight() <= 0) {
238 setImplicitHeight(naturalSize().height());
240 scheduleImageUpdate();
243void SvgItem::scheduleImageUpdate()
249void SvgItem::updatePolish()
255 m_textureChanged =
true;
256 m_svg->setContainsMultipleImages(!m_elementID.isEmpty());
257 m_image = m_svg->image(
QSize(width(), height()), m_elementID);
261void SvgItem::geometryChange(
const QRectF &newGeometry,
const QRectF &oldGeometry)
263 if (newGeometry.
size() != oldGeometry.
size() && newGeometry.
isValid()) {
264 scheduleImageUpdate();
270void SvgItem::updateDevicePixelRatio()
273 if (newDevicePixelRatio != m_svg->devicePixelRatio()) {
274 m_svg->setDevicePixelRatio(newDevicePixelRatio);
275 m_textureChanged =
true;
281 if (change == ItemSceneChange && value.window) {
282 updateDevicePixelRatio();
284 updateDevicePixelRatio();
292#include "moc_svgitem.cpp"
A theme aware image-centric SVG class.
void update(Part *part, const QByteArray &data, qint64 dataSize)
KGUIADDONS_EXPORT QWindow * window(QObject *job)
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)
qreal devicePixelRatio() const const