File indexing completed on 2024-12-22 03:45:40

0001 /*
0002     SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 #ifndef SVGITEM_P
0008 #define SVGITEM_P
0009 
0010 #include <QImage>
0011 #include <QQuickItem>
0012 
0013 namespace Kirigami
0014 {
0015 namespace Platform
0016 {
0017 class PlatformTheme;
0018 }
0019 };
0020 
0021 namespace KSvg
0022 {
0023 class Svg;
0024 
0025 /**
0026  * @class SvgItem
0027  * @short Displays an SVG or an element from an SVG file
0028  */
0029 class SvgItem : public QQuickItem
0030 {
0031     Q_OBJECT
0032 
0033     /**
0034      * @brief This property specifies the relative path of the Svg in the theme.
0035      *
0036      * Example: "widgets/background"
0037      *
0038      * @property QString imagePath
0039      */
0040     Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged)
0041 
0042     /**
0043      * @brief This property specifies the sub-element of the SVG to be
0044      * rendered.
0045      *
0046      * If this is empty, the whole SVG document will be rendered.
0047      *
0048      * @property QString elementId
0049      */
0050     Q_PROPERTY(QString elementId READ elementId WRITE setElementId NOTIFY elementIdChanged)
0051 
0052     /**
0053      * @brief This property holds the SVG's natural, unscaled size.
0054      *
0055      * This is useful if a pixel-perfect rendering of outlines is needed.
0056      *
0057      * @property QSizeF naturalSize
0058      */
0059     Q_PROPERTY(QSizeF naturalSize READ naturalSize NOTIFY naturalSizeChanged)
0060 
0061     /**
0062      * @brief This property holds the rectangle of the selected elementId
0063      * relative to the unscaled size of the SVG document.
0064      *
0065      * Note that this property will holds the entire SVG if element id is not
0066      * selected.
0067      *
0068      * @property QRectF elementRect
0069      */
0070     Q_PROPERTY(QRectF elementRect READ elementRect NOTIFY elementRectChanged)
0071 
0072     /**
0073      * @brief This property holds the internal SVG instance.
0074      *
0075      * Usually, specifying just the imagePath is enough. Use this if you have
0076      * many items taking the same SVG as source, and you want to share the
0077      * internal SVG object.
0078      *
0079      * @property KSvg::Svg svg
0080      */
0081     Q_PROPERTY(KSvg::Svg *svg READ svg WRITE setSvg NOTIFY svgChanged)
0082 
0083 public:
0084     /// @cond INTERNAL_DOCS
0085 
0086     explicit SvgItem(QQuickItem *parent = nullptr);
0087     ~SvgItem() override;
0088 
0089     void setImagePath(const QString &path);
0090     QString imagePath() const;
0091 
0092     void setElementId(const QString &elementID);
0093     QString elementId() const;
0094 
0095     void setSvg(KSvg::Svg *svg);
0096     KSvg::Svg *svg() const;
0097 
0098     QSizeF naturalSize() const;
0099 
0100     QRectF elementRect() const;
0101 
0102     QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) override;
0103     /// @endcond
0104 
0105     void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override;
0106 
0107 protected:
0108     void componentComplete() override;
0109 
0110 Q_SIGNALS:
0111     void imagePathChanged();
0112     void elementIdChanged();
0113     void svgChanged();
0114     void naturalSizeChanged();
0115     void elementRectChanged();
0116 
0117 protected Q_SLOTS:
0118     /// @cond INTERNAL_DOCS
0119     void updateNeeded();
0120     /// @endcond
0121 
0122 private:
0123     void updateDevicePixelRatio();
0124     void scheduleImageUpdate();
0125     void updatePolish() override;
0126     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0127 
0128     QPointer<KSvg::Svg> m_svg;
0129     Kirigami::Platform::PlatformTheme *m_kirigamiTheme;
0130     QString m_elementID;
0131     QImage m_image;
0132     bool m_textureChanged;
0133 };
0134 }
0135 
0136 #endif