Commit 20ea201c authored by Volker Krause's avatar Volker Krause

Use a real model for the journey results

parent 1258e37e
......@@ -47,7 +47,7 @@ set(itinerary_app_srcs
${qml_srcs}
brightnessmanager.cpp
livedatamanager.cpp
journeyquerycontroller.cpp
journeyquerymodel.cpp
publictransport.cpp
)
if (ANDROID)
......
......@@ -29,7 +29,7 @@ Kirigami.ScrollablePage {
id: root
title: i18n("Alternative Connections")
Component.onCompleted: _journeyQueryController.queryJourney(batchId);
Component.onCompleted: _journeyQueryModel.queryJourney(batchId);
Component {
id: sectionDelegate
......@@ -147,7 +147,7 @@ Kirigami.ScrollablePage {
id: journeyDelegate
Kirigami.Card {
id: top
property var journey: modelData
property var journey: model.journey
header: Rectangle {
id: headerBackground
......@@ -201,17 +201,17 @@ Kirigami.ScrollablePage {
anchors.fill: parent
clip: true
delegate: journeyDelegate
model: _journeyQueryController.journeys
model: _journeyQueryModel
QQC2.BusyIndicator {
anchors.centerIn: parent
running: _journeyQueryController.loading
running: _journeyQueryModel.loading
}
QQC2.Label {
anchors.centerIn: parent
width: parent.width
text: _journeyQueryController.errorMessage
text: _journeyQueryModel.errorMessage
color: Kirigami.Theme.negativeTextColor
wrapMode: Text.Wrap
}
......
......@@ -15,7 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "journeyquerycontroller.h"
#include "journeyquerymodel.h"
#include "logging.h"
#include "reservationmanager.h"
#include "publictransport.h"
......@@ -32,44 +32,38 @@
using namespace KItinerary;
JourneyQueryController::JourneyQueryController(QObject *parent)
: QObject(parent)
JourneyQueryModel::JourneyQueryModel(QObject *parent)
: QAbstractListModel(parent)
{
}
JourneyQueryController::~JourneyQueryController() = default;
JourneyQueryModel::~JourneyQueryModel() = default;
void JourneyQueryController::setReservationManager(ReservationManager *mgr)
void JourneyQueryModel::setReservationManager(ReservationManager *mgr)
{
m_resMgr = mgr;
}
void JourneyQueryController::setPublicTransportManager(KPublicTransport::Manager *mgr)
void JourneyQueryModel::setPublicTransportManager(KPublicTransport::Manager *mgr)
{
m_ptMgr = mgr;
}
bool JourneyQueryController::isLoading() const
bool JourneyQueryModel::isLoading() const
{
return m_isLoading;
}
QString JourneyQueryController::errorMessage() const
QString JourneyQueryModel::errorMessage() const
{
return m_errorMsg;
}
QVariantList JourneyQueryController::journeys() const
void JourneyQueryModel::queryJourney(const QString &batchId)
{
QVariantList l;
l.reserve(m_journeys.size());
std::transform(m_journeys.begin(), m_journeys.end(), std::back_inserter(l), [](const auto &journey) { return QVariant::fromValue(journey); });
return l;
}
void JourneyQueryController::queryJourney(const QString &batchId)
{
qDebug() << batchId;
beginResetModel();
m_journeys.clear();
endResetModel();
const auto res = m_resMgr->reservation(batchId);
if (!JsonLd::isA<TrainReservation>(res)) {
......@@ -91,11 +85,36 @@ void JourneyQueryController::queryJourney(const QString &batchId)
m_isLoading = false;
emit loadingChanged();
if (reply->error() == KPublicTransport::JourneyReply::NoError) {
beginResetModel();
m_journeys = reply->takeResult();
emit journeysChanged();
endResetModel();
} else {
m_errorMsg = reply->errorString();
emit errorMessageChanged();
}
});
}
int JourneyQueryModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_journeys.size();
}
QVariant JourneyQueryModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case JourneyRole:
return QVariant::fromValue(m_journeys[index.row()]);
}
return {};
}
QHash<int, QByteArray> JourneyQueryModel::roleNames() const
{
auto r = QAbstractListModel::roleNames();
r.insert(JourneyRole, "journey");
return r;
}
......@@ -15,10 +15,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef JOURNEYQUERYCONTROLLER_H
#define JOURNEYQUERYCONTROLLER_H
#ifndef JOURNEYQUERYMODEL_H
#define JOURNEYQUERYMODEL_H
#include <QObject>
#include <QAbstractListModel>
#include <QString>
#include <vector>
......@@ -31,16 +31,19 @@ class Manager;
class ReservationManager;
/** Alternative train connection query handling. */
class JourneyQueryController : public QObject
class JourneyQueryModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(bool loading READ isLoading NOTIFY loadingChanged)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
Q_PROPERTY(QVariantList journeys READ journeys NOTIFY journeysChanged)
enum Role {
JourneyRole = Qt::UserRole
};
public:
explicit JourneyQueryController(QObject *parent = nullptr);
~JourneyQueryController();
explicit JourneyQueryModel(QObject *parent = nullptr);
~JourneyQueryModel();
void setReservationManager(ReservationManager *mgr);
void setPublicTransportManager(KPublicTransport::Manager *mgr);
......@@ -49,12 +52,14 @@ public:
bool isLoading() const;
QString errorMessage() const;
QVariantList journeys() const;
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
Q_SIGNALS:
void loadingChanged();
void errorMessageChanged();
void journeysChanged();
private:
ReservationManager *m_resMgr;
......@@ -65,4 +70,4 @@ private:
};
#endif // JOURNEYQUERYCONTROLLER_H
#endif // JOURNEYQUERYMODEL_H
......@@ -22,7 +22,7 @@
#include "brightnessmanager.h"
#include "countryinformation.h"
#include "countrymodel.h"
#include "journeyquerycontroller.h"
#include "journeyquerymodel.h"
#include "livedatamanager.h"
#include "localizer.h"
#include "pkpassmanager.h"
......@@ -151,9 +151,9 @@ int main(int argc, char **argv)
liveDataMgr.setPollingEnabled(settings.queryLiveData());
QObject::connect(&settings, &Settings::queryLiveDataChanged, &liveDataMgr, &LiveDataManager::setPollingEnabled);
JourneyQueryController journeyQueryController;
journeyQueryController.setReservationManager(&resMgr);
journeyQueryController.setPublicTransportManager(&ptMgr);
JourneyQueryModel journeyQueryModel;
journeyQueryModel.setReservationManager(&resMgr);
journeyQueryModel.setPublicTransportManager(&ptMgr);
#ifndef Q_OS_ANDROID
QObject::connect(&service, &KDBusService::activateRequested, [&parser, &appController](const QStringList &args, const QString &workingDir) {
......@@ -220,7 +220,7 @@ int main(int argc, char **argv)
engine.rootContext()->setContextProperty(QStringLiteral("_weatherForecastManager"), &weatherForecastMgr);
engine.rootContext()->setContextProperty(QStringLiteral("_brightnessManager"), &brightnessManager);
engine.rootContext()->setContextProperty(QStringLiteral("_liveDataManager"), &liveDataMgr);
engine.rootContext()->setContextProperty(QStringLiteral("_journeyQueryController"), &journeyQueryController);
engine.rootContext()->setContextProperty(QStringLiteral("_journeyQueryModel"), &journeyQueryModel);
engine.load(QStringLiteral("qrc:/main.qml"));
handlePositionalArguments(&appController, parser.positionalArguments());
......
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