Commit f7855583 authored by Dan Leinir Turthra Jensen's avatar Dan Leinir Turthra Jensen 🌈
Browse files

Lamdafy some data signal handling, for less publics and easier code

parent 6501d337
...@@ -41,10 +41,19 @@ public: ...@@ -41,10 +41,19 @@ public:
{ {
accountIDs = accountsManager->accountList(); accountIDs = accountsManager->accountList();
connect(accountsManager, SIGNAL(accountCreated(Accounts::AccountId)), connect(accountsManager, &Accounts::Manager::accountCreated,
q, SLOT(accountCreated(Accounts::AccountId))); q, [this](Accounts::AccountId accountId){
connect(accountsManager, SIGNAL(accountRemoved(Accounts::AccountId)), int row = accountIDs.count();
q, SLOT(accountRemoved(Accounts::AccountId))); q->beginInsertRows(QModelIndex(), row, row);
accountIDs.insert(row, accountId);
q->endInsertRows();
});
connect(accountsManager, &Accounts::Manager::accountRemoved,
q, [this](Accounts::AccountId accountId) {
q->beginRemoveRows(QModelIndex(), accountIDs.indexOf(accountId), accountIDs.indexOf(accountId));
removeAccount(accountId);
q->endRemoveRows();
});
}; };
virtual ~Private() virtual ~Private()
{ {
...@@ -69,13 +78,17 @@ Accounts::Account* AccountsModel::Private::accountById(int id) ...@@ -69,13 +78,17 @@ Accounts::Account* AccountsModel::Private::accountById(int id)
return accounts.value(id); return accounts.value(id);
} }
// If we don't yet have this account cached, get it and connect it up to the model
Accounts::Account* account = accountsManager->account(id); Accounts::Account* account = accountsManager->account(id);
if (!account) { if (!account) {
qDebug() << "\t Failed to get the account from manager"; qDebug() << "\t Failed to get the account from manager";
return nullptr; return nullptr;
} }
connect(account, SIGNAL(displayNameChanged(QString)), q, SLOT(accountUpdated())); connect(account, &Accounts::Account::displayNameChanged, q, [this,account](){
QModelIndex accountIndex = q->index(accountIDs.indexOf(account->id()));
Q_EMIT q->dataChanged(accountIndex, accountIndex, QVector<int>() << AccountsModel::DisplayNameRole);
});
accounts[id] = account; accounts[id] = account;
return account; return account;
...@@ -178,30 +191,3 @@ QVariant AccountsModel::data(const QModelIndex& index, int role) const ...@@ -178,30 +191,3 @@ QVariant AccountsModel::data(const QModelIndex& index, int role) const
return data; return data;
} }
void AccountsModel::accountCreated(Accounts::AccountId accountId)
{
qDebug() << "AccountsModel::accountCreated: " << accountId;
int row = d->accountIDs.count();
beginInsertRows(QModelIndex(), row, row);
d->accountIDs.insert(row, accountId);
endInsertRows();
}
void AccountsModel::accountRemoved(Accounts::AccountId accountId)
{
qDebug() << "AccountsModel::accountRemoved: " << accountId;
beginRemoveRows(QModelIndex(), d->accountIDs.indexOf(accountId), d->accountIDs.indexOf(accountId));
d->removeAccount(accountId);
endRemoveRows();
}
void AccountsModel::accountUpdated()
{
Accounts::Account *acc = qobject_cast<Accounts::Account*>(sender());
Accounts::AccountId accountId = acc->id();
qDebug() << "Account updated: " << accountId;
QModelIndex accountIndex = index(d->accountIDs.indexOf(accountId), 0);
Q_EMIT dataChanged(accountIndex, accountIndex);
}
...@@ -48,11 +48,6 @@ class KACCOUNTS_EXPORT AccountsModel : public QAbstractListModel ...@@ -48,11 +48,6 @@ class KACCOUNTS_EXPORT AccountsModel : public QAbstractListModel
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
public Q_SLOTS:
void accountCreated(Accounts::AccountId accountId);
void accountRemoved(Accounts::AccountId accountId);
void accountUpdated();
private: private:
class Private; class Private;
Private *d; Private *d;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment