1 | #include "contactgroupmodel.h" |
---|
2 | |
---|
3 | #include <QImage> |
---|
4 | #include <QPixmap> |
---|
5 | #include <QDebug> |
---|
6 | |
---|
7 | ContactGroupListModel::ContactGroupListModel(QObject* parent) |
---|
8 | : QAbstractListModel(parent) |
---|
9 | { |
---|
10 | connect(BCContactManager::instance(), SIGNAL(contactAdded(IContact*)), |
---|
11 | this, SLOT(dataUpdated(IContact*))); |
---|
12 | connect(BCContactManager::instance(), SIGNAL(contactUpdated(IContact*)), |
---|
13 | this, SLOT(dataUpdated(IContact*))); |
---|
14 | connect(BCContactManager::instance(), SIGNAL(contactRemoved(IContact*)), |
---|
15 | this, SLOT(dataUpdated(IContact*))); |
---|
16 | |
---|
17 | connect(BCGroupManager::instance(), SIGNAL(groupAdded(IGroup*)), |
---|
18 | this, SLOT(dataUpdated(IGroup*))); |
---|
19 | connect(BCGroupManager::instance(), SIGNAL(groupUpdated(IGroup*)), |
---|
20 | this, SLOT(dataUpdated(IGroup*))); |
---|
21 | connect(BCGroupManager::instance(), SIGNAL(groupRemoved(IGroup*)), |
---|
22 | this, SLOT(dataUpdated(IGroup*))); |
---|
23 | |
---|
24 | reloadList(); |
---|
25 | } |
---|
26 | ContactGroupListModel::~ContactGroupListModel() { |
---|
27 | disconnect(BCContactManager::instance(), SIGNAL(contactAdded(IContact*)), |
---|
28 | this, SLOT(dataUpdated(IContact*))); |
---|
29 | disconnect(BCContactManager::instance(), SIGNAL(contactUpdated(IContact*)), |
---|
30 | this, SLOT(dataUpdated(IContact*))); |
---|
31 | disconnect(BCContactManager::instance(), SIGNAL(contactRemoved(IContact*)), |
---|
32 | this, SLOT(dataUpdated(IContact*))); |
---|
33 | |
---|
34 | disconnect(BCGroupManager::instance(), SIGNAL(groupAdded(IGroup*)), |
---|
35 | this, SLOT(dataUpdated(IGroup*))); |
---|
36 | disconnect(BCGroupManager::instance(), SIGNAL(groupUpdated(IGroup*)), |
---|
37 | this, SLOT(dataUpdated(IGroup*))); |
---|
38 | disconnect(BCGroupManager::instance(), SIGNAL(groupRemoved(IGroup*)), |
---|
39 | this, SLOT(dataUpdated(IGroup*))); |
---|
40 | } |
---|
41 | |
---|
42 | void ContactGroupListModel::dataUpdated(IContact* contact) { |
---|
43 | dataUpdated(static_cast<IInterface*>(contact)); |
---|
44 | } |
---|
45 | void ContactGroupListModel::dataUpdated(IGroup* group) { |
---|
46 | dataUpdated(static_cast<IInterface*>(group)); |
---|
47 | } |
---|
48 | void ContactGroupListModel::dataUpdated(IInterface* contactOrGroup) { |
---|
49 | int row = items_.indexOf(contactOrGroup); |
---|
50 | QModelIndex idx1 = index(row, 0); |
---|
51 | |
---|
52 | row = items_.indexOf(contactOrGroup); |
---|
53 | |
---|
54 | reloadList(); |
---|
55 | |
---|
56 | if (row < 0) { // New contact/group added |
---|
57 | reset(); |
---|
58 | } else { |
---|
59 | row = items_.indexOf(contactOrGroup); |
---|
60 | |
---|
61 | if (row < 0) { // Contact/group removed |
---|
62 | reset(); |
---|
63 | } else { |
---|
64 | QModelIndex idx2 = index(row, 0); |
---|
65 | |
---|
66 | emit dataChanged(idx1, idx1); |
---|
67 | if (idx1 != idx2) { |
---|
68 | emit dataChanged(idx2, idx2); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | void ContactGroupListModel::reloadList() { |
---|
75 | items_.clear(); |
---|
76 | foreach(IContact* contact, BCContactManager::instance()->getContactList()) { |
---|
77 | items_.append(contact); |
---|
78 | } |
---|
79 | foreach(IGroup* group, BCGroupManager::instance()->getGroupList()) { |
---|
80 | items_.append(group); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | void ContactGroupListModel::addFilteredContact(IContact* contact) { |
---|
85 | filteredItems_.insert(contact); |
---|
86 | items_.removeAll(contact); |
---|
87 | |
---|
88 | /* Filter groups, from whose all contacts are filtered */ |
---|
89 | foreach (IGroup* group, groups()) { |
---|
90 | bool hasUnfilteredContacts = false; |
---|
91 | foreach (IContact* contact, group->contacts()) { |
---|
92 | if (items_.contains(contact)) { |
---|
93 | hasUnfilteredContacts = true; |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | if (!hasUnfilteredContacts) { |
---|
98 | /* Add filtered Group */ |
---|
99 | filteredItems_.insert(group); |
---|
100 | items_.removeAll(group); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | reset(); |
---|
105 | } |
---|
106 | void ContactGroupListModel::removeFilteredContact(IContact* contact) { |
---|
107 | items_.append(contact); |
---|
108 | filteredItems_.remove(contact); |
---|
109 | |
---|
110 | /* Remove filter from groups, when all its contacts are unfiltered */ |
---|
111 | foreach (IGroup* group, filteredGroups()) { |
---|
112 | bool hasFilteredContacts = false; |
---|
113 | foreach (IContact* contact, group->contacts()) { |
---|
114 | if (filteredItems_.contains(contact)) { |
---|
115 | hasFilteredContacts = true; |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | if (!hasFilteredContacts) { |
---|
120 | /* Remove filtered group */ |
---|
121 | items_.append(group); |
---|
122 | filteredItems_.remove(group); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | reset(); |
---|
127 | } |
---|
128 | |
---|
129 | IInterface* ContactGroupListModel::dataObject(const QModelIndex& index) const { |
---|
130 | return items_.at(index.row()); |
---|
131 | } |
---|
132 | |
---|
133 | QVariant ContactGroupListModel::data(const QModelIndex& index, int role) const { |
---|
134 | if (!index.isValid() || index.row() < 0 || index.row() >= items_.size()) |
---|
135 | return QVariant(); |
---|
136 | |
---|
137 | IInterface* contactOrGroup = items_.at(index.row()); |
---|
138 | IContact* contact = dynamic_cast<IContact*>(contactOrGroup); |
---|
139 | IGroup* group = dynamic_cast<IGroup*>(contactOrGroup); |
---|
140 | |
---|
141 | switch (role) { |
---|
142 | case Qt::DisplayRole: |
---|
143 | if (contact != NULL) { |
---|
144 | QString contactStr = contact->name(); |
---|
145 | QString aliasStr = ""; |
---|
146 | foreach(QString alias, contact->aliases()) { |
---|
147 | aliasStr += alias + ", "; |
---|
148 | } |
---|
149 | aliasStr.remove(QRegExp(", $")); |
---|
150 | if (aliasStr != "") { |
---|
151 | contactStr += " (" + aliasStr + ")"; |
---|
152 | } |
---|
153 | return contactStr; |
---|
154 | } |
---|
155 | if (group != NULL) { |
---|
156 | return "<" + group->name() + ">"; |
---|
157 | } |
---|
158 | break; |
---|
159 | |
---|
160 | case Qt::EditRole: |
---|
161 | if (contact != NULL) |
---|
162 | return contact->name(); |
---|
163 | if (group != NULL) |
---|
164 | return group->name(); |
---|
165 | break; |
---|
166 | |
---|
167 | case Qt::DecorationRole: |
---|
168 | QImage img; |
---|
169 | if (contact != NULL) { |
---|
170 | img = contact->image(); |
---|
171 | } |
---|
172 | if (group != NULL) { |
---|
173 | img = group->image(); |
---|
174 | } |
---|
175 | return QPixmap::fromImage(img.scaled(32, 32, Qt::KeepAspectRatio)); |
---|
176 | } |
---|
177 | |
---|
178 | /* if (role == Qt::TextAlignmentRole) { |
---|
179 | switch (index.column()) { |
---|
180 | case COL_NAME: |
---|
181 | case COL_GATEWAY_NAME: |
---|
182 | return Qt::AlignLeft + Qt::AlignVCenter; |
---|
183 | case COL_ENABLED: |
---|
184 | return Qt::AlignCenter + Qt::AlignVCenter; |
---|
185 | } |
---|
186 | }*/ |
---|
187 | |
---|
188 | return QVariant(); |
---|
189 | } |
---|
190 | |
---|
191 | QSet<IGroup*> ContactGroupListModel::groups() const { |
---|
192 | QSet<IGroup*> result; |
---|
193 | foreach (IInterface* i, items_) { |
---|
194 | IGroup* g = dynamic_cast<IGroup*>(i); |
---|
195 | if (g != NULL) |
---|
196 | result.insert(g); |
---|
197 | } |
---|
198 | return result; |
---|
199 | } |
---|
200 | QSet<IContact*> ContactGroupListModel::contacts() const { |
---|
201 | QSet<IContact*> result; |
---|
202 | foreach (IInterface* i, items_) { |
---|
203 | IContact* c = dynamic_cast<IContact*>(i); |
---|
204 | if (c != NULL) |
---|
205 | result.insert(c); |
---|
206 | } |
---|
207 | return result; |
---|
208 | } |
---|
209 | QSet<IGroup*> ContactGroupListModel::filteredGroups() const { |
---|
210 | QSet<IGroup*> result; |
---|
211 | foreach (IInterface* i, filteredItems_) { |
---|
212 | IGroup* g = dynamic_cast<IGroup*>(i); |
---|
213 | if (g != NULL) |
---|
214 | result.insert(g); |
---|
215 | } |
---|
216 | return result; |
---|
217 | } |
---|
218 | QSet<IContact*> ContactGroupListModel::filteredContacts() const { |
---|
219 | QSet<IContact*> result; |
---|
220 | foreach (IInterface* i, filteredItems_) { |
---|
221 | IContact* c = dynamic_cast<IContact*>(i); |
---|
222 | if (c != NULL) |
---|
223 | result.insert(c); |
---|
224 | } |
---|
225 | return result; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | int ContactGroupListModel::columnCount(const QModelIndex& parent) const { |
---|
230 | return (parent.isValid()) ? 0 : 1; |
---|
231 | } |
---|
232 | |
---|
233 | int ContactGroupListModel::rowCount(const QModelIndex& parent) const { |
---|
234 | return (parent.isValid()) ? 0 : items_.count(); |
---|
235 | } |
---|