diff --git a/src/publictransport/datatypes/departure.cpp b/src/publictransport/datatypes/departure.cpp index fe04dbb..00004a7 100644 --- a/src/publictransport/datatypes/departure.cpp +++ b/src/publictransport/datatypes/departure.cpp @@ -1,79 +1,86 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "departure.h" #include "datatypes_p.h" #include using namespace KPublicTransport; namespace KPublicTransport { class DeparturePrivate : public QSharedData { public: QDateTime scheduledTime; QDateTime actualTime; Route route; Location stopPoint; }; } KPUBLICTRANSPORT_MAKE_GADGET(Departure) QDateTime Departure::scheduledTime() const { return d->scheduledTime; } void Departure::setScheduledTime(const QDateTime &scheduledTime) { d.detach(); d->scheduledTime = scheduledTime; } QDateTime Departure::actualTime() const { return d->actualTime; } void Departure::setActualTime(const QDateTime &actualTime) { d.detach(); d->actualTime = actualTime; } +bool Departure::hasRealTime() const +{ + return d->actualTime.isValid(); +} + Route Departure::route() const { return d->route; } void Departure::setRoute(const Route &route) { d.detach(); d->route = route; } Location Departure::stopPoint() const { return d->stopPoint; } void Departure::setStopPoint(const Location &stopPoint) { d.detach(); d->stopPoint = stopPoint; } + +#include "moc_departure.cpp" diff --git a/src/publictransport/datatypes/departure.h b/src/publictransport/datatypes/departure.h index c4f5790..74a369b 100644 --- a/src/publictransport/datatypes/departure.h +++ b/src/publictransport/datatypes/departure.h @@ -1,65 +1,68 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KPUBLICTRANSPORT_DEPARTURE_H #define KPUBLICTRANSPORT_DEPARTURE_H #include "datatypes.h" #include "line.h" #include "location.h" class QDateTime; namespace KPublicTransport { class DeparturePrivate; /** Information about a departure of a vehicle at a stop area. */ class Departure { KPUBLICTRANSPORT_GADGET(Departure) /** Planned departure time. */ Q_PROPERTY(QDateTime scheduledTime READ scheduledTime WRITE setScheduledTime) /** Actual departure time, if available. * Set to invalid to indicate real-time data is not available. */ Q_PROPERTY(QDateTime actualTime READ actualTime WRITE setActualTime) /** @c true if this has real-time data. */ - Q_PROPERTY(bool isRealTime READ isRealTime STORED false) + Q_PROPERTY(bool hasRealTime READ hasRealTime STORED false) /** The departing route. */ Q_PROPERTY(KPublicTransport::Route route READ route WRITE setRoute) /** The stop point of this departure. */ Q_PROPERTY(KPublicTransport::Location stopPoint READ stopPoint WRITE setStopPoint) public: QDateTime scheduledTime() const; void setScheduledTime(const QDateTime &scheduledTime); QDateTime actualTime() const; void setActualTime(const QDateTime &actualTime); + + bool hasRealTime() const; + Route route() const; void setRoute(const Route &route); Location stopPoint() const; void setStopPoint(const Location &stopPoint); }; } Q_DECLARE_METATYPE(KPublicTransport::Departure) #endif // KPUBLICTRANSPORT_DEPARTURE_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 62568d3..a7f7a4c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,8 @@ add_executable(pkpassviewer pkpassviewer.cpp pkpassviewer.qrc) target_link_libraries(pkpassviewer itinerary) +add_executable(departurequery departurequery.cpp departurequery.qrc) +target_link_libraries(departurequery KPublicTransport Qt5::Quick) + add_executable(journeyquery journeyquery.cpp journeyquery.qrc) target_link_libraries(journeyquery KPublicTransport Qt5::Quick) diff --git a/tests/departurequery.cpp b/tests/departurequery.cpp new file mode 100644 index 0000000..e5c5c33 --- /dev/null +++ b/tests/departurequery.cpp @@ -0,0 +1,115 @@ +/* + Copyright (C) 2018 Volker Krause + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +using namespace KPublicTransport; + +class QueryManager : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged) + Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) +public: + QueryManager() + { + ptMgr.setNetworkAccessManager(&nam); + } + + Q_INVOKABLE void queryDeparture(double fromLat, double fromLon) + { + engine->rootContext()->setContextProperty(QStringLiteral("_departures"), QVariantList()); + m_loading = true; + emit loadingChanged(); + m_errorMsg.clear(); + emit errorMessageChanged(); + + Location from; + from.setCoordinate(fromLat, fromLon); + + auto reply = ptMgr.queryDeparture(DepartureRequest(from)); + QObject::connect(reply, &DepartureReply::finished, [reply, this]{ + m_loading = false; + emit loadingChanged(); + + if (reply->error() == DepartureReply::NoError) { + const auto res = reply->departures(); + QVariantList l; + l.reserve(res.size()); + std::transform(res.begin(), res.end(), std::back_inserter(l), [](const auto &journey) { return QVariant::fromValue(journey); }); + engine->rootContext()->setContextProperty(QStringLiteral("_departures"), l); + + for (const auto &departure : res) { + qDebug() << departure.stopPoint().name() << departure.route().line().name() << departure.route().direction() << departure.scheduledTime(); + } + } else { + m_errorMsg = reply->errorString(); + emit errorMessageChanged(); + } + }); + } + + bool loading() const { return m_loading; } + QString errorMessage() const { return m_errorMsg; } + + QQmlEngine *engine = nullptr; + +signals: + void loadingChanged(); + void errorMessageChanged(); + +private: + QNetworkAccessManager nam; + Manager ptMgr; + QString m_errorMsg; + bool m_loading = false; +}; + +int main(int argc, char **argv) +{ + QCoreApplication::setApplicationName(QStringLiteral("departurequery")); + QCoreApplication::setOrganizationName(QStringLiteral("KDE")); + QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org")); + + QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); + QGuiApplication app(argc, argv); + + qmlRegisterUncreatableType("org.kde.kpublictransport", 1, 0, "Line", {}); + + QueryManager mgr; + QQmlApplicationEngine engine; + mgr.engine = &engine; + engine.rootContext()->setContextProperty(QStringLiteral("_queryMgr"), &mgr); + engine.load(QStringLiteral("qrc:/departurequery.qml")); + return app.exec(); +} + +#include "departurequery.moc" diff --git a/tests/departurequery.qml b/tests/departurequery.qml new file mode 100644 index 0000000..22072e6 --- /dev/null +++ b/tests/departurequery.qml @@ -0,0 +1,115 @@ +/* + Copyright (C) 2018 Volker Krause + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +import QtQuick 2.5 +import QtQuick.Layouts 1.1 +import QtQuick.Controls 2.1 as QQC2 +import org.kde.kirigami 2.0 as Kirigami +import org.kde.kpublictransport 1.0 + +Kirigami.ApplicationWindow { + title: "Departure Query" + reachableModeEnabled: false + + width: 480 + height: 720 + + pageStack.initialPage: departureQueryPage + + ListModel { + id: exampleModel + ListElement { name: "CDG"; lat: 2.57110; lon: 49.00406 } + ListElement { name: "Paris Gare de Lyon"; lat: 2.37708; lon: 48.84388 } + ListElement { name: "ZRH"; lat: 8.56275; lon: 47.45050 } + ListElement { name: "Randa"; lat: 7.78315; lon: 46.09901 } + ListElement { name: "Brussels Gare de Midi"; lat: 4.33620; lon: 50.83588 } + ListElement { name: "Fosdem"; lat: 4.38116; lon: 50.81360 } + ListElement { name: "VIE"; lat: 16.56312; lon: 48.12083 } + ListElement { name: "Akademy 2018 Accomodation"; lat: 16.37859; lon: 48.18282 } + ListElement { name: "Akademy 2018 BBQ"; lat: 16.43191; lon: 48.21612 } + ListElement { name: "LEI"; lat: -2.37251; lon: 36.84774; } + ListElement { name: "Akademy 2017 Accomodation"; lat: -2.44788; lon: 36.83731 } + ListElement { name: "Akademy 2017 Venue"; lat: -2.40377; lon: 36.82784 } + ListElement { name: "TXL"; lat: 13.29281; lon: 52.55420; } + ListElement { name: "Akademy 2016 Venue"; lat: 13.41644; lon: 52.52068 } + ListElement { name: "SXF"; lat: 13.51870; lon: 52.38841 } + ListElement { name: "Brno central station"; lat: 16.61287; lon: 49.19069 } + ListElement { name: "Akademy 2014 venue"; lat: 16.57564; lon: 49.22462 } + } + + Component { + id: departureQueryPage + Kirigami.Page { + ColumnLayout { + anchors.fill: parent + QQC2.ComboBox { + id: exampleSelector + Layout.fillWidth: true + model: exampleModel + textRole: "name" + onCurrentIndexChanged: { + var obj = exampleModel.get(currentIndex); + _queryMgr.queryDeparture(obj.lat, obj.lon); + } + } + + ListView { + Layout.fillHeight: true + Layout.fillWidth: true + model: _departures + delegate: Item { + implicitHeight: delegateLayout.implicitHeight + implicitWidth: delegateLayout.implicitWidth + ColumnLayout { + id: delegateLayout + Text { + text: "From: " + modelData.stopPoint.name + } + Text { + text: "Line: " + modelData.route.line.modeString + " " + modelData.route.line.name + " to " + modelData.route.direction + } + Text { + text: "Time: " + modelData.scheduledTime + } + } + Rectangle { + anchors.left: parent.left + anchors.leftMargin: -8 + height: parent.height + width: 4 + color: modelData.route.line.color + } + } + + QQC2.BusyIndicator { + anchors.centerIn: parent + running: _queryMgr.loading + } + + QQC2.Label { + anchors.centerIn: parent + width: parent.width + text: _queryMgr.errorMessage + color: Kirigami.Theme.negativeTextColor + wrapMode: Text.Wrap + } + } + + } + } + } +} diff --git a/tests/departurequery.qrc b/tests/departurequery.qrc new file mode 100644 index 0000000..b91d47b --- /dev/null +++ b/tests/departurequery.qrc @@ -0,0 +1,5 @@ + + + departurequery.qml + +