| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org> |
| 3 | SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | #ifndef SVGITEM_P |
| 8 | #define SVGITEM_P |
| 9 | |
| 10 | #include <QImage> |
| 11 | #include <QQuickItem> |
| 12 | |
| 13 | #include <qqmlregistration.h> |
| 14 | |
| 15 | namespace Kirigami |
| 16 | { |
| 17 | namespace Platform |
| 18 | { |
| 19 | class PlatformTheme; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | namespace KSvg |
| 24 | { |
| 25 | class Svg; |
| 26 | |
| 27 | /*! |
| 28 | * \qmltype SvgItem |
| 29 | * \inqmlmodule org.kde.ksvg |
| 30 | * |
| 31 | * \brief Displays an SVG or an element from an SVG file. |
| 32 | */ |
| 33 | class SvgItem : public QQuickItem |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | QML_ELEMENT |
| 37 | |
| 38 | /*! |
| 39 | * \brief This property specifies the relative path of the Svg in the theme. |
| 40 | * |
| 41 | * Example: "widgets/background" |
| 42 | * |
| 43 | * \qmlproperty string SvgItem::imagePath |
| 44 | */ |
| 45 | Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged) |
| 46 | |
| 47 | /*! |
| 48 | * \brief This property specifies the sub-element of the SVG to be |
| 49 | * rendered. |
| 50 | * |
| 51 | * If this is empty, the whole SVG document will be rendered. |
| 52 | * |
| 53 | * \qmlproperty string SvgItem::elementId |
| 54 | */ |
| 55 | Q_PROPERTY(QString elementId READ elementId WRITE setElementId NOTIFY elementIdChanged) |
| 56 | |
| 57 | /*! |
| 58 | * \brief This property holds the SVG's natural, unscaled size. |
| 59 | * |
| 60 | * This is useful if a pixel-perfect rendering of outlines is needed. |
| 61 | * |
| 62 | * \qmlproperty size SvgItem::naturalSize |
| 63 | */ |
| 64 | Q_PROPERTY(QSizeF naturalSize READ naturalSize NOTIFY naturalSizeChanged) |
| 65 | |
| 66 | /*! |
| 67 | * \brief This property holds the rectangle of the selected elementId |
| 68 | * relative to the unscaled size of the SVG document. |
| 69 | * |
| 70 | * Note that this property will holds the entire SVG if element id is not |
| 71 | * selected. |
| 72 | * |
| 73 | * \qmlproperty rect SvgItem::elementRect |
| 74 | */ |
| 75 | Q_PROPERTY(QRectF elementRect READ elementRect NOTIFY elementRectChanged) |
| 76 | |
| 77 | /*! |
| 78 | * \brief This property holds the internal SVG instance. |
| 79 | * |
| 80 | * Usually, specifying just the imagePath is enough. Use this if you have |
| 81 | * many items taking the same SVG as source, and you want to share the |
| 82 | * internal SVG object. |
| 83 | * |
| 84 | * \qmlproperty KSvg::Svg SvgItem::svg |
| 85 | */ |
| 86 | Q_PROPERTY(KSvg::Svg *svg READ svg WRITE setSvg NOTIFY svgChanged) |
| 87 | |
| 88 | public: |
| 89 | explicit SvgItem(QQuickItem *parent = nullptr); |
| 90 | ~SvgItem() override; |
| 91 | |
| 92 | void setImagePath(const QString &path); |
| 93 | QString imagePath() const; |
| 94 | |
| 95 | void setElementId(const QString &elementID); |
| 96 | QString elementId() const; |
| 97 | |
| 98 | void setSvg(KSvg::Svg *svg); |
| 99 | KSvg::Svg *svg() const; |
| 100 | |
| 101 | QSizeF naturalSize() const; |
| 102 | |
| 103 | QRectF elementRect() const; |
| 104 | |
| 105 | QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) override; |
| 106 | |
| 107 | void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override; |
| 108 | |
| 109 | protected: |
| 110 | void componentComplete() override; |
| 111 | |
| 112 | Q_SIGNALS: |
| 113 | void imagePathChanged(); |
| 114 | void elementIdChanged(); |
| 115 | void svgChanged(); |
| 116 | void naturalSizeChanged(); |
| 117 | void elementRectChanged(); |
| 118 | |
| 119 | protected Q_SLOTS: |
| 120 | void updateNeeded(); |
| 121 | |
| 122 | private: |
| 123 | void updateDevicePixelRatio(); |
| 124 | void scheduleImageUpdate(); |
| 125 | void updatePolish() override; |
| 126 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
| 127 | |
| 128 | QPointer<KSvg::Svg> m_svg; |
| 129 | Kirigami::Platform::PlatformTheme *m_kirigamiTheme; |
| 130 | QString m_elementID; |
| 131 | QImage m_image; |
| 132 | bool m_textureChanged; |
| 133 | }; |
| 134 | } |
| 135 | |
| 136 | #endif |
| 137 | |