KPublicTransport

abstractquerymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "abstractquerymodel.h"
8#include "abstractquerymodel_p.h"
9#include "assetrepository_p.h"
10#include "datatypes/attributionutil_p.h"
11
12#include <KPublicTransport/Attribution>
13#include <KPublicTransport/Manager>
14#include <KPublicTransport/Reply>
15
16#include <QDebug>
17#include <QTimer>
18
19using namespace KPublicTransport;
20
21AbstractQueryModelPrivate::~AbstractQueryModelPrivate() = default;
22
23void AbstractQueryModelPrivate::setLoading(bool l)
24{
25 if (m_loading == l) {
26 return;
27 }
28 m_loading = l;
29 Q_EMIT q_ptr->loadingChanged();
30}
31
32void AbstractQueryModelPrivate::setErrorMessage(const QString &msg)
33{
34 if (m_errorMessage == msg) {
35 return;
36 }
37 m_errorMessage = msg;
38 Q_EMIT q_ptr->errorMessageChanged();
39}
40
41void AbstractQueryModelPrivate::monitorReply(Reply *reply)
42{
43 m_reply = reply;
44 QObject::connect(reply, &Reply::finished, q_ptr, [this, reply]() {
45 setLoading(false);
46 reply->deleteLater();
47 m_reply = nullptr;
49 AttributionUtil::merge(m_attributions, std::move(reply->takeAttributions()));
50 Q_EMIT q_ptr->attributionsChanged();
51 } else {
53 }
54 });
55}
56
57void AbstractQueryModelPrivate::query()
58{
59 if (!m_manager) {
60 return;
61 }
62
63 q_ptr->cancel();
64 m_queryTimer.start(m_queryDelay);
65}
66
67
68AbstractQueryModel::AbstractQueryModel(AbstractQueryModelPrivate* dd, QObject* parent)
69 : QAbstractListModel(parent)
70 , d_ptr(dd)
71{
72 d_ptr->q_ptr = this;
73
74 d_ptr->m_queryTimer.setSingleShot(true);
75 connect(&d_ptr->m_queryTimer, &QTimer::timeout, this, [this]() {
76 clear();
77 d_ptr->doQuery();
78 });
79
80 connect(AssetRepository::instance(), &AssetRepository::downloadFinished, this, [this]() {
81 const auto rows = rowCount();
82 if (rows > 0) {
83 Q_EMIT dataChanged(index(0, 0), index(rows - 1, 0));
84 }
85 });
86}
87
88AbstractQueryModel::~AbstractQueryModel() = default;
89
91{
92 return d_ptr->m_manager;
93}
94
95void AbstractQueryModel::setManager(Manager *mgr)
96{
97 if (d_ptr->m_manager == mgr) {
98 return;
99 }
100
101 d_ptr->m_manager = mgr;
102 Q_EMIT managerChanged();
103 d_ptr->query();
104}
105
106bool AbstractQueryModel::isLoading() const
107{
108 return d_ptr->m_loading;
109}
110
112{
113 return d_ptr->m_errorMessage;
114}
115
116const std::vector<Attribution>& AbstractQueryModel::attributions() const
117{
118 return d_ptr->m_attributions;
119}
120
121QVariantList AbstractQueryModel::attributionsVariant() const
122{
123 QVariantList l;
124 l.reserve(d_ptr->m_attributions.size());
125 std::transform(d_ptr->m_attributions.begin(), d_ptr->m_attributions.end(), std::back_inserter(l), [](const auto &attr) { return QVariant::fromValue(attr); });
126 return l;
127}
128
130{
131 d_ptr->setLoading(false);
132 delete d_ptr->m_reply;
133 d_ptr->m_reply = nullptr;
134}
135
137{
138 cancel();
139 if (rowCount() > 0) {
141 d_ptr->doClearResults();
143 }
144
145 if (!d_ptr->m_attributions.empty()) {
146 d_ptr->m_attributions.clear();
147 Q_EMIT attributionsChanged();
148 }
149
150 d_ptr->setErrorMessage({});
151}
152
153#include "moc_abstractquerymodel.moc"
QVariantList attributions
Attributions for the provided data.
Q_INVOKABLE void cancel()
Cancel ongoing query operations, but keep the results that are already there.
Q_INVOKABLE void clear()
Cancel any ongoing query operations, and clear the results.
QString errorMessage
Contains the error message if all backends failed to provide a result.
KPublicTransport::Manager * manager
Sets the KPublicTransport::Manager instance.
Query response base class.
Definition reply.h:25
void finished()
Emitted whenever the corresponding search has been completed.
QString errorString() const
Textual error message.
Definition reply.cpp:54
std::vector< Attribution > && takeAttributions()
Returns the attributions for the provided data for moving them elsewhere.
Definition reply.cpp:89
Error error() const
Error code.
Definition reply.cpp:46
@ NoError
Nothing went wrong.
Definition reply.h:32
KCRASH_EXPORT void setErrorMessage(const QString &message)
Query operations and data types for accessing realtime public transport information from online servi...
virtual int rowCount(const QModelIndex &parent) const const=0
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.