1 | #include "grouptablemodel.h" |
---|
2 | |
---|
3 | #include <QDebug> |
---|
4 | |
---|
5 | #include "../../business/BCGroupManager.h" |
---|
6 | |
---|
7 | GroupTableModel::GroupTableModel(QObject* parent) |
---|
8 | : QAbstractTableModel(parent) |
---|
9 | { |
---|
10 | connect(BCGroupManager::instance(), SIGNAL(groupAdded(IGroup*)), |
---|
11 | this, SLOT(listAltered())); |
---|
12 | connect(BCGroupManager::instance(), SIGNAL(groupUpdated(IGroup*)), |
---|
13 | this, SLOT(groupUpdated(IGroup*))); |
---|
14 | connect(BCGroupManager::instance(), SIGNAL(groupRemoved(IGroup*)), |
---|
15 | this, SLOT(listAltered())); |
---|
16 | } |
---|
17 | GroupTableModel::~GroupTableModel() { |
---|
18 | disconnect(BCGroupManager::instance(), SIGNAL(groupAdded(IGroup*)), |
---|
19 | this, SLOT(listAltered())); |
---|
20 | disconnect(BCGroupManager::instance(), SIGNAL(groupUpdated(IGroup*)), |
---|
21 | this, SLOT(groupUpdated(IGroup*))); |
---|
22 | disconnect(BCGroupManager::instance(), SIGNAL(groupRemoved(IGroup*)), |
---|
23 | this, SLOT(listAltered())); |
---|
24 | } |
---|
25 | |
---|
26 | void GroupTableModel::listAltered() { |
---|
27 | reloadList(); |
---|
28 | } |
---|
29 | |
---|
30 | void GroupTableModel::groupUpdated(IGroup* group) { |
---|
31 | int row = items_.indexOf(group); |
---|
32 | |
---|
33 | QModelIndex idx_start = index(row, 0); |
---|
34 | QModelIndex idx_end = index(row, MaxCol); |
---|
35 | |
---|
36 | if (idx_start.isValid() && idx_end.isValid()) { |
---|
37 | emit dataChanged(idx_start, idx_end); |
---|
38 | } else { |
---|
39 | reset(); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | void GroupTableModel::reloadList() { |
---|
44 | items_.clear(); |
---|
45 | foreach(IGroup* group, BCGroupManager::instance()->getGroupList()) { |
---|
46 | items_.append(group); |
---|
47 | } |
---|
48 | reset(); |
---|
49 | } |
---|
50 | |
---|
51 | QVariant GroupTableModel::headerData(int section, Qt::Orientation orientation, int role) const { |
---|
52 | if (orientation == Qt::Horizontal) { |
---|
53 | if (role == Qt::DisplayRole) { |
---|
54 | switch (section) { |
---|
55 | case ColImage: return ""; |
---|
56 | case ColName: return tr("Name"); |
---|
57 | case ColMembers: return tr("Members"); |
---|
58 | } |
---|
59 | } |
---|
60 | if (role == Qt::TextAlignmentRole) { |
---|
61 | return Qt::AlignLeft + Qt::AlignVCenter; |
---|
62 | } |
---|
63 | } |
---|
64 | return QAbstractTableModel::headerData(section, orientation, role); |
---|
65 | } |
---|
66 | |
---|
67 | IGroup* GroupTableModel::dataObject(const QModelIndex& index) const { |
---|
68 | return items_.at(index.row()); |
---|
69 | } |
---|
70 | |
---|
71 | QVariant GroupTableModel::data(const QModelIndex& index, int role) const { |
---|
72 | if (!index.isValid() || index.row() < 0 || index.row() >= items_.size()) |
---|
73 | return QVariant(); |
---|
74 | |
---|
75 | const IGroup* group = dataObject(index); |
---|
76 | |
---|
77 | if (role == Qt::DisplayRole) { |
---|
78 | switch (index.column()) { |
---|
79 | case ColImage: |
---|
80 | return ""; |
---|
81 | case ColName: |
---|
82 | return group->name(); |
---|
83 | case ColMembers: |
---|
84 | return getMembersStr(group); |
---|
85 | } |
---|
86 | } |
---|
87 | if (role == Qt::DecorationRole) { |
---|
88 | switch (index.column()) { |
---|
89 | case ColImage: |
---|
90 | return group->image(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | return QVariant(); |
---|
95 | } |
---|
96 | |
---|
97 | int GroupTableModel::columnCount(const QModelIndex& parent) const { |
---|
98 | return (parent.isValid()) ? 0 : MaxCol+1; |
---|
99 | } |
---|
100 | |
---|
101 | int GroupTableModel::rowCount(const QModelIndex& parent) const { |
---|
102 | return (parent.isValid()) ? 0 : items_.count(); |
---|
103 | } |
---|
104 | |
---|
105 | bool GroupTableModel::removeRows(int row, int count, const QModelIndex& parent) { |
---|
106 | if (parent.isValid()) |
---|
107 | return false; |
---|
108 | |
---|
109 | int lastRow = row + count - 1; |
---|
110 | beginRemoveRows(parent, row, lastRow); |
---|
111 | for (int i = lastRow; i >= row; --i) { |
---|
112 | BCGroupManager::instance()->removeGroup(items_.at(i)->id()); |
---|
113 | } |
---|
114 | endRemoveRows(); |
---|
115 | return true; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | QString GroupTableModel::getMembersStr(const IGroup* group) const { |
---|
120 | QStringList members; |
---|
121 | foreach (IContact* contact, group->contacts()) { |
---|
122 | members.append(contact->name()); |
---|
123 | } |
---|
124 | members.sort(); |
---|
125 | return members.join(", "); |
---|
126 | } |
---|