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

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPUBLICTRANSPORT_VEHICLE_H
0008 #define KPUBLICTRANSPORT_VEHICLE_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include "datatypes.h"
0013 
0014 namespace KPublicTransport {
0015 
0016 class VehicleSectionPrivate;
0017 
0018 /** Information about a part of a vehicle.
0019  *  This typically describes a coach of a train.
0020  *  @see Vehicle
0021  */
0022 class KPUBLICTRANSPORT_EXPORT VehicleSection
0023 {
0024     KPUBLICTRANSPORT_GADGET(VehicleSection)
0025 
0026     /** Human readable identifier of this section, typically the coach number.
0027      *  Can be empty for sections closed to passengers, such as train engines.
0028      */
0029     KPUBLICTRANSPORT_PROPERTY(QString, name, setName)
0030 
0031     /** Relative position [0-1] of the begin of this vehicle section on the platform.
0032      *  0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
0033      */
0034     KPUBLICTRANSPORT_PROPERTY(float, platformPositionBegin, setPlatformPositionBegin)
0035     /** Relative position [0-1] of the end of this vehicle section on the platform.
0036      *  0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
0037      */
0038     KPUBLICTRANSPORT_PROPERTY(float, platformPositionEnd, setPlatformPositionEnd)
0039 
0040     /** Vehicle section type. */
0041     enum Type {
0042         UnknownType, ///< no information about the vehicle section type available
0043         Engine, ///< train engine, not accessible by passengers, only shown for orientation
0044         PowerCar, ///< power car of a train, similar to Engine, distinction exists just for better visualization
0045         ControlCar, ///< usually at the head of the train, but accessible for passengers and the same way as a PassengerCar
0046         PassengerCar, ///< passenger car of a train
0047         RestaurantCar, ///< full-car restaurant
0048         SleepingCar, ///< sleeping passenger car of an overnight train
0049         CouchetteCar, ///< couchette passenger car of an overnight train
0050         CarTransportCar, ///< car for transporting cars
0051     };
0052     Q_ENUM(Type)
0053     /** Type of this vehicle section. */
0054     KPUBLICTRANSPORT_PROPERTY(Type, type, setType)
0055 
0056     /** Classes available in a vehicle section. */
0057     enum Class {
0058         UnknownClass = 0,
0059         FirstClass = 1, ///< 1st class
0060         SecondClass = 2, ///< 2nd class
0061         ThirdClass = 4 ///< 3rd class
0062     };
0063     Q_DECLARE_FLAGS(Classes, Class)
0064     Q_FLAG(Classes)
0065     /** Classes available in this vehicle section.
0066      *  Can be more than one.
0067      */
0068     KPUBLICTRANSPORT_PROPERTY(Classes, classes, setClasses)
0069 
0070     /** Amenities or other features available in a vehicle section. */
0071     enum Feature {
0072         NoFeatures = 0,
0073         AirConditioning = 1, ///< vehicle section is air conditioned
0074         Restaurant = 2, ///< vehicle has a place to obtain food/drinks (but not necessarily a full-scale RestaurantCar)
0075         ToddlerArea = 4, ///< vehicle section contains infrastructure for toddler maintenance
0076         WheelchairAccessible = 8, ///< wheelchair access supported
0077         SilentArea = 16, ///< wishful thinking usually
0078         BikeStorage = 32, ///< vehicle section contains space for bikes
0079         // TODO there's a few more we get from DB
0080     };
0081     Q_DECLARE_FLAGS(Features, Feature)
0082     Q_FLAG(Features)
0083     /** Features available in this vehicle section.
0084      *  Can be more than one.
0085      */
0086     KPUBLICTRANSPORT_PROPERTY(Features, features, setFeatures)
0087     /** Feature flag as a variant list, for consumption in QML. */
0088     Q_PROPERTY(QVariantList featureList READ featureList STORED false)
0089 
0090     /** Number of decks in this vehicle section. */
0091     KPUBLICTRANSPORT_PROPERTY(int, deckCount, setDeckCount)
0092 
0093     /** Vehicle section side.
0094      *  Front is towards the smaller platform coordinate, Back is the opposite direction.
0095      */
0096     enum Side {
0097         NoSide = 0,
0098         Front = 1,
0099         Back = 2
0100     };
0101     Q_DECLARE_FLAGS(Sides, Side)
0102     Q_FLAG(Sides)
0103     /** Sides on which this vehicle section is connected to neighboring sections
0104      *  in a way that passengers can move between those sections.
0105      *  This matters for example for a double segment train with to control cars
0106      *  in the middle of its full layout.
0107      */
0108     KPUBLICTRANSPORT_PROPERTY(Sides, connectedSides, setConnectedSides)
0109 
0110     /** Name of the platform section(s) this coach is position in.
0111      *  This is primarily meant as a fallback when exact platform positions aren't available.
0112      */
0113     KPUBLICTRANSPORT_PROPERTY(QString, platformSectionName, setPlatformSectionName)
0114 
0115     /** Returns @c true if this vehicle section has a valid platform position set. */
0116     bool hasPlatformPosition() const;
0117 
0118     /** Merge two VehicleSection instances. */
0119     static VehicleSection merge(const VehicleSection &lhs, const VehicleSection &rhs);
0120 
0121     /** Serializes one vehicle section to JSON. */
0122     static QJsonObject toJson(const VehicleSection &section);
0123     /** Serializes a vector of vehicle sections to JSON. */
0124     static QJsonArray toJson(const std::vector<VehicleSection> &sections);
0125     /** Deserialize an object from JSON. */
0126     static VehicleSection fromJson(const QJsonObject &obj);
0127     /** Deserialize a vector of vehicle sections from JSON. */
0128     static std::vector<VehicleSection> fromJson(const QJsonArray &array);
0129 
0130 private:
0131     QVariantList featureList() const;
0132 };
0133 
0134 class VehiclePrivate;
0135 
0136 /** Information about the vehicle used on a journey.
0137  *  This is typically only available for trains, and describes their coach layout.
0138  *
0139  *  A vehicle object always is tied to a specific Platform object, to which all positions
0140  *  refer to.
0141  *  @see Platform
0142  */
0143 class KPUBLICTRANSPORT_EXPORT Vehicle
0144 {
0145     KPUBLICTRANSPORT_GADGET(Vehicle)
0146     /** Human readable identifier of this vehicle, typically a train number. */
0147     KPUBLICTRANSPORT_PROPERTY(QString, name, setName)
0148 
0149     /** Direction of travel. */
0150     enum Direction {
0151         UnknownDirection,
0152         Forward, ///< vehicle departs toward the 0 platform coordinate
0153         Backward ///< vehicle departs toward the 1 platforma coordinate
0154     };
0155     Q_ENUM(Direction)
0156     /** Direction of travel of this vehicle. */
0157     KPUBLICTRANSPORT_PROPERTY(Direction, direction, setDirection)
0158 
0159     /** Journey sections for consumption by QML. */
0160     Q_PROPERTY(QVariantList sections READ sectionsVariant)
0161 
0162     /** Relative position [0-1] of the begin of this vehicle on the platform.
0163      *  0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
0164      */
0165     Q_PROPERTY(float platformPositionBegin READ platformPositionBegin STORED false)
0166     /** Relative position [0-1] of the end of this vehicle on the platform.
0167      *  0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
0168      */
0169     Q_PROPERTY(float platformPositionEnd READ platformPositionEnd STORED false)
0170 
0171 public:
0172     /** Returns @c true if this object contains no information beyond the default values. */
0173     bool isEmpty() const;
0174 
0175     /** The vehicle sections. */
0176     const std::vector<VehicleSection>& sections() const;
0177     /** Moves the vehicle sections out of this object. */
0178     std::vector<VehicleSection>&& takeSections();
0179     /** Sets the vehicle sections. */
0180     void setSections(std::vector<VehicleSection> &&sections);
0181     void setSections(const std::vector<VehicleSection> &sections);
0182 
0183     float platformPositionBegin() const;
0184     float platformPositionEnd() const;
0185 
0186     /** Returns the center position of the vehicle section named @p sectionName
0187      *  in relative platform coordinates.
0188      *  Useful for centering a view on a selected section for example.
0189      */
0190     Q_INVOKABLE float platformPositionForSection(const QString &sectionName) const;
0191 
0192     /** Checks whether all vehicle sections have platform positions set. */
0193     bool hasPlatformPositions() const;
0194     /** Check whether all vehicle sections have platform section names set. */
0195     bool hasPlatformSectionNames() const;
0196 
0197     /** Merge two Vehicle instances. */
0198     static Vehicle merge(const Vehicle &lhs, const Vehicle &rhs);
0199 
0200     /** Serializes one vehicle object to JSON. */
0201     static QJsonObject toJson(const Vehicle &vehicle);
0202     /** Serializes multiple vehicle objects to JSON. */
0203     static QJsonArray toJson(const std::vector<Vehicle> &vehicles);
0204     /** Deserialize an object from JSON. */
0205     static Vehicle fromJson(const QJsonObject &obj);
0206     /** Deserialize multiple objects from JSON. */
0207     static std::vector<Vehicle> fromJson(const QJsonArray &array);
0208 
0209 private:
0210     QVariantList sectionsVariant() const;
0211 };
0212 
0213 Q_DECLARE_OPERATORS_FOR_FLAGS(VehicleSection::Classes)
0214 Q_DECLARE_OPERATORS_FOR_FLAGS(VehicleSection::Features)
0215 Q_DECLARE_OPERATORS_FOR_FLAGS(VehicleSection::Sides)
0216 
0217 }
0218 
0219 Q_DECLARE_METATYPE(KPublicTransport::VehicleSection)
0220 Q_DECLARE_METATYPE(KPublicTransport::Vehicle)
0221 
0222 #endif // KPUBLICTRANSPORT_VEHICLE_H