| 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 | /*! |
| 15 | * \qmltype QImageItem |
| 16 | * \inqmlmodule org.kde.kquickcontrols.addons |
| 17 | */ |
| 18 | class QImageItem : public QQuickPaintedItem |
| 19 | { |
| 20 | Q_OBJECT |
| 21 | QML_ELEMENT |
| 22 | |
| 23 | /*! \qmlproperty QImage QImageItem::image */ |
| 24 | Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged RESET resetImage) |
| 25 | /*! \qmlproperty int QImageItem::nativeWidth */ |
| 26 | Q_PROPERTY(int nativeWidth READ nativeWidth NOTIFY nativeWidthChanged) |
| 27 | /*! \qmlproperty int QImageItem::nativeHeight */ |
| 28 | Q_PROPERTY(int nativeHeight READ nativeHeight NOTIFY nativeHeightChanged) |
| 29 | /*! \qmlproperty int QImageItem::paintedWidth */ |
| 30 | Q_PROPERTY(int paintedWidth READ paintedWidth NOTIFY paintedWidthChanged) |
| 31 | /*! \qmlproperty int QImageItem::paintedHeight */ |
| 32 | Q_PROPERTY(int paintedHeight READ paintedHeight NOTIFY paintedHeightChanged) |
| 33 | /*! \qmlproperty FillMode QImageItem::fillMode |
| 34 | * |
| 35 | * \value Stretch |
| 36 | * The image is scaled to fit. |
| 37 | * \value PreserveAspectFit |
| 38 | * the image is scaled uniformly to fit without cropping. |
| 39 | * \value PreserveAspectCrop |
| 40 | * The image is scaled uniformly to fill, cropping if necessary. |
| 41 | * \value Tile |
| 42 | * The image is duplicated horizontally and vertically. |
| 43 | * \value TileVertically |
| 44 | * The image is stretched horizontally and tiled vertically. |
| 45 | * \value TileHorizontally |
| 46 | * The image is stretched vertically and tiled horizontally. |
| 47 | * \value [since 5.96] Pad |
| 48 | * The image is not transformed. |
| 49 | * |
| 50 | */ |
| 51 | Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged) |
| 52 | /*! \qmlproperty bool QImageItem::null */ |
| 53 | Q_PROPERTY(bool null READ isNull NOTIFY nullChanged) |
| 54 | |
| 55 | public: |
| 56 | enum FillMode { |
| 57 | Stretch, |
| 58 | PreserveAspectFit, |
| 59 | PreserveAspectCrop, |
| 60 | Tile, |
| 61 | TileVertically, |
| 62 | TileHorizontally, |
| 63 | Pad, |
| 64 | }; |
| 65 | Q_ENUM(FillMode) |
| 66 | |
| 67 | explicit QImageItem(QQuickItem *parent = nullptr); |
| 68 | ~QImageItem() override; |
| 69 | |
| 70 | void setImage(const QImage &image); |
| 71 | QImage image() const; |
| 72 | void resetImage(); |
| 73 | |
| 74 | int nativeWidth() const; |
| 75 | int nativeHeight() const; |
| 76 | |
| 77 | int paintedWidth() const; |
| 78 | int paintedHeight() const; |
| 79 | |
| 80 | FillMode fillMode() const; |
| 81 | void setFillMode(FillMode mode); |
| 82 | |
| 83 | void paint(QPainter *painter) override; |
| 84 | |
| 85 | bool isNull() const; |
| 86 | |
| 87 | Q_SIGNALS: |
| 88 | /*! \qmlsignal QImageItem::nativeWidthChanged() */ |
| 89 | void nativeWidthChanged(); |
| 90 | /*! \qmlsignal QImageItem::nativeHeightChanged() */ |
| 91 | void nativeHeightChanged(); |
| 92 | /*! \qmlsignal QImageItem::fillModeChanged() */ |
| 93 | void fillModeChanged(); |
| 94 | /*! \qmlsignal QImageItem::imageChanged() */ |
| 95 | void imageChanged(); |
| 96 | /*! \qmlsignal QImageItem::nullChanged() */ |
| 97 | void nullChanged(); |
| 98 | /*! \qmlsignal QImageItem::paintedWidthChanged() */ |
| 99 | void paintedWidthChanged(); |
| 100 | /*! \qmlsignal QImageItem::paintedHeightChanged() */ |
| 101 | void paintedHeightChanged(); |
| 102 | |
| 103 | protected: |
| 104 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
| 105 | |
| 106 | private: |
| 107 | QImage m_image; |
| 108 | FillMode m_fillMode; |
| 109 | QRect m_paintedRect; |
| 110 | |
| 111 | private Q_SLOTS: |
| 112 | void updatePaintedRect(); |
| 113 | }; |
| 114 | |
| 115 | #endif |
| 116 | |