Move relevant models to devtools

This commit is contained in:
James Graham
2025-04-15 19:09:42 +01:00
parent e787eaabcd
commit 2d5d2c6c06
8 changed files with 14 additions and 10 deletions

View File

@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2022 James Graham <james.h.graham@protonmail.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "statefiltermodel.h"
#include "statemodel.h"
bool StateFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
Q_UNUSED(sourceParent);
// No need to run the check if there are no items in m_stateEventTypesFiltered.
if (m_stateEventTypesFiltered.empty()) {
return true;
}
return !m_stateEventTypesFiltered.contains(sourceModel()->data(sourceModel()->index(sourceRow, 0), StateModel::TypeRole).toString());
}
void StateFilterModel::addStateEventTypeFiltered(const QString &stateEventType)
{
if (!m_stateEventTypesFiltered.contains(stateEventType)) {
m_stateEventTypesFiltered.append(stateEventType);
invalidateFilter();
}
}
void StateFilterModel::removeStateEventTypeFiltered(const QString &stateEventType)
{
if (m_stateEventTypesFiltered.contains(stateEventType)) {
m_stateEventTypesFiltered.removeAll(stateEventType);
invalidateFilter();
}
}
#include "moc_statefiltermodel.cpp"

View File

@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2022 James Graham <james.h.graham@protonmail.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#pragma once
#include <QQmlEngine>
#include <QSortFilterProxyModel>
/**
* @class StateFilterModel
*
* This class creates a custom QSortFilterProxyModel for filtering a list of state events.
* Event types can be filtered out by adding them to m_stateEventTypesFiltered.
*/
class StateFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
QML_ELEMENT
public:
/**
* @brief Custom filter function checking if an event type has been filtered out.
*
* The filter rejects a row if the state event type has been added to m_stateEventTypesFiltered.
*
* @sa m_stateEventTypesFiltered
*/
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
/**
* @brief Add an event type to m_stateEventTypesFiltered.
*
* @sa m_stateEventTypesFiltered
*/
Q_INVOKABLE void addStateEventTypeFiltered(const QString &stateEventType);
/**
* @brief Remove an event type from m_stateEventTypesFiltered.
*
* @sa m_stateEventTypesFiltered
*/
Q_INVOKABLE void removeStateEventTypeFiltered(const QString &stateEventType);
private:
QStringList m_stateEventTypesFiltered;
};

View File

@@ -0,0 +1,90 @@
// 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();
if (room) {
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 QString &type, const QString &stateKey)
{
return QJsonDocument(m_room->currentState().get(type, stateKey)->fullJson()).toJson();
}
QByteArray StateKeysModel::stateEventContentJson(const QString &type, const QString &stateKey)
{
return QJsonDocument(m_room->currentState().get(type, stateKey)->contentJson()).toJson();
}
#include "moc_statekeysmodel.cpp"

View File

@@ -0,0 +1,88 @@
// 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 QString &type, const QString &stateKey);
/**
* @brief Get the content JSON for an event.
*/
Q_INVOKABLE QByteArray stateEventContentJson(const QString &type, const QString &stateKey);
Q_SIGNALS:
void roomChanged();
void eventTypeChanged();
private:
QPointer<NeoChatRoom> m_room;
QString m_eventType;
QVector<const Quotient::StateEvent *> m_stateKeys;
void loadState();
};

View File

@@ -0,0 +1,82 @@
// SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
// SPDX-License-Identifier: LGPL-2.0-or-later
#include "statemodel.h"
StateModel::StateModel(QObject *parent)
: QAbstractListModel(parent)
{
}
QHash<int, QByteArray> StateModel::roleNames() const
{
return {
{TypeRole, "type"},
{EventCountRole, "eventCount"},
{StateKeyRole, "stateKey"},
};
}
QVariant StateModel::data(const QModelIndex &index, int role) const
{
auto row = index.row();
switch (role) {
case TypeRole:
return m_stateEvents.keys()[row];
case EventCountRole:
return m_stateEvents.values()[row].count();
case StateKeyRole:
return m_stateEvents.values()[row][0];
}
return {};
}
int StateModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_stateEvents.count();
}
NeoChatRoom *StateModel::room() const
{
return m_room;
}
void StateModel::loadState()
{
beginResetModel();
m_stateEvents.clear();
if (!m_room) {
return;
}
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();
loadState();
connect(room, &NeoChatRoom::changed, this, [this] {
loadState();
});
}
QByteArray StateModel::stateEventJson(const QString &type, const QString &stateKey)
{
return QJsonDocument(m_room->currentState().get(type, stateKey)->fullJson()).toJson();
}
QByteArray StateModel::stateEventContentJson(const QString &type, const QString &stateKey)
{
return QJsonDocument(m_room->currentState().get(type, stateKey)->contentJson()).toJson();
}
#include "moc_statemodel.cpp"

View File

@@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
// SPDX-License-Identifier: LGPL-2.0-or-later
#pragma once
#include <QAbstractListModel>
#include <QQmlEngine>
#include "neochatroom.h"
/**
* @class StateModel
*
* This class defines the model for visualising the state events in a room.
*/
class StateModel : 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)
public:
/**
* @brief Defines the model roles.
*/
enum Roles {
TypeRole = 0, /**< The type of the state event. */
EventCountRole, /**< Number of events of this type. */
StateKeyRole, /**<State key. Only valid if there's exactly one event of this type. */
};
Q_ENUM(Roles)
explicit StateModel(QObject *parent = nullptr);
NeoChatRoom *room() const;
void setRoom(NeoChatRoom *room);
/**
* @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 QString &type, const QString &stateKey);
/**
* @brief Get the content JSON for an event.
*/
Q_INVOKABLE QByteArray stateEventContentJson(const QString &type, const QString &stateKey);
Q_SIGNALS:
void roomChanged();
private:
QPointer<NeoChatRoom> m_room;
/**
* @brief A map from state event type to state keys
*/
QMap<QString, QList<QString>> m_stateEvents;
void loadState();
};