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 | namespace Kirigami |
14 | { |
15 | namespace Platform |
16 | { |
17 | class PlatformTheme; |
18 | } |
19 | }; |
20 | |
21 | namespace KSvg |
22 | { |
23 | class Svg; |
24 | |
25 | /** |
26 | * @class SvgItem |
27 | * @short Displays an SVG or an element from an SVG file |
28 | */ |
29 | class SvgItem : public QQuickItem |
30 | { |
31 | Q_OBJECT |
32 | |
33 | /** |
34 | * @brief This property specifies the relative path of the Svg in the theme. |
35 | * |
36 | * Example: "widgets/background" |
37 | * |
38 | * @property QString imagePath |
39 | */ |
40 | Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged) |
41 | |
42 | /** |
43 | * @brief This property specifies the sub-element of the SVG to be |
44 | * rendered. |
45 | * |
46 | * If this is empty, the whole SVG document will be rendered. |
47 | * |
48 | * @property QString elementId |
49 | */ |
50 | Q_PROPERTY(QString elementId READ elementId WRITE setElementId NOTIFY elementIdChanged) |
51 | |
52 | /** |
53 | * @brief This property holds the SVG's natural, unscaled size. |
54 | * |
55 | * This is useful if a pixel-perfect rendering of outlines is needed. |
56 | * |
57 | * @property QSizeF naturalSize |
58 | */ |
59 | Q_PROPERTY(QSizeF naturalSize READ naturalSize NOTIFY naturalSizeChanged) |
60 | |
61 | /** |
62 | * @brief This property holds the rectangle of the selected elementId |
63 | * relative to the unscaled size of the SVG document. |
64 | * |
65 | * Note that this property will holds the entire SVG if element id is not |
66 | * selected. |
67 | * |
68 | * @property QRectF elementRect |
69 | */ |
70 | Q_PROPERTY(QRectF elementRect READ elementRect NOTIFY elementRectChanged) |
71 | |
72 | /** |
73 | * @brief This property holds the internal SVG instance. |
74 | * |
75 | * Usually, specifying just the imagePath is enough. Use this if you have |
76 | * many items taking the same SVG as source, and you want to share the |
77 | * internal SVG object. |
78 | * |
79 | * @property KSvg::Svg svg |
80 | */ |
81 | Q_PROPERTY(KSvg::Svg *svg READ svg WRITE setSvg NOTIFY svgChanged) |
82 | |
83 | public: |
84 | /// @cond INTERNAL_DOCS |
85 | |
86 | explicit SvgItem(QQuickItem *parent = nullptr); |
87 | ~SvgItem() override; |
88 | |
89 | void setImagePath(const QString &path); |
90 | QString imagePath() const; |
91 | |
92 | void setElementId(const QString &elementID); |
93 | QString elementId() const; |
94 | |
95 | void setSvg(KSvg::Svg *svg); |
96 | KSvg::Svg *svg() const; |
97 | |
98 | QSizeF naturalSize() const; |
99 | |
100 | QRectF elementRect() const; |
101 | |
102 | QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) override; |
103 | /// @endcond |
104 | |
105 | void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override; |
106 | |
107 | protected: |
108 | void componentComplete() override; |
109 | |
110 | Q_SIGNALS: |
111 | void imagePathChanged(); |
112 | void elementIdChanged(); |
113 | void svgChanged(); |
114 | void naturalSizeChanged(); |
115 | void elementRectChanged(); |
116 | |
117 | protected Q_SLOTS: |
118 | /// @cond INTERNAL_DOCS |
119 | void updateNeeded(); |
120 | /// @endcond |
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 | |