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 <KLocalizedString>
13
14#include <QMetaEnum>
15
16using namespace Qt::Literals::StringLiterals;
17using namespace KPublicTransport;
18
19namespace KPublicTransport {
20class RentalVehicleNetworkPrivate : public QSharedData
21{
22public:
23 QString name;
24 RentalVehicle::VehicleTypes vehicleTypes = RentalVehicle::Unknown;
25};
26
27class RentalVehicleStationPrivate : public QSharedData
28{
29public:
30 int availableVehicles = -1;
31 int capacity = -1;
32 RentalVehicleNetwork network;
33 std::vector<int> capacities;
34 std::vector<int> availabilities;
35};
36
37class RentalVehiclePrivate : public QSharedData
38{
39public:
40 RentalVehicle::VehicleType type = RentalVehicle::Unknown;
41 int remainingRange = -1;
42 KPublicTransport::RentalVehicleNetwork network;
43 QUrl webBookingUrl;
44 QUrl appBookingUrl;
45};
46}
47
48KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicleNetwork)
49KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleNetwork, QString, name, setName)
50KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleNetwork, RentalVehicle::VehicleTypes, vehicleTypes, setVehicleTypes)
51
53{
54 return !d->name.isEmpty();
55}
56
57/** Checks if two instances refer to the same network. */
59{
60 return lhs.name() == rhs.name();
61}
62
64{
65 return Json::toJson(network);
66}
67
69{
70 return Json::fromJson<RentalVehicleNetwork>(obj);
71}
72
73KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicleStation)
74KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, int, availableVehicles, setAvailableVehicles)
75KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, int, capacity, setCapacity)
76KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicleStation, RentalVehicleNetwork, network, setNetwork)
77
79{
80 return d->network.isValid() || d->capacity >= 0 || d->availableVehicles >= 0;
81}
82
83RentalVehicle::VehicleTypes RentalVehicleStation::supportedVehicleTypes() const
84{
85 RentalVehicle::VehicleTypes types = {};
87 for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
88 if (d->capacities[i] >= 0) {
89 types |= (RentalVehicle::VehicleType)me.value(i);
90 }
91 }
92 return types | availableVehicleTypes();
93}
94
95RentalVehicle::VehicleTypes RentalVehicleStation::availableVehicleTypes() const
96{
97 RentalVehicle::VehicleTypes types = {};
99 for (auto i = 0; i < me.keyCount() && i < (int)d->availabilities.size(); ++i) {
100 if (d->availabilities[i] >= 0) {
101 types |= (RentalVehicle::VehicleType)me.value(i);
102 }
103 }
104 return types;
105}
106
108{
110 for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
111 if (me.value(i) == type) {
112 return d->capacities[i];
113 }
114 }
115 return -1;
116}
117
119{
121 for (auto i = 0; i < me.keyCount(); ++i) {
122 if (me.value(i) != type) {
123 continue;
124 }
125 d->capacities.resize(std::max(d->capacities.size(), (std::size_t)(i + 1)), -1);
126 d->capacities[i] = capacity;
127 return;
128 }
129}
130
132{
134 for (auto i = 0; i < me.keyCount() && i < (int)d->availabilities.size(); ++i) {
135 if (me.value(i) == type) {
136 return d->availabilities[i];
137 }
138 }
139 return -1;
140}
141
143{
145 for (auto i = 0; i < me.keyCount(); ++i) {
146 if (me.value(i) != type) {
147 continue;
148 }
149 d->availabilities.resize(std::max(d->availabilities.size(), (std::size_t)(i + 1)), -1);
150 d->availabilities[i] = count;
151 return;
152 }
153}
154
156{
158 for (auto i = 0; i < me.keyCount() && i < (int)d->capacities.size(); ++i) {
159 if (d->capacities[i] >= 0) {
161 }
162 }
164}
165
170
171static QJsonValue typeVectorToJson(const std::vector<int> &v)
172{
173 if (v.empty()) {
174 return {};
175 }
176 QJsonObject obj;
178 for (auto i = 0; i < me.keyCount() && i < (int)v.size(); ++i) {
179 if (v[i] < 0) {
180 continue;
181 }
182 obj.insert(QLatin1String(me.key(i)), v[i]);
183 }
184 return obj.isEmpty() ? QJsonValue() : obj;
185}
186
188{
189 auto obj = Json::toJson(station);
190 if (station.network().isValid()) {
191 obj.insert(QLatin1String("network"), RentalVehicleNetwork::toJson(station.network()));
192 }
193 auto v = typeVectorToJson(station.d->capacities);
194 if (v.isObject()) {
195 obj.insert(QLatin1String("capacitiesByType"), v);
196 }
197 v = typeVectorToJson(station.d->availabilities);
198 if (v.isObject()) {
199 obj.insert(QLatin1String("availabilitiesByType"), v);
200 }
201 return obj;
202}
203
204static std::vector<int> typeVectorFromJson(const QJsonValue &v)
205{
206 std::vector<int> out;
207 const auto obj = v.toObject();
208 if (obj.isEmpty()) {
209 return out;
210 }
211
213 for (auto i = 0; i < me.keyCount(); ++i) {
214 const auto it = obj.find(QLatin1String(me.key(i)));
215 if (it == obj.end()) {
216 continue;
217 }
218 out.resize(i + 1, -1);
219 out[i] = it.value().toInt();
220 }
221
222 return out;
223}
224
226{
227 auto station = Json::fromJson<RentalVehicleStation>(obj);
228 station.setNetwork(RentalVehicleNetwork::fromJson(obj.value(QLatin1String("network")).toObject()));
229 station.d->capacities = typeVectorFromJson(obj.value(QLatin1String("capacitiesByType")));
230 station.d->availabilities = typeVectorFromJson(obj.value(QLatin1String("availabilitiesByType")));
231 return station;
232}
233
234KPUBLICTRANSPORT_MAKE_GADGET(RentalVehicle)
235KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, RentalVehicle::VehicleType, type, setType)
236KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, int, remainingRange, setRemainingRange)
237KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, RentalVehicleNetwork, network, setNetwork)
238KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, QUrl, webBookingUrl, setWebBookingUrl)
239KPUBLICTRANSPORT_MAKE_PROPERTY(RentalVehicle, QUrl, appBookingUrl, setAppBookingUrl)
240
242{
243 switch (type) {
244 case RentalVehicle::Unknown:
245 break;
247 case RentalVehicle::Pedelec: // TODO
248 return IndividualTransport::modeIconName(IndividualTransport::Bike);
250 return u"qrc:///org.kde.kpublictransport/assets/images/motorcycle.svg"_s;
252 return u"qrc:///org.kde.kpublictransport/assets/images/scooter.svg"_s;
254 return IndividualTransport::modeIconName(IndividualTransport::Car);
255 }
256 return u"question"_s;
257}
258
260{
261 return vehicleTypeIconName(type());
262}
263
265{
266 switch (type()) {
267 case RentalVehicle::Unknown:
268 break;
271 return i18nc("rental vehicle type", "Rented bike");
273 return i18nc("rental vehicle type", "Rented moped");
275 return i18nc("rental vehicle type", "Rented kick scooter");
277 return i18nc("rental vehicle type", "Rented car");
278 }
279
280 return {};
281}
282
284{
285 auto obj = Json::toJson(vehicle);
286 if (vehicle.d->remainingRange < 0) {
287 obj.remove(QLatin1String("remainingRange"));
288 }
289 if (vehicle.network().isValid()) {
290 obj.insert(QLatin1String("network"), RentalVehicleNetwork::toJson(vehicle.network()));
291 }
292 return obj;
293}
294
296{
297 auto v = Json::fromJson<RentalVehicle>(obj);
298 v.setNetwork(RentalVehicleNetwork::fromJson(obj.value(QLatin1String("network")).toObject()));
299 return v;
300}
301
302#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.
QString label
Label shortly describing this transport for display.
@ 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.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
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-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:50:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.