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

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