Devtools Improvements

- Now has tabs setup as more features are added
- First extra tab has basic server info
- Use mobileform to make it look nicer
- For the room data tab allow the room to be changed from within devtools
- For the room data tab allow m.room.member events to be filtered out so other event types can be found easily
- For the room data tab allow viewing room account data

network/neochat#557
This commit is contained in:
James Graham
2023-04-29 15:20:51 +00:00
committed by Tobias Fella
parent ca805917de
commit 85b40ca536
14 changed files with 347 additions and 23 deletions

View File

@@ -0,0 +1,32 @@
// 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();
}
}

View File

@@ -0,0 +1,44 @@
// 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 <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
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

@@ -10,7 +10,7 @@ StateModel::StateModel(QObject *parent)
QHash<int, QByteArray> StateModel::roleNames() const
{
return {{TypeRole, "type"}, {StateKeyRole, "stateKey"}, {SourceRole, "source"}};
return {{TypeRole, "type"}, {StateKeyRole, "stateKey"}};
}
QVariant StateModel::data(const QModelIndex &index, int role) const
{
@@ -18,11 +18,9 @@ QVariant StateModel::data(const QModelIndex &index, int role) const
auto row = index.row();
switch (role) {
case TypeRole:
return m_room->currentState().events().keys()[row].first;
return m_stateEvents[row].first;
case StateKeyRole:
return m_room->currentState().events().keys()[row].second;
case SourceRole:
return QJsonDocument(m_room->currentState().events()[m_room->currentState().events().keys()[row]]->fullJson()).toJson();
return m_stateEvents[row].second;
}
#endif
return {};
@@ -48,9 +46,27 @@ void StateModel::setRoom(NeoChatRoom *room)
m_room = room;
Q_EMIT roomChanged();
beginResetModel();
m_stateEvents.clear();
#ifdef QUOTIENT_07
m_stateEvents = m_room->currentState().events().keys();
#endif
endResetModel();
connect(room, &NeoChatRoom::changed, this, [this] {
beginResetModel();
m_stateEvents.clear();
#ifdef QUOTIENT_07
m_stateEvents = m_room->currentState().events().keys();
#endif
endResetModel();
});
}
QByteArray StateModel::stateEventJson(const QModelIndex &index)
{
auto row = index.row();
#ifdef QUOTIENT_07
return QJsonDocument(m_room->currentState().events()[m_stateEvents[row]]->fullJson()).toJson();
#else
return {};
#endif
}

View File

@@ -26,9 +26,8 @@ public:
* @brief Defines the model roles.
*/
enum Roles {
TypeRole, /**< The type of the state event. */
TypeRole = 0, /**< The type of the state event. */
StateKeyRole, /**< The state key of the state event. */
SourceRole, /**< The full event source JSON. */
};
Q_ENUM(Roles);
@@ -57,10 +56,25 @@ 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);
Q_SIGNALS:
void roomChanged();
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.
*/
QList<std::pair<QString, QString>> m_stateEvents;
};