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