File indexing completed on 2024-11-03 10:41:30

0001 // SPDX-FileCopyrightText: 2021 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include "events/imagepackevent.h"
0007 #include "neochatroom.h"
0008 #include <QAbstractListModel>
0009 #include <QObject>
0010 #include <QVector>
0011 
0012 class ImagePacksModel;
0013 
0014 /**
0015  * @class StickerModel
0016  *
0017  * A model to visualise a set of stickers.
0018  *
0019  * The stickers are obtained from a Matrix image pack. See Matrix MSC2545 for more details.
0020  * https://github.com/Sorunome/matrix-doc/blob/soru/emotes/proposals/2545-emotes.md
0021  */
0022 class StickerModel : public QAbstractListModel
0023 {
0024     Q_OBJECT
0025 
0026     /**
0027      * @brief The image pack that the stickers come from.
0028      *
0029      * @sa ImagePacksModel
0030      */
0031     Q_PROPERTY(ImagePacksModel *model READ model WRITE setModel NOTIFY modelChanged)
0032 
0033     /**
0034      * @brief The index of the pack in the ImagePacksModel.
0035      *
0036      * @sa ImagePacksModel
0037      */
0038     Q_PROPERTY(int packIndex READ packIndex WRITE setPackIndex NOTIFY packIndexChanged)
0039 
0040     /**
0041      * @brief The current room that the model is being used in.
0042      */
0043     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0044 
0045 public:
0046     /**
0047      * @brief Defines the model roles.
0048      */
0049     enum Roles {
0050         UrlRole = Qt::UserRole + 1, /**< The source mxc URL for the image. */
0051         BodyRole, /**< The image caption, if any. */
0052         IsStickerRole, /**< Whether this emoticon is a sticker. */
0053         IsEmojiRole, /**< Whether this emoticon is an emoji. */
0054     };
0055 
0056     explicit StickerModel(QObject *parent = nullptr);
0057 
0058     /**
0059      * @brief Get the given role value at the given index.
0060      *
0061      * @sa QAbstractItemModel::data
0062      */
0063     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0064 
0065     /**
0066      * @brief Number of rows in the model.
0067      *
0068      * @sa QAbstractItemModel::rowCount
0069      */
0070     [[nodiscard]] int rowCount(const QModelIndex &index) const override;
0071 
0072     /**
0073      * @brief Returns a mapping from Role enum values to role names.
0074      *
0075      * @sa Roles, QAbstractItemModel::roleNames()
0076      */
0077     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0078 
0079     [[nodiscard]] ImagePacksModel *model() const;
0080     void setModel(ImagePacksModel *model);
0081 
0082     [[nodiscard]] int packIndex() const;
0083     void setPackIndex(int index);
0084 
0085     [[nodiscard]] NeoChatRoom *room() const;
0086     void setRoom(NeoChatRoom *room);
0087 
0088     /**
0089      * @brief Post the sticker at the given index as an event in the room.
0090      */
0091     Q_INVOKABLE void postSticker(int index);
0092 
0093 Q_SIGNALS:
0094     void roomChanged();
0095     void modelChanged();
0096     void packIndexChanged();
0097 
0098 private:
0099     ImagePacksModel *m_model = nullptr;
0100     int m_index = 0;
0101     QVector<Quotient::ImagePackEventContent::ImagePackImage> m_images;
0102     NeoChatRoom *m_room;
0103     void reloadImages();
0104 };