Debian Codegraph @

src/timeline/TimelineFilter.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "TimelineFilter.h"
6
7 #include <QCoreApplication>
8 #include <QEvent>
9
10 #include "Logging.h"
11 #include "TimelineModel.h"
12
13 /// Searching currently can be done incrementally. For that we define a specific role to filter on
14 /// and then process that role in chunk. This is the `FilterRole`. Of course we need to then also
15 /// send proper update signals. Filtering then works as follows:
16 ///
17 /// - At first no range is filtered (incrementalSearchIndex == 0).
18 /// - Then, when filtering is requested, we start posting events to the
19 /// event loop with lower than low priority (low prio - 1). The only thing those events do is
20 /// increment the incrementalSearchIndex and emit a dataChanged for that range of events.
21 /// - This then causes those events to be reevaluated if they should be visible.
22
23 static int FilterRole = Qt::UserRole * 3;
24
25 static QEvent::Type
26 getFilterEventType()
27 {
28 static QEvent::Type t = static_cast<QEvent::Type>(QEvent::registerEventType());
29 return t;
30 }
31
32 TimelineFilter::TimelineFilter(QObject *parent)
33 : QSortFilterProxyModel(parent)
34 {
35 setDynamicSortFilter(true);
36 setFilterRole(FilterRole);
37 }
38
39 void
40 TimelineFilter::startFiltering()
41 {
42 incrementalSearchIndex = 0;
43 emit isFilteringChanged();
•44
45 #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
46 endFilterChange();
47 #else
48 invalidateFilter();
•49 #endif
50
51 beginResetModel();
52 endResetModel();
53
54 continueFiltering();
55 }
56
57 void
58 TimelineFilter::continueFiltering()
59 {
60 if (auto s = source(); s) {
61 if (s->rowCount() > incrementalSearchIndex) {
62 auto ev = new QEvent(getFilterEventType());
63 // request filtering a new chunk with lower than low priority.
64 QCoreApplication::postEvent(this, ev, Qt::LowEventPriority - 1);
65 } else {
66 // We reached the end, so fetch more!
67 fetchAgain();
68 }
69 }
70 }
71
72 bool
73 TimelineFilter::event(QEvent *ev)
74 {
75 if (ev->type() == getFilterEventType()) {
76 if (incrementalSearchIndex < std::numeric_limits<int>::max()) {
77 int orgIndex = incrementalSearchIndex;
78 // process the next 100 events by claiming their "filterrole" data has changed.
79 incrementalSearchIndex += 100;
80
81 if (auto s = source(); s) {
82 auto count = s->rowCount();
83 if (incrementalSearchIndex >= count) {
84 incrementalSearchIndex = std::numeric_limits<int>::max();
85 }
86 nhlog::ui()->debug("Filter progress {}/{}", incrementalSearchIndex, count);
87 s->dataChanged(s->index(orgIndex),
88 s->index(std::min(incrementalSearchIndex, count - 1)),
89 {FilterRole});
90
91 continueFiltering();
92 }
93 emit isFilteringChanged();
94 }
95 return true;
96 }
97 return QSortFilterProxyModel::event(ev);
98 }
99
100 void
101 TimelineFilter::setThreadId(const QString &t)
102 {
103 nhlog::ui()->debug("Filtering by thread '{}'", t.toStdString());
104 if (this->threadId != t) {
105 this->threadId = t;
106
107 emit threadIdChanged();
108 startFiltering();
109 fetchMore({});
110 }
111 }
112
113 void
114 TimelineFilter::setContentFilter(const QString &c)
115 {
116 nhlog::ui()->debug("Filtering by content '{}'", c.toStdString());
117 if (this->contentFilter != c) {
118 this->contentFilter = c;
119
120 emit contentFilterChanged();
121 startFiltering();
122 fetchMore({});
123 }
124 }
125
126 void
127 TimelineFilter::fetchAgain()
128 {
129 if (threadId.isEmpty() && contentFilter.isEmpty())
130 return;
131
132 if (auto s = source(); s && incrementalSearchIndex == std::numeric_limits<int>::max()) {
133 if (this->rowCount() == cachedCount && s->canFetchMore(QModelIndex()) &&
134 // If we already have the event id of the thread in the timeline and we are filtering by
135 // thread, we can stop fetching more messages In theory an event could have been edited
136 // earlier in the timeline into the thread. So in theory this check is insufficient and
137 // we should instead verify that all events referring to this thread are in the timeline
138 // instead of just the thread root, but only Nheko supports that atm and the check would
139 // be expensive.
140 // TODO(Nico): check that all thread referrencing events are in the timeline by also
141 // checking all edits inside the thread.
142 (threadId.isEmpty() || s->idToIndex(threadId) == -1))
143 s->fetchMore(QModelIndex());
144 else
145 cachedCount = this->rowCount();
146 }
147 }
148
149 void
150 TimelineFilter::sourceDataChanged(const QModelIndex &topLeft,
151 const QModelIndex &bottomRight,
152 const QVector<int> &roles)
153 {
154 if (!roles.contains(TimelineModel::Roles::Body) && !roles.contains(TimelineModel::ThreadId))
155 return;
156
157 if (auto s = source()) {
158 s->dataChanged(topLeft, bottomRight, {FilterRole});
159 }
160 }
161
162 void
163 TimelineFilter::setSource(TimelineModel *s)
164 {
165 if (auto orig = this->source(); orig != s) {
•166 #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
167 beginFilterChange();
168 #endif
169
170 cachedCount = 0;
171 incrementalSearchIndex = 0;
172
173 if (orig) {
174 disconnect(orig,
175 &TimelineModel::currentIndexChanged,
176 this,
177 &TimelineFilter::currentIndexChanged);
178 disconnect(orig, &TimelineModel::fetchedMore, this, &TimelineFilter::fetchAgain);
179 disconnect(orig, &TimelineModel::dataChanged, this, &TimelineFilter::sourceDataChanged);
180 }
181
182 this->setSourceModel(s);
183
184 if (s) {
185 connect(
186 s, &TimelineModel::currentIndexChanged, this, &TimelineFilter::currentIndexChanged);
187 connect(s,
188 &TimelineModel::fetchedMore,
189 this,
190 &TimelineFilter::fetchAgain,
191 Qt::QueuedConnection);
192 connect(s,
193 &TimelineModel::dataChanged,
194 this,
195 &TimelineFilter::sourceDataChanged,
196 Qt::QueuedConnection);
197 }
198
199 // reset the search index a second time just to be safe.
200 incrementalSearchIndex = 0;
201
202 emit sourceChanged();
203 emit isFilteringChanged();
•204
205 #if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
206 endFilterChange();
207 #else
208 invalidateFilter();
•209 #endif
210 }
211 }
212
213 TimelineModel *
214 TimelineFilter::source() const
215 {
216 return qobject_cast<TimelineModel *>(sourceModel());
217 }
218
219 void
220 TimelineFilter::setCurrentIndex(int idx)
221 {
222 // TODO: maybe send read receipt in thread timeline? Or not at all?
223 if (auto s = source()) {
224 s->setCurrentIndex(this->mapToSource(index(idx, 0)).row());
225 }
226 }
227
228 int
229 TimelineFilter::currentIndex() const
230 {
231 if (auto s = source())
232 return this->mapFromSource(s->index(s->currentIndex())).row();
233 else
234 return -1;
235 }
236
237 bool
238 TimelineFilter::isFiltering() const
239 {
240 return incrementalSearchIndex != std::numeric_limits<int>::max() &&
241 !(threadId.isEmpty() && contentFilter.isEmpty());
242 }
243
244 bool
245 TimelineFilter::filterAcceptsRow(int source_row, const QModelIndex &) const
246 {
247 // this chunk is still unfiltered.
248 if (source_row > incrementalSearchIndex)
249 return false;
250
251 if (threadId.isEmpty() && contentFilter.isEmpty())
252 return true;
253
254 if (auto s = sourceModel()) {
255 auto idx = s->index(source_row, 0);
256 if (!contentFilter.isEmpty() && !s->data(idx, TimelineModel::Body)
257 .toString()
258 .contains(contentFilter, Qt::CaseInsensitive)) {
259 return false;
260 }
261
262 if (threadId.isEmpty())
263 return true;
264
265 return s->data(idx, TimelineModel::EventId) == threadId ||
266 s->data(idx, TimelineModel::ThreadId) == threadId;
267 } else {
268 return true;
269 }
270 }
271
272 #include "moc_TimelineFilter.cpp"