File indexing completed on 2024-05-05 05:01:26

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