Commit 17da55e1 authored by Volker Krause's avatar Volker Krause
Browse files

Add a configurable query delay for the location query model

This is useful when doing as-you-type completion, so we don't run a query
for each key stroke, just to immediately cancel it again.
parent fff2caa3
......@@ -56,16 +56,12 @@ void AbstractQueryModelPrivate::monitorReply(Reply *reply)
void AbstractQueryModelPrivate::query()
{
if (m_pendingQuery || !m_manager) {
if (!m_manager) {
return;
}
m_pendingQuery = true;
QTimer::singleShot(0, q_ptr, [this]() {
m_pendingQuery = false;
q_ptr->clear();
doQuery();
});
q_ptr->cancel();
m_queryTimer.start(m_queryDelay);
}
......@@ -75,6 +71,12 @@ AbstractQueryModel::AbstractQueryModel(AbstractQueryModelPrivate* dd, QObject* p
{
d_ptr->q_ptr = this;
d_ptr->m_queryTimer.setSingleShot(true);
connect(&d_ptr->m_queryTimer, &QTimer::timeout, this, [this]() {
clear();
d_ptr->doQuery();
});
connect(AssetRepository::instance(), &AssetRepository::downloadFinished, this, [this]() {
const auto rows = rowCount();
if (rows > 0) {
......
......@@ -8,6 +8,9 @@
#define KPUBLICTRANSPORT_ABSTRACTQUERYMODEL_P_H
#include <QAbstractListModel>
#include <QTimer>
#include <chrono>
namespace KPublicTransport {
......@@ -37,9 +40,11 @@ public:
std::vector<Attribution> m_attributions;
QTimer m_queryTimer;
std::chrono::milliseconds m_queryDelay = std::chrono::milliseconds(0);
QString m_errorMessage;
bool m_loading = false;
bool m_pendingQuery = false;
};
}
......
......@@ -129,6 +129,23 @@ void LocationQueryModel::setRequest(const LocationRequest &req)
d->query();
}
int LocationQueryModel::queryDelay() const
{
Q_D(const LocationQueryModel);
return d->m_queryDelay.count();
}
void LocationQueryModel::setQueryDelay(int ms)
{
Q_D(LocationQueryModel);
if (d->m_queryDelay == std::chrono::milliseconds(ms)) {
return;
}
d->m_queryDelay = std::chrono::milliseconds(ms);
Q_EMIT queryDelayChanged();
}
int LocationQueryModel::rowCount(const QModelIndex &parent) const
{
Q_D(const LocationQueryModel);
......
......@@ -27,6 +27,12 @@ class KPUBLICTRANSPORT_EXPORT LocationQueryModel : public AbstractQueryModel
/** Specify the actual location query. */
Q_PROPERTY(KPublicTransport::LocationRequest request READ request WRITE setRequest NOTIFY requestChanged)
/** Delay in milliseconds until the query is actually performed.
* Useful when expecting high frequency changes to the request, eg. for completion during typing.
* @default no delay
*/
Q_PROPERTY(int queryDelay READ queryDelay WRITE setQueryDelay NOTIFY queryDelayChanged)
public:
explicit LocationQueryModel(QObject *parent = nullptr);
~LocationQueryModel() override;
......@@ -34,6 +40,9 @@ public:
LocationRequest request() const;
void setRequest(const LocationRequest &req);
int queryDelay() const;
void setQueryDelay(int ms);
enum Roles {
LocationRole = Qt::UserRole
};
......@@ -48,6 +57,7 @@ public:
Q_SIGNALS:
void requestChanged();
void queryDelayChanged();
private:
friend class LocationQueryModelPrivate;
......
......@@ -40,12 +40,12 @@ int main(int argc, char **argv)
auto completer = new QCompleter;
auto model = new LocationQueryModel(completer);
model->setManager(&ptMgr);
model->setQueryDelay(250);
completer->setModel(model);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setFilterMode(Qt::MatchContains);
completer->setCompletionRole(Qt::DisplayRole);
lineEdit->setCompleter(completer);
// TODO compress queries with a short timer
QObject::connect(lineEdit, &QLineEdit::textEdited, model, [model](const auto &text) {
LocationRequest req;
req.setName(text);
......
......@@ -75,6 +75,7 @@ Kirigami.ApplicationWindow {
LocationQueryModel {
id: locationQueryModel
manager: ptMgr
queryDelay: 500
}
LocationHistoryModel {
id: locationHistoryModel
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment