Make searching incremental to allow some userinteraction still
This commit is contained in:
parent
2a4eac04b9
commit
561085ef9d
@ -4,12 +4,59 @@
|
|||||||
|
|
||||||
#include "TimelineFilter.h"
|
#include "TimelineFilter.h"
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QEvent>
|
||||||
|
|
||||||
#include "Logging.h"
|
#include "Logging.h"
|
||||||
|
|
||||||
|
static int FilterRole = Qt::UserRole * 3;
|
||||||
|
|
||||||
|
static QEvent::Type
|
||||||
|
getFilterEventType()
|
||||||
|
{
|
||||||
|
static QEvent::Type t = static_cast<QEvent::Type>(QEvent::registerEventType());
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
TimelineFilter::TimelineFilter(QObject *parent)
|
TimelineFilter::TimelineFilter(QObject *parent)
|
||||||
: QSortFilterProxyModel(parent)
|
: QSortFilterProxyModel(parent)
|
||||||
{
|
{
|
||||||
setDynamicSortFilter(true);
|
setDynamicSortFilter(true);
|
||||||
|
setFilterRole(FilterRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TimelineFilter::startFiltering()
|
||||||
|
{
|
||||||
|
incrementalSearchIndex = 0;
|
||||||
|
continueFiltering();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TimelineFilter::continueFiltering()
|
||||||
|
{
|
||||||
|
if (auto s = source(); s && s->rowCount() > incrementalSearchIndex) {
|
||||||
|
auto ev = new QEvent(getFilterEventType());
|
||||||
|
QCoreApplication::postEvent(this, ev, Qt::LowEventPriority - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
TimelineFilter::event(QEvent *ev)
|
||||||
|
{
|
||||||
|
if (ev->type() == getFilterEventType()) {
|
||||||
|
int orgIndex = incrementalSearchIndex;
|
||||||
|
incrementalSearchIndex += 30;
|
||||||
|
|
||||||
|
if (auto s = source(); s) {
|
||||||
|
s->dataChanged(s->index(orgIndex),
|
||||||
|
s->index(std::min(incrementalSearchIndex, s->rowCount() - 1)),
|
||||||
|
{FilterRole});
|
||||||
|
continueFiltering();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return QSortFilterProxyModel::event(ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -18,10 +65,10 @@ TimelineFilter::setThreadId(const QString &t)
|
|||||||
nhlog::ui()->debug("Filtering by thread '{}'", t.toStdString());
|
nhlog::ui()->debug("Filtering by thread '{}'", t.toStdString());
|
||||||
if (this->threadId != t) {
|
if (this->threadId != t) {
|
||||||
this->threadId = t;
|
this->threadId = t;
|
||||||
invalidateFilter();
|
|
||||||
|
|
||||||
fetchMore({});
|
|
||||||
emit threadIdChanged();
|
emit threadIdChanged();
|
||||||
|
startFiltering();
|
||||||
|
fetchMore({});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,10 +78,10 @@ TimelineFilter::setContentFilter(const QString &c)
|
|||||||
nhlog::ui()->debug("Filtering by content '{}'", c.toStdString());
|
nhlog::ui()->debug("Filtering by content '{}'", c.toStdString());
|
||||||
if (this->contentFilter != c) {
|
if (this->contentFilter != c) {
|
||||||
this->contentFilter = c;
|
this->contentFilter = c;
|
||||||
invalidateFilter();
|
|
||||||
|
|
||||||
fetchMore({});
|
|
||||||
emit contentFilterChanged();
|
emit contentFilterChanged();
|
||||||
|
startFiltering();
|
||||||
|
fetchMore({});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,11 +99,25 @@ TimelineFilter::fetchAgain()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TimelineFilter::sourceDataChanged(const QModelIndex &topLeft,
|
||||||
|
const QModelIndex &bottomRight,
|
||||||
|
const QVector<int> &roles)
|
||||||
|
{
|
||||||
|
if (!roles.contains(TimelineModel::Roles::Body) && !roles.contains(TimelineModel::ThreadId))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (auto s = source()) {
|
||||||
|
s->dataChanged(topLeft, bottomRight, {FilterRole});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
TimelineFilter::setSource(TimelineModel *s)
|
TimelineFilter::setSource(TimelineModel *s)
|
||||||
{
|
{
|
||||||
if (auto orig = this->source(); orig != s) {
|
if (auto orig = this->source(); orig != s) {
|
||||||
cachedCount = 0;
|
cachedCount = 0;
|
||||||
|
incrementalSearchIndex = 0;
|
||||||
|
|
||||||
if (orig) {
|
if (orig) {
|
||||||
disconnect(orig,
|
disconnect(orig,
|
||||||
@ -64,6 +125,7 @@ TimelineFilter::setSource(TimelineModel *s)
|
|||||||
this,
|
this,
|
||||||
&TimelineFilter::currentIndexChanged);
|
&TimelineFilter::currentIndexChanged);
|
||||||
disconnect(orig, &TimelineModel::fetchedMore, this, &TimelineFilter::fetchAgain);
|
disconnect(orig, &TimelineModel::fetchedMore, this, &TimelineFilter::fetchAgain);
|
||||||
|
disconnect(orig, &TimelineModel::dataChanged, this, &TimelineFilter::sourceDataChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->setSourceModel(s);
|
this->setSourceModel(s);
|
||||||
@ -71,7 +133,13 @@ TimelineFilter::setSource(TimelineModel *s)
|
|||||||
connect(s, &TimelineModel::currentIndexChanged, this, &TimelineFilter::currentIndexChanged);
|
connect(s, &TimelineModel::currentIndexChanged, this, &TimelineFilter::currentIndexChanged);
|
||||||
connect(
|
connect(
|
||||||
s, &TimelineModel::fetchedMore, this, &TimelineFilter::fetchAgain, Qt::QueuedConnection);
|
s, &TimelineModel::fetchedMore, this, &TimelineFilter::fetchAgain, Qt::QueuedConnection);
|
||||||
|
connect(s,
|
||||||
|
&TimelineModel::dataChanged,
|
||||||
|
this,
|
||||||
|
&TimelineFilter::sourceDataChanged,
|
||||||
|
Qt::QueuedConnection);
|
||||||
|
|
||||||
|
incrementalSearchIndex = 0;
|
||||||
emit sourceChanged();
|
emit sourceChanged();
|
||||||
invalidateFilter();
|
invalidateFilter();
|
||||||
}
|
}
|
||||||
@ -104,6 +172,9 @@ TimelineFilter::currentIndex() const
|
|||||||
bool
|
bool
|
||||||
TimelineFilter::filterAcceptsRow(int source_row, const QModelIndex &) const
|
TimelineFilter::filterAcceptsRow(int source_row, const QModelIndex &) const
|
||||||
{
|
{
|
||||||
|
if (source_row > incrementalSearchIndex)
|
||||||
|
return true;
|
||||||
|
|
||||||
if (threadId.isEmpty() && contentFilter.isEmpty())
|
if (threadId.isEmpty() && contentFilter.isEmpty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -39,6 +39,8 @@ public:
|
|||||||
return data(index(i, 0), role);
|
return data(index(i, 0), role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool event(QEvent *ev) override;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void threadIdChanged();
|
void threadIdChanged();
|
||||||
void contentFilterChanged();
|
void contentFilterChanged();
|
||||||
@ -47,11 +49,17 @@ signals:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void fetchAgain();
|
void fetchAgain();
|
||||||
|
void sourceDataChanged(const QModelIndex &topLeft,
|
||||||
|
const QModelIndex &bottomRight,
|
||||||
|
const QVector<int> &roles);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
|
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void startFiltering();
|
||||||
|
void continueFiltering();
|
||||||
|
|
||||||
QString threadId, contentFilter;
|
QString threadId, contentFilter;
|
||||||
int cachedCount = 0;
|
int cachedCount = 0, incrementalSearchIndex = 0;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user