File indexing completed on 2024-12-01 13:08:32

0001 // SPDX-FileCopyrightText: 2021-2023 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 <QAbstractListModel>
0008 #include <QPointer>
0009 #include <QVector>
0010 
0011 class NeoChatRoom;
0012 
0013 /**
0014  * @class ImagePacksModel
0015  *
0016  * Defines the model for visualising image packs.
0017  *
0018  * See Matrix MSC2545 for more details on image packs.
0019  * https://github.com/Sorunome/matrix-doc/blob/soru/emotes/proposals/2545-emotes.md
0020  */
0021 class ImagePacksModel : public QAbstractListModel
0022 {
0023     Q_OBJECT
0024 
0025     /**
0026      * @brief The current room that the model is being used in.
0027      */
0028     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0029 
0030     /**
0031      * @brief Whether sticker image packs should be shown.
0032      */
0033     Q_PROPERTY(bool showStickers READ showStickers WRITE setShowStickers NOTIFY showStickersChanged)
0034 
0035     /**
0036      * @brief Whether emoticon image packs should be shown.
0037      */
0038     Q_PROPERTY(bool showEmoticons READ showEmoticons WRITE setShowEmoticons NOTIFY showEmoticonsChanged)
0039 
0040 public:
0041     /**
0042      * @brief Defines the model roles.
0043      */
0044     enum Roles {
0045         DisplayNameRole = Qt::DisplayRole, /**< The display name of the image pack. */
0046         AvatarUrlRole, /**< The source mxc URL for the pack avatar. */
0047         AttributionRole, /**< The attribution for the pack author(s). */
0048         IdRole, /**< The ID of the image pack. */
0049     };
0050     Q_ENUM(Roles)
0051 
0052     explicit ImagePacksModel(QObject *parent = nullptr);
0053 
0054     /**
0055      * @brief Get the given role value at the given index.
0056      *
0057      * @sa QAbstractItemModel::data
0058      */
0059     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0060 
0061     /**
0062      * @brief Number of rows in the model.
0063      *
0064      * @sa QAbstractItemModel::rowCount
0065      */
0066     [[nodiscard]] int rowCount(const QModelIndex &index) const override;
0067 
0068     /**
0069      * @brief Returns a mapping from Role enum values to role names.
0070      *
0071      * @sa Roles, QAbstractItemModel::roleNames()
0072      */
0073     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0074 
0075     [[nodiscard]] NeoChatRoom *room() const;
0076     void setRoom(NeoChatRoom *room);
0077 
0078     [[nodiscard]] bool showStickers() const;
0079     void setShowStickers(bool showStickers);
0080 
0081     [[nodiscard]] bool showEmoticons() const;
0082     void setShowEmoticons(bool showEmoticons);
0083 
0084     /**
0085      * @brief Return a vector of the images in the pack at the given index.
0086      */
0087     [[nodiscard]] QVector<Quotient::ImagePackEventContent::ImagePackImage> images(int index);
0088 
0089 Q_SIGNALS:
0090     void roomChanged();
0091     void showStickersChanged();
0092     void showEmoticonsChanged();
0093     void imagesLoaded();
0094 
0095 private:
0096     QPointer<NeoChatRoom> m_room;
0097     QVector<Quotient::ImagePackEventContent> m_events;
0098     bool m_showStickers = true;
0099     bool m_showEmoticons = true;
0100     void reloadImages();
0101 };