File indexing completed on 2024-05-05 16:58:40

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 
0008 #include "neochatroom.h"
0009 
0010 /**
0011  * @class StateModel
0012  *
0013  * This class defines the model for visualising the state events in a room.
0014  */
0015 class StateModel : public QAbstractListModel
0016 {
0017     Q_OBJECT
0018 
0019     /**
0020      * @brief The current room that the model is getting its state events from.
0021      */
0022     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0023 
0024 public:
0025     /**
0026      * @brief Defines the model roles.
0027      */
0028     enum Roles {
0029         TypeRole = 0, /**< The type of the state event. */
0030         StateKeyRole, /**< The state key of the state event. */
0031     };
0032     Q_ENUM(Roles)
0033 
0034     explicit StateModel(QObject *parent = nullptr);
0035 
0036     NeoChatRoom *room() const;
0037     void setRoom(NeoChatRoom *room);
0038 
0039     /**
0040      * @brief Get the given role value at the given index.
0041      *
0042      * @sa QAbstractItemModel::data
0043      */
0044     QVariant data(const QModelIndex &index, int role) const override;
0045 
0046     /**
0047      * @brief Number of rows in the model.
0048      *
0049      * @sa QAbstractItemModel::rowCount
0050      */
0051     int rowCount(const QModelIndex &parent) const override;
0052 
0053     /**
0054      * @brief Returns a mapping from Role enum values to role names.
0055      *
0056      * @sa Roles, QAbstractItemModel::roleNames()
0057      */
0058     QHash<int, QByteArray> roleNames() const override;
0059     /**
0060      * @brief Get the full JSON for an event.
0061      *
0062      * This is used to avoid having the model hold all the JSON data. The JSON for
0063      * a single item is only ever shown, no need to access simultaneously.
0064      */
0065     Q_INVOKABLE QByteArray stateEventJson(const QModelIndex &index);
0066 
0067 Q_SIGNALS:
0068     void roomChanged();
0069 
0070 private:
0071     NeoChatRoom *m_room = nullptr;
0072 
0073     /**
0074      * @brief The room state events in a QList.
0075      *
0076      * This is done for performance, accessing all the data from the parent QHash
0077      * was slower.
0078      */
0079     QList<std::pair<QString, QString>> m_stateEvents;
0080 };