From ef255243ecc3c3e8839f800ad5d218a183905cd0 Mon Sep 17 00:00:00 2001
From: Link Dupont
Date: Mon, 17 Oct 2022 11:54:53 -0400
Subject: [PATCH] Use ComboBox as AccountRegistry view
Replace the RowLayout with a ComboBox. This permits a higher number
of accounts to be used in the application without reducing the
usability.
BUG: 460601
---
imports/NeoChat/Page/RoomListPage.qml | 30 +++++++++------------------
src/neochataccountregistry.cpp | 13 +++++++++---
src/neochataccountregistry.h | 6 ++++++
3 files changed, 26 insertions(+), 23 deletions(-)
diff --git a/imports/NeoChat/Page/RoomListPage.qml b/imports/NeoChat/Page/RoomListPage.qml
index 164043cdc..24e741007 100644
--- a/imports/NeoChat/Page/RoomListPage.qml
+++ b/imports/NeoChat/Page/RoomListPage.qml
@@ -369,27 +369,17 @@ Kirigami.ScrollablePage {
}
footer: QQC2.ToolBar {
- visible: accountList.count > 1 && !collapsedMode
+ visible: AccountRegistry.accountCount > 1 && !collapsedMode
height: visible ? implicitHeight : 0
- leftPadding: 0
- rightPadding: 0
- topPadding: 0
- bottomPadding: 0
- contentItem: RowLayout {
- spacing: 0
- Repeater {
- id: accountList
- model: AccountRegistry
- delegate: Kirigami.BasicListItem {
- checkable: true
- checked: Controller.activeConnection && Controller.activeConnection.localUserId === model.connection.localUserId
- onClicked: Controller.activeConnection = model.connection
- Layout.fillWidth: true
- Layout.fillHeight: true
- text: model.connection.localUserId
- subtitle: model.connection.localUser.accountLabel
- }
- }
+
+ contentItem: QQC2.ComboBox {
+ id: accountList
+
+ model: AccountRegistry
+ textRole: "userId"
+ valueRole: "connection"
+ onActivated: Controller.activeConnection = currentValue
+ Component.onCompleted: currentIndex = indexOfValue(Controller.activeConnection)
}
}
}
diff --git a/src/neochataccountregistry.cpp b/src/neochataccountregistry.cpp
index ae250f021..e1cbfb0a8 100644
--- a/src/neochataccountregistry.cpp
+++ b/src/neochataccountregistry.cpp
@@ -15,6 +15,7 @@ void AccountRegistry::add(Connection *c)
beginInsertRows(QModelIndex(), m_accounts.size(), m_accounts.size());
m_accounts += c;
endInsertRows();
+ emit accountCountChanged();
}
void AccountRegistry::drop(Connection *c)
@@ -23,6 +24,7 @@ void AccountRegistry::drop(Connection *c)
m_accounts.removeOne(c);
endRemoveRows();
Q_ASSERT(!m_accounts.contains(c));
+ emit accountCountChanged();
}
bool AccountRegistry::isLoggedIn(const QString &userId) const
@@ -51,8 +53,13 @@ QVariant AccountRegistry::data(const QModelIndex &index, int role) const
const auto account = m_accounts[index.row()];
- if (role == ConnectionRole) {
- return QVariant::fromValue(account);
+ switch (role) {
+ case ConnectionRole:
+ return QVariant::fromValue(account);
+ case UserIdRole:
+ return QVariant::fromValue(account->userId());
+ default:
+ return {};
}
return {};
@@ -69,7 +76,7 @@ int AccountRegistry::rowCount(const QModelIndex &parent) const
QHash AccountRegistry::roleNames() const
{
- return {{ConnectionRole, "connection"}};
+ return {{ConnectionRole, "connection"}, {UserIdRole, "userId"}};
}
bool AccountRegistry::isEmpty() const
diff --git a/src/neochataccountregistry.h b/src/neochataccountregistry.h
index ecb1dbdd3..859149511 100644
--- a/src/neochataccountregistry.h
+++ b/src/neochataccountregistry.h
@@ -15,9 +15,12 @@ class Connection;
class AccountRegistry : public QAbstractListModel
{
Q_OBJECT
+ Q_PROPERTY(int accountCount READ count NOTIFY accountCountChanged);
+
public:
enum EventRoles {
ConnectionRole = Qt::UserRole + 1,
+ UserIdRole = Qt::DisplayRole,
};
static AccountRegistry &instance()
@@ -40,6 +43,9 @@ public:
[[nodiscard]] QHash roleNames() const override;
+Q_SIGNALS:
+ void accountCountChanged();
+
private:
AccountRegistry();