From ca805917de5976650e59ce222fc5b7092181a588 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 29 Apr 2023 14:53:01 +0000 Subject: [PATCH] Document and Cleanup userlistmodel Document and cleanup userlist model. - Remove unneeded enum UserTypes - Cleanup includes and remove need to include QPointer - make clear that it is a user or users that are being refreshed Note: breaks libquotient 0.6 compatibility because of the changes to how m_currentRoom is handled --- src/main.cpp | 1 - src/models/userlistmodel.cpp | 64 ++++++++++--------------------- src/models/userlistmodel.h | 73 ++++++++++++++++++++++-------------- 3 files changed, 65 insertions(+), 73 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e6d53ed46..8710a3412 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -243,7 +243,6 @@ int main(int argc, char *argv[]) qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "PushNotificationState", "ENUM"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "PushNotificationAction", "ENUM"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "NeoChatRoomType", "ENUM"); - qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "UserType", "ENUM"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "NeoChatUser", {}); qRegisterMetaType("User*"); diff --git a/src/models/userlistmodel.cpp b/src/models/userlistmodel.cpp index 3a0f891e7..1f1d1034f 100644 --- a/src/models/userlistmodel.cpp +++ b/src/models/userlistmodel.cpp @@ -7,7 +7,6 @@ #include #include "neochatroom.h" -#include "neochatuser.h" using namespace Quotient; @@ -23,40 +22,20 @@ void UserListModel::setRoom(NeoChatRoom *room) return; } - beginResetModel(); if (m_currentRoom) { m_currentRoom->disconnect(this); - // m_currentRoom->connection()->disconnect(this); - for (User *user : std::as_const(m_users)) { - user->disconnect(this); - } - m_users.clear(); } m_currentRoom = room; + if (m_currentRoom) { connect(m_currentRoom, &Room::userAdded, this, &UserListModel::userAdded); connect(m_currentRoom, &Room::userRemoved, this, &UserListModel::userRemoved); connect(m_currentRoom, &Room::memberAboutToRename, this, &UserListModel::userRemoved); connect(m_currentRoom, &Room::memberRenamed, this, &UserListModel::userAdded); - connect(m_currentRoom, &Room::changed, this, &UserListModel::refreshAll); - { - m_users = m_currentRoom->users(); - std::sort(m_users.begin(), m_users.end(), room->memberSorter()); - } - for (User *user : std::as_const(m_users)) { -#ifdef QUOTIENT_07 - connect(user, &User::defaultAvatarChanged, this, [this, user]() { - avatarChanged(user, m_currentRoom); - }); -#else - connect(user, &User::avatarChanged, this, &UserListModel::avatarChanged); -#endif - } - connect(m_currentRoom->connection(), &Connection::loggedOut, this, [this]() { - setRoom(nullptr); - }); + connect(m_currentRoom, &Room::changed, this, &UserListModel::refreshAllUsers); } - endResetModel(); + + refreshAllUsers(); Q_EMIT roomChanged(); } @@ -80,7 +59,9 @@ QVariant UserListModel::data(const QModelIndex &index, int role) const } if (index.row() >= m_users.count()) { - return QStringLiteral("DEADBEEF"); + qDebug() << "UserListModel, something's wrong: index.row() >= " + "users.count()"; + return {}; } auto user = m_users.at(index.row()); if (role == NameRole) { @@ -133,7 +114,6 @@ int UserListModel::rowCount(const QModelIndex &parent) const if (parent.isValid()) { return 0; } - return m_users.count(); } @@ -145,10 +125,12 @@ void UserListModel::userAdded(Quotient::User *user) endInsertRows(); #ifdef QUOTIENT_07 connect(user, &User::defaultAvatarChanged, this, [this, user]() { - avatarChanged(user, m_currentRoom); + refreshUser(user, {AvatarRole}); }); #else - connect(user, &Quotient::User::avatarChanged, this, &UserListModel::avatarChanged); + connect(user, &Quotient::User::avatarChanged, this, [this, user]() { + refreshUser(user, {AvatarRole}); + }); #endif } @@ -165,7 +147,7 @@ void UserListModel::userRemoved(Quotient::User *user) } } -void UserListModel::refresh(Quotient::User *user, const QVector &roles) +void UserListModel::refreshUser(Quotient::User *user, const QVector &roles) { auto pos = findUserPos(user); if (pos != m_users.size()) { @@ -175,7 +157,7 @@ void UserListModel::refresh(Quotient::User *user, const QVector &roles) } } -void UserListModel::refreshAll() +void UserListModel::refreshAllUsers() { beginResetModel(); for (User *user : std::as_const(m_users)) { @@ -183,17 +165,18 @@ void UserListModel::refreshAll() } m_users.clear(); - { - m_users = m_currentRoom->users(); - std::sort(m_users.begin(), m_users.end(), m_currentRoom->memberSorter()); - } + m_users = m_currentRoom->users(); + std::sort(m_users.begin(), m_users.end(), m_currentRoom->memberSorter()); + for (User *user : std::as_const(m_users)) { #ifdef QUOTIENT_07 connect(user, &User::defaultAvatarChanged, this, [this, user]() { - avatarChanged(user, m_currentRoom); + refreshUser(user, {AvatarRole}); }); #else - connect(user, &User::avatarChanged, this, &UserListModel::avatarChanged); + connect(user, &User::avatarChanged, this, [this, user]() { + refreshUser(user, {AvatarRole}); + }); #endif } connect(m_currentRoom->connection(), &Connection::loggedOut, this, [this]() { @@ -203,13 +186,6 @@ void UserListModel::refreshAll() Q_EMIT usersRefreshed(); } -void UserListModel::avatarChanged(Quotient::User *user, const Quotient::Room *context) -{ - if (context == m_currentRoom) { - refresh(user, {AvatarRole}); - } -} - int UserListModel::findUserPos(Quotient::User *user) const { return findUserPos(m_currentRoom->safeMemberName(user->id())); diff --git a/src/models/userlistmodel.h b/src/models/userlistmodel.h index e48f2e147..6bb609036 100644 --- a/src/models/userlistmodel.h +++ b/src/models/userlistmodel.h @@ -3,8 +3,6 @@ #pragma once -#include - #include #include #include @@ -14,37 +12,38 @@ class NeoChatRoom; namespace Quotient { class User; -class Room; } -class UserType : public QObject -{ - Q_OBJECT - -public: - enum Types { - Owner = 1, - Admin, - Moderator, - Member, - Muted, - Custom, - }; - Q_ENUM(Types) -}; - +/** + * @class UserListModel + * + * This class defines the model for listing the users in a room. + * + * As well as gathering all the users from a room, the model ensures that they are + * sorted in alphabetical order. + * + * @sa NeoChatRoom + */ class UserListModel : public QAbstractListModel { Q_OBJECT + + /** + * @brief The room that the model is getting its users from. + */ Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged) + public: + /** + * @brief Defines the model roles. + */ enum EventRoles { - NameRole = Qt::UserRole + 1, - UserIdRole, - AvatarRole, - ObjectRole, - PowerLevelRole, - PowerLevelStringRole, + NameRole = Qt::UserRole + 1, /**< The user's display name in the current room. */ + UserIdRole, /**< Matrix ID of the user. */ + AvatarRole, /**< The source URL for the user's avatar in the current room. */ + ObjectRole, /**< The QObject for the user. */ + PowerLevelRole, /**< The user's power level in the current room. */ + PowerLevelStringRole, /**< The name of the user's power level in the current room. */ }; UserListModel(QObject *parent = nullptr); @@ -52,11 +51,30 @@ public: [[nodiscard]] NeoChatRoom *room() const; void setRoom(NeoChatRoom *room); + /** + * @brief The user at the given index of the model. + */ [[nodiscard]] Quotient::User *userAt(QModelIndex index) const; + /** + * @brief Get the given role value at the given index. + * + * @sa QAbstractItemModel::data + */ [[nodiscard]] QVariant data(const QModelIndex &index, int role = NameRole) const override; + + /** + * @brief Number of rows in the model. + * + * @sa QAbstractItemModel::rowCount + */ [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override; + /** + * @brief Returns a mapping from Role enum values to role names. + * + * @sa EventRoles, QAbstractItemModel::roleNames() + */ [[nodiscard]] QHash roleNames() const override; Q_SIGNALS: @@ -66,9 +84,8 @@ Q_SIGNALS: private Q_SLOTS: void userAdded(Quotient::User *user); void userRemoved(Quotient::User *user); - void refresh(Quotient::User *user, const QVector &roles = {}); - void refreshAll(); - void avatarChanged(Quotient::User *user, const Quotient::Room *context); + void refreshUser(Quotient::User *user, const QVector &roles = {}); + void refreshAllUsers(); private: QPointer m_currentRoom;