KPublicTransport

vehicle.h
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef KPUBLICTRANSPORT_VEHICLE_H
8 #define KPUBLICTRANSPORT_VEHICLE_H
9 
10 #include "kpublictransport_export.h"
11 
12 #include "datatypes.h"
13 
14 namespace KPublicTransport {
15 
16 class VehicleSectionPrivate;
17 
18 /** Information about a part of a vehicle.
19  * This typically describes a coach of a train.
20  * @see Vehicle
21  */
22 class KPUBLICTRANSPORT_EXPORT VehicleSection
23 {
24  KPUBLICTRANSPORT_GADGET(VehicleSection)
25 
26  /** Human readable identifier of this section, typically the coach number.
27  * Can be empty for sections closed to passengers, such as train engines.
28  */
29  KPUBLICTRANSPORT_PROPERTY(QString, name, setName)
30 
31  /** Relative position [0-1] of the begin of this vehicle section on the platform.
32  * 0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
33  */
34  KPUBLICTRANSPORT_PROPERTY(float, platformPositionBegin, setPlatformPositionBegin)
35  /** Relative position [0-1] of the end of this vehicle section on the platform.
36  * 0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
37  */
38  KPUBLICTRANSPORT_PROPERTY(float, platformPositionEnd, setPlatformPositionEnd)
39 
40  /** Vehicle section type. */
41  enum Type {
42  UnknownType, ///< no information about the vehicle section type available
43  Engine, ///< train engine, not accessible by passengers, only shown for orientation
44  PowerCar, ///< power car of a train, similar to Engine, distinction exists just for better visualization
45  ControlCar, ///< usually at the head of the train, but accessible for passengers and the same way as a PassengerCar
46  PassengerCar, ///< passenger car of a train
47  RestaurantCar, ///< full-car restaurant
48  };
49  Q_ENUM(Type)
50  /** Type of this vehicle section. */
51  KPUBLICTRANSPORT_PROPERTY(Type, type, setType)
52 
53  /** Classes available in a vehicle section. */
54  enum Class {
55  UnknownClass = 0,
56  FirstClass = 1, ///< 1st class
57  SecondClass = 2, ///< 2nd class
58  ThirdClass = 4 ///< 3rd class
59  };
60  Q_DECLARE_FLAGS(Classes, Class)
61  Q_FLAG(Classes)
62  /** Classes available in this vehicle section.
63  * Can be more than one.
64  */
65  KPUBLICTRANSPORT_PROPERTY(Classes, classes, setClasses)
66 
67  /** Amenities or other features available in a vehicle section. */
68  enum Feature {
69  NoFeatures = 0,
70  AirConditioning = 1, ///< vehicle section is air conditioned
71  Restaurant = 2, ///< vehicle has a place to obtain food/drinks (but not necessarily a full-scale RestaurantCar)
72  ToddlerArea = 4, ///< vehicle section contains infrastructure for toddler maintenance
73  WheelchairAccessible = 8, ///< wheelchair access supported
74  SilentArea = 16, ///< wishful thinking usually
75  BikeStorage = 32, ///< vehicle section contains space for bikes
76  // TODO there's a few more we get from DB
77  };
78  Q_DECLARE_FLAGS(Features, Feature)
79  Q_FLAG(Features)
80  /** Features available in this vehicle section.
81  * Can be more than one.
82  */
83  KPUBLICTRANSPORT_PROPERTY(Features, features, setFeatures)
84  /** Feature flag as a variant list, for consumption in QML. */
85  Q_PROPERTY(QVariantList featureList READ featureList STORED false)
86 
87  /** Number of decks in this vehicle section. */
88  KPUBLICTRANSPORT_PROPERTY(int, deckCount, setDeckCount)
89 
90  /** Vehicle section side.
91  * Front is towards the smaller platform coordinate, Back is the opposite direction.
92  */
93  enum Side {
94  NoSide = 0,
95  Front = 1,
96  Back = 2
97  };
98  Q_DECLARE_FLAGS(Sides, Side)
99  Q_FLAG(Sides)
100  /** Sides on which this vehicle section is connected to neighboring sections
101  * in a way that passengers can move between those sections.
102  * This matters for example for a double segment train with to control cars
103  * in the middle of its full layout.
104  */
105  KPUBLICTRANSPORT_PROPERTY(Sides, connectedSides, setConnectedSides)
106 
107  /** Name of the platform section(s) this coach is position in.
108  * This is primarily meant as a fallback when exact platform positions aren't available.
109  */
110  KPUBLICTRANSPORT_PROPERTY(QString, platformSectionName, setPlatformSectionName)
111 
112  /** Returns @c true if this vehicle section has a valid platform position set. */
113  bool hasPlatformPosition() const;
114 
115  /** Merge two VehicleSection instances. */
116  static VehicleSection merge(const VehicleSection &lhs, const VehicleSection &rhs);
117 
118  /** Serializes one vehicle section to JSON. */
119  static QJsonObject toJson(const VehicleSection &section);
120  /** Serializes a vector of vehicle sections to JSON. */
121  static QJsonArray toJson(const std::vector<VehicleSection> &sections);
122  /** Deserialize an object from JSON. */
123  static VehicleSection fromJson(const QJsonObject &obj);
124  /** Deserialize a vector of vehicle sections from JSON. */
125  static std::vector<VehicleSection> fromJson(const QJsonArray &array);
126 
127 private:
128  QVariantList featureList() const;
129 };
130 
131 class VehiclePrivate;
132 
133 /** Information about the vehicle used on a journey.
134  * This is typically only available for trains, and describes their coach layout.
135  *
136  * A vehicle object always is tied to a specific Platform object, to which all positions
137  * refer to.
138  * @see Platform
139  */
140 class KPUBLICTRANSPORT_EXPORT Vehicle
141 {
142  KPUBLICTRANSPORT_GADGET(Vehicle)
143  /** Human readable identifier of this vehicle, typically a train number. */
144  KPUBLICTRANSPORT_PROPERTY(QString, name, setName)
145 
146  /** Direction of travel. */
147  enum Direction {
148  UnknownDirection,
149  Forward, ///< vehicle departs toward the 0 platform coordinate
150  Backward ///< vehicle departs toward the 1 platforma coordinate
151  };
152  Q_ENUM(Direction)
153  /** Direction of travel of this vehicle. */
154  KPUBLICTRANSPORT_PROPERTY(Direction, direction, setDirection)
155 
156  /** Journey sections for consumption by QML. */
157  Q_PROPERTY(QVariantList sections READ sectionsVariant)
158 
159  /** Relative position [0-1] of the begin of this vehicle on the platform.
160  * 0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
161  */
162  Q_PROPERTY(float platformPositionBegin READ platformPositionBegin STORED false)
163  /** Relative position [0-1] of the end of this vehicle on the platform.
164  * 0 representing the begin of the platform in platform coordinate (@see Platform), 1 being the opposite end.
165  */
166  Q_PROPERTY(float platformPositionEnd READ platformPositionEnd STORED false)
167 
168 public:
169  /** Returns @c true if this object contains no information beyond the default values. */
170  bool isEmpty() const;
171 
172  /** The vehicle sections. */
173  const std::vector<VehicleSection>& sections() const;
174  /** Moves the vehicle sections out of this object. */
175  std::vector<VehicleSection>&& takeSections();
176  /** Sets the vehicle sections. */
177  void setSections(std::vector<VehicleSection> &&sections);
178  void setSections(const std::vector<VehicleSection> &sections);
179 
180  float platformPositionBegin() const;
181  float platformPositionEnd() const;
182 
183  /** Returns the center position of the vehicle section named @p sectionName
184  * in relative platform coordinates.
185  * Useful for centering a view on a selected section for example.
186  */
187  Q_INVOKABLE float platformPositionForSection(const QString &sectionName) const;
188 
189  /** Checks whether all vehicle sections have platform positions set. */
190  bool hasPlatformPositions() const;
191  /** Check whether all vehicle sections have platform section names set. */
192  bool hasPlatformSectionNames() const;
193 
194  /** Merge two Vehicle instances. */
195  static Vehicle merge(const Vehicle &lhs, const Vehicle &rhs);
196 
197  /** Serializes one vehicle object to JSON. */
198  static QJsonObject toJson(const Vehicle &vehicle);
199  /** Serializes multiple vehicle objects to JSON. */
200  static QJsonArray toJson(const std::vector<Vehicle> &vehicles);
201  /** Deserialize an object from JSON. */
202  static Vehicle fromJson(const QJsonObject &obj);
203  /** Deserialize multiple objects from JSON. */
204  static std::vector<Vehicle> fromJson(const QJsonArray &array);
205 
206 private:
207  QVariantList sectionsVariant() const;
208 };
209 
210 Q_DECLARE_OPERATORS_FOR_FLAGS(VehicleSection::Classes)
211 Q_DECLARE_OPERATORS_FOR_FLAGS(VehicleSection::Features)
212 Q_DECLARE_OPERATORS_FOR_FLAGS(VehicleSection::Sides)
213 
214 }
215 
216 Q_DECLARE_METATYPE(KPublicTransport::VehicleSection)
217 Q_DECLARE_METATYPE(KPublicTransport::Vehicle)
218 
219 #endif // KPUBLICTRANSPORT_VEHICLE_H
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
Information about a part of a vehicle.
Definition: vehicle.h:22
Information about the vehicle used on a journey.
Definition: vehicle.h:140
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Oct 15 2021 23:07:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.