File indexing completed on 2024-05-12 04:42:43

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPUBLICTRANSPORT_JOURNEY_H
0008 #define KPUBLICTRANSPORT_JOURNEY_H
0009 
0010 #include "datatypes.h"
0011 #include "disruption.h"
0012 #include "individualtransport.h"
0013 #include "line.h"
0014 #include "load.h"
0015 #include "location.h"
0016 #include "path.h"
0017 #include "platform.h"
0018 #include "vehicle.h"
0019 
0020 #include <QDateTime>
0021 
0022 #include <vector>
0023 
0024 namespace KPublicTransport {
0025 
0026 class JourneySectionPrivate;
0027 class RentalVehicle;
0028 class Stopover;
0029 
0030 /** A segment of a journey plan. */
0031 class KPUBLICTRANSPORT_EXPORT JourneySection
0032 {
0033     KPUBLICTRANSPORT_GADGET(JourneySection)
0034 
0035 public:
0036     /** Mode of transport.
0037      *  These categories are fairly coarse, for a more detailed break-down of PublicTransport see Line::Mode.
0038      */
0039     enum Mode {
0040         Invalid = 0,
0041         PublicTransport = 1,
0042         Transfer = 2,
0043         Walking = 4,
0044         Waiting = 8,
0045         RentedVehicle = 16, ///< free floating or dock-based rental bike service, electric scooters, car sharing services, ie. any vehicle you drive yourself but that isn't your own
0046         IndividualTransport = 32, ///< using your own vehicle (bike, car, etc).
0047     };
0048     Q_ENUM(Mode)
0049     Q_DECLARE_FLAGS(Modes, Mode)
0050     Q_FLAG(Modes)
0051 
0052     /** Mode of transport for this section. */
0053     KPUBLICTRANSPORT_PROPERTY(Mode, mode, setMode)
0054 
0055     /** Planned departure time. */
0056     KPUBLICTRANSPORT_PROPERTY(QDateTime, scheduledDepartureTime, setScheduledDepartureTime)
0057     /** Actual departure time, if available.
0058      *  Set to invalid to indicate real-time data is not available.
0059      */
0060     KPUBLICTRANSPORT_PROPERTY(QDateTime, expectedDepartureTime, setExpectedDepartureTime)
0061     /** @c true if this has real-time data. */
0062     Q_PROPERTY(bool hasExpectedDepartureTime READ hasExpectedDepartureTime STORED false)
0063     /** Difference to schedule in minutes. */
0064     Q_PROPERTY(int departureDelay READ departureDelay STORED false)
0065 
0066     /** Planned arrival time. */
0067     KPUBLICTRANSPORT_PROPERTY(QDateTime, scheduledArrivalTime, setScheduledArrivalTime)
0068     /** Actual arrival time, if available.
0069      *  Set to invalid to indicate real-time data is not available.
0070      */
0071     KPUBLICTRANSPORT_PROPERTY(QDateTime, expectedArrivalTime, setExpectedArrivalTime)
0072     /** @c true if this has real-time data. */
0073     Q_PROPERTY(bool hasExpectedArrivalTime READ hasExpectedArrivalTime STORED false)
0074     /** Difference to schedule in minutes. */
0075     Q_PROPERTY(int arrivalDelay READ arrivalDelay STORED false)
0076 
0077     /** Duration of the section in seconds. */
0078     Q_PROPERTY(int duration READ duration STORED false)
0079     /** Distance of the section in meter. */
0080     KPUBLICTRANSPORT_PROPERTY(int, distance, setDistance)
0081 
0082     /** Departure location of this segment. */
0083     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Location, from, setFrom)
0084     /** Arrival location of this segment. */
0085     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Location, to, setTo)
0086     /** Route to take on this segment. */
0087     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Route, route, setRoute)
0088 
0089     /** Planned departure platform. */
0090     KPUBLICTRANSPORT_PROPERTY(QString, scheduledDeparturePlatform, setScheduledDeparturePlatform)
0091     /** Actual departure platform, in case real-time information are available. */
0092     KPUBLICTRANSPORT_PROPERTY(QString, expectedDeparturePlatform, setExpectedDeparturePlatform)
0093     /** @c true if real-time platform information are available. */
0094     Q_PROPERTY(bool hasExpectedDeparturePlatform READ hasExpectedDeparturePlatform STORED false)
0095     /** @c true if we have real-time platform information and the platform changed. */
0096     Q_PROPERTY(bool departurePlatformChanged READ departurePlatformChanged STORED false)
0097 
0098     /** Planned arrival platform. */
0099     KPUBLICTRANSPORT_PROPERTY(QString, scheduledArrivalPlatform, setScheduledArrivalPlatform)
0100     /** Actual arrival platform, in case real-time information are available. */
0101     KPUBLICTRANSPORT_PROPERTY(QString, expectedArrivalPlatform, setExpectedArrivalPlatform)
0102     /** @c true if real-time platform information are available. */
0103     Q_PROPERTY(bool hasExpectedArrivalPlatform READ hasExpectedArrivalPlatform STORED false)
0104     /** @c true if we have real-time platform information and the platform changed. */
0105     Q_PROPERTY(bool arrivalPlatformChanged READ arrivalPlatformChanged STORED false)
0106 
0107     /** Disruption effect on this section, if any. */
0108     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Disruption::Effect, disruptionEffect, setDisruptionEffect)
0109     /** General human-readable notes on this service, e.g. details about a disruption. */
0110     KPUBLICTRANSPORT_PROPERTY(QStringList, notes, setNotes)
0111 
0112     /** Intermediate stops for consumption by QML. */
0113     Q_PROPERTY(QVariantList intermediateStops READ intermediateStopsVariant)
0114 
0115     /** All departure information represented as Stopover object. */
0116     Q_PROPERTY(KPublicTransport::Stopover departure READ departure STORED false)
0117     /** All arrival information represented as Stopover object. */
0118     Q_PROPERTY(KPublicTransport::Stopover arrival READ arrival STORED false)
0119 
0120     /** CO₂ emission during this journey section, in gram.
0121      *  In case the backend doesn't provide this value, it is estimated based on the
0122      *  distance travelled during this section and the mode of transport, based on
0123      *  average emission values from https://en.wikipedia.org/wiki/Environmental_impact_of_transport
0124      *  This value can be 0 (e.g. in case of walk or wait sections), or -1 if no
0125      *  information is available.
0126      */
0127     KPUBLICTRANSPORT_PROPERTY(int, co2Emission, setCo2Emission)
0128 
0129     /** Vehicle load information for this journey section.
0130      *  Contains LoadInfo objects for consumption by QML.
0131      */
0132     Q_PROPERTY(QVariantList loadInformation READ loadInformationVariant STORED false)
0133 
0134     /** Information about a rental vehicle, for sections using one. */
0135     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::RentalVehicle, rentalVehicle, setRentalVehicle)
0136 
0137     /** Movement path for this journey section.
0138      *  This can be navigation instructions for individual transport modes and transfers,
0139      *  or the path a public transport vehicle takes.
0140      */
0141     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Path, path, setPath)
0142 
0143     /** Vehicle coach layout information at departure. */
0144     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Vehicle, departureVehicleLayout, setDepartureVehicleLayout)
0145     /** Platform layout information at departure. */
0146     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Platform, departurePlatformLayout, setDeparturePlatformLayout)
0147     /** Vehicle coach layout information at arrival.
0148      *  Note that this does not necessarily need to be the same as departureVehicleLayout, as e.g. trains
0149      *  can be split up or merged along the way.
0150      */
0151     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Vehicle, arrivalVehicleLayout, setArrivalVehicleLayout)
0152     /** Platform layout information at arrival. */
0153     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Platform, arrivalPlatformLayout, setArrivalPlatformLayout)
0154 
0155     /** Individual transport details for sections using your own vehicle. */
0156     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::IndividualTransport, individualTransport, setIndividualTransport)
0157 
0158 public:
0159     bool hasExpectedDepartureTime() const;
0160     int departureDelay() const;
0161     bool hasExpectedArrivalTime() const;
0162     int arrivalDelay() const;
0163 
0164     int duration() const;
0165 
0166     bool hasExpectedDeparturePlatform() const;
0167     bool departurePlatformChanged() const;
0168     bool hasExpectedArrivalPlatform() const;
0169     bool arrivalPlatformChanged() const;
0170 
0171     /** Adds a note. This will check for duplicates and normalize the notes. */
0172     void addNote(const QString &note);
0173     void addNotes(const QStringList &notes);
0174 
0175     /** Intermediate stop-overs along this journey section.
0176      *  This does not include the departure and arrival stops, and might be empty on backends not providing this information.
0177      */
0178     const std::vector<Stopover>& intermediateStops() const;
0179     /** Moves the intermediate stops out of this object. */
0180     std::vector<Stopover>&& takeIntermediateStops();
0181     /** Set the intermediate stops. */
0182     void setIntermediateStops(std::vector<Stopover> &&stops);
0183 
0184     /** Returns the departure stopover of this journey section.
0185      *  This is the same information as accessible by individual properties,
0186      *  so this is mainly useful if you have to interface with code expecting a Stopover object.
0187      */
0188     Stopover departure() const;
0189     /**
0190      * Sets all departure properties from a given Stopover.
0191      * This effects location and time, but doesn't modify intermediate stops or paths.
0192      */
0193     void setDeparture(const Stopover &departure);
0194 
0195     /** Returns the arrival stopover of this journey section.
0196      *  This is the same information as accessible by individual properties,
0197      *  so this is mainly useful if you have to interface with code expecting a Stopover object.
0198      */
0199     Stopover arrival() const;
0200     /**
0201      * Sets all arrival properties from a given Stopover.
0202      * This effects location and time, but doesn't modify intermediate stops or paths.
0203      */
0204     void setArrival(const Stopover &arrival);
0205 
0206     /** Vehicle load information for this journey section, if available. */
0207     const std::vector<LoadInfo>& loadInformation() const;
0208     /** Moves the load information out of this object for modification. */
0209     std::vector<LoadInfo>&& takeLoadInformation();
0210     /** Set the vehicle load information for this journey section. */
0211     void setLoadInformation(std::vector<LoadInfo>&& loadInfo);
0212 
0213     /** Augment line meta data.
0214      *  @param download if set to @p true, trigger the download of locally missing assets.
0215      */
0216     void applyMetaData(bool download);
0217 
0218     /** Checks if two instances refer to the same journey section (which does not necessarily mean they are exactly equal). */
0219     static bool isSame(const JourneySection &lhs, const JourneySection &rhs);
0220 
0221     /** Merge two instances.
0222      *  This assumes isSame(lhs, rhs) and tries to preserve the most detailed information.
0223      */
0224     static JourneySection merge(const JourneySection &lhs, const JourneySection &rhs);
0225 
0226     /** Serializes one journey section to JSON. */
0227     static QJsonObject toJson(const JourneySection &section);
0228     /** Serializes a vector of journey sections to JSON. */
0229     static QJsonArray toJson(const std::vector<JourneySection> &sections);
0230     /** Deserialize an object from JSON. */
0231     static JourneySection fromJson(const QJsonObject &obj);
0232     /** Deserialize a vector of journey sections from JSON. */
0233     static std::vector<JourneySection> fromJson(const QJsonArray &array);
0234 
0235 private:
0236     QVariantList intermediateStopsVariant() const;
0237     QVariantList loadInformationVariant() const;
0238 };
0239 
0240 Q_DECLARE_OPERATORS_FOR_FLAGS(JourneySection::Modes)
0241 
0242 class JourneyPrivate;
0243 
0244 /** A journey plan. */
0245 class KPUBLICTRANSPORT_EXPORT Journey
0246 {
0247     KPUBLICTRANSPORT_GADGET(Journey)
0248     /** Journey sections for consumption by QML. */
0249     Q_PROPERTY(QVariantList sections READ sectionsVariant)
0250     /** Departure time of the journey, according to schedule. */
0251     Q_PROPERTY(QDateTime scheduledDepartureTime READ scheduledDepartureTime STORED false)
0252     /** @c true if this has real-time data. */
0253     Q_PROPERTY(bool hasExpectedDepartureTime READ hasExpectedDepartureTime STORED false)
0254     /** Actual departure time, if available.
0255      *  Set to invalid to indicate real-time data is not available.
0256      */
0257     Q_PROPERTY(QDateTime expectedDepartureTime READ expectedDepartureTime STORED false)
0258     /** Difference to schedule in minutes. */
0259     Q_PROPERTY(int departureDelay READ departureDelay STORED false)
0260 
0261     /** Arrival time of the journey, according to schedule. */
0262     Q_PROPERTY(QDateTime scheduledArrivalTime READ scheduledArrivalTime STORED false)
0263     /** @c true if this has real-time data. */
0264     Q_PROPERTY(bool hasExpectedArrivalTime READ hasExpectedArrivalTime STORED false)
0265     /** Actual arrival time, if available.
0266      *  Set to invalid to indicate real-time data is not available.
0267      */
0268     Q_PROPERTY(QDateTime expectedArrivalTime READ expectedArrivalTime STORED false)
0269     /** Difference to schedule in minutes. */
0270     Q_PROPERTY(int arrivalDelay READ arrivalDelay STORED false)
0271 
0272     /** Duration of the entire journey in seconds. */
0273     Q_PROPERTY(int duration READ duration STORED false)
0274     /** Number of changes on this journey. */
0275     Q_PROPERTY(int numberOfChanges READ numberOfChanges STORED false)
0276     /** Worst disruption effect of any of the journey sections. */
0277     Q_PROPERTY(KPublicTransport::Disruption::Effect disruptionEffect READ disruptionEffect STORED false)
0278 
0279 public:
0280     /** The journey sections. */
0281     const std::vector<JourneySection>& sections() const;
0282     /** Moves the journey sections out of this object. */
0283     std::vector<JourneySection>&& takeSections();
0284     /** Sets the journey sections. */
0285     void setSections(std::vector<JourneySection> &&sections);
0286 
0287     QDateTime scheduledDepartureTime() const;
0288     bool hasExpectedDepartureTime() const;
0289     QDateTime expectedDepartureTime() const;
0290     int departureDelay() const;
0291 
0292     QDateTime scheduledArrivalTime() const;
0293     bool hasExpectedArrivalTime() const;
0294     QDateTime expectedArrivalTime() const;
0295     int arrivalDelay() const;
0296 
0297     int duration() const;
0298     int numberOfChanges() const;
0299     Disruption::Effect disruptionEffect() const;
0300 
0301     /** Augment line meta data.
0302      *  @param download if set to @p true, trigger the download of locally missing assets.
0303      */
0304     void applyMetaData(bool download);
0305 
0306     /** Checks if two instances refer to the same journey (which does not necessarily mean they are exactly equal). */
0307     static bool isSame(const Journey &lhs, const Journey &rhs);
0308 
0309     /** Merge two instances.
0310      *  This assumes isSame(lhs, rhs) and tries to preserve the most detailed information.
0311      */
0312     static Journey merge(const Journey &lhs, const Journey &rhs);
0313 
0314     /** Serializes one journey object to JSON. */
0315     static QJsonObject toJson(const Journey &journey);
0316     /** Serializes a vector of journey objects to JSON. */
0317     static QJsonArray toJson(const std::vector<Journey> &journeys);
0318     /** Deserialize an object from JSON. */
0319     static Journey fromJson(const QJsonObject &obj);
0320     /** Deserialize a list of journey from JSON. */
0321     static std::vector<Journey> fromJson(const QJsonArray &array);
0322 
0323 private:
0324     QVariantList sectionsVariant() const;
0325 };
0326 
0327 }
0328 
0329 Q_DECLARE_METATYPE(KPublicTransport::JourneySection)
0330 Q_DECLARE_METATYPE(KPublicTransport::Journey)
0331 
0332 #endif // KPUBLICTRANSPORT_JOURNEY_H