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