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

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #pragma once
0005 
0006 #include "neochatroom.h"
0007 #include <QAbstractListModel>
0008 #include <QQmlEngine>
0009 #include <Quotient/events/reactionevent.h>
0010 
0011 namespace Quotient
0012 {
0013 class User;
0014 }
0015 
0016 /**
0017  * @class ReactionModel
0018  *
0019  * This class defines the model for visualising a list of reactions to an event.
0020  */
0021 class ReactionModel : public QAbstractListModel
0022 {
0023     Q_OBJECT
0024     QML_ELEMENT
0025     QML_UNCREATABLE("")
0026 
0027 public:
0028     /**
0029      * @brief Definition of an reaction.
0030      */
0031     struct Reaction {
0032         QString reaction; /**< The reaction emoji. */
0033         QVariantList authors; /**< The list of authors who sent the given reaction. */
0034     };
0035 
0036     /**
0037      * @brief Defines the model roles.
0038      */
0039     enum Roles {
0040         TextContentRole = Qt::DisplayRole, /**< The text to show in the reaction. */
0041         ReactionRole, /**< The reaction emoji. */
0042         ToolTipRole, /**< The tool tip to show for the reaction. */
0043         AuthorsRole, /**< The list of authors who sent the given reaction. */
0044         HasLocalUser, /**< Whether the local user is in the list of authors. */
0045     };
0046 
0047     explicit ReactionModel(const Quotient::RoomMessageEvent *event, const NeoChatRoom *room);
0048 
0049     /**
0050      * @brief Get the given role value at the given index.
0051      *
0052      * @sa QAbstractItemModel::data
0053      */
0054     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0055 
0056     /**
0057      * @brief Number of rows in the model.
0058      *
0059      * @sa QAbstractItemModel::rowCount
0060      */
0061     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0062 
0063     /**
0064      * @brief Returns a mapping from Role enum values to role names.
0065      *
0066      * @sa Roles, QAbstractItemModel::roleNames()
0067      */
0068     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0069 
0070 private:
0071     const NeoChatRoom *m_room;
0072     const Quotient::RoomMessageEvent *m_event;
0073     QList<Reaction> m_reactions;
0074 
0075     void updateReactions();
0076     static QString reactionText(const QString &text);
0077 };
0078 Q_DECLARE_METATYPE(ReactionModel *)