1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#include "threadlistmodel.h"
#include <QFont>
ThreadListModel::ThreadListModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int ThreadListModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_threads.size();
}
int ThreadListModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : ColumnCount;
}
QVariant ThreadListModel::data(const QModelIndex &index, int role) const
{
// A stale index from a view that has not caught up with a clear() can carry
// any row or column, so both bounds are checked rather than trusted.
if (!index.isValid() || index.row() < 0 || index.row() >= m_threads.size()
|| index.column() < 0 || index.column() >= ColumnCount) {
return {};
}
const ThreadSummary &thread = m_threads.at(index.row());
if (role == ThreadIdRole)
return thread.threadId;
if (role == Qt::DisplayRole) {
switch (index.column()) {
case DateColumn:
return thread.date.toString(QStringLiteral("yyyy-MM-dd hh:mm"));
case AuthorsColumn:
return thread.authors;
case SubjectColumn:
return thread.totalCount > 1
? QStringLiteral("%1 (%2)").arg(thread.subject)
.arg(thread.totalCount)
: thread.subject;
case TagsColumn:
return thread.tags.join(QLatin1Char(' '));
default:
return {};
}
}
if (role == Qt::FontRole && thread.isUnread()) {
QFont font;
font.setBold(true);
return font;
}
return {};
}
QVariant ThreadListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
return {};
switch (section) {
case DateColumn: return QStringLiteral("Date");
case AuthorsColumn: return QStringLiteral("From");
case SubjectColumn: return QStringLiteral("Subject");
case TagsColumn: return QStringLiteral("Tags");
default: return {};
}
}
void ThreadListModel::appendBatch(const QVector<ThreadSummary> &batch)
{
// beginInsertRows with an empty range violates Qt's contract, so the guard
// has to come before the signal, not inside it.
if (batch.isEmpty())
return;
const int first = m_threads.size();
beginInsertRows({}, first, first + batch.size() - 1);
m_threads.append(batch);
endInsertRows();
}
void ThreadListModel::clear()
{
beginResetModel();
m_threads.clear();
endResetModel();
}
ThreadSummary ThreadListModel::threadAt(int row) const
{
if (row < 0 || row >= m_threads.size())
return {};
return m_threads.at(row);
}
void ThreadListModel::applyTagChange(const QString &threadId,
const QStringList &added,
const QStringList &removed)
{
for (int row = 0; row < m_threads.size(); ++row) {
if (m_threads.at(row).threadId != threadId)
continue;
QStringList &tags = m_threads[row].tags;
for (const QString &tag : removed)
tags.removeAll(tag);
for (const QString &tag : added) {
if (!tags.contains(tag))
tags.append(tag);
}
// The whole row repaints: unread state drives the font of every column,
// not just the tags one.
emit dataChanged(index(row, 0), index(row, ColumnCount - 1));
return;
}
}
|