7#include "journeyquerymodel.h"
8#include "abstractquerymodel_p.h"
10#include "../datatypes/journeyutil_p.h"
12#include <KPublicTransport/Attribution>
13#include <KPublicTransport/Journey>
14#include <KPublicTransport/JourneyReply>
15#include <KPublicTransport/Manager>
22class JourneyQueryModelPrivate :
public AbstractQueryModelPrivate
25 void doQuery()
override;
26 void doClearResults()
override;
27 void mergeResults(
const std::vector<Journey> &newJourneys);
29 std::vector<Journey> m_journeys;
31 JourneyRequest m_request;
32 JourneyRequest m_nextRequest;
33 JourneyRequest m_prevRequest;
35 Q_DECLARE_PUBLIC(JourneyQueryModel)
39void JourneyQueryModelPrivate::doQuery()
41 Q_Q(JourneyQueryModel);
42 if (!m_manager || !m_request.isValid()) {
49 Q_EMIT q->canQueryPrevNextChanged();
51 auto reply = m_manager->queryJourney(m_request);
54 Q_Q(JourneyQueryModel);
56 m_nextRequest = reply->nextRequest();
57 m_prevRequest = reply->previousRequest();
58 Q_EMIT q->canQueryPrevNextChanged();
62 mergeResults(reply->takeResult());
66void JourneyQueryModelPrivate::doClearResults()
71void JourneyQueryModelPrivate::mergeResults(
const std::vector<Journey> &newJourneys)
73 Q_Q(JourneyQueryModel);
74 for (
const auto &jny : newJourneys) {
75 auto it = std::lower_bound(m_journeys.begin(), m_journeys.end(), jny, JourneyUtil::firstTransportDepartureLessThan);
78 while (it != m_journeys.end() && JourneyUtil::firstTransportDepartureEqual(jny, *it)) {
82 const auto row = std::distance(m_journeys.begin(), it);
83 const auto idx = q->index(row, 0);
84 Q_EMIT q->dataChanged(idx, idx);
94 const auto row = std::distance(m_journeys.begin(), it);
95 q->beginInsertRows({}, row, row);
96 m_journeys.insert(it, jny);
102JourneyQueryModel::JourneyQueryModel(QObject *parent)
105 connect(
this, &AbstractQueryModel::loadingChanged,
this, &JourneyQueryModel::canQueryPrevNextChanged);
108JourneyQueryModel::~JourneyQueryModel() =
default;
112 Q_D(
const JourneyQueryModel);
118 Q_D(JourneyQueryModel);
127 return !d->m_loading && !d->m_journeys.empty() && d->m_nextRequest.isValid();
132 Q_D(JourneyQueryModel);
134 qCWarning(
Log) <<
"Cannot query next journeys";
139 auto reply = d->m_manager->queryJourney(d->m_nextRequest);
140 d->monitorReply(reply);
142 Q_D(JourneyQueryModel);
144 d->m_nextRequest = reply->nextRequest();
146 d->m_nextRequest = {};
148 Q_EMIT canQueryPrevNextChanged();
151 Q_D(JourneyQueryModel);
152 d->mergeResults(reply->takeResult());
159 return !d->m_loading && !d->m_journeys.empty() && d->m_prevRequest.isValid();
164 Q_D(JourneyQueryModel);
166 qCWarning(
Log) <<
"Cannot query previous journeys";
171 auto reply = d->m_manager->queryJourney(d->m_prevRequest);
172 d->monitorReply(reply);
174 Q_D(JourneyQueryModel);
176 d->m_prevRequest = reply->previousRequest();
178 d->m_prevRequest = {};
180 Q_EMIT canQueryPrevNextChanged();
183 Q_D(JourneyQueryModel);
184 d->mergeResults(reply->takeResult());
188int JourneyQueryModel::rowCount(
const QModelIndex& parent)
const
194 return d->m_journeys.size();
197QVariant JourneyQueryModel::data(
const QModelIndex& index,
int role)
const
199 Q_D(
const JourneyQueryModel);
200 if (!
index.isValid()) {
204 const auto jny = d->m_journeys[
index.row()];
208 case ScheduledDepartureTime:
209 return jny.scheduledDepartureTime();
210 case ScheduledArrivalTime:
211 return jny.scheduledArrivalTime();
213 return jny.scheduledDepartureTime().date();
219QHash<int, QByteArray> JourneyQueryModel::roleNames()
const
222 r.insert(JourneyRole,
"journey");
223 r.insert(ScheduledDepartureTime,
"scheduledDepartureTime");
224 r.insert(ScheduledArrivalTime,
"scheduledArrivalTime");
231 Q_D(
const JourneyQueryModel);
232 return d->m_journeys;
235#include "moc_journeyquerymodel.cpp"
Common base class for query models, do not use directly.
Model representing journey query results.
void setRequest(const JourneyRequest &req)
Specify the actual journey query.
bool canQueryPrevious
Whether querying for earlier journey is possible.
bool canQueryNext
Whether querying for later journeys is possible.
const std::vector< Journey > & journeys() const
The current model content.
KPublicTransport::JourneyRequest request
Specify the requested journey.
@ ScheduledDepartureDate
Useful for creating sections.
Q_INVOKABLE void queryPrevious()
Search for earlier journeys.
Q_INVOKABLE void queryNext()
Search for later journeys.
Describes a journey search.
static bool isSame(const Journey &lhs, const Journey &rhs)
Checks if two instances refer to the same journey (which does not necessarily mean they are exactly e...
static Journey merge(const Journey &lhs, const Journey &rhs)
Merge two instances.
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.
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)