-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomfilterproxymodel.cpp
294 lines (268 loc) · 10.2 KB
/
customfilterproxymodel.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "customfilterproxymodel.h"
#include "gstreamerlogmodel.h"
#include "timestamp.h"
#include <QtCore/QMetaProperty>
#include <QtGui/QColor>
#include <QtGui/QGuiApplication>
#include <QtGui/QFont>
class CustomFilterProxyModel::Private
{
public:
Private(CustomFilterProxyModel *parent);
QModelIndex findNearestTimestamp(int minRow, int maxRow, const Timestamp ×tamp) const;
private:
CustomFilterProxyModel *q;
public:
QString filter;
mutable int progress = -1;
};
CustomFilterProxyModel::Private::Private(CustomFilterProxyModel *parent)
: q(parent)
{}
QModelIndex CustomFilterProxyModel::Private::findNearestTimestamp(int minRow, int maxRow, const Timestamp ×tamp) const
{
if (minRow == maxRow) {
return q->index(minRow, GStreamerLogModel::TimestampColumn);
}
if (minRow + 1 == maxRow) {
const auto minIndex =q->index(minRow, GStreamerLogModel::TimestampColumn);
const auto maxIndex = q->index(maxRow, GStreamerLogModel::TimestampColumn);
const auto minTimestamp = Timestamp::fromString(minIndex.data().toString());
const auto maxTimestamp = Timestamp::fromString(maxIndex.data().toString());
auto former = minTimestamp.secsTo(timestamp);
auto latter = timestamp.secsTo(maxTimestamp);
if (former == latter) {
former = minTimestamp.msecsTo(timestamp);
latter = timestamp.msecsTo(maxTimestamp);
}
if (former == latter) {
former = minTimestamp.usecsTo(timestamp);
latter = timestamp.usecsTo(maxTimestamp);
}
if (former == latter) {
former = minTimestamp.nsecsTo(timestamp);
latter = timestamp.nsecsTo(maxTimestamp);
}
return former < latter ? minIndex : maxIndex;
}
const int midRow = minRow + (maxRow - minRow) / 2;
const auto midIndex = q->index(midRow, GStreamerLogModel::TimestampColumn);
const auto midTimestamp = Timestamp::fromString(midIndex.data().toString());
const auto comp = timestamp <=> midTimestamp;
if (comp < 0)
return findNearestTimestamp(minRow, midRow, timestamp);
else if (comp > 0)
return findNearestTimestamp(midRow, maxRow, timestamp);
return midIndex;
}
CustomFilterProxyModel::CustomFilterProxyModel(QObject *parent)
: QSortFilterProxyModel{parent}
, d{new Private(this)}
{
connect(this, &CustomFilterProxyModel::filterChanged, this, &CustomFilterProxyModel::invalidate);
}
CustomFilterProxyModel::~CustomFilterProxyModel() = default;
QString CustomFilterProxyModel::filter() const
{
return d->filter;
}
void CustomFilterProxyModel::setFilter(const QString &filter)
{
if (d->filter == filter) return;
d->filter = filter;
emit filterChanged(filter);
}
int CustomFilterProxyModel::progress() const
{
return d->progress;
}
void CustomFilterProxyModel::setProgress(int progress) const
{
if (d->progress == progress) return;
d->progress = progress;
auto that = const_cast<CustomFilterProxyModel *>(this);
emit that->progressChanged(progress);
}
QVariant CustomFilterProxyModel::data(const QModelIndex &index, int role) const
{
QVariant ret = QSortFilterProxyModel::data(index, role);
if (index.column() == GStreamerLogModel::TimestampColumn) {
auto hasGap = [&](int a, int b) {
const auto previousTimestamp = Timestamp::fromString(index.siblingAtRow(a).data().toString());
const auto currentTimestamp = Timestamp::fromString(index.siblingAtRow(b).data().toString());
return previousTimestamp.secsTo(currentTimestamp) > 0;
};
switch (role) {
case Qt::BackgroundRole: {
const auto row = index.row();
if (row > 0) {
if (hasGap(row - 1, row)) {
ret = QColor(Qt::red);
} else if (row < rowCount() - 1) {
if (hasGap(row, row + 1)) {
ret = QColor(Qt::red);
}
}
}
break; }
case Qt::ForegroundRole: {
const auto row = index.row();
if (row > 0) {
if (hasGap(row - 1, row)) {
ret = QColor(Qt::white);
} else if (row < rowCount() - 1) {
if (hasGap(row, row + 1)) {
ret = QColor(Qt::white);
}
}
}
break; }
default:
break;
}
}
if (role == Qt::FontRole) {
if (!d->filter.isEmpty()) {
static const auto mo = &GStreamerLogLine::staticMetaObject;
const auto property = mo->property(index.column());
const auto filters = d->filter.split(QLatin1Char(' '), Qt::SkipEmptyParts);
QVariant value = index.data(Qt::DisplayRole);
bool matched = false;
for (const auto &filter : filters) {
auto keyword = filter;
if (filter.contains(':')) {
const auto columnName = filter.section(':', 0, 0);
if (columnName == property.name()) {
keyword = filter.section(':', 1);
} else {
continue;
}
} else if (index.column() != GStreamerLogModel::MessageColumn) {
continue;
}
switch (property.typeId()) {
case QMetaType::QString: {
if (value.toString().contains(keyword, Qt::CaseInsensitive))
matched = true;
break; }
case QMetaType::Int: {
if (value.toInt() == keyword.toInt())
matched = true;
break; }
default:
break;
}
if (matched)
break;
}
if (matched) {
QFont font = ret.value<QFont>();
font.setBold(true);
ret = QVariant::fromValue(font);
}
}
}
return ret;
}
bool CustomFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
static int rowCount = 1;
if (source_row == 0)
rowCount = sourceModel()->rowCount() - 1;
setProgress(source_row * 100 / rowCount);
if (!d->filter.isEmpty()) {
static const auto mo = &GStreamerLogLine::staticMetaObject;
static const auto properties = mo->propertyCount();
static QHash<QString, int> name2column;
if (name2column.isEmpty()) {
for (int i = 0; i < properties; ++i) {
const auto property = mo->property(i);
name2column.insert(property.name(), i);
}
}
auto filters = d->filter.split(' ', Qt::SkipEmptyParts);
for (auto i = filters.length() - 1; i >= 0; i--) {
QString filter = filters.at(i);
int column = GStreamerLogModel::MessageColumn;
QString keyword = filter;
if (filter.contains(':')) {
const auto columnName = filter.section(':', 0, 0);
if (name2column.contains(columnName)) {
column = name2column.value(columnName);
keyword = filter.section(':', 1);
}
}
const auto value = sourceModel()->index(source_row, column, source_parent).data();
const auto property = mo->property(column);
switch (property.typeId()) {
case QMetaType::QString:
if (value.toString().contains(keyword, Qt::CaseInsensitive))
filters.removeAt(i);
break;
case QMetaType::Int:
if (value.toInt() == keyword.toInt())
filters.removeAt(i);
break;
default:
qWarning() << property.typeId() << "not supported";
break;
}
}
if (!filters.isEmpty()) return false;
}
return true;
}
QModelIndexList CustomFilterProxyModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
{
QModelIndexList ret;
bool wrap = flags & Qt::MatchWrap;
bool backword = flags & Qt::MatchRecursive; // abuse recursive flag for backwards search
bool timestampOnly = flags & Qt::MatchStartsWith; // abusing this flag
if (timestampOnly) {
const auto count = rowCount();
if (count == 0) return ret;
const auto index = d->findNearestTimestamp(0, count - 1, value.value<Timestamp>());
if (index.isValid())
ret << index;
return ret;
}
auto decrementIndex = [&] (const QModelIndex &index) -> QModelIndex {
if (!index.isValid())
return QModelIndex();
if (index.column() > 0)
return index.sibling(index.row(), index.column() - 1);
if (index.row() > 0)
return index.sibling(index.row() - 1, columnCount(index.parent()) - 1);
if (wrap)
return index.sibling(rowCount(index.parent()) - 1, columnCount(index.parent()) - 1);
return QModelIndex();
};
auto incrementIndex = [&] (const QModelIndex &index) -> QModelIndex {
if (!index.isValid())
return QModelIndex();
if (index.column() + 1 < columnCount(index.parent()))
return index.sibling(index.row(), index.column() + 1);
if (index.row() + 1 < rowCount(index.parent()))
return index.sibling(index.row() + 1, 0);
if (wrap)
return index.sibling(0, 0);
return QModelIndex();
};
auto nextIndex = [&](const QModelIndex &index) -> QModelIndex {
return backword ? decrementIndex(index) : incrementIndex(index);
};
auto next = [&](const QModelIndex &index) {
return (ret.size() < hits || hits == -1) && index.isValid();
};
QModelIndex index = nextIndex(start);
if (index == start)
return ret;
QString text = value.toString();
while (next(index) && index != start) {
const auto v = index.data(role);
if (v.toString().contains(text))
ret.append(index);
index = nextIndex(index);
}
return ret;
}