1
0
forked from blue/squawk
squawk/ui/models/accounts.cpp

86 lines
2.0 KiB
C++
Raw Normal View History

2019-03-30 20:13:13 +00:00
#include "accounts.h"
2019-04-02 15:46:18 +00:00
#include "../../global.h"
#include <QIcon>
2019-03-30 20:13:13 +00:00
std::deque<QString> Models::Accounts::columns = {
"name",
"server",
"state"
};
Models::Accounts::Accounts(QObject* parent):
QAbstractTableModel(parent),
accs()
{
}
Models::Accounts::~Accounts()
{
}
2019-04-03 18:15:36 +00:00
QVariant Models::Accounts::data (const QModelIndex& index, int role) const
2019-03-30 20:13:13 +00:00
{
QVariant answer;
switch (role) {
2019-04-03 18:15:36 +00:00
case Qt::DisplayRole:
answer = accs[index.row()]->data(index.column());
2019-03-30 20:13:13 +00:00
break;
2019-04-02 15:46:18 +00:00
case Qt::DecorationRole:
if (index.column() == 2) {
2019-04-03 18:15:36 +00:00
answer = QIcon::fromTheme(Shared::ConnectionStateThemeIcons[accs[index.row()]->getState()]);
2019-04-02 15:46:18 +00:00
}
break;
2019-03-30 20:13:13 +00:00
default:
break;
}
return answer;
}
int Models::Accounts::columnCount ( const QModelIndex& parent ) const
{
return columns.size();
}
int Models::Accounts::rowCount ( const QModelIndex& parent ) const
{
return accs.size();
}
QVariant Models::Accounts::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
return columns[section];
}
return QVariant();
}
2019-04-03 18:15:36 +00:00
void Models::Accounts::addAccount(Account* account)
2019-03-30 20:13:13 +00:00
{
beginInsertRows(QModelIndex(), accs.size(), accs.size());
2019-04-03 18:15:36 +00:00
accs.push_back(account);
2019-04-07 14:02:41 +00:00
connect(account, SIGNAL(childChanged(Item*, int, int)), this, SLOT(onAccountChanged(Item*, int, int)));
2019-03-30 20:13:13 +00:00
endInsertRows();
}
2019-04-02 15:46:18 +00:00
2019-04-07 14:02:41 +00:00
void Models::Accounts::onAccountChanged(Item* item, int row, int col)
2019-04-02 15:46:18 +00:00
{
2019-04-07 14:02:41 +00:00
Account* acc = getAccount(row);
if (item != acc) {
return; //it means the signal is emitted by one of accounts' children, not exactly him, this model has no interest in that
}
2019-04-03 18:15:36 +00:00
2019-04-07 14:02:41 +00:00
if (col < columnCount(QModelIndex())) {
emit dataChanged(createIndex(row, col, this), createIndex(row, col, this));
2019-04-02 15:46:18 +00:00
}
}
2019-04-03 18:15:36 +00:00
Models::Account * Models::Accounts::getAccount(int index)
{
return accs[index];
}