KPublicTransport

feature.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "feature.h"
7#include "datatypes_p.h"
8#include "json_p.h"
9#include "mergeutil_p.h"
10
11#include <KLocalizedString>
12
13using namespace Qt::Literals::StringLiterals;
14using namespace KPublicTransport;
15
16namespace KPublicTransport {
17class FeaturePrivate : public QSharedData {
18public:
19 QString name;
20 QString description;
21 Feature::Type type = Feature::NoFeature;
22 Feature::Availability availability = Feature::Unknown;
23 Disruption::Effect disruptionEffect = Disruption::NormalService;
24 int quantity = 0;
25};
26}
27
28KPUBLICTRANSPORT_MAKE_GADGET(Feature)
29KPUBLICTRANSPORT_MAKE_PROPERTY(Feature, QString, name, setName)
30KPUBLICTRANSPORT_MAKE_PROPERTY(Feature, QString, description, setDescription)
31KPUBLICTRANSPORT_MAKE_PROPERTY(Feature, Feature::Type, type, setType)
32KPUBLICTRANSPORT_MAKE_PROPERTY(Feature, Feature::Availability, availability, setAvailability)
33KPUBLICTRANSPORT_MAKE_PROPERTY(Feature, Disruption::Effect, disruptionEffect, setDisruptionEffect)
34KPUBLICTRANSPORT_MAKE_PROPERTY(Feature, int, quantity, setQuantity)
35
36Feature::Feature(Type type, Availability availability)
37 : Feature()
38{
39 d->type = type;
40 d->availability = availability;
41}
42
44{
45 return d->name.isEmpty() ? Feature::typeDisplayName(d->type) : d->name;
46}
47
49{
50 switch (type) {
51 case Feature::AirConditioning:
52 return i18nc("train coach feature", "Air conditioning");
53 case Feature::Restaurant:
54 return i18nc("train coach feature", "Bistro or restaurant");
55 case Feature::ToddlerArea:
56 return i18nc("train coach feature", "Toddler area");
57 case Feature::FamilyArea:
58 return i18nc("train coach feature", "Family area");
59 case Feature::WheelchairAccessible:
60 return i18nc("train coach feature", "Wheelchair accessible");
61 case Feature::SilentArea:
62 return i18nc("train coach feature", "Quiet area");
63 case Feature::BusinessArea:
64 return i18nc("train coach feature", "Business area");
65 case Feature::BikeStorage:
66 return i18nc("train coach feature", "Bike storage");
67 case Feature::Toilet:
68 return i18nc("train coach feature", "Toilet");
69 case Feature::WheelchairAccessibleToilet:
70 return i18nc("train coach feature", "Wheelchair accessible toilet");
71 case Feature::InformationPoint:
72 return i18nc("train coach feature", "Information point");
73 case Feature::WiFi:
74 return i18nc("train coach feature", "Wi-Fi");
75 case Feature::PowerSockets:
76 return i18nc("train coach feature", "Power sockets");
77 break;
78 case Feature::Other:
79 case Feature::NoFeature:
80 break;
81 }
82
83 return {};
84}
85
87{
88 return Feature::typeIconName(d->type);
89}
90
91QString Feature::typeIconName(Feature::Type type)
92{
93 switch (type) {
94 case Feature::NoFeature:
95 return {};
96 case Feature::AirConditioning:
97 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-airconditioning.svg"_s;
98 case Feature::Restaurant:
99 return u"qrc:///org.kde.kpublictransport/assets/images/train-coach-restaurant.svg"_s;
100 case Feature::ToddlerArea:
101 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-toddlerarea.svg"_s;
102 case Feature::FamilyArea:
103 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-family.svg"_s;
104 case Feature::WheelchairAccessible:
105 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-wheelchair-accessible.svg"_s;
106 case Feature::SilentArea:
107 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-silentarea.svg"_s;
108 case Feature::BusinessArea:
109 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-businessarea.svg"_s;
110 case Feature::BikeStorage:
111 return u"qrc:///org.kde.kpublictransport/assets/images/transport-mode-bike.svg"_s;
112 case Feature::Toilet:
113 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-toilet.svg"_s;
114 case Feature::WheelchairAccessibleToilet:
115 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-wheelchair-toilet.svg"_s;
116 case Feature::InformationPoint:
117 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-information.svg"_s;
118 case Feature::WiFi:
119 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-wifi.svg"_s;
120 case Feature::PowerSockets:
121 return u"qrc:///org.kde.kpublictransport/assets/images/vehicle-feature-powersocket.svg"_s;
122 case Feature::Other:
123 break;
124 }
125
126 return u"documentinfo"_s;
127}
128
130{
131 auto obj = Json::toJson(feature);
132 if (!feature.d->quantity) {
133 obj.remove("quantity"_L1);
134 }
135 return obj;
136}
137
138QJsonArray Feature::toJson(const std::vector<Feature> &features)
139{
140 return Json::toJson(features);
141}
142
144{
145 return Json::fromJson<Feature>(obj);
146}
147
148std::vector<Feature> Feature::fromJson(const QJsonArray &array)
149{
150 return Json::fromJson<Feature>(array);
151}
152
153bool Feature::isSame(const Feature &lhs, const Feature &rhs)
154{
155 if (lhs.type() != rhs.type()) {
156 return false;
157 }
158
159 if (lhs.type() == Feature::Other) {
160 return lhs.name().compare(rhs.name(), Qt::CaseInsensitive) == 0;
161 }
162
163 return true;
164}
165
166[[nodiscard]] static Feature::Availability mergeAvailability(Feature::Availability lhs, Feature::Availability rhs)
167{
168 if (lhs == Feature::Unknown) {
169 return rhs;
170 }
171 if (rhs == Feature::Unknown) {
172 return lhs;
173 }
174
175 return lhs == Feature::Available ? rhs : lhs;
176}
177
178Feature Feature::merge(const Feature &lhs, const Feature &rhs)
179{
180 Feature res(lhs);
181 res.setName(MergeUtil::mergeString(lhs.name(), rhs.name()));
182 res.setDescription(MergeUtil::mergeString(lhs.description(), rhs.description()));
183 res.setAvailability(mergeAvailability(lhs.availability(), rhs.availability()));
184 res.setDisruptionEffect(std::max(lhs.disruptionEffect(), rhs.disruptionEffect()));
185 res.setQuantity(std::max(lhs.quantity(), rhs.quantity()));
186 return res;
187}
188
189#include "moc_feature.cpp"
An amenity, facility or other relevant property of a vehicle (train, bus, etc), vehicle part (e....
Definition feature.h:20
static Feature merge(const Feature &lhs, const Feature &rhs)
Merge two features referring to the same thing.
Definition feature.cpp:178
static QString typeDisplayName(KPublicTransport::Feature::Type type)
Generic display name for feature of type.
Definition feature.cpp:48
QString description
An optional longer human-readable description.
Definition feature.h:37
QString name
A short human-readable label of this feature.
Definition feature.h:24
static bool isSame(const Feature &lhs, const Feature &rhs)
Returns true if both features refer to the same thing.
Definition feature.cpp:153
static Feature fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition feature.cpp:143
static QJsonObject toJson(const Feature &feature)
Serializes one object to JSON.
Definition feature.cpp:129
KPublicTransport::Disruption::Effect disruptionEffect
Distruption effects on the feature.
Definition feature.h:72
QString displayName
A short humand-readable label for this feature.
Definition feature.h:31
int quantity
Amount of instances of this feature.
Definition feature.h:77
QString typeIconName
Icon representing the given feature type type.
Definition feature.h:82
QString i18nc(const char *context, const char *text, const TYPE &arg...)
Effect
Disruption effects, numerical sorted so that higher values imply more severe disruptions.
Definition disruption.h:25
Query operations and data types for accessing realtime public transport information from online servi...
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool isEmpty() const const
CaseInsensitive
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.