KPublicTransport

platform.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "platform.h"
8#include "json_p.h"
9#include "datatypes_p.h"
10
11#include <QDebug>
12#include <QVariant>
13
14using namespace KPublicTransport;
15
16namespace KPublicTransport {
17
18class PlatformSectionPrivate : public QSharedData
19{
20public:
21 QString name;
22 float begin = -1.0f;
23 float end = -1.0f;
24};
25
26class PlatformPrivate : public QSharedData
27{
28public:
29 QString name;
30 std::vector<PlatformSection> sections;
31 int length = -1;
32};
33
34}
35
36KPUBLICTRANSPORT_MAKE_GADGET(PlatformSection)
37KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, QString, name, setName)
38KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, float, begin, setBegin)
39KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, float, end, setEnd)
40
41QJsonObject PlatformSection::toJson(const PlatformSection &section)
42{
43 return Json::toJson(section);
44}
45
46QJsonArray PlatformSection::toJson(const std::vector<PlatformSection> &sections)
47{
48 return Json::toJson(sections);
49}
50
51PlatformSection PlatformSection::fromJson(const QJsonObject &obj)
52{
53 return Json::fromJson<PlatformSection>(obj);
54}
55
56std::vector<PlatformSection> PlatformSection::fromJson(const QJsonArray &array)
57{
58 return Json::fromJson<PlatformSection>(array);
59}
60
61
62KPUBLICTRANSPORT_MAKE_GADGET(Platform)
63KPUBLICTRANSPORT_MAKE_PROPERTY(Platform, QString, name, setName)
64KPUBLICTRANSPORT_MAKE_PROPERTY(Platform, int, length, setLength)
65
66bool Platform::isEmpty() const
67{
68 return d->name.isEmpty() && d->length <= 0.0 && d->sections.empty();
69}
70
71const std::vector<PlatformSection>& Platform::sections() const
72{
73 return d->sections;
74}
75
76std::vector<PlatformSection>&& Platform::takeSections()
77{
78 d.detach();
79 return std::move(d->sections);
80}
81
82void Platform::setSections(std::vector<PlatformSection> &&sections)
83{
84 d.detach();
85 d->sections = std::move(sections);
86}
87
89{
90 return d->length > 1.0;
91}
92
94{
95 // TODO expand this
96 return lhs.sections().empty() ? rhs : lhs;
97}
98
100{
101 auto obj = Json::toJson(platform);
102 if (!platform.sections().empty()) {
103 obj.insert(QLatin1String("sections"), PlatformSection::toJson(platform.sections()));
104 }
105 return obj;
106}
107
108QJsonArray Platform::toJson(const std::vector<Platform> &platforms)
109{
110 return Json::toJson(platforms);
111}
112
114{
115 auto p = Json::fromJson<Platform>(obj);
116 p.setSections(PlatformSection::fromJson(obj.value(QLatin1String("sections")).toArray()));
117 return p;
118}
119
120std::vector<Platform> Platform::fromJson(const QJsonArray &array)
121{
122 return Json::fromJson<Platform>(array);
123}
124
125QVariantList Platform::sectionsVariant() const
126{
127 QVariantList l;
128 l.reserve(d->sections.size());
129 std::transform(d->sections.begin(), d->sections.end(), std::back_inserter(l), [](const auto &sec) { return QVariant::fromValue(sec); });
130 return l;
131}
132
133#include "moc_platform.cpp"
Information about a part of a platform.
Definition platform.h:20
Information about the layout of a station platform.
Definition platform.h:45
static Platform fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition platform.cpp:113
bool hasAbsoluteLength
true if the absolute length of the platform in meter is known.
Definition platform.h:68
QString name
Human readable identifier of this platform.
Definition platform.h:52
static Platform merge(const Platform &lhs, const Platform &rhs)
Merge two platform instances.
Definition platform.cpp:93
void setSections(std::vector< PlatformSection > &&sections)
Sets the platform sections.
Definition platform.cpp:82
static QJsonObject toJson(const Platform &platform)
Serializes one platform object to JSON.
Definition platform.cpp:99
std::vector< PlatformSection > && takeSections()
Moves the platform sections out of this object.
Definition platform.cpp:76
QVariantList sections
Platform sections for consumption by QML.
Definition platform.h:62
Query operations and data types for accessing realtime public transport information from online servi...
QJsonValue value(QLatin1StringView key) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:57:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.