diff --git a/autotests/navitiaparsertest.cpp b/autotests/navitiaparsertest.cpp index bfd725c..3653a13 100644 --- a/autotests/navitiaparsertest.cpp +++ b/autotests/navitiaparsertest.cpp @@ -1,95 +1,113 @@ /* 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 class NavitiaParserTest : public QObject { Q_OBJECT private: QByteArray readFile(const char *fn) { QFile f(QString::fromUtf8(fn)); f.open(QFile::ReadOnly); return f.readAll(); } private slots: void initTestCase() { qputenv("TZ", "UTC"); } void testParseJourneys() { const auto res = KPublicTransport::NavitiaParser::parseJourneys(readFile(SOURCE_DIR "/data/navitia/journey-response.json")); QCOMPARE(res.size(), 4); { const auto journey = res[0]; QCOMPARE(journey.sections().size(), 6); - auto sec = journey.sections()[1]; + auto sec = journey.sections()[0]; + QCOMPARE(sec.mode(), KPublicTransport::JourneySection::Walking); + + sec = journey.sections()[1]; + QCOMPARE(sec.mode(), KPublicTransport::JourneySection::PublicTransport); QCOMPARE(sec.departureTime(), QDateTime({2018, 12, 2}, {22, 06})); QCOMPARE(sec.arrivalTime(), QDateTime({2018, 12, 2}, {22, 41})); + QCOMPARE(sec.from().name(), QStringLiteral("Aéroport CDG 2 TGV (Le Mesnil-Amelot)")); QCOMPARE(sec.from().latitude(), 49.0047f); + QCOMPARE(sec.to().name(), QStringLiteral("Châtelet les Halles (Paris)")); QCOMPARE(sec.to().longitude(), 2.34701f); QCOMPARE(sec.route().line().name(), QStringLiteral("B")); + QCOMPARE(sec.route().line().color(), QColor(123, 163, 220)); + QCOMPARE(sec.route().line().textColor(), QColor(255, 255, 255)); + + sec = journey.sections()[2]; + QCOMPARE(sec.mode(), KPublicTransport::JourneySection::Transfer); + + sec = journey.sections()[3]; + QCOMPARE(sec.mode(), KPublicTransport::JourneySection::Waiting); sec = journey.sections()[4]; QCOMPARE(sec.departureTime(), QDateTime({2018, 12, 2}, {22, 49})); QCOMPARE(sec.arrivalTime(), QDateTime({2018, 12, 2}, {22, 51})); QCOMPARE(sec.route().line().name(), QStringLiteral("A")); + QCOMPARE(sec.route().line().color(), QColor(QStringLiteral("#D1302F"))); + QCOMPARE(sec.route().line().textColor(), QColor(255, 255, 255)); + QCOMPARE(sec.from().name(), QStringLiteral("Châtelet les Halles (Paris)")); + QCOMPARE(sec.to().name(), QStringLiteral("Gare de Lyon RER A (Paris)")); } { const auto journey = res[1]; QCOMPARE(journey.sections().size(), 6); auto sec = journey.sections()[1]; QCOMPARE(sec.route().line().name(), QStringLiteral("B")); sec = journey.sections()[4]; QCOMPARE(sec.route().line().name(), QStringLiteral("65")); } { const auto journey = res[2]; QCOMPARE(journey.sections().size(), 6); auto sec = journey.sections()[1]; QCOMPARE(sec.route().line().name(), QStringLiteral("B")); sec = journey.sections()[4]; QCOMPARE(sec.route().line().name(), QStringLiteral("91")); } { const auto journey = res[3]; QCOMPARE(journey.sections().size(), 3); auto sec = journey.sections()[1]; QCOMPARE(sec.route().line().name(), QStringLiteral("DIRECT 4")); } } }; QTEST_GUILESS_MAIN(NavitiaParserTest) #include "navitiaparsertest.moc" diff --git a/src/publictransport/backends/navitiaparser.cpp b/src/publictransport/backends/navitiaparser.cpp index 7cb9415..bbbb6be 100644 --- a/src/publictransport/backends/navitiaparser.cpp +++ b/src/publictransport/backends/navitiaparser.cpp @@ -1,89 +1,104 @@ /* 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 "navitiaparser.h" #include +#include #include #include #include #include using namespace KPublicTransport; static Location parseLocation(const QJsonObject &obj) { Location loc; + loc.setName(obj.value(QLatin1String("name")).toString()); // TODO parse more fields const auto embObj = obj.value(obj.value(QLatin1String("embedded_type")).toString()).toObject(); const auto coord = embObj.value(QLatin1String("coord")).toObject(); loc.setCoordinate(coord.value(QLatin1String("lat")).toString().toDouble(), coord.value(QLatin1String("lon")).toString().toDouble()); return loc; } static JourneySection parseJourneySection(const QJsonObject &obj) { const auto displayInfo = obj.value(QLatin1String("display_informations")).toObject(); Line line; line.setName(displayInfo.value(QLatin1String("label")).toString()); - // TODO parse colors + line.setColor(QColor(QLatin1Char('#') + displayInfo.value(QLatin1String("color")).toString())); + line.setTextColor(QColor(QLatin1Char('#') + displayInfo.value(QLatin1String("text_color")).toString())); Route route; route.setDirection(displayInfo.value(QLatin1String("direction")).toString()); route.setLine(line); JourneySection section; section.setRoute(route); section.setDepartureTime(QDateTime::fromString(obj.value(QLatin1String("departure_date_time")).toString(), QStringLiteral("yyyyMMddTHHmmss"))); section.setArrivalTime(QDateTime::fromString(obj.value(QLatin1String("arrival_date_time")).toString(), QStringLiteral("yyyyMMddTHHmmss"))); section.setFrom(parseLocation(obj.value(QLatin1String("from")).toObject())); section.setTo(parseLocation(obj.value(QLatin1String("to")).toObject())); + + const auto typeStr = obj.value(QLatin1String("type")).toString(); + if (typeStr == QLatin1String("public_transport")) { + section.setMode(JourneySection::PublicTransport); + } else if (typeStr == QLatin1String("transfer")) { + section.setMode(JourneySection::Transfer); + } else if (typeStr == QLatin1String("street_network")) { + section.setMode(JourneySection::Walking); + } else if (typeStr == QLatin1String("waiting")) { + section.setMode(JourneySection::Waiting); + } + return section; } static Journey parseJourney(const QJsonObject &obj) { Journey journey; const auto secArray = obj.value(QLatin1String("sections")).toArray(); std::vector sections; sections.reserve(secArray.size()); for (const auto &v : secArray) { sections.push_back(parseJourneySection(v.toObject())); } journey.setSections(std::move(sections)); return journey; } std::vector NavitiaParser::parseJourneys(const QByteArray &data) { const auto topObj = QJsonDocument::fromJson(data).object(); const auto journeys = topObj.value(QLatin1String("journeys")).toArray(); std::vector res; res.reserve(journeys.size()); for (const auto &v : journeys) { res.push_back(parseJourney(v.toObject())); } return res; } diff --git a/src/publictransport/datatypes/journey.cpp b/src/publictransport/datatypes/journey.cpp index 11ee626..c11f612 100644 --- a/src/publictransport/datatypes/journey.cpp +++ b/src/publictransport/datatypes/journey.cpp @@ -1,112 +1,126 @@ /* 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 "journey.h" #include "datatypes_p.h" using namespace KPublicTransport; namespace KPublicTransport { class JourneySectionPrivate : public QSharedData { public: + JourneySection::Mode mode = JourneySection::Invalid; QDateTime departureTime; QDateTime arrivalTime; Location from; Location to; Route route; }; class JourneyPrivate : public QSharedData { public: std::vector sections; }; } KPUBLICTRANSPORT_MAKE_GADGET(JourneySection) +JourneySection::Mode JourneySection::mode() const +{ + return d->mode; +} + +void JourneySection::setMode(JourneySection::Mode mode) +{ + d.detach(); + d->mode = mode; +} + QDateTime JourneySection::departureTime() const { return d->departureTime; } void JourneySection::setDepartureTime(const QDateTime &dt) { d.detach(); d->departureTime = dt; } QDateTime JourneySection::arrivalTime() const { return d->arrivalTime; } void JourneySection::setArrivalTime(const QDateTime &dt) { d.detach(); d->arrivalTime = dt; } Location JourneySection::from() const { return d->from; } void JourneySection::setFrom(const Location &from) { d.detach(); d->from = from; } Location JourneySection::to() const { return d->to; } void JourneySection::setTo(const Location &to) { d.detach(); d->to = to; } Route JourneySection::route() const { return d->route; } void JourneySection::setRoute(const Route &route) { d.detach(); d->route = route; } KPUBLICTRANSPORT_MAKE_GADGET(Journey) const std::vector& Journey::sections() const { return d->sections; } void Journey::setSections(std::vector &§ions) { d.detach(); d->sections = std::move(sections); } + +#include "moc_journey.cpp" diff --git a/src/publictransport/datatypes/journey.h b/src/publictransport/datatypes/journey.h index 96c77d7..4732a77 100644 --- a/src/publictransport/datatypes/journey.h +++ b/src/publictransport/datatypes/journey.h @@ -1,81 +1,94 @@ /* 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_JOURNEY_H #define KPUBLICTRANSPORT_JOURNEY_H #include "datatypes.h" #include "line.h" #include "location.h" #include #include namespace KPublicTransport { class JourneySectionPrivate; /** A segment of a journey plan. */ class JourneySection { KPUBLICTRANSPORT_GADGET(JourneySection) + /** Mode of transport for this section. */ + Q_PROPERTY(Mode mode READ mode WRITE setMode) /** Departue time for this segment. */ Q_PROPERTY(QDateTime departureTime READ departureTime WRITE setDepartureTime) /** Arrival time for this segment. */ Q_PROPERTY(QDateTime arrivalTime READ arrivalTime WRITE setArrivalTime) /** Departure location of this segment. */ Q_PROPERTY(KPublicTransport::Location from READ from WRITE setFrom) /** Arrival location of this segment. */ Q_PROPERTY(KPublicTransport::Location to READ to WRITE setTo) /** Route to take on this segment. */ Q_PROPERTY(Route route READ route WRITE setRoute) // TODO: planned vs. expected times? public: + /** Mode of transport. */ + enum Mode { + Invalid, + PublicTransport, + Transfer, + Walking, + Waiting + }; + Q_ENUM(Mode) + Mode mode() const; + void setMode(Mode mode); QDateTime departureTime() const; void setDepartureTime(const QDateTime &dt); QDateTime arrivalTime() const; void setArrivalTime(const QDateTime &dt); Location from() const; void setFrom(const Location &from); Location to() const; void setTo(const Location &to); Route route() const; void setRoute(const Route &route); }; class JourneyPrivate; /** A journey plan. */ class Journey { KPUBLICTRANSPORT_GADGET(Journey) public: /** The journey sections. */ const std::vector& sections() const; /** Sets the journey sections. */ void setSections(std::vector &§ions); }; } Q_DECLARE_METATYPE(KPublicTransport::JourneySection) Q_DECLARE_METATYPE(KPublicTransport::Journey) #endif // KPUBLICTRANSPORT_JOURNEY_H diff --git a/src/publictransport/datatypes/line.cpp b/src/publictransport/datatypes/line.cpp index ade7a32..5cbf4ed 100644 --- a/src/publictransport/datatypes/line.cpp +++ b/src/publictransport/datatypes/line.cpp @@ -1,99 +1,101 @@ /* 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 "line.h" #include "datatypes_p.h" #include using namespace KPublicTransport; namespace KPublicTransport { class LinePrivate : public QSharedData { public: QString name; QColor color; QColor textColor; }; class RoutePrivate : public QSharedData { public: Line line; QString direction; }; } KPUBLICTRANSPORT_MAKE_GADGET(Line) QString Line::name() const { return d->name; } void Line::setName(const QString &name) { d.detach(); d->name = name; } QColor Line::color() const { return d->color; } void Line::setColor(const QColor &color) { d.detach(); d->color = color; } QColor Line::textColor() const { return d->textColor; } void Line::setTextColor(const QColor &textColor) { d.detach(); d->textColor = textColor; } KPUBLICTRANSPORT_MAKE_GADGET(Route) Line Route::line() const { return d->line; } void Route::setLine(const Line &line) { d.detach(); d->line = line; } QString Route::direction() const { return d->direction; } void Route::setDirection(const QString &direction) { d.detach(); d->direction = direction; } + +#include "moc_line.cpp" diff --git a/src/publictransport/datatypes/location.cpp b/src/publictransport/datatypes/location.cpp index 34a0c10..2582f0d 100644 --- a/src/publictransport/datatypes/location.cpp +++ b/src/publictransport/datatypes/location.cpp @@ -1,54 +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 . */ #include "location.h" #include "datatypes_p.h" #include using namespace KPublicTransport; namespace KPublicTransport { class LocationPrivate : public QSharedData { public: + QString name; float latitude = NAN; float longitude = NAN; }; } KPUBLICTRANSPORT_MAKE_GADGET(Location) +QString Location::name() const +{ + return d->name; +} + +void Location::setName(const QString &name) +{ + d.detach(); + d->name = name; +} + float Location::latitude() const { return d->latitude; } float Location::longitude() const { return d->longitude; } void Location::setCoordinate(float latitude, float longitude) { d.detach(); d->latitude = latitude; d->longitude = longitude; } + +#include "moc_location.cpp" diff --git a/src/publictransport/datatypes/location.h b/src/publictransport/datatypes/location.h index 1f77fd9..3c2e415 100644 --- a/src/publictransport/datatypes/location.h +++ b/src/publictransport/datatypes/location.h @@ -1,44 +1,49 @@ /* 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_LOCATION_H #define KPUBLICTRANSPORT_LOCATION_H #include "datatypes.h" namespace KPublicTransport { class LocationPrivate; /** A location. */ class Location { KPUBLICTRANSPORT_GADGET(Location) + /** Human-readable name of the location. */ + Q_PROPERTY(QString name READ name WRITE setName) - // TODO: type, coords, id, address, name + // TODO: type, coords, id, address public: + QString name() const; + void setName(const QString &name); + float latitude() const; float longitude() const; void setCoordinate(float latitude, float longitude); }; } Q_DECLARE_METATYPE(KPublicTransport::Location) #endif // KPUBLICTRANSPORT_LOCATION_H