1 /*
2     SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
3     SPDX-FileCopyrightText: 2015 Luca Beltrame <lbeltrame@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef QIMAGEITEM_H
9 #define QIMAGEITEM_H
10 
11 #include <QImage>
12 #include <QQuickPaintedItem>
13 
14 class QImageItem : public QQuickPaintedItem
15 {
16     Q_OBJECT
17 
18     Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged RESET resetImage)
19     Q_PROPERTY(bool smooth READ smooth WRITE setSmooth)
20     Q_PROPERTY(int nativeWidth READ nativeWidth NOTIFY nativeWidthChanged)
21     Q_PROPERTY(int nativeHeight READ nativeHeight NOTIFY nativeHeightChanged)
22     Q_PROPERTY(int paintedWidth READ paintedWidth NOTIFY paintedWidthChanged)
23     Q_PROPERTY(int paintedHeight READ paintedHeight NOTIFY paintedHeightChanged)
24     Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
25     Q_PROPERTY(bool null READ isNull NOTIFY nullChanged)
26 
27 public:
28     enum FillMode {
29         Stretch, // the image is scaled to fit
30         PreserveAspectFit, // the image is scaled uniformly to fit without cropping
31         PreserveAspectCrop, // the image is scaled uniformly to fill, cropping if necessary
32         Tile, // the image is duplicated horizontally and vertically
33         TileVertically, // the image is stretched horizontally and tiled vertically
34         TileHorizontally, // the image is stretched vertically and tiled horizontally
35     };
36     Q_ENUM(FillMode)
37 
38     explicit QImageItem(QQuickItem *parent = nullptr);
39     ~QImageItem() override;
40 
41     void setImage(const QImage &image);
42     QImage image() const;
43     void resetImage();
44 
45     void setSmooth(const bool smooth);
46     bool smooth() const;
47 
48     int nativeWidth() const;
49     int nativeHeight() const;
50 
51     int paintedWidth() const;
52     int paintedHeight() const;
53 
54     FillMode fillMode() const;
55     void setFillMode(FillMode mode);
56 
57     void paint(QPainter *painter) override;
58 
59     bool isNull() const;
60 
61 Q_SIGNALS:
62     void nativeWidthChanged();
63     void nativeHeightChanged();
64     void fillModeChanged();
65     void imageChanged();
66     void nullChanged();
67     void paintedWidthChanged();
68     void paintedHeightChanged();
69 
70 protected:
71     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
72 
73 private:
74     QImage m_image;
75     bool m_smooth;
76     FillMode m_fillMode;
77     QRect m_paintedRect;
78 
79 private Q_SLOTS:
80     void updatePaintedRect();
81 };
82 
83 #endif
84