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

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 <QAbstractListModel>
0007 
0008 class NeoChatUser;
0009 
0010 /**
0011  * @class ReactionModel
0012  *
0013  * This class defines the model for visualising a list of reactions to an event.
0014  */
0015 class ReactionModel : public QAbstractListModel
0016 {
0017     Q_OBJECT
0018 
0019 public:
0020     /**
0021      * @brief Definition of an reaction.
0022      */
0023     struct Reaction {
0024         QString reaction; /**< The reaction emoji. */
0025         QVariantList authors; /**< The list of authors who sent the given reaction. */
0026     };
0027 
0028     /**
0029      * @brief Defines the model roles.
0030      */
0031     enum Roles {
0032         TextRole = Qt::DisplayRole, /**< The text to show in the reaction. */
0033         ReactionRole, /**< The reaction emoji. */
0034         ToolTipRole, /**< The tool tip to show for the reaction. */
0035         AuthorsRole, /**< The list of authors who sent the given reaction. */
0036         HasLocalUser, /**< Whether the local user is in the list of authors. */
0037     };
0038 
0039     explicit ReactionModel(QObject *parent = nullptr, QList<Reaction> reactions = {}, NeoChatUser *localUser = nullptr);
0040 
0041     /**
0042      * @brief Get the given role value at the given index.
0043      *
0044      * @sa QAbstractItemModel::data
0045      */
0046     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0047 
0048     /**
0049      * @brief Number of rows in the model.
0050      *
0051      * @sa QAbstractItemModel::rowCount
0052      */
0053     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0054 
0055     /**
0056      * @brief Returns a mapping from Role enum values to role names.
0057      *
0058      * @sa Roles, QAbstractItemModel::roleNames()
0059      */
0060     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0061 
0062     /**
0063      * @brief Set the reactions data in the model.
0064      */
0065     void setReactions(QList<Reaction> reactions);
0066 
0067 private:
0068     QList<Reaction> m_reactions;
0069 
0070     NeoChatUser *m_localUser;
0071 };
0072 Q_DECLARE_METATYPE(ReactionModel *)