1 | #include "accountmodel.h" |
---|
2 | |
---|
3 | #include <QDebug> |
---|
4 | |
---|
5 | AccountModel::AccountModel(QObject* parent) |
---|
6 | : QAbstractTableModel(parent) |
---|
7 | { |
---|
8 | connect(BCAccountManager::instance(), SIGNAL(accountAdded(IAccount*)), |
---|
9 | this, SLOT(accountDataChanged(IAccount*))); |
---|
10 | connect(BCAccountManager::instance(), SIGNAL(accountDataChanged(IAccount*)), |
---|
11 | this, SLOT(accountDataChanged(IAccount*))); |
---|
12 | connect(BCAccountManager::instance(), SIGNAL(accountRemoved(IAccount*)), |
---|
13 | this, SLOT(accountDataChanged(IAccount*))); |
---|
14 | } |
---|
15 | AccountModel::~AccountModel() { |
---|
16 | disconnect(BCAccountManager::instance(), SIGNAL(accountAdded(IAccount*)), |
---|
17 | this, SLOT(accountDataChanged(IAccount*))); |
---|
18 | disconnect(BCAccountManager::instance(), SIGNAL(accountDataChanged(IAccount*)), |
---|
19 | this, SLOT(accountDataChanged(IAccount*))); |
---|
20 | disconnect(BCAccountManager::instance(), SIGNAL(accountRemoved(IAccount*)), |
---|
21 | this, SLOT(accountDataChanged(IAccount*))); |
---|
22 | } |
---|
23 | |
---|
24 | void AccountModel::accountUpdated(IAccount* account) { |
---|
25 | int row = BCAccountManager::instance()->getAccountList().values().indexOf(account); |
---|
26 | |
---|
27 | QModelIndex idx_start = index(row, 0); |
---|
28 | QModelIndex idx_end = index(row, COL_COUNT); |
---|
29 | emit dataChanged(idx_start, idx_end); |
---|
30 | } |
---|
31 | |
---|
32 | QVariant AccountModel::headerData(int section, Qt::Orientation orientation, int role) const { |
---|
33 | if (orientation == Qt::Horizontal) { |
---|
34 | if (role == Qt::DisplayRole) { |
---|
35 | switch (section) { |
---|
36 | case COL_NAME: return tr("Name"); |
---|
37 | case COL_GATEWAY_NAME: return tr("Gateway"); |
---|
38 | case COL_ENABLED: return tr("Enabled"); |
---|
39 | } |
---|
40 | } |
---|
41 | if (role == Qt::TextAlignmentRole) { |
---|
42 | return Qt::AlignLeft + Qt::AlignVCenter; |
---|
43 | } |
---|
44 | } |
---|
45 | return QAbstractTableModel::headerData(section, orientation, role); |
---|
46 | } |
---|
47 | |
---|
48 | IAccount* AccountModel::dataObject(const QModelIndex& index) const { |
---|
49 | return BCAccountManager::instance()->getAccountList().values().at(index.row()); |
---|
50 | } |
---|
51 | |
---|
52 | QVariant AccountModel::data(const QModelIndex& index, int role) const { |
---|
53 | QList<IAccount*> accounts = BCAccountManager::instance()->getAccountList().values(); |
---|
54 | if (!index.isValid() || index.row() < 0 || index.row() >= accounts.size()) |
---|
55 | return QVariant(); |
---|
56 | |
---|
57 | const IAccount* account = accounts.at(index.row()); |
---|
58 | |
---|
59 | if (role == Qt::DisplayRole) { |
---|
60 | switch (index.column()) { |
---|
61 | case COL_NAME: |
---|
62 | return account->name(); |
---|
63 | case COL_GATEWAY_NAME: |
---|
64 | return account->gateway()->name(); |
---|
65 | } |
---|
66 | } |
---|
67 | if (role == Qt::CheckStateRole) { |
---|
68 | switch (index.column()) { |
---|
69 | case COL_ENABLED: |
---|
70 | return (account->isEnabled()) ? Qt::Checked : Qt::Unchecked; |
---|
71 | } |
---|
72 | } |
---|
73 | if (role == Qt::DecorationRole) { |
---|
74 | switch (index.column()) { |
---|
75 | case COL_GATEWAY_NAME: |
---|
76 | return account->gateway()->icon(); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | if (role == Qt::TextAlignmentRole) { |
---|
81 | switch (index.column()) { |
---|
82 | case COL_NAME: |
---|
83 | case COL_GATEWAY_NAME: |
---|
84 | return Qt::AlignLeft + Qt::AlignVCenter; |
---|
85 | case COL_ENABLED: |
---|
86 | return Qt::AlignCenter + Qt::AlignVCenter; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | return QVariant(); |
---|
91 | } |
---|
92 | |
---|
93 | Qt::ItemFlags AccountModel::flags(const QModelIndex& index) const { |
---|
94 | if (!index.isValid()) |
---|
95 | return Qt::ItemIsEnabled; |
---|
96 | |
---|
97 | Qt::ItemFlags flags = QAbstractTableModel::flags(index); |
---|
98 | if (index.column() == COL_ENABLED) |
---|
99 | flags |= Qt::ItemIsUserCheckable; |
---|
100 | return flags; |
---|
101 | } |
---|
102 | |
---|
103 | bool AccountModel::setData(const QModelIndex& index, const QVariant& value, int role /* = Qt::EditRole */) { |
---|
104 | if (!index.isValid()) { |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | if (role == Qt::CheckStateRole) { |
---|
109 | if (index.column() == COL_ENABLED) { |
---|
110 | IAccount* account = dataObject(index); |
---|
111 | Q_ASSERT(account); |
---|
112 | |
---|
113 | const Qt::CheckState cs = static_cast<Qt::CheckState>(value.toInt()); |
---|
114 | account->setEnabled(cs != Qt::Unchecked); |
---|
115 | BCAccountManager::instance()->saveAccount(account); |
---|
116 | |
---|
117 | return true; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | int AccountModel::columnCount(const QModelIndex& parent) const { |
---|
125 | return (parent.isValid()) ? 0 : COL_COUNT; |
---|
126 | } |
---|
127 | |
---|
128 | int AccountModel::rowCount(const QModelIndex& parent) const { |
---|
129 | return (parent.isValid()) ? 0 : BCAccountManager::instance()->getAccountList().count(); |
---|
130 | } |
---|
131 | |
---|
132 | bool AccountModel::removeRows(int row, int count, const QModelIndex& parent) { |
---|
133 | if (parent.isValid()) |
---|
134 | return false; |
---|
135 | |
---|
136 | int lastRow = row + count - 1; |
---|
137 | beginRemoveRows(parent, row, lastRow); |
---|
138 | QList<IAccount*> accounts = BCAccountManager::instance()->getAccountList().values(); |
---|
139 | for (int i = lastRow; i >= row; --i) { |
---|
140 | BCAccountManager::instance()->removeAccount(accounts.at(i)->id()); |
---|
141 | } |
---|
142 | endRemoveRows(); |
---|
143 | return true; |
---|
144 | } |
---|