7#include "stopoverquerymodel.h"
8#include "abstractquerymodel_p.h"
10#include "datatypes/stopoverutil_p.h"
12#include <KPublicTransport/Attribution>
13#include <KPublicTransport/Manager>
14#include <KPublicTransport/Stopover>
15#include <KPublicTransport/StopoverReply>
23class StopoverQueryModelPrivate :
public AbstractQueryModelPrivate
26 void doQuery()
override;
27 void doClearResults()
override;
28 void mergeResults(
const std::vector<Stopover> &newDepartures);
30 std::vector<Stopover> m_departures;
32 StopoverRequest m_request;
33 StopoverRequest m_nextRequest;
34 StopoverRequest m_prevRequest;
36 Q_DECLARE_PUBLIC(StopoverQueryModel)
40void StopoverQueryModelPrivate::doQuery()
42 Q_Q(StopoverQueryModel);
43 if (!m_manager || !m_request.isValid()) {
50 Q_EMIT q->canQueryPrevNextChanged();
52 auto reply = m_manager->queryStopover(m_request);
56 m_nextRequest = reply->nextRequest();
57 m_prevRequest = reply->previousRequest();
58 Q_EMIT q->canQueryPrevNextChanged();
62 mergeResults(reply->takeResult());
66void StopoverQueryModelPrivate::doClearResults()
71void StopoverQueryModelPrivate::mergeResults(
const std::vector<Stopover> &newDepartures)
73 Q_Q(StopoverQueryModel);
74 for (
const auto &dep : newDepartures) {
75 auto it = std::lower_bound(m_departures.begin(), m_departures.end(), dep, [
this](
const auto &lhs,
const auto &rhs) {
76 return StopoverUtil::timeLessThan(m_request, lhs, rhs);
80 while (it != m_departures.end() && StopoverUtil::timeEqual(m_request, dep, *it)) {
84 const auto row = std::distance(m_departures.begin(), it);
85 const auto idx = q->index(row, 0);
86 Q_EMIT q->dataChanged(idx, idx);
96 const auto row = std::distance(m_departures.begin(), it);
97 q->beginInsertRows({}, row, row);
98 m_departures.insert(it, dep);
104StopoverQueryModel::StopoverQueryModel(QObject *parent)
107 connect(
this, &AbstractQueryModel::loadingChanged,
this, &StopoverQueryModel::canQueryPrevNextChanged);
110StopoverQueryModel::~StopoverQueryModel() =
default;
114 Q_D(
const StopoverQueryModel);
120 Q_D(StopoverQueryModel);
128 Q_D(
const StopoverQueryModel);
129 return !d->m_loading && !d->m_departures.empty() && d->m_nextRequest.isValid();
134 Q_D(StopoverQueryModel);
136 qCWarning(
Log) <<
"Cannot query next journeys";
141 auto reply = d->m_manager->queryStopover(d->m_nextRequest);
142 d->monitorReply(reply);
144 Q_D(StopoverQueryModel);
146 d->m_nextRequest = reply->nextRequest();
148 d->m_nextRequest = {};
150 Q_EMIT canQueryPrevNextChanged();
153 Q_D(StopoverQueryModel);
154 d->mergeResults(reply->takeResult());
161 return !d->m_loading && !d->m_departures.empty() && d->m_prevRequest.isValid();
166 Q_D(StopoverQueryModel);
168 qCWarning(
Log) <<
"Cannot query previous journeys";
173 auto reply = d->m_manager->queryStopover(d->m_prevRequest);
174 d->monitorReply(reply);
176 Q_D(StopoverQueryModel);
178 d->m_prevRequest = reply->previousRequest();
180 d->m_prevRequest = {};
182 Q_EMIT canQueryPrevNextChanged();
185 Q_D(StopoverQueryModel);
186 d->mergeResults(reply->takeResult());
190int StopoverQueryModel::rowCount(
const QModelIndex& parent)
const
196 return d->m_departures.size();
199QVariant StopoverQueryModel::data(
const QModelIndex& index,
int role)
const
201 Q_D(
const StopoverQueryModel);
202 if (!
index.isValid()) {
214QHash<int, QByteArray> StopoverQueryModel::roleNames()
const
217 r.insert(DepartureRole,
"departure");
223 Q_D(
const StopoverQueryModel);
224 return d->m_departures;
227#include "moc_stopoverquerymodel.moc"
Common base class for query models, do not use directly.
void finished()
Emitted whenever the corresponding search has been completed.
void updated()
Emitted whenever new results are available, even before the search has been completed.
@ NoError
Nothing went wrong.
Model representing arrival or departure query results.
bool canQueryNext
Whether querying for later departures is possible.
KPublicTransport::StopoverRequest request
Specify the actual departure query.
Q_INVOKABLE void queryNext()
Search for later journeys.
bool canQueryPrevious
Whether querying for earlier journey is possible.
Q_INVOKABLE void queryPrevious()
Search for earlier journeys.
const std::vector< Stopover > & departures() const
The current model content.
Describes an arrival or departure search.
static Stopover merge(const Stopover &lhs, const Stopover &rhs)
Merge two departure instances.
static bool isSame(const Stopover &lhs, const Stopover &rhs)
Checks if to instances refer to the same departure (which does not necessarily mean they are exactly ...
Query operations and data types for accessing realtime public transport information from online servi...
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QHash< int, QByteArray > roleNames() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)