KPublicTransport

rentalvehicle.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "rentalvehicle.h"
8 #include "datatypes_p.h"
9 #include "json_p.h"
10 
11 #include <QMetaEnum>
12 
13 using namespace KPublicTransport;
14 
15 namespace KPublicTransport {
16 class RentalVehicleNetworkPrivate : public QSharedData
17 {
18 public:
19  QString name;
20  RentalVehicle::VehicleTypes vehicleTypes = RentalVehicle::Unknown;
21 };
22 
23 class RentalVehicleStationPrivate : public QSharedData
24 {
25 public:
26  int availableVehicles = -1;
27  int capacity = -1;
28  RentalVehicleNetwork network;
29  std::vector<int> capacities;
30  std::vector<int> availabilities;
31 };
32 
33 class RentalVehiclePrivate : public QSharedData
34 {
35 public:
36  RentalVehicle::VehicleType type = RentalVehicle::Unknown;
37  int remainingRange = -1;
39 };
40 }
41 
42 KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicleNetwork)
43 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleNetwork, QString, name, setName)
44 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleNetwork, RentalVehicle::VehicleTypes, vehicleTypes, setVehicleTypes)
45 
46 bool RentalVehicleNetwork::isValid() const
47 {
48  return !d->name.isEmpty();
49 }
50 
51 /** Checks if two instances refer to the same network. */
52 bool RentalVehicleNetwork::isSame(const RentalVehicleNetwork &lhs, const RentalVehicleNetwork &rhs)
53 {
54  return lhs.name() == rhs.name();
55 }
56 
57 QJsonObject RentalVehicleNetwork::toJson(const RentalVehicleNetwork &network)
58 {
59  return Json::toJson(network);
60 }
61 
62 RentalVehicleNetwork RentalVehicleNetwork::fromJson(const QJsonObject &obj)
63 {
64  return Json::fromJson<RentalVehicleNetwork>(obj);
65 }
66 
67 KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicleStation)
68 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, int, availableVehicles, setAvailableVehicles)
69 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, int, capacity, setCapacity)
70 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, RentalVehicleNetwork, network, setNetwork)
71 
72 bool RentalVehicleStation::isValid() const
73 {
74  return d->network.isValid() || d->capacity >= 0 || d->availableVehicles >= 0;
75 }
76 
77 RentalVehicle::VehicleTypes RentalVehicleStation::supportedVehicleTypes() const
78 {
79  RentalVehicle::VehicleTypes types = {};
80  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
81  for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
82  if (d->capacities[i] >= 0) {
83  types |= (RentalVehicle::VehicleType)me.value(i);
84  }
85  }
86  return types | availableVehicleTypes();
87 }
88 
89 RentalVehicle::VehicleTypes KPublicTransport::RentalVehicleStation::availableVehicleTypes() const
90 {
91  RentalVehicle::VehicleTypes types = {};
92  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
93  for (auto i = 0; i < me.keyCount() && i < (int)d->availabilities.size(); ++i) {
94  if (d->availabilities[i] >= 0) {
95  types |= (RentalVehicle::VehicleType)me.value(i);
96  }
97  }
98  return types;
99 }
100 
102 {
103  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
104  for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
105  if (me.value(i) == type) {
106  return d->capacities[i];
107  }
108  }
109  return -1;
110 }
111 
113 {
114  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
115  for (auto i = 0; i < me.keyCount(); ++i) {
116  if (me.value(i) != type) {
117  continue;
118  }
119  d->capacities.resize(std::max(d->capacities.size(), (std::size_t)(i + 1)), -1);
120  d->capacities[i] = capacity;
121  return;
122  }
123 }
124 
126 {
127  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
128  for (auto i = 0; i < me.keyCount() && i < (int)d->availabilities.size(); ++i) {
129  if (me.value(i) == type) {
130  return d->availabilities[i];
131  }
132  }
133  return -1;
134 }
135 
137 {
138  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
139  for (auto i = 0; i < me.keyCount(); ++i) {
140  if (me.value(i) != type) {
141  continue;
142  }
143  d->availabilities.resize(std::max(d->availabilities.size(), (std::size_t)(i + 1)), -1);
144  d->availabilities[i] = count;
145  return;
146  }
147 }
148 
150 {
151  return RentalVehicleNetwork::isSame(lhs.network(), rhs.network());
152 }
153 
154 static QJsonValue typeVectorToJson(const std::vector<int> &v)
155 {
156  if (v.empty()) {
157  return {};
158  }
159  QJsonObject obj;
160  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
161  for (auto i = 0; i < me.keyCount() && i < (int)v.size(); ++i) {
162  if (v[i] < 0) {
163  continue;
164  }
165  obj.insert(QLatin1String(me.key(i)), v[i]);
166  }
167  return obj.isEmpty() ? QJsonValue() : obj;
168 }
169 
171 {
172  auto obj = Json::toJson(station);
173  if (station.network().isValid()) {
174  obj.insert(QStringLiteral("network"), RentalVehicleNetwork::toJson(station.network()));
175  }
176  auto v = typeVectorToJson(station.d->capacities);
177  if (v.isObject()) {
178  obj.insert(QLatin1String("capacitiesByType"), v);
179  }
180  v = typeVectorToJson(station.d->availabilities);
181  if (v.isObject()) {
182  obj.insert(QLatin1String("availabilitiesByType"), v);
183  }
184  return obj;
185 }
186 
187 static std::vector<int> typeVectorFromJson(const QJsonValue &v)
188 {
189  std::vector<int> out;
190  const auto obj = v.toObject();
191  if (obj.isEmpty()) {
192  return out;
193  }
194 
195  const auto me = QMetaEnum::fromType<RentalVehicle::VehicleType>();
196  for (auto i = 0; i < me.keyCount(); ++i) {
197  const auto it = obj.find(QLatin1String(me.key(i)));
198  if (it == obj.end()) {
199  continue;
200  }
201  out.resize(i + 1, -1);
202  out[i] = it.value().toInt();
203  }
204 
205  return out;
206 }
207 
209 {
210  auto station = Json::fromJson<RentalVehicleStation>(obj);
211  station.setNetwork(RentalVehicleNetwork::fromJson(obj.value(QLatin1String("network")).toObject()));
212  station.d->capacities = typeVectorFromJson(obj.value(QLatin1String("capacitiesByType")));
213  station.d->availabilities = typeVectorFromJson(obj.value(QLatin1String("availabilitiesByType")));
214  return station;
215 }
216 
217 KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicle)
218 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, RentalVehicle::VehicleType, type, setType)
219 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, int, remainingRange, setRemainingRange)
220 KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, RentalVehicleNetwork, network, setNetwork)
221 
222 QJsonObject RentalVehicle::toJson(const RentalVehicle &vehicle)
223 {
224  auto obj = Json::toJson(vehicle);
225  if (vehicle.d->remainingRange < 0) {
226  obj.remove(QLatin1String("remainingRange"));
227  }
228  if (vehicle.network().isValid()) {
229  obj.insert(QLatin1String("network"), RentalVehicleNetwork::toJson(vehicle.network()));
230  }
231  return obj;
232 }
233 
235 {
236  auto v = Json::fromJson<RentalVehicle>(obj);
237  v.setNetwork(RentalVehicleNetwork::fromJson(obj.value(QLatin1String("network")).toObject()));
238  return v;
239 }
240 
241 #include "moc_rentalvehicle.cpp"
static QJsonObject toJson(const RentalVehicleStation &station)
Serializes one object to JSON.
QString name
Human-visible name of this network.
static bool isSame(const RentalVehicleNetwork &lhs, const RentalVehicleNetwork &rhs)
Checks if two instances refer to the same network.
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
static RentalVehicle fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
static RentalVehicleNetwork fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
bool isEmpty() const const
Additional information for a vehicle renting station, attached to Location objects.
Definition: rentalvehicle.h:72
A vehicle sharing system/network.
QJsonObject toObject() const const
bool isEmpty() const const
static bool isSame(const RentalVehicleStation &lhs, const RentalVehicleStation &rhs)
Checks if two instances refer to the same station.
int availableVehicles
Number of available (rentable) vehicles at this station.
Definition: rentalvehicle.h:76
QJsonObject::iterator find(const QString &key)
void setCapacity(RentalVehicle::VehicleType type, int capacity)
Set the capacity for a specific vehicle type.
int capacity
Number of dock positions at this station.
Definition: rentalvehicle.h:80
static RentalVehicleStation fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
bool isValid
Not an empty/default constructed object.
QJsonValue value(const QString &key) const const
An individual rental vehicle used on a JourneySection, ie.
Definition: rentalvehicle.h:27
QJsonObject::iterator insert(const QString &key, const QJsonValue &value)
static QJsonObject toJson(const RentalVehicleNetwork &network)
Serializes one object to JSON.
KPublicTransport::RentalVehicleNetwork network
Sharing network operator.
Definition: rentalvehicle.h:83
void setAvailableVehicles(RentalVehicle::VehicleType type, int count)
Sets the number of available vehicles for a given vehicle type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.