Fix avatar issue in accountlistmodel.

This commit is contained in:
Black Hat 2018-09-10 07:03:43 +08:00
parent 5c55856df3
commit f5b24f32b8
2 changed files with 17 additions and 10 deletions

View File

@ -13,15 +13,7 @@ void AccountListModel::setController(Controller* value) {
m_controller = value;
for (auto c : m_controller->connections()) {
connect(c->user(), &User::avatarChanged, [=] {
const auto it =
std::find(m_connections.begin(), m_connections.end(), c);
if (it == m_connections.end()) {
return;
}
const auto idx = index(it - m_connections.begin());
emit dataChanged(idx, idx, {AvatarRole});
});
connectConnectionSignals(c);
m_connections.append(c);
};
@ -31,6 +23,7 @@ void AccountListModel::setController(Controller* value) {
}
beginInsertRows(QModelIndex(), m_connections.count(),
m_connections.count());
connectConnectionSignals(conn);
m_connections.append(conn);
endInsertRows();
});
@ -71,7 +64,9 @@ QVariant AccountListModel::data(const QModelIndex& index, int role) const {
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) {
return QVariant::fromValue(m_connection);
@ -86,6 +81,17 @@ int AccountListModel::rowCount(const QModelIndex& parent) const {
return m_connections.count();
}
void AccountListModel::connectConnectionSignals(Connection* conn) {
connect(conn->user(), &User::avatarChanged, [=] {
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> roles;
roles[NameRole] = "name";

View File

@ -32,6 +32,7 @@ class AccountListModel : public QAbstractListModel {
Controller* m_controller;
QVector<Connection*> m_connections;
void connectConnectionSignals(Connection* conn);
signals:
void controllerChanged();
};