KPublicTransport

locationrequest.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
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 
20 using namespace KPublicTransport;
21 
22 namespace KPublicTransport {
23 class LocationRequestPrivate : public QSharedData {
24 public:
26  QStringList backendIds;
27  int maximumDistance = 1000;
28  int maximumResults = 10;
30 };
31 }
32 
33 KPUBLICTRANSPORT_MAKE_GADGET(LocationRequest)
34 KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, int, maximumDistance, setMaximumDistance)
35 KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, int, maximumResults, setMaximumResults)
36 KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, Location::Types, types, setTypes)
37 
38 LocationRequest::LocationRequest(const Location &locaction)
39  : d(new LocationRequestPrivate)
40 {
41  d->location = locaction;
42 }
43 
45 {
46  return hasCoordinate() || !d->location.name().isEmpty();
47 }
48 
49 Location LocationRequest::location() const
50 {
51  return d->location;
52 }
53 
54 void LocationRequest::setLocation(const Location &location)
55 {
56  d.detach();
57  d->location = location;
58 }
59 
60 float LocationRequest::latitude() const
61 {
62  return d->location.latitude();
63 }
64 
66 {
67  d.detach();
68  d->location.setLatitude(lat);
69 }
70 
71 float LocationRequest::longitude() const
72 {
73  return d->location.longitude();
74 }
75 
77 {
78  d.detach();
79  d->location.setLongitude(lon);
80 }
81 
82 void LocationRequest::setCoordinate(float lat, float lon)
83 {
84  d.detach();
85  d->location.setCoordinate(lat, lon);
86 }
87 
89 {
90  return d->location.hasCoordinate();
91 }
92 
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()));
113  hash.addData(QByteArray::number(maximumDistance()));
114  hash.addData(QByteArray::number(maximumResults()));
115  return QString::fromUtf8(hash.result().toHex());
116 }
117 
118 QJsonObject LocationRequest::toJson(const LocationRequest &req)
119 {
120  auto obj = Json::toJson(req);
121  obj.insert(QStringLiteral("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"
a public transport stop (train station, bus stop, etc)
Definition: location.h:36
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
bool isValid() const
Returns true if this is a valid request, that is it has enough parameters set to perform a query...
QByteArray toHex() const const
QStringList backendIds() const
Identifiers of the backends that should be queried.
a pick-up/drop-off point for dock-based rental bike/scooter systems
Definition: location.h:37
QVariant location(const QVariant &res)
void setName(const QString &name)
Search by name or name fragment.
QString fromUtf8(const char *str, int size)
QByteArray number(int n, int base)
void addData(const char *data, int length)
QString name() const
The name of the location to search.
void setCoordinate(float lat, float lon)
Search by geo coordinate.
QString cacheKey() const
Unique string representation used for caching results.
float latitude() const
Latitude of the location to search.
void setBackendIds(const QStringList &backendIds)
Set identifiers of backends that should be queried.
static QJsonObject toJson(const Location &loc)
Serializes one Location object to JSON.
Definition: location.cpp:444
bool hasCoordinate() const
Returns true if a valid geo coordinate has been set.
QByteArray result() const const
Describes a location search.
void setLatitude(float lat)
Sets the latitude of the location to search.
KPublicTransport::Location location
Location object containing the search parameters.
float longitude() const
Longitude of the location to search.
void setLongitude(float lon)
Sets the longitude of the location to search.
Types types(const QStringList &mimeTypes)
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.