Clean up the AccountListModel
This commit is contained in:
@@ -8,31 +8,16 @@
|
|||||||
AccountListModel::AccountListModel(QObject *parent)
|
AccountListModel::AccountListModel(QObject *parent)
|
||||||
: QAbstractListModel(parent)
|
: QAbstractListModel(parent)
|
||||||
{
|
{
|
||||||
m_connections.clear();
|
|
||||||
m_connections += Controller::instance().connections();
|
|
||||||
|
|
||||||
connect(&Controller::instance(), &Controller::connectionAdded, this, [=](Connection *conn) {
|
connect(&Controller::instance(), &Controller::connectionAdded, this, [=](Connection *conn) {
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
beginInsertRows(QModelIndex(), m_connections.count(), m_connections.count());
|
beginInsertRows(QModelIndex(), Controller::instance().connections().count(), Controller::instance().connections().count());
|
||||||
m_connections.append(conn);
|
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
});
|
});
|
||||||
connect(&Controller::instance(), &Controller::connectionDropped, this, [=](Connection *conn) {
|
connect(&Controller::instance(), &Controller::connectionDropped, this, [=](Connection *conn) {
|
||||||
if (!conn) {
|
beginResetModel();
|
||||||
qDebug() << "Trying to remove null connection";
|
endResetModel();
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,19 +27,17 @@ QVariant AccountListModel::data(const QModelIndex &index, int role) const
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index.row() >= m_connections.count()) {
|
if (index.row() >= Controller::instance().connections().count()) {
|
||||||
qDebug() << "AccountListModel, something's wrong: index.row() >= "
|
|
||||||
"m_users.count()";
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto m_connection = m_connections.at(index.row());
|
auto connection = Controller::instance().connections().at(index.row());
|
||||||
|
|
||||||
if (role == UserRole) {
|
if (role == UserRole) {
|
||||||
return QVariant::fromValue(m_connection->user());
|
return QVariant::fromValue(connection->user());
|
||||||
}
|
}
|
||||||
if (role == ConnectionRole) {
|
if (role == ConnectionRole) {
|
||||||
return QVariant::fromValue(m_connection);
|
return QVariant::fromValue(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
@@ -66,7 +49,7 @@ int AccountListModel::rowCount(const QModelIndex &parent) const
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_connections.count();
|
return Controller::instance().connections().count();
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QByteArray> AccountListModel::roleNames() const
|
QHash<int, QByteArray> AccountListModel::roleNames() const
|
||||||
|
|||||||
@@ -23,7 +23,4 @@ public:
|
|||||||
[[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
[[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
|
|
||||||
[[nodiscard]] QHash<int, QByteArray> roleNames() const override;
|
[[nodiscard]] QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
private:
|
|
||||||
QVector<Connection *> m_connections;
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user