KPublicTransport

rentalvehicle.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "rentalvehicle.h"
8#include "datatypes_p.h"
9#include "individualtransport.h"
10#include "json_p.h"
11
12#include <QMetaEnum>
13
14using namespace Qt::Literals::StringLiterals;
15using namespace KPublicTransport;
16
17namespace KPublicTransport {
18class RentalVehicleNetworkPrivate : public QSharedData
19{
20public:
21 QString name;
22 RentalVehicle::VehicleTypes vehicleTypes = RentalVehicle::Unknown;
23};
24
25class RentalVehicleStationPrivate : public QSharedData
26{
27public:
28 int availableVehicles = -1;
29 int capacity = -1;
31 std::vector<int> capacities;
32 std::vector<int> availabilities;
33};
34
35class RentalVehiclePrivate : public QSharedData
36{
37public:
38 RentalVehicle::VehicleType type = RentalVehicle::Unknown;
39 int remainingRange = -1;
41 QUrl webBookingUrl;
42 QUrl appBookingUrl;
43};
44}
45
46KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicleNetwork)
47KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleNetwork, QString, name, setName)
48KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleNetwork, RentalVehicle::VehicleTypes, vehicleTypes, setVehicleTypes)
49
51{
52 return !d->name.isEmpty();
53}
54
55/** Checks if two instances refer to the same network. */
57{
58 return lhs.name() == rhs.name();
59}
60
62{
63 return Json::toJson(network);
64}
65
67{
68 return Json::fromJson<RentalVehicleNetwork>(obj);
69}
70
71KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicleStation)
72KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, int, availableVehicles, setAvailableVehicles)
73KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, int, capacity, setCapacity)
74KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, RentalVehicleNetwork, network, setNetwork)
75
77{
78 return d->network.isValid() || d->capacity >= 0 || d->availableVehicles >= 0;
79}
80
82{
85 for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
86 if (d->capacities[i] >= 0) {
87 types |= (RentalVehicle::VehicleType)me.value(i);
88 }
89 }
90 return types | availableVehicleTypes();
91}
92
94{
97 for (auto i = 0; i < me.keyCount() && i < (int)d->availabilities.size(); ++i) {
98 if (d->availabilities[i] >= 0) {
99 types |= (RentalVehicle::VehicleType)me.value(i);
100 }
101 }
102 return types;
103}
104
106{
108 for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
109 if (me.value(i) == type) {
110 return d->capacities[i];
111 }
112 }
113 return -1;
114}
115
117{
119 for (auto i = 0; i < me.keyCount(); ++i) {
120 if (me.value(i) != type) {
121 continue;
122 }
123 d->capacities.resize(std::max(d->capacities.size(), (std::size_t)(i + 1)), -1);
124 d->capacities[i] = capacity;
125 return;
126 }
127}
128
130{
132 for (auto i = 0; i < me.keyCount() && i < (int)d->availabilities.size(); ++i) {
133 if (me.value(i) == type) {
134 return d->availabilities[i];
135 }
136 }
137 return -1;
138}
139
141{
143 for (auto i = 0; i < me.keyCount(); ++i) {
144 if (me.value(i) != type) {
145 continue;
146 }
147 d->availabilities.resize(std::max(d->availabilities.size(), (std::size_t)(i + 1)), -1);
148 d->availabilities[i] = count;
149 return;
150 }
151}
152
154{
156 for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
157 if (d->capacities[i] >= 0) {
159 }
160 }
162}
163
168
169static QJsonValue typeVectorToJson(const std::vector<int> &v)
170{
171 if (v.empty()) {
172 return {};
173 }
174 QJsonObject obj;
176 for (auto i = 0; i < me.keyCount() && i < (int)v.size(); ++i) {
177 if (v[i] < 0) {
178 continue;
179 }
180 obj.insert(QLatin1String(me.key(i)), v[i]);
181 }
182 return obj.isEmpty() ? QJsonValue() : obj;
183}
184
186{
187 auto obj = Json::toJson(station);
188 if (station.network().isValid()) {
189 obj.insert(QLatin1String("network"), RentalVehicleNetwork::toJson(station.network()));
190 }
191 auto v = typeVectorToJson(station.d->capacities);
192 if (v.isObject()) {
193 obj.insert(QLatin1String("capacitiesByType"), v);
194 }
195 v = typeVectorToJson(station.d->availabilities);
196 if (v.isObject()) {
197 obj.insert(QLatin1String("availabilitiesByType"), v);
198 }
199 return obj;
200}
201
202static std::vector<int> typeVectorFromJson(const QJsonValue &v)
203{
204 std::vector<int> out;
205 const auto obj = v.toObject();
206 if (obj.isEmpty()) {
207 return out;
208 }
209
211 for (auto i = 0; i < me.keyCount(); ++i) {
212 const auto it = obj.find(QLatin1String(me.key(i)));
213 if (it == obj.end()) {
214 continue;
215 }
216 out.resize(i + 1, -1);
217 out[i] = it.value().toInt();
218 }
219
220 return out;
221}
222
224{
225 auto station = Json::fromJson<RentalVehicleStation>(obj);
226 station.setNetwork(RentalVehicleNetwork::fromJson(obj.value(QLatin1String("network")).toObject()));
227 station.d->capacities = typeVectorFromJson(obj.value(QLatin1String("capacitiesByType")));
228 station.d->availabilities = typeVectorFromJson(obj.value(QLatin1String("availabilitiesByType")));
229 return station;
230}
231
232KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicle)
233KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, RentalVehicle::VehicleType, type, setType)
234KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, int, remainingRange, setRemainingRange)
235KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, RentalVehicleNetwork, network, setNetwork)
236KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, QUrl, webBookingUrl, setWebBookingUrl)
237KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, QUrl, appBookingUrl, setAppBookingUrl)
238
239QString RentalVehicle::vehicleTypeIconName(VehicleType type)
240{
241 switch (type) {
242 case RentalVehicle::Unknown:
243 break;
245 case RentalVehicle::Pedelec: // TODO
246 return IndividualTransport::modeIconName(IndividualTransport::Bike);
248 return u"qrc:///org.kde.kpublictransport/assets/images/motorcycle.svg"_s;
250 return u"qrc:///org.kde.kpublictransport/assets/images/scooter.svg"_s;
252 return IndividualTransport::modeIconName(IndividualTransport::Car);
253 }
254 return u"question"_s;
255}
256
258{
259 return vehicleTypeIconName(type());
260}
261
263{
264 auto obj = Json::toJson(vehicle);
265 if (vehicle.d->remainingRange < 0) {
266 obj.remove(QLatin1String("remainingRange"));
267 }
268 if (vehicle.network().isValid()) {
269 obj.insert(QLatin1String("network"), RentalVehicleNetwork::toJson(vehicle.network()));
270 }
271 return obj;
272}
273
275{
276 auto v = Json::fromJson<RentalVehicle>(obj);
277 v.setNetwork(RentalVehicleNetwork::fromJson(obj.value(QLatin1String("network")).toObject()));
278 return v;
279}
280
281#include "moc_rentalvehicle.cpp"
QString modeIconName
Name of an icon to represent this transport mode.
A vehicle sharing system/network.
bool isValid
Not an empty/default constructed object.
static RentalVehicleNetwork fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
static QJsonObject toJson(const RentalVehicleNetwork &network)
Serializes one object to JSON.
static bool isSame(const RentalVehicleNetwork &lhs, const RentalVehicleNetwork &rhs)
Checks if two instances refer to the same network.
QString name
Human-visible name of this network.
Additional information for a vehicle renting station, attached to Location objects.
static RentalVehicleStation fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
KPublicTransport::RentalVehicleNetwork network
Sharing network operator.
int capacity
Number of dock positions at this station.
KPublicTransport::RentalVehicle::VehicleTypes supportedVehicleTypes
Supported vehicle types at this station.
QString iconName
Icon representing this rental vehicle station.
int availableVehicles
Number of available (rentable) vehicles at this station.
static bool isSame(const RentalVehicleStation &lhs, const RentalVehicleStation &rhs)
Checks if two instances refer to the same station.
void setAvailableVehicles(RentalVehicle::VehicleType type, int count)
Sets the number of available vehicles for a given vehicle type.
bool isValid
Not an empty/default constructed object.
void setCapacity(RentalVehicle::VehicleType type, int capacity)
Set the capacity for a specific vehicle type.
static QJsonObject toJson(const RentalVehicleStation &station)
Serializes one object to JSON.
KPublicTransport::RentalVehicle::VehicleTypes availableVehicleTypes
Available vehicle types at this station.
An individual rental vehicle used on a JourneySection, ie.
@ ElectricMoped
motorcycle-like electric scooters
@ Car
electrical- or combustion-powered car
@ ElectricKickScooter
"e scooter", electrically assisted kick scooters, not to be confused with motorcycle-like scooters
@ Bicycle
human-powered bicylce
@ Pedelec
bicycle with electric assistance
static RentalVehicle fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
static QJsonObject toJson(const RentalVehicle &vehicle)
Serializes one object to JSON.
KPublicTransport::RentalVehicleNetwork network
Sharing network operator.
VehicleType type
Vehicle type.
QString vehicleTypeIconName
Icon representing the vehicle type.
int remainingRange
Remaining range of the vehicle in meters.
Query operations and data types for accessing realtime public transport information from online servi...
iterator end()
iterator find(QLatin1StringView key)
iterator insert(QLatin1StringView key, const QJsonValue &value)
bool isEmpty() const const
void remove(QLatin1StringView key)
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QMetaEnum fromType()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.