Warning, file /libraries/kirigami-addons/autotests/example_albummodel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QUrl>
0008 
0009 /**
0010  * @class ItemObject
0011  *
0012  * An example QObject containing the minimum required properties for AlbumMaximizeComponent.
0013  */
0014 class ItemObject : public QObject
0015 {
0016     Q_OBJECT
0017 
0018     /**
0019      * @brief The source for the item.
0020      */
0021     Q_PROPERTY(QUrl source READ source CONSTANT)
0022 
0023     /**
0024      * @brief The width of the item.
0025      */
0026     Q_PROPERTY(qreal sourceWidth READ sourceWidth CONSTANT)
0027 
0028     /**
0029      * @brief The height of the item.
0030      */
0031     Q_PROPERTY(qreal sourceHeight READ sourceHeight CONSTANT)
0032 
0033     /**
0034      * @brief Source for the temporary content.
0035      *
0036      * Typically used when downloading the image to show a thumbnail or other
0037      * temporary image while the main image downloads.
0038      */
0039     Q_PROPERTY(QUrl tempSource READ tempSource CONSTANT)
0040 
0041     /**
0042      * @brief The delegate type that should be shown for this item.
0043      *
0044      * @sa Type
0045      */
0046     Q_PROPERTY(Type type READ type CONSTANT)
0047 
0048     /**
0049      * @brief The caption for the item.
0050      *
0051      * Typically set to the filename if no caption is available.
0052      */
0053     Q_PROPERTY(QString caption READ caption CONSTANT)
0054 
0055 public:
0056     /**
0057      * @brief The media type for the album item.
0058      */
0059     enum Type {
0060         Image = 0, /**< The media has an image or animated image mime type. */
0061         Video, /**< The media has a video mime type. */
0062     };
0063     Q_ENUM(Type);
0064 
0065     ItemObject(QObject *parent = nullptr,
0066                QUrl source = {},
0067                qreal sourceWidth = {},
0068                qreal sourceHeight = {},
0069                QUrl tempSource = {},
0070                Type type = Type::Image,
0071                QString caption = {})
0072         : QObject(parent)
0073         , m_source(source)
0074         , m_sourceWidth(sourceWidth)
0075         , m_sourceHeight(sourceHeight)
0076         , m_tempSource(tempSource)
0077         , m_type(type)
0078         , m_caption(caption)
0079     {
0080     };
0081 
0082     QUrl source () const {return m_source;}
0083     qreal sourceWidth() const {return m_sourceWidth;}
0084     qreal sourceHeight() const {return m_sourceHeight;}
0085     QUrl tempSource () const {return m_tempSource;}
0086     Type type () const {return m_type;}
0087     QString caption () const {return m_caption;}
0088 
0089 private:
0090     QUrl m_source;
0091     qreal m_sourceWidth;
0092     qreal m_sourceHeight;
0093     QUrl m_tempSource;
0094     Type m_type;
0095     QString m_caption;
0096 };
0097 Q_DECLARE_METATYPE(ItemObject *)
0098 
0099 /**
0100  * @class ExampleAlbumModel
0101  *
0102  * This class defines an example model for populating an AlbumMaximizeComponent.
0103  *
0104  * This example provides a minimal implementation using a QList of structs with the
0105  * necessary parameters. This is not important for any implementation, the data
0106  * can be sourced however the designer desires. Instead you must ensure that your model
0107  * passes the same 4 roles as a minimum to the AlbumMaximizeComponent.
0108  *
0109  * @note All 4 params are required by the QML so they must be populated, even if
0110  *       only with an empty value.
0111  */
0112 class ExampleAlbumModel : public QAbstractListModel
0113 {
0114     Q_OBJECT
0115 
0116     /**
0117      * @brief The source for any images in the model.
0118      *
0119      * @note This is just an easy interface to allow the data to be set from QML for
0120      *       writing tests. In an actual implementation the data can come from anywhere.
0121      */
0122     Q_PROPERTY(QUrl testImage READ testImage WRITE setTestImage NOTIFY testImageChanged)
0123 
0124     /**
0125      * @brief The source for any videos in the model.
0126      *
0127      * @note This is just an easy interface to allow the data to be set from QML for
0128      *       writing tests. In an actual implementation the data can come from anywhere.
0129      */
0130     Q_PROPERTY(QUrl testVideo READ testVideo WRITE setTestVideo NOTIFY testVideoChanged)
0131 
0132 public:
0133     /**
0134      * @brief Defines the model roles.
0135      */
0136     enum Roles {
0137         /**
0138          * @brief The source for the item.
0139          */
0140         SourceRole = Qt::DisplayRole,
0141         SourceWidthRole,
0142         SourceHeightRole,
0143         /**
0144          * @brief Source for the temporary content.
0145          *
0146          * Typically used when downloading the image to show a thumbnail or other
0147          * temporary image while the main image downloads.
0148          */
0149         TempSourceRole,
0150         /**
0151          * @brief The delegate type that should be shown for this item.
0152          *
0153          * @sa Type
0154          */
0155         TypeRole, /**< The delegate type that should be shown for this item. */
0156         /**
0157          * @brief The caption for the item.
0158          *
0159          * Typically set to the filename if no caption is available.
0160          */
0161         CaptionRole,
0162     };
0163 
0164     /**
0165      * @brief Define the data required to represent a model item.
0166      */
0167     struct Item {
0168         QUrl source; /**< The source for the item. */
0169         QUrl tempSource; /**< Source for the temporary content. */
0170         ItemObject::Type type; /**< The delegate type that should be shown for this item. */
0171         QString caption; /**< The caption for the item. */
0172     };
0173 
0174     ExampleAlbumModel(QObject *parent = nullptr);
0175 
0176     QUrl testImage() const;
0177     void setTestImage(QUrl image);
0178 
0179     QUrl testVideo() const;
0180     void setTestVideo(QUrl image);
0181 
0182     /**
0183      * @brief Get the given role value at the given index.
0184      *
0185      * @sa QAbstractItemModel::data
0186      */
0187     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0188 
0189     /**
0190      * @brief Number of rows in the model.
0191      *
0192      * @sa QAbstractItemModel::rowCount
0193      */
0194     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0195 
0196     /**
0197      * @brief Returns a mapping from Role enum values to role names.
0198      *
0199      * @sa EventRoles, QAbstractItemModel::roleNames()
0200      */
0201     QHash<int, QByteArray> roleNames() const override;
0202 
0203 Q_SIGNALS:
0204     void testImageChanged();
0205     void testVideoChanged();
0206 
0207 private:
0208     QList<ItemObject *> m_items;
0209 
0210     QUrl m_testImage;
0211     QUrl m_testVideo;
0212 
0213     void resetModel();
0214 };