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

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_LOCATION_H
0008 #define KPUBLICTRANSPORT_LOCATION_H
0009 
0010 #include "datatypes.h"
0011 #include <QVariant>
0012 
0013 class QJsonArray;
0014 class QJsonObject;
0015 class QTimeZone;
0016 template <typename K, typename T> class QHash;
0017 
0018 namespace KPublicTransport {
0019 
0020 class LocationPrivate;
0021 class Equipment;
0022 class RentalVehicle;
0023 class RentalVehicleStation;
0024 
0025 /** A location.
0026  *  This can be a train station, a bus stop, a rental vehicle dock, a free-floating vehicle position,
0027  *  an elevator or escalator, etc.
0028  */
0029 class KPUBLICTRANSPORT_EXPORT Location
0030 {
0031     KPUBLICTRANSPORT_GADGET(Location)
0032 public:
0033     /** Type of location. */
0034     enum Type {
0035         Place = 0, ///< a location that isn't of any specific type
0036         Stop = 1, ///< a public transport stop (train station, bus stop, etc)
0037         RentedVehicleStation = 2, ///< a pick-up/drop-off point for dock-based rental bike/scooter systems
0038         RentedVehicle = 4, ///< a free-floating rental bike/scooter
0039         Equipment = 8, ///< elevator/escalator
0040         CarpoolPickupDropoff = 16, ///< a pickup or dropoff location for a carpool trip
0041     };
0042     Q_ENUM(Type)
0043     Q_DECLARE_FLAGS(Types, Type)
0044     Q_FLAG(Types)
0045 
0046     /** Location type. */
0047     KPUBLICTRANSPORT_PROPERTY(Type, type, setType)
0048 
0049     /** Human-readable name of the location. */
0050     KPUBLICTRANSPORT_PROPERTY(QString, name, setName)
0051     /** Latitude of the location, in degree, NaN if unknown. */
0052     KPUBLICTRANSPORT_PROPERTY(float, latitude, setLatitude)
0053     /** Longitude of the location, in degree, NaN if unknown. */
0054     KPUBLICTRANSPORT_PROPERTY(float, longitude, setLongitude)
0055 
0056     /** Street address of the location, if known. */
0057     KPUBLICTRANSPORT_PROPERTY(QString, streetAddress, setStreetAddress)
0058     /** Postal code of the location, if known. */
0059     KPUBLICTRANSPORT_PROPERTY(QString, postalCode, setPostalCode)
0060     /** Locality/city of the location, if known. */
0061     KPUBLICTRANSPORT_PROPERTY(QString, locality, setLocality)
0062     /** Region (as in ISO 3166-2) of the location, if known. */
0063     KPUBLICTRANSPORT_PROPERTY(QString, region, setRegion)
0064     /** Country of the location as ISO 3166-1 alpha 2 code, if known. */
0065     KPUBLICTRANSPORT_PROPERTY(QString, country, setCountry)
0066 
0067     Q_PROPERTY(bool hasCoordinate READ hasCoordinate STORED false)
0068 
0069     /** Location type specific data.
0070      *  Depending on the location type this can be e.g. a RentalVehicleStation or an Equipment instance.
0071      */
0072     KPUBLICTRANSPORT_PROPERTY(QVariant, data, setData)
0073 
0074     /** Rental vehicle dock information, if applicable for this location. */
0075     Q_PROPERTY(KPublicTransport::RentalVehicleStation rentalVehicleStation READ rentalVehicleStation STORED false)
0076     /** Rental vehicle information, if applicable for this location. */
0077     Q_PROPERTY(KPublicTransport::RentalVehicle rentalVehicle READ rentalVehicle STORED false)
0078     /** Equipment information, if applicable for this location. */
0079     Q_PROPERTY(KPublicTransport::Equipment equipment READ equipment STORED false)
0080 
0081 public:
0082     void setCoordinate(float latitude, float longitude);
0083     bool hasCoordinate() const;
0084 
0085     /** Returns @c true if this is an default-constructed location object not specifying any location. */
0086     bool isEmpty() const;
0087 
0088     /** The timezone this location is in, if known. */
0089     QTimeZone timeZone() const;
0090     void setTimeZone(const QTimeZone &tz);
0091 
0092     /** Location identifiers. */
0093     Q_INVOKABLE QString identifier(const QString &identifierType) const;
0094     void setIdentifier(const QString &identifierType, const QString &id);
0095     QHash<QString, QString> identifiers() const;
0096 
0097     /** Checks if to instances refer to the same location (which does not necessarily mean they are exactly equal). */
0098     static bool isSame(const Location &lhs, const Location &rhs);
0099     /** Checks if two location names refer to the same location. */
0100     static bool isSameName(const QString &lhs, const QString &rhs);
0101 
0102     /** Merge two departure instances.
0103      *  This assumes isSame(lhs, rhs) and tries to preserve the most detailed information.
0104      */
0105     static Location merge(const Location &lhs, const Location &rhs);
0106 
0107     /** Compute the distance between two geo coordinates, in meters. */
0108     static float distance(float lat1, float lon1, float lat2, float lon2);
0109     /** Computes the distance in meters between two locations.
0110      *  Returns MAX_INT if one of the arguments has no coordinates set.
0111      */
0112     static float distance(const Location &lhs, const Location &rhs);
0113 
0114     RentalVehicleStation rentalVehicleStation() const;
0115     [[deprecated("use setData instead")]] void setRentalVehicleStation(const RentalVehicleStation &dock);
0116     RentalVehicle rentalVehicle() const;
0117     KPublicTransport::Equipment equipment() const;
0118 
0119     /** Serializes one Location object to JSON. */
0120     static QJsonObject toJson(const Location &loc);
0121     /** Serializes an array of Location objects to JSON. */
0122     static QJsonArray toJson(const std::vector<Location> &locs);
0123     /** Deserialize a Location object from JSON. */
0124     static Location fromJson(const QJsonObject &obj);
0125     /** Dezerializes an array Location objects from JSON. */
0126     static std::vector<Location> fromJson(const QJsonArray &a);
0127 
0128 };
0129 
0130 Q_DECLARE_OPERATORS_FOR_FLAGS(Location::Types)
0131 
0132 }
0133 
0134 Q_DECLARE_METATYPE(KPublicTransport::Location)
0135 
0136 #endif // KPUBLICTRANSPORT_LOCATION_H