KPublicTransport

platform.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
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 
14 using namespace KPublicTransport;
15 
16 namespace KPublicTransport {
17 
18 class PlatformSectionPrivate : public QSharedData
19 {
20 public:
21  QString name;
22  float begin = -1.0f;
23  float end = -1.0f;
24 };
25 
26 class PlatformPrivate : public QSharedData
27 {
28 public:
29  QString name;
30  std::vector<PlatformSection> sections;
31  int length = -1;
32 };
33 
34 }
35 
36 KPUBLICTRANSPORT_MAKE_GADGET(PlatformSection)
37 KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, QString, name, setName)
38 KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, float, begin, setBegin)
39 KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, float, end, setEnd)
40 
41 QJsonObject PlatformSection::toJson(const PlatformSection &section)
42 {
43  return Json::toJson(section);
44 }
45 
46 QJsonArray PlatformSection::toJson(const std::vector<PlatformSection> &sections)
47 {
48  return Json::toJson(sections);
49 }
50 
51 PlatformSection PlatformSection::fromJson(const QJsonObject &obj)
52 {
53  return Json::fromJson<PlatformSection>(obj);
54 }
55 
56 std::vector<PlatformSection> PlatformSection::fromJson(const QJsonArray &array)
57 {
58  return Json::fromJson<PlatformSection>(array);
59 }
60 
61 
62 KPUBLICTRANSPORT_MAKE_GADGET(Platform)
63 KPUBLICTRANSPORT_MAKE_PROPERTY(Platform, QString, name, setName)
64 KPUBLICTRANSPORT_MAKE_PROPERTY(Platform, int, length, setLength)
65 
66 bool Platform::isEmpty() const
67 {
68  return d->name.isEmpty() && d->length <= 0.0 && d->sections.empty();
69 }
70 
71 const std::vector<PlatformSection>& Platform::sections() const
72 {
73  return d->sections;
74 }
75 
76 std::vector<PlatformSection>&& Platform::takeSections()
77 {
78  d.detach();
79  return std::move(d->sections);
80 }
81 
82 void Platform::setSections(std::vector<PlatformSection> &&sections)
83 {
84  d.detach();
85  d->sections = std::move(sections);
86 }
87 
88 bool Platform::hasAbsoluteLength() const
89 {
90  return d->length > 1.0;
91 }
92 
93 Platform Platform::merge(const Platform &lhs, const Platform &rhs)
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(QStringLiteral("sections"), PlatformSection::toJson(platform.sections()));
104  }
105  return obj;
106 }
107 
108 QJsonArray 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 
120 std::vector<Platform> Platform::fromJson(const QJsonArray &array)
121 {
122  return Json::fromJson<Platform>(array);
123 }
124 
125 QVariantList 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"
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
std::vector< PlatformSection > && takeSections()
Moves the platform sections out of this object.
Definition: platform.cpp:76
QString name
Human readable identifier of this platform section.
Definition: platform.h:24
void setSections(std::vector< PlatformSection > &&sections)
Sets the platform sections.
Definition: platform.cpp:82
QVariantList sections
Platform sections for consumption by QML.
Definition: platform.h:62
QJsonArray toArray() const const
bool isEmpty() const const
static Platform fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition: platform.cpp:113
static QJsonObject toJson(const Platform &platform)
Serializes one platform object to JSON.
Definition: platform.cpp:99
QVariant fromValue(const T &value)
Information about a part of a platform.
Definition: platform.h:19
Information about the layout of a station platform.
Definition: platform.h:44
const std::vector< PlatformSection > & sections() const
The platform sections.
QJsonValue value(const QString &key) const const
static Platform merge(const Platform &lhs, const Platform &rhs)
Merge two platform instances.
Definition: platform.cpp:93
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.