Devtools: Split state keys into a separate list
This commit is contained in:
85
src/models/statekeysmodel.cpp
Normal file
85
src/models/statekeysmodel.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
// SPDX-FileCopyrightText: 2024 Tobias Fella <tobias.fella@kde.org>
|
||||
// SPDX-License-Identifier: LGPL-2.0-or-later
|
||||
|
||||
#include "statekeysmodel.h"
|
||||
|
||||
StateKeysModel::StateKeysModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> StateKeysModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{StateKeyRole, "stateKey"},
|
||||
};
|
||||
}
|
||||
QVariant StateKeysModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
|
||||
const auto row = index.row();
|
||||
switch (role) {
|
||||
case StateKeyRole:
|
||||
return m_stateKeys[row]->stateKey();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
int StateKeysModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return m_stateKeys.count();
|
||||
}
|
||||
|
||||
NeoChatRoom *StateKeysModel::room() const
|
||||
{
|
||||
return m_room;
|
||||
}
|
||||
|
||||
void StateKeysModel::loadState()
|
||||
{
|
||||
if (!m_room || m_eventType.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginResetModel();
|
||||
m_stateKeys = m_room->currentState().eventsOfType(m_eventType);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void StateKeysModel::setRoom(NeoChatRoom *room)
|
||||
{
|
||||
if (m_room) {
|
||||
disconnect(m_room, nullptr, this, nullptr);
|
||||
}
|
||||
|
||||
m_room = room;
|
||||
Q_EMIT roomChanged();
|
||||
loadState();
|
||||
|
||||
connect(room, &NeoChatRoom::changed, this, [this] {
|
||||
loadState();
|
||||
});
|
||||
}
|
||||
|
||||
QString StateKeysModel::eventType() const
|
||||
{
|
||||
return m_eventType;
|
||||
}
|
||||
|
||||
void StateKeysModel::setEventType(const QString &eventType)
|
||||
{
|
||||
m_eventType = eventType;
|
||||
Q_EMIT eventTypeChanged();
|
||||
loadState();
|
||||
}
|
||||
|
||||
QByteArray StateKeysModel::stateEventJson(const QModelIndex &index)
|
||||
{
|
||||
const auto row = index.row();
|
||||
const auto event = m_stateKeys[row];
|
||||
const auto json = event->fullJson();
|
||||
return QJsonDocument(json).toJson();
|
||||
}
|
||||
|
||||
#include "moc_statekeysmodel.cpp"
|
||||
83
src/models/statekeysmodel.h
Normal file
83
src/models/statekeysmodel.h
Normal file
@@ -0,0 +1,83 @@
|
||||
// SPDX-FileCopyrightText: 2024 Tobias Fella <tobias.fella@kde.org>
|
||||
// SPDX-License-Identifier: LGPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QQmlEngine>
|
||||
|
||||
#include "neochatroom.h"
|
||||
|
||||
/**
|
||||
* @class StateKeysModel
|
||||
*
|
||||
* This class defines the model for visualising the state keys for a certain type in a room.
|
||||
*/
|
||||
class StateKeysModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
|
||||
/**
|
||||
* @brief The current room that the model is getting its state events from.
|
||||
*/
|
||||
Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged REQUIRED)
|
||||
|
||||
/**
|
||||
* @brief The event type to list the stateKeys for
|
||||
*/
|
||||
Q_PROPERTY(QString eventType READ eventType WRITE setEventType NOTIFY eventTypeChanged REQUIRED)
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Defines the model roles.
|
||||
*/
|
||||
enum Roles {
|
||||
StateKeyRole, /**< The state key of the state event. */
|
||||
};
|
||||
Q_ENUM(Roles)
|
||||
|
||||
explicit StateKeysModel(QObject *parent = nullptr);
|
||||
|
||||
NeoChatRoom *room() const;
|
||||
void setRoom(NeoChatRoom *room);
|
||||
|
||||
QString eventType() const;
|
||||
void setEventType(const QString &eventType);
|
||||
|
||||
/**
|
||||
* @brief Get the given role value at the given index.
|
||||
*
|
||||
* @sa QAbstractItemModel::data
|
||||
*/
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
/**
|
||||
* @brief Number of rows in the model.
|
||||
*
|
||||
* @sa QAbstractItemModel::rowCount
|
||||
*/
|
||||
int rowCount(const QModelIndex &parent) const override;
|
||||
|
||||
/**
|
||||
* @brief Returns a mapping from Role enum values to role names.
|
||||
*
|
||||
* @sa Roles, QAbstractItemModel::roleNames()
|
||||
*/
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
/**
|
||||
* @brief Get the full JSON for an event.
|
||||
*/
|
||||
Q_INVOKABLE QByteArray stateEventJson(const QModelIndex &index);
|
||||
|
||||
Q_SIGNALS:
|
||||
void roomChanged();
|
||||
void eventTypeChanged();
|
||||
|
||||
private:
|
||||
NeoChatRoom *m_room = nullptr;
|
||||
QString m_eventType;
|
||||
QVector<const Quotient::StateEvent *> m_stateKeys;
|
||||
void loadState();
|
||||
};
|
||||
@@ -10,16 +10,16 @@ StateModel::StateModel(QObject *parent)
|
||||
|
||||
QHash<int, QByteArray> StateModel::roleNames() const
|
||||
{
|
||||
return {{TypeRole, "type"}, {StateKeyRole, "stateKey"}};
|
||||
return {{TypeRole, "type"}, {EventCountRole, "eventCount"}};
|
||||
}
|
||||
QVariant StateModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
auto row = index.row();
|
||||
switch (role) {
|
||||
case TypeRole:
|
||||
return m_stateEvents[row].first;
|
||||
case StateKeyRole:
|
||||
return m_stateEvents[row].second;
|
||||
return m_stateEvents.keys()[row];
|
||||
case EventCountRole:
|
||||
return m_stateEvents.values()[row].count();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -27,7 +27,7 @@ QVariant StateModel::data(const QModelIndex &index, int role) const
|
||||
int StateModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return m_room->currentState().events().size();
|
||||
return m_stateEvents.count();
|
||||
}
|
||||
|
||||
NeoChatRoom *StateModel::room() const
|
||||
@@ -35,26 +35,39 @@ NeoChatRoom *StateModel::room() const
|
||||
return m_room;
|
||||
}
|
||||
|
||||
void StateModel::loadState()
|
||||
{
|
||||
beginResetModel();
|
||||
m_stateEvents.clear();
|
||||
const auto keys = m_room->currentState().events().keys();
|
||||
for (const auto &[type, stateKey] : keys) {
|
||||
if (!m_stateEvents.contains(type)) {
|
||||
m_stateEvents[type] = {};
|
||||
}
|
||||
m_stateEvents[type] += stateKey;
|
||||
}
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void StateModel::setRoom(NeoChatRoom *room)
|
||||
{
|
||||
m_room = room;
|
||||
Q_EMIT roomChanged();
|
||||
beginResetModel();
|
||||
m_stateEvents.clear();
|
||||
m_stateEvents = m_room->currentState().events().keys();
|
||||
endResetModel();
|
||||
loadState();
|
||||
|
||||
connect(room, &NeoChatRoom::changed, this, [this] {
|
||||
beginResetModel();
|
||||
m_stateEvents.clear();
|
||||
m_stateEvents = m_room->currentState().events().keys();
|
||||
endResetModel();
|
||||
loadState();
|
||||
});
|
||||
}
|
||||
|
||||
QByteArray StateModel::stateEventJson(const QModelIndex &index)
|
||||
{
|
||||
auto row = index.row();
|
||||
return QJsonDocument(m_room->currentState().events()[m_stateEvents[row]]->fullJson()).toJson();
|
||||
const auto type = m_stateEvents.keys()[row];
|
||||
const auto stateKey = m_stateEvents.values()[row][0];
|
||||
const auto event = m_room->currentState().get(type, stateKey);
|
||||
|
||||
return QJsonDocument(event->fullJson()).toJson();
|
||||
}
|
||||
|
||||
#include "moc_statemodel.cpp"
|
||||
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
*/
|
||||
enum Roles {
|
||||
TypeRole = 0, /**< The type of the state event. */
|
||||
StateKeyRole, /**< The state key of the state event. */
|
||||
EventCountRole, /**< Number of events of this type. */
|
||||
};
|
||||
Q_ENUM(Roles)
|
||||
|
||||
@@ -58,11 +58,9 @@ public:
|
||||
* @sa Roles, QAbstractItemModel::roleNames()
|
||||
*/
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
/**
|
||||
* @brief Get the full JSON for an event.
|
||||
*
|
||||
* This is used to avoid having the model hold all the JSON data. The JSON for
|
||||
* a single item is only ever shown, no need to access simultaneously.
|
||||
*/
|
||||
Q_INVOKABLE QByteArray stateEventJson(const QModelIndex &index);
|
||||
|
||||
@@ -73,10 +71,8 @@ private:
|
||||
NeoChatRoom *m_room = nullptr;
|
||||
|
||||
/**
|
||||
* @brief The room state events in a QList.
|
||||
*
|
||||
* This is done for performance, accessing all the data from the parent QHash
|
||||
* was slower.
|
||||
* @brief A map from state event type to number of events of that type
|
||||
*/
|
||||
QList<std::pair<QString, QString>> m_stateEvents;
|
||||
QMap<QString, QList<QString>> m_stateEvents;
|
||||
void loadState();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user