From 323cd4962e3a1875369fc53277dff50f82fa06e5 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 10 Sep 2023 09:00:57 +0000 Subject: [PATCH] Explore rooms page RoundedItemDelegate Move the explore rooms page to using the new RoundedItemDelegate --- src/models/publicroomlistmodel.cpp | 19 +++---- src/models/publicroomlistmodel.h | 8 +-- src/qml/Page/ExplorerDelegate.qml | 91 ++++++++++++++++++++++++++++++ src/qml/Page/JoinRoomPage.qml | 68 ++-------------------- src/res.qrc | 1 + 5 files changed, 109 insertions(+), 78 deletions(-) create mode 100644 src/qml/Page/ExplorerDelegate.qml diff --git a/src/models/publicroomlistmodel.cpp b/src/models/publicroomlistmodel.cpp index 11362d7eb..f8a015f57 100644 --- a/src/models/publicroomlistmodel.cpp +++ b/src/models/publicroomlistmodel.cpp @@ -171,7 +171,7 @@ QVariant PublicRoomListModel::data(const QModelIndex &index, int role) const return {}; } auto room = rooms.at(index.row()); - if (role == NameRole) { + if (role == DisplayNameRole) { auto displayName = room.name; if (!displayName.isEmpty()) { return displayName; @@ -188,18 +188,17 @@ QVariant PublicRoomListModel::data(const QModelIndex &index, int role) const return room.roomId; } - if (role == AvatarRole) { + if (role == AvatarUrlRole) { auto avatarUrl = room.avatarUrl; - - if (avatarUrl.isEmpty()) { - return {}; + if (avatarUrl.isEmpty() || !m_connection) { + return QUrl(); } - return avatarUrl.url().remove(0, 6); + return m_connection->makeMediaUrl(avatarUrl); } if (role == TopicRole) { return room.topic; } - if (role == RoomIDRole) { + if (role == RoomIdRole) { return room.roomId; } if (role == AliasRole) { @@ -232,10 +231,10 @@ QHash PublicRoomListModel::roleNames() const { QHash roles; - roles[NameRole] = "name"; - roles[AvatarRole] = "avatar"; + roles[DisplayNameRole] = "displayName"; + roles[AvatarUrlRole] = "avatarUrl"; roles[TopicRole] = "topic"; - roles[RoomIDRole] = "roomID"; + roles[RoomIdRole] = "roomId"; roles[MemberCountRole] = "memberCount"; roles[AllowGuestsRole] = "allowGuests"; roles[WorldReadableRole] = "worldReadable"; diff --git a/src/models/publicroomlistmodel.h b/src/models/publicroomlistmodel.h index 7e412d48f..6f188b9ff 100644 --- a/src/models/publicroomlistmodel.h +++ b/src/models/publicroomlistmodel.h @@ -58,10 +58,10 @@ public: * @brief Defines the model roles. */ enum EventRoles { - NameRole = Qt::DisplayRole + 1, /**< The name of the room. */ - AvatarRole, /**< The source URL for the room's avatar. */ + DisplayNameRole = Qt::DisplayRole + 1, /**< The name of the room. */ + AvatarUrlRole, /**< The source URL for the room's avatar. */ TopicRole, /**< The room topic. */ - RoomIDRole, /**< The room matrix ID. */ + RoomIdRole, /**< The room matrix ID. */ AliasRole, /**< The room canonical alias. */ MemberCountRole, /**< The number of members in the room. */ AllowGuestsRole, /**< Whether the room allows guest users. */ @@ -76,7 +76,7 @@ public: * * @sa QAbstractItemModel::data */ - [[nodiscard]] QVariant data(const QModelIndex &index, int role = NameRole) const override; + [[nodiscard]] QVariant data(const QModelIndex &index, int role = DisplayNameRole) const override; /** * @brief Number of rows in the model. diff --git a/src/qml/Page/ExplorerDelegate.qml b/src/qml/Page/ExplorerDelegate.qml new file mode 100644 index 000000000..7c1f4717e --- /dev/null +++ b/src/qml/Page/ExplorerDelegate.qml @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: 2023 James Graham +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + +import QtQuick 2.15 +import QtQuick.Controls 2.15 as QQC2 +import QtQuick.Layouts 1.15 + +import org.kde.kirigami 2.15 as Kirigami +import org.kde.kirigamiaddons.delegates 1.0 as Delegates +import org.kde.kirigamiaddons.labs.components 1.0 as Components + +import org.kde.neochat 1.0 + +Delegates.RoundedItemDelegate { + id: root + + required property NeoChatConnection connection + required property string roomId + required property string displayName + required property url avatarUrl + required property string alias + required property string topic + required property int memberCount + required property bool isJoined + property bool justJoined: false + + signal roomSelected() + + onClicked: { + if (!isJoined) { + Controller.joinRoom(root.roomId) + justJoined = true; + } else { + RoomManager.enterRoom(root.connection.room(root.roomId)) + } + root.roomSelected() + } + + contentItem: RowLayout { + Components.Avatar { + Layout.preferredWidth: Kirigami.Units.gridUnit * 2 + Layout.preferredHeight: Kirigami.Units.gridUnit * 2 + + source: root.avatarUrl + name: root.displayName + } + ColumnLayout { + Layout.fillWidth: true + RowLayout { + Layout.fillWidth: true + Kirigami.Heading { + Layout.fillWidth: true + level: 4 + text: root.displayName + font.bold: true + textFormat: Text.PlainText + elide: Text.ElideRight + wrapMode: Text.NoWrap + } + QQC2.Label { + visible: root.isJoined || root.justJoined + text: i18n("Joined") + color: Kirigami.Theme.linkColor + } + } + QQC2.Label { + Layout.fillWidth: true + visible: text + text: root.topic ? root.topic.replace(/(\r\n\t|\n|\r\t)/gm," ") : "" + textFormat: Text.PlainText + elide: Text.ElideRight + wrapMode: Text.NoWrap + } + RowLayout { + Layout.fillWidth: true + Kirigami.Icon { + source: "user" + color: Kirigami.Theme.disabledTextColor + implicitHeight: Kirigami.Units.iconSizes.small + implicitWidth: Kirigami.Units.iconSizes.small + } + QQC2.Label { + text: root.memberCount + " " + (root.alias ?? root.roomId) + color: Kirigami.Theme.disabledTextColor + elide: Text.ElideRight + Layout.fillWidth: true + } + } + } + } +} diff --git a/src/qml/Page/JoinRoomPage.qml b/src/qml/Page/JoinRoomPage.qml index ce6944340..c5ee4ec25 100644 --- a/src/qml/Page/JoinRoomPage.qml +++ b/src/qml/Page/JoinRoomPage.qml @@ -195,6 +195,7 @@ Kirigami.ScrollablePage { ListView { id: publicRoomsListView + topMargin: Kirigami.Units.smallSpacing clip: true model: PublicRoomListModel { id: publicRoomListModel @@ -208,70 +209,9 @@ Kirigami.ScrollablePage { if(publicRoomListModel.hasMore && contentHeight - contentY < publicRoomsListView.height + 200) publicRoomListModel.next(); } - delegate: Kirigami.AbstractListItem { - property bool justJoined: false - width: publicRoomsListView.width - onClicked: { - if (!isJoined) { - Controller.joinRoom(roomID) - justJoined = true; - } else { - RoomManager.enterRoom(connection.room(roomID)) - } - applicationWindow().pageStack.layers.pop(); - } - contentItem: RowLayout { - KirigamiComponents.Avatar { - Layout.preferredWidth: Kirigami.Units.gridUnit * 2 - Layout.preferredHeight: Kirigami.Units.gridUnit * 2 - - source: model.avatar ? ("image://mxc/" + model.avatar) : "" - name: model.name - } - ColumnLayout { - Layout.fillWidth: true - RowLayout { - Layout.fillWidth: true - Kirigami.Heading { - Layout.fillWidth: true - level: 4 - text: name - font.bold: true - textFormat: Text.PlainText - elide: Text.ElideRight - wrapMode: Text.NoWrap - } - QQC2.Label { - visible: isJoined || justJoined - text: i18n("Joined") - color: Kirigami.Theme.linkColor - } - } - QQC2.Label { - Layout.fillWidth: true - visible: text - text: topic ? topic.replace(/(\r\n\t|\n|\r\t)/gm," ") : "" - textFormat: Text.PlainText - elide: Text.ElideRight - wrapMode: Text.NoWrap - } - RowLayout { - Layout.fillWidth: true - Kirigami.Icon { - source: "user" - color: Kirigami.Theme.disabledTextColor - implicitHeight: Kirigami.Units.iconSizes.small - implicitWidth: Kirigami.Units.iconSizes.small - } - QQC2.Label { - text: memberCount + " " + (alias ?? roomID) - color: Kirigami.Theme.disabledTextColor - elide: Text.ElideRight - Layout.fillWidth: true - } - } - } - } + delegate: ExplorerDelegate { + connection: root.connection + onRoomSelected: root.closeDialog() } footer: RowLayout { diff --git a/src/res.qrc b/src/res.qrc index 60937d6f4..7c494c23e 100644 --- a/src/res.qrc +++ b/src/res.qrc @@ -18,6 +18,7 @@ qml/Page/RoomPage.qml qml/Page/RoomWindow.qml qml/Page/JoinRoomPage.qml + qml/Page/ExplorerDelegate.qml qml/Page/InviteUserPage.qml qml/Page/StartChatPage.qml qml/Page/ImageEditorPage.qml