Improve accountlistmodel.

square-messages
Black Hat 2018-09-10 12:56:16 +08:00
parent 647a2cdbf2
commit 20113fb47f
4 changed files with 97 additions and 68 deletions

View File

@ -28,52 +28,112 @@ Page {
id: accountSettingsListView id: accountSettingsListView
delegate: SwipeDelegate { delegate: Column {
width: accountSettingsListView.width SwipeDelegate {
height: 64 width: accountSettingsListView.width
height: 64
clip: true clip: true
Row { Row {
anchors.fill: parent anchors.fill: parent
anchors.margins: 8 anchors.margins: 8
spacing: 8 spacing: 8
ImageItem { ImageItem {
width: parent.height
height: parent.height
hint: user.displayName
defaultColor: Util.stringToColor(user.displayName)
image: user.avatar
}
ColumnLayout {
Label {
text: user.displayName
}
Label {
text: user.id
}
}
}
swipe.right: Rectangle {
width: parent.height width: parent.height
height: parent.height height: parent.height
anchors.right: parent.right
hint: name color: Material.accent
defaultColor: Util.stringToColor(name)
image: avatar MaterialIcon {
anchors.fill: parent
icon: "\ue879"
color: "white"
}
SwipeDelegate.onClicked: matriqueController.logout(connection)
} }
ColumnLayout { onClicked: accountSettingsListView.currentIndex == index ? accountSettingsListView.currentIndex = -1 : accountSettingsListView.currentIndex = index
Label {
text: name
}
Label {
text: accountID
}
}
} }
swipe.right: Rectangle { Rectangle {
width: parent.height width: parent.width
height: parent.height height: 2
anchors.right: parent.right visible: accountSettingsListView.currentIndex == index
color: Material.accent color: Material.accent
}
MaterialIcon { ColumnLayout {
anchors.fill: parent visible: accountSettingsListView.currentIndex == index
width: parent.width - 32
anchors.horizontalCenter: parent.horizontalCenter
icon: "\ue879" RowLayout {
color: "white" Layout.fillWidth: true
spacing: 16
Label { text: "Homeserver:" }
TextField {
Layout.fillWidth: true
text: connection.homeserver
selectByMouse: true
}
} }
SwipeDelegate.onClicked: matriqueController.logout(connection) RowLayout {
Layout.fillWidth: true
spacing: 16
Label { text: "Device ID:" }
TextField {
Layout.fillWidth: true
text: connection.deviceId
selectByMouse: true
}
}
RowLayout {
Layout.fillWidth: true
spacing: 16
Label { text: "Access Token:" }
TextField {
Layout.fillWidth: true
text: connection.accessToken
selectByMouse: true
}
}
} }
} }
} }

View File

@ -123,8 +123,8 @@ ApplicationWindow {
anchors.fill: parent anchors.fill: parent
anchors.margins: 12 anchors.margins: 12
hint: name hint: user.displayName
image: avatar image: user.avatar
defaultColor: Material.accent defaultColor: Material.accent
} }

View File

@ -12,10 +12,7 @@ void AccountListModel::setController(Controller* value) {
m_controller = value; m_controller = value;
for (auto c : m_controller->connections()) { for (auto c : m_controller->connections()) m_connections.append(c);
connectConnectionSignals(c);
m_connections.append(c);
};
connect(m_controller, &Controller::connectionAdded, this, connect(m_controller, &Controller::connectionAdded, this,
[=](Connection* conn) { [=](Connection* conn) {
@ -23,7 +20,6 @@ void AccountListModel::setController(Controller* value) {
} }
beginInsertRows(QModelIndex(), m_connections.count(), beginInsertRows(QModelIndex(), m_connections.count(),
m_connections.count()); m_connections.count());
connectConnectionSignals(conn);
m_connections.append(conn); m_connections.append(conn);
endInsertRows(); endInsertRows();
}); });
@ -57,16 +53,8 @@ QVariant AccountListModel::data(const QModelIndex& index, int role) const {
return QVariant(); return QVariant();
} }
auto m_connection = m_connections.at(index.row()); auto m_connection = m_connections.at(index.row());
if (role == NameRole) { if (role == UserRole) {
return m_connection->user()->displayname(); return QVariant::fromValue(m_connection->user());
}
if (role == AccountIDRole) {
return m_connection->user()->id();
}
if (role == AvatarRole) {
if (!m_connection->user()->avatarUrl().isEmpty())
return m_connection->user()->avatar(64);
return QImage();
} }
if (role == ConnectionRole) { if (role == ConnectionRole) {
return QVariant::fromValue(m_connection); return QVariant::fromValue(m_connection);
@ -81,22 +69,9 @@ int AccountListModel::rowCount(const QModelIndex& parent) const {
return m_connections.count(); return m_connections.count();
} }
void AccountListModel::connectConnectionSignals(Connection* conn) {
connect(conn->user(), &User::avatarChanged, this, [=] {
const auto it = std::find(m_connections.begin(), m_connections.end(), conn);
if (it == m_connections.end()) {
return;
}
const auto idx = index(it - m_connections.begin());
emit dataChanged(idx, idx, {AvatarRole});
});
}
QHash<int, QByteArray> AccountListModel::roleNames() const { QHash<int, QByteArray> AccountListModel::roleNames() const {
QHash<int, QByteArray> roles; QHash<int, QByteArray> roles;
roles[NameRole] = "name"; roles[UserRole] = "user";
roles[AccountIDRole] = "accountID";
roles[AvatarRole] = "avatar";
roles[ConnectionRole] = "connection"; roles[ConnectionRole] = "connection";
return roles; return roles;
} }

View File

@ -11,16 +11,11 @@ class AccountListModel : public QAbstractListModel {
Q_PROPERTY(Controller* controller READ controller WRITE setController NOTIFY Q_PROPERTY(Controller* controller READ controller WRITE setController NOTIFY
controllerChanged) controllerChanged)
public: public:
enum EventRoles { enum EventRoles { UserRole = Qt::UserRole + 1, ConnectionRole };
NameRole = Qt::UserRole + 1,
AccountIDRole,
AvatarRole,
ConnectionRole
};
AccountListModel(QObject* parent = nullptr); AccountListModel(QObject* parent = nullptr);
QVariant data(const QModelIndex& index, int role = NameRole) const override; QVariant data(const QModelIndex& index, int role = UserRole) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
@ -32,7 +27,6 @@ class AccountListModel : public QAbstractListModel {
Controller* m_controller; Controller* m_controller;
QVector<Connection*> m_connections; QVector<Connection*> m_connections;
void connectConnectionSignals(Connection* conn);
signals: signals:
void controllerChanged(); void controllerChanged();
}; };