Clean up the AccountListModel

This commit is contained in:
Tobias Fella
2021-07-27 23:30:36 +02:00
parent de7354f056
commit 00494f8f88
2 changed files with 8 additions and 28 deletions

View File

@@ -8,31 +8,16 @@
AccountListModel::AccountListModel(QObject *parent)
: QAbstractListModel(parent)
{
m_connections.clear();
m_connections += Controller::instance().connections();
connect(&Controller::instance(), &Controller::connectionAdded, this, [=](Connection *conn) {
if (!conn) {
return;
}
beginInsertRows(QModelIndex(), m_connections.count(), m_connections.count());
m_connections.append(conn);
beginInsertRows(QModelIndex(), Controller::instance().connections().count(), Controller::instance().connections().count());
endInsertRows();
});
connect(&Controller::instance(), &Controller::connectionDropped, this, [=](Connection *conn) {
if (!conn) {
qDebug() << "Trying to remove null connection";
return;
}
conn->disconnect(this);
const auto it = std::find(m_connections.begin(), m_connections.end(), conn);
if (it == m_connections.end()) {
return; // Already deleted, nothing to do
}
const int row = it - m_connections.begin();
beginRemoveRows(QModelIndex(), row, row);
m_connections.erase(it);
endRemoveRows();
beginResetModel();
endResetModel();
});
}
@@ -42,19 +27,17 @@ QVariant AccountListModel::data(const QModelIndex &index, int role) const
return {};
}
if (index.row() >= m_connections.count()) {
qDebug() << "AccountListModel, something's wrong: index.row() >= "
"m_users.count()";
if (index.row() >= Controller::instance().connections().count()) {
return {};
}
auto m_connection = m_connections.at(index.row());
auto connection = Controller::instance().connections().at(index.row());
if (role == UserRole) {
return QVariant::fromValue(m_connection->user());
return QVariant::fromValue(connection->user());
}
if (role == ConnectionRole) {
return QVariant::fromValue(m_connection);
return QVariant::fromValue(connection);
}
return {};
@@ -66,7 +49,7 @@ int AccountListModel::rowCount(const QModelIndex &parent) const
return 0;
}
return m_connections.count();
return Controller::instance().connections().count();
}
QHash<int, QByteArray> AccountListModel::roleNames() const