File indexing completed on 2024-05-12 15:54:52

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2021 Mikel Johnson <mikel5764@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef VECTOR_IMAGE_H
0008 #define VECTOR_IMAGE_H
0009 
0010 #include <QPainter>
0011 #include <QQuickItem>
0012 #include <QQuickPaintedItem>
0013 #include <QSvgRenderer>
0014 #include <memory>
0015 
0016 class VectorImage : public QQuickPaintedItem
0017 {
0018     Q_OBJECT
0019     Q_PROPERTY(Status status READ status NOTIFY statusChanged) // read only
0020     Q_PROPERTY(QRectF sourceClipRect READ sourceClipRect WRITE setSourceClipRect NOTIFY sourceClipRectChanged)
0021     Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
0022     Q_PROPERTY(QSize sourceSize READ sourceSize NOTIFY sourceSizeChanged) // read only
0023 public:
0024     VectorImage(QQuickItem *parent = nullptr);
0025 
0026     enum Status { Null, Ready, Loading, Error };
0027     Q_ENUM(Status)
0028 
0029     void paint(QPainter *painter) override;
0030     void itemChange(ItemChange change, const ItemChangeData &value) override;
0031 
0032     QRectF sourceClipRect() const
0033     {
0034         return m_sourceClipRect;
0035     }
0036 
0037     QUrl source() const
0038     {
0039         return m_source;
0040     }
0041 
0042     QSize sourceSize() const
0043     {
0044         return m_sourceSize;
0045     }
0046 
0047     Status status() const
0048     {
0049         return m_status;
0050     }
0051 
0052     void setSourceClipRect(const QRectF &sourceClipRect);
0053     void setSource(const QUrl &source);
0054     void setSourceSize(const QSize &sourceSize);
0055     void setStatus(Status status);
0056 
0057 Q_SIGNALS:
0058     void sourceClipRectChanged();
0059     void sourceChanged();
0060     void sourceSizeChanged();
0061     void statusChanged(VectorImage::Status);
0062 
0063 private:
0064     QUrl m_source;
0065     std::unique_ptr<QSvgRenderer> m_renderer;
0066     QRectF m_sourceClipRect;
0067     QRectF m_viewBoxF;
0068     QSize m_sourceSize;
0069     qreal m_devicePixelRatio;
0070     Status m_status;
0071 };
0072 
0073 #endif // VECTOR_IMAGE_H