File indexing completed on 2024-05-12 04:42:45

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "platform.h"
0008 #include "json_p.h"
0009 #include "datatypes_p.h"
0010 
0011 #include <QDebug>
0012 #include <QVariant>
0013 
0014 using namespace KPublicTransport;
0015 
0016 namespace KPublicTransport {
0017 
0018 class PlatformSectionPrivate : public QSharedData
0019 {
0020 public:
0021     QString name;
0022     float begin = -1.0f;
0023     float end = -1.0f;
0024 };
0025 
0026 class PlatformPrivate : public QSharedData
0027 {
0028 public:
0029     QString name;
0030     std::vector<PlatformSection> sections;
0031     int length = -1;
0032 };
0033 
0034 }
0035 
0036 KPUBLICTRANSPORT_MAKE_GADGET(PlatformSection)
0037 KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, QString, name, setName)
0038 KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, float, begin, setBegin)
0039 KPUBLICTRANSPORT_MAKE_PROPERTY(PlatformSection, float, end, setEnd)
0040 
0041 QJsonObject PlatformSection::toJson(const PlatformSection &section)
0042 {
0043     return Json::toJson(section);
0044 }
0045 
0046 QJsonArray PlatformSection::toJson(const std::vector<PlatformSection> &sections)
0047 {
0048     return Json::toJson(sections);
0049 }
0050 
0051 PlatformSection PlatformSection::fromJson(const QJsonObject &obj)
0052 {
0053     return Json::fromJson<PlatformSection>(obj);
0054 }
0055 
0056 std::vector<PlatformSection> PlatformSection::fromJson(const QJsonArray &array)
0057 {
0058     return Json::fromJson<PlatformSection>(array);
0059 }
0060 
0061 
0062 KPUBLICTRANSPORT_MAKE_GADGET(Platform)
0063 KPUBLICTRANSPORT_MAKE_PROPERTY(Platform, QString, name, setName)
0064 KPUBLICTRANSPORT_MAKE_PROPERTY(Platform, int, length, setLength)
0065 
0066 bool Platform::isEmpty() const
0067 {
0068     return d->name.isEmpty() && d->length <= 0.0 && d->sections.empty();
0069 }
0070 
0071 const std::vector<PlatformSection>& Platform::sections() const
0072 {
0073     return d->sections;
0074 }
0075 
0076 std::vector<PlatformSection>&& Platform::takeSections()
0077 {
0078     d.detach();
0079     return std::move(d->sections);
0080 }
0081 
0082 void Platform::setSections(std::vector<PlatformSection> &&sections)
0083 {
0084     d.detach();
0085     d->sections = std::move(sections);
0086 }
0087 
0088 bool Platform::hasAbsoluteLength() const
0089 {
0090     return d->length > 1.0;
0091 }
0092 
0093 Platform Platform::merge(const Platform &lhs, const Platform &rhs)
0094 {
0095     // TODO expand this
0096     return lhs.sections().empty() ? rhs : lhs;
0097 }
0098 
0099 QJsonObject Platform::toJson(const Platform &platform)
0100 {
0101     auto obj = Json::toJson(platform);
0102     if (!platform.sections().empty()) {
0103         obj.insert(QLatin1String("sections"), PlatformSection::toJson(platform.sections()));
0104     }
0105     return obj;
0106 }
0107 
0108 QJsonArray Platform::toJson(const std::vector<Platform> &platforms)
0109 {
0110     return Json::toJson(platforms);
0111 }
0112 
0113 Platform Platform::fromJson(const QJsonObject &obj)
0114 {
0115     auto p = Json::fromJson<Platform>(obj);
0116     p.setSections(PlatformSection::fromJson(obj.value(QLatin1String("sections")).toArray()));
0117     return p;
0118 }
0119 
0120 std::vector<Platform> Platform::fromJson(const QJsonArray &array)
0121 {
0122     return Json::fromJson<Platform>(array);
0123 }
0124 
0125 QVariantList Platform::sectionsVariant() const
0126 {
0127     QVariantList l;
0128     l.reserve(d->sections.size());
0129     std::transform(d->sections.begin(), d->sections.end(), std::back_inserter(l), [](const auto &sec) { return QVariant::fromValue(sec); });
0130     return l;
0131 }
0132 
0133 #include "moc_platform.cpp"