File indexing completed on 2024-12-08 07:33:44

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