File indexing completed on 2024-12-01 04:20:08

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2011 Marco Martin <mart@kde.org>
0003  * SPDX-FileCopyrightText: (C) 2020 Luca Beltrame <lbeltrame@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-or-later
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QImage>
0011 #include <QQuickPaintedItem>
0012 
0013 class ImageItem : public QQuickPaintedItem
0014 {
0015     Q_OBJECT
0016 
0017     Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged RESET resetImage)
0018     Q_PROPERTY(int nativeWidth READ nativeWidth NOTIFY nativeWidthChanged)
0019     Q_PROPERTY(int nativeHeight READ nativeHeight NOTIFY nativeHeightChanged)
0020     Q_PROPERTY(int paintedWidth READ paintedWidth NOTIFY paintedWidthChanged)
0021     Q_PROPERTY(int paintedHeight READ paintedHeight NOTIFY paintedHeightChanged)
0022     Q_PROPERTY(int verticalPadding READ verticalPadding NOTIFY verticalPaddingChanged)
0023     Q_PROPERTY(int horizontalPadding READ horizontalPadding NOTIFY horizontalPaddingChanged)
0024     Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
0025     Q_PROPERTY(bool null READ isNull NOTIFY nullChanged)
0026 
0027 public:
0028     enum FillMode {
0029         Stretch, // the image is scaled to fit
0030         PreserveAspectFit, // the image is scaled uniformly to fit without cropping
0031         PreserveAspectCrop, // the image is scaled uniformly to fill, cropping if necessary
0032         Tile, // the image is duplicated horizontally and vertically
0033         TileVertically, // the image is stretched horizontally and tiled vertically
0034         TileHorizontally // the image is stretched vertically and tiled horizontally
0035     };
0036     Q_ENUM(FillMode)
0037 
0038     explicit ImageItem(QQuickItem *parent = nullptr);
0039     ~ImageItem() override = default;
0040 
0041     void setImage(const QImage &image);
0042     QImage image() const;
0043     void resetImage();
0044 
0045     int nativeWidth() const;
0046     int nativeHeight() const;
0047 
0048     int paintedWidth() const;
0049     int paintedHeight() const;
0050     int verticalPadding() const;
0051     int horizontalPadding() const;
0052 
0053     FillMode fillMode() const;
0054     void setFillMode(FillMode mode);
0055 
0056     void paint(QPainter *painter) override;
0057 
0058     bool isNull() const;
0059 
0060 Q_SIGNALS:
0061     void nativeWidthChanged();
0062     void nativeHeightChanged();
0063     void fillModeChanged();
0064     void imageChanged();
0065     void nullChanged();
0066     void paintedWidthChanged();
0067     void paintedHeightChanged();
0068     void verticalPaddingChanged();
0069     void horizontalPaddingChanged();
0070 
0071 protected:
0072 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0073     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0074 #else
0075     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0076 #endif
0077 
0078 private:
0079     QImage m_image;
0080     bool m_smooth;
0081     FillMode m_fillMode;
0082     QRect m_paintedRect;
0083 
0084 private Q_SLOTS:
0085     void updatePaintedRect();
0086 };