File indexing completed on 2024-05-19 15:09:25

0001 /*
0002     SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2015 Luca Beltrame <lbeltrame@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef QIMAGEITEM_H
0009 #define QIMAGEITEM_H
0010 
0011 #include <QImage>
0012 #include <QQuickPaintedItem>
0013 
0014 class QImageItem : public QQuickPaintedItem
0015 {
0016     Q_OBJECT
0017 
0018     Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged RESET resetImage)
0019     Q_PROPERTY(bool smooth READ smooth WRITE setSmooth)
0020     Q_PROPERTY(int nativeWidth READ nativeWidth NOTIFY nativeWidthChanged)
0021     Q_PROPERTY(int nativeHeight READ nativeHeight NOTIFY nativeHeightChanged)
0022     Q_PROPERTY(int paintedWidth READ paintedWidth NOTIFY paintedWidthChanged)
0023     Q_PROPERTY(int paintedHeight READ paintedHeight NOTIFY paintedHeightChanged)
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         Pad, /**< the image is not transformed @since 5.96 **/
0036     };
0037     Q_ENUM(FillMode)
0038 
0039     explicit QImageItem(QQuickItem *parent = nullptr);
0040     ~QImageItem() override;
0041 
0042     void setImage(const QImage &image);
0043     QImage image() const;
0044     void resetImage();
0045 
0046     void setSmooth(const bool smooth);
0047     bool smooth() const;
0048 
0049     int nativeWidth() const;
0050     int nativeHeight() const;
0051 
0052     int paintedWidth() const;
0053     int paintedHeight() const;
0054 
0055     FillMode fillMode() const;
0056     void setFillMode(FillMode mode);
0057 
0058     void paint(QPainter *painter) override;
0059 
0060     bool isNull() const;
0061 
0062 Q_SIGNALS:
0063     void nativeWidthChanged();
0064     void nativeHeightChanged();
0065     void fillModeChanged();
0066     void imageChanged();
0067     void nullChanged();
0068     void paintedWidthChanged();
0069     void paintedHeightChanged();
0070 
0071 protected:
0072     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0073 
0074 private:
0075     QImage m_image;
0076     bool m_smooth;
0077     FillMode m_fillMode;
0078     QRect m_paintedRect;
0079 
0080 private Q_SLOTS:
0081     void updatePaintedRect();
0082 };
0083 
0084 #endif