KPublicTransport

locationrequest.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "locationrequest.h"
8#include "datatypes/datatypes_p.h"
9#include "datatypes/json_p.h"
10#include "datatypes/locationutil_p.h"
11
12#include <QCryptographicHash>
13#include <QDebug>
14#include <QMetaEnum>
15#include <QSharedData>
16#include <QStringList>
17
18#include <cmath>
19
20using namespace KPublicTransport;
21
22namespace KPublicTransport {
23class LocationRequestPrivate : public QSharedData {
24public:
25 Location location;
26 QStringList backendIds;
27 int maximumDistance = 1000;
28 int maximumResults = 10;
30};
31}
32
33KPUBLICTRANSPORT_MAKE_GADGET(LocationRequest)
34KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, int, maximumDistance, setMaximumDistance)
35KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, int, maximumResults, setMaximumResults)
36KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, Location::Types, types, setTypes)
37
39 : d(new LocationRequestPrivate)
40{
41 d->location = locaction;
42}
43
45{
46 return hasCoordinate() || !d->location.name().isEmpty();
47}
48
50{
51 return d->location;
52}
53
54void LocationRequest::setLocation(const Location &location)
55{
56 d.detach();
57 d->location = location;
58}
59
60double LocationRequest::latitude() const
61{
62 return d->location.latitude();
63}
64
66{
67 d.detach();
68 d->location.setLatitude(lat);
69}
70
71double LocationRequest::longitude() const
72{
73 return d->location.longitude();
74}
75
77{
78 d.detach();
79 d->location.setLongitude(lon);
80}
81
82void LocationRequest::setCoordinate(double lat, double lon)
83{
84 d.detach();
85 d->location.setCoordinate(lat, lon);
86}
87
89{
90 return d->location.hasCoordinate();
91}
92
93QString LocationRequest::name() const
94{
95 if (d->location.name().isEmpty()) {
96 return QString(d->location.locality() + QLatin1Char(' ') + d->location.streetAddress()).trimmed();
97 }
98 return d->location.name();
99}
100
102{
103 d.detach();
104 d->location.setName(name);
105}
106
108{
110 hash.addData(LocationUtil::cacheKey(d->location).toUtf8());
111 const auto me = QMetaEnum::fromType<Location::Types>();
112 hash.addData(me.valueToKeys(types()));
115 return QString::fromUtf8(hash.result().toHex());
116}
117
118QJsonObject LocationRequest::toJson(const LocationRequest &req)
119{
120 auto obj = Json::toJson(req);
121 obj.insert(QLatin1String("location"), Location::toJson(req.location()));
122 return obj;
123}
124
126{
127 return d->backendIds;
128}
129
131{
132 d.detach();
133 d->backendIds = backendIds;
134}
135
136#include "moc_locationrequest.cpp"
Describes a location search.
bool isValid() const
Returns true if this is a valid request, that is it has enough parameters set to perform a query.
void setLatitude(double lat)
Sets the latitude of the location to search.
void setBackendIds(const QStringList &backendIds)
Set identifiers of backends that should be queried.
QString cacheKey() const
Unique string representation used for caching results.
void setLongitude(double lon)
Sets the longitude of the location to search.
void setCoordinate(double lat, double lon)
Search by geo coordinate.
void setName(const QString &name)
Search by name or name fragment.
int maximumResults
The maximum amount of expected results.
KPublicTransport::Location::Types types
The type of locations you are interested in.
KPublicTransport::Location location
Location object containing the search parameters.
bool hasCoordinate() const
Returns true if a valid geo coordinate has been set.
QStringList backendIds() const
Identifiers of the backends that should be queried.
int maximumDistance
When searching by coordinate, the expected maximum distance of the results in meters.
@ RentedVehicleStation
a pick-up/drop-off point for dock-based rental bike/scooter systems
Definition location.h:38
@ Stop
a public transport stop (train station, bus stop, etc)
Definition location.h:37
static QJsonObject toJson(const Location &loc)
Serializes one Location object to JSON.
Definition location.cpp:494
double latitude
Latitude of the location, in degree, NaN if unknown.
Definition location.h:54
Query operations and data types for accessing realtime public transport information from online servi...
QByteArray number(double n, char format, int precision)
QByteArray toHex(char separator) const const
bool addData(QIODevice *device)
QByteArray result() const const
QMetaEnum fromType()
QString fromUtf8(QByteArrayView str)
QString trimmed() const const
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.