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