KPublicTransport

path.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "path.h"
8#include "datatypes_p.h"
9#include "json_p.h"
10#include "../geo/geojson_p.h"
11#include "location.h"
12
13#include <QLineF>
14
15using namespace Qt::Literals::StringLiterals;
16using namespace KPublicTransport;
17
18namespace KPublicTransport {
19class PathSectionPrivate : public QSharedData {
20public:
21 QPolygonF path;
22 QString description;
23 int floorLevelChange = 0;
25};
26}
27
28KPUBLICTRANSPORT_MAKE_GADGET(PathSection)
29KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QPolygonF, path, setPath)
30KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, QString, description, setDescription)
31KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, int, floorLevelChange, setFloorLevelChange)
32KPUBLICTRANSPORT_MAKE_PROPERTY(PathSection, PathSection::Maneuver, maneuver, setManeuver)
33
34int PathSection::distance() const
35{
36 if (d->path.size() < 2) {
37 return 0;
38 }
39
40 float dist = 0;
41 for (auto it = d->path.begin(); it != std::prev(d->path.end()); ++it) {
42 const auto nextIt = std::next(it);
43 dist += Location::distance((*it).y(), (*it).x(), (*nextIt).y(), (*nextIt).x());
44 }
45 return dist;
46}
47
49{
50 const auto p1 = startPoint();
51 const auto p2 = endPoint();
52 if (d->path.size() < 2 || p1 == p2) {
53 return -1;
54 }
55 return static_cast<int>(450 - QLineF(p1.x(), -p1.y(), p2.x(), -p2.y()).angle()) % 360;
56}
57
59{
60 return d->path.empty() ? QPointF() : d->path.constFirst();
61}
62
64{
65 return d->path.empty() ? QPointF() : d->path.constLast();
66}
67
69{
70 switch (maneuver) {
72 return u"qrc:///org.kde.kpublictransport/assets/images/transport-mode-walk.svg"_s;
74 return u"qrc:///org.kde.kpublictransport/assets/images/path-elevator.svg"_s;
76 return u"qrc:///org.kde.kpublictransport/assets/images/path-escalator.svg"_s;
78 return u"qrc:///org.kde.kpublictransport/assets/images/path-stairs.svg"_s;
79 }
80
81 return {};
82}
83
85{
87}
88
90{
91 auto obj = Json::toJson(section);
92 if (!section.path().empty()) {
93 obj.insert(QLatin1String("path"), GeoJson::writeLineString(section.path()));
94 }
95 if (section.maneuver() == PathSection::Move) {
96 obj.remove(QLatin1String("maneuver"));
97 }
98 if (section.floorLevelChange() == 0) {
99 obj.remove(QLatin1String("floorLevelChange"));
100 }
101 return obj;
102}
103
104QJsonArray PathSection::toJson(const std::vector<PathSection> &sections)
105{
106 return Json::toJson(sections);
107}
108
110{
111 auto section = Json::fromJson<PathSection>(obj);
112 section.setPath(GeoJson::readLineString(obj.value(QLatin1String("path")).toObject()));
113 return section;
114}
115
116std::vector<PathSection> PathSection::fromJson(const QJsonArray &array)
117{
118 return Json::fromJson<PathSection>(array);
119}
120
121
122namespace KPublicTransport {
123class PathPrivate : public QSharedData {
124public:
125 std::vector<PathSection> sections;
126};
127}
128
129KPUBLICTRANSPORT_MAKE_GADGET(Path)
130
131bool Path::isEmpty() const
132{
133 return d->sections.empty();
134}
135
136const std::vector<PathSection>& Path::sections() const
137{
138 return d->sections;
139}
140
141std::vector<PathSection>&& Path::takeSections()
142{
143 d.detach();
144 return std::move(d->sections);
145}
146
147void Path::setSections(std::vector<PathSection> &&sections)
148{
149 d.detach();
150 d->sections = std::move(sections);
151}
152
153int Path::distance() const
154{
155 return std::accumulate(d->sections.begin(), d->sections.end(), 0, [](int d, const auto &sec) { return d + sec.distance(); });
156}
157
159{
160 return isEmpty() ? QPointF() : d->sections.front().startPoint();
161}
162
164{
165 return isEmpty() ? QPointF() : d->sections.front().endPoint();
166}
167
169{
170 auto obj = Json::toJson(path);
171 obj.insert(QLatin1String("sections"), PathSection::toJson(path.sections()));
172 return obj;
173}
174
176{
177 auto path = Json::fromJson<Path>(obj);
178 path.setSections(PathSection::fromJson(obj.value(QLatin1String("sections")).toArray()));
179 return path;
180}
181
182int Path::sectionCount() const
183{
184 return d->sections.size();
185}
186
187#include "moc_path.cpp"
A section of a Path.
Definition path.h:25
static Q_INVOKABLE QString maneuverIconName(KPublicTransport::PathSection::Maneuver maneuver)
An icon representing maneuver.
Definition path.cpp:68
Maneuver
Maneuver associated with a path section.
Definition path.h:46
@ Elevator
Take an elevator.
Definition path.h:49
@ Escalator
Take an escalator.
Definition path.h:50
@ Move
Move/drive with the default mode of transport for this path.
Definition path.h:47
@ Stairs
Walk up or down stairs.
Definition path.h:48
QString iconName
An icon representing the maneuver.
Definition path.h:59
QPointF startPoint() const
First point on the path of this section.
Definition path.cpp:58
QPolygonF path
The geo coordinate poly-line followed by this path section.
Definition path.h:28
static QJsonObject toJson(const PathSection &section)
Serializes one path section section to JSON.
Definition path.cpp:89
QPointF endPoint() const
Last point on the path of this section.
Definition path.cpp:63
int floorLevelChange
Floor level change during this path section.
Definition path.h:42
static PathSection fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition path.cpp:109
int direction
The overall direction of this section in degree.
Definition path.h:37
Maneuver maneuver
Movement maneuver for this path section.
Definition path.h:54
A path followed by any kind of location change.
Definition path.h:101
std::vector< PathSection > && takeSections()
Moves the path sections out of this object.
Definition path.cpp:141
int sectionCount
Number of path sections for QML.
Definition path.h:107
int distance
The length of this path in meters.
Definition path.h:110
bool isEmpty() const
Returns true if this is an empty/not-set path.
Definition path.cpp:131
QPointF startPoint() const
First point on this path.
Definition path.cpp:158
static Path fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition path.cpp:175
void setSections(std::vector< PathSection > &&sections)
Sets the path sections.
Definition path.cpp:147
QPointF endPoint() const
Last point on this path.
Definition path.cpp:163
static QJsonObject toJson(const Path &path)
Serializes one path object to JSON.
Definition path.cpp:168
std::vector< KPublicTransport::PathSection > sections
Access to path sections for QML.
Definition path.h:105
Query operations and data types for accessing realtime public transport information from online servi...
QJsonValue value(QLatin1StringView key) const const
qreal angle() const const
bool empty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:12:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.