Kgapi

staticmapurl.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Jan Grulich <grulja@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "staticmapurl.h"
8
9#include <QUrlQuery>
10
11using namespace KGAPI2;
12
13class Q_DECL_HIDDEN StaticMapUrl::Private
14{
15public:
16 Private();
17 Private(const Private &other);
18
19 void init(const Private &other);
20
21 StaticMapUrl::LocationType locationType;
22 StaticMapUrl::ImageFormat format;
23
24 QString locationString;
25 KContacts::Address locationAddress;
26 KContacts::Geo locationGeo;
27
28 StaticMapUrl::MapType maptype;
31 StaticMapUrl::Scale scale;
32 bool sensor;
33 QSize size;
34
35 QString visibleString;
36 KContacts::Address visibleAddress;
37 KContacts::Geo visibleGeo;
38 StaticMapUrl::LocationType visibleLocationType;
39
40 qint32 zoom;
41};
42
43StaticMapUrl::Private::Private()
44 : locationType(StaticMapUrl::Undefined)
45 , format(StaticMapUrl::PNG)
46 , maptype(StaticMapUrl::Roadmap)
47 , scale(StaticMapUrl::Normal)
48 , sensor(false)
49 , visibleLocationType(StaticMapUrl::Undefined)
50 , zoom(-1)
51{
52}
53
54StaticMapUrl::Private::Private(const Private &other)
55{
56 init(other);
57}
58
59void StaticMapUrl::Private::init(const StaticMapUrl::Private &other)
60{
61 locationType = other.locationType;
62 format = other.format;
63 locationString = other.locationString;
64 locationAddress = other.locationAddress;
65 locationGeo = other.locationGeo;
66 maptype = other.maptype;
67 markers = other.markers;
68 paths = other.paths;
69 scale = other.scale;
70 sensor = other.sensor;
71 size = other.size;
72 visibleString = other.visibleString;
73 visibleAddress = other.visibleAddress;
74 visibleGeo = other.visibleGeo;
75 visibleLocationType = other.visibleLocationType;
76 zoom = other.zoom;
77}
78
80 : d(new Private)
81{
82}
83
85 : d(new Private(*(other.d)))
86{
87}
88
90{
91 delete d;
92}
93
95{
96 if (&other == this) {
97 return *this;
98 }
99
100 d->init(*(other.d));
101 return *this;
102}
103
104StaticMapUrl::StaticMapUrl(const QString &location, const QSize &size, quint32 zoom, bool sensor)
105 : d(new Private)
106{
107 setLocation(location);
108 setSize(size);
109 setZoomLevel(zoom);
110 setSensorUsed(sensor);
111}
112
113StaticMapUrl::StaticMapUrl(const KContacts::Address &address, const QSize &size, quint32 zoom, bool sensor)
114 : d(new Private)
115{
116 setLocation(address);
117 setSize(size);
118 setZoomLevel(zoom);
119 setSensorUsed(sensor);
120}
121
122StaticMapUrl::StaticMapUrl(const KContacts::Geo &geo, const QSize &size, quint32 zoom, bool sensor)
123 : d(new Private)
124{
125 setLocation(geo);
126 setSize(size);
127 setZoomLevel(zoom);
128 setSensorUsed(sensor);
129}
130
131StaticMapUrl::LocationType StaticMapUrl::locationType() const
132{
133 return d->locationType;
134}
135
136StaticMapUrl::ImageFormat StaticMapUrl::format() const
137{
138 return d->format;
139}
140
141void StaticMapUrl::setFormat(const StaticMapUrl::ImageFormat format)
142{
143 d->format = format;
144}
145
147{
148 bool maOrPa = true;
149
150 if (d->markers.isEmpty()) {
151 for (const StaticMapPath &path : std::as_const(d->paths)) {
152 if (!path.isValid()) {
153 maOrPa = false;
154 }
155 }
156 } else {
157 for (const StaticMapMarker &marker : std::as_const(d->markers)) {
158 if (!marker.isValid()) {
159 maOrPa = false;
160 }
161 }
162 }
163
164 if (maOrPa) {
165 if ((d->locationType == Undefined || d->zoom == -1) && (d->visibleLocationType == Undefined)) {
166 return false;
167 }
168 }
169
170 return !(d->size.isEmpty());
171}
172
174{
175 return d->locationString;
176}
177
179{
180 d->locationString = location;
181 d->locationType = String;
182 d->locationAddress.clear();
183 d->locationGeo.setLatitude(91);
184 d->locationGeo.setLongitude(181);
185}
186
188{
189 return d->locationAddress;
190}
191
193{
194 d->locationAddress = address;
195 d->locationType = KABCAddress;
196 d->locationString.clear();
197 d->locationGeo.setLatitude(91);
198 d->locationGeo.setLongitude(181);
199}
200
202{
203 return d->locationGeo;
204}
205
207{
208 d->locationGeo = geo;
209 d->locationType = KABCGeo;
210 d->locationString.clear();
211 d->locationAddress.clear();
212}
213
214StaticMapUrl::MapType StaticMapUrl::mapType() const
215{
216 return d->maptype;
217}
218
219void StaticMapUrl::setMapType(const StaticMapUrl::MapType type)
220{
221 d->maptype = type;
222}
223
225{
226 return d->markers;
227}
228
230{
232 markers << marker;
233 d->markers = markers;
234}
235
237{
238 d->markers = markers;
239}
240
242{
243 return d->paths;
244}
245
247{
249 paths << path;
250 d->paths = paths;
251}
252
254{
255 d->paths = paths;
256}
257
259{
260 return d->size;
261}
262
264{
265 d->size = size;
266}
267
268StaticMapUrl::Scale StaticMapUrl::scale() const
269{
270 return d->scale;
271}
272
273void StaticMapUrl::setScale(const Scale scale)
274{
275 d->scale = scale;
276}
277
279{
280 return d->sensor;
281}
282
283void StaticMapUrl::setSensorUsed(const bool sensor)
284{
285 d->sensor = sensor;
286}
287
289{
290 return d->visibleString;
291}
292
294{
295 d->visibleString = location;
296 d->visibleLocationType = String;
297 d->visibleAddress.clear();
298 d->visibleGeo.setLatitude(911);
299 d->visibleGeo.setLongitude(181);
300}
301
303{
304 return d->locationAddress;
305}
306
308{
309 d->visibleAddress = address;
310 d->visibleLocationType = KABCAddress;
311 d->visibleString.clear();
312 d->visibleGeo.setLatitude(911);
313 d->visibleGeo.setLongitude(181);
314}
315
317{
318 return d->locationGeo;
319}
320
322{
323 d->visibleGeo = geo;
324 d->visibleLocationType = KABCGeo;
325 d->visibleString.clear();
326 d->visibleAddress.clear();
327}
328
329StaticMapUrl::LocationType StaticMapUrl::visibleLocationType() const
330{
331 return d->visibleLocationType;
332}
333
335{
336 return d->zoom;
337}
338
339void StaticMapUrl::setZoomLevel(const quint32 zoom)
340{
341 d->zoom = zoom;
342}
343
345{
346 QUrl url(QStringLiteral("http://maps.googleapis.com/maps/api/staticmap"));
347 QUrlQuery query(url);
348
349 if (d->locationType != Undefined) {
350 QString param;
351
352 switch (d->locationType) {
353 case Undefined:
354 case String:
355 param = d->locationString;
358 param.replace(QLatin1Char(' '), QLatin1Char('+'));
359 query.addQueryItem(QStringLiteral("center"), param);
360 break;
361 case KABCAddress:
362 param = d->locationAddress.formatted(KContacts::AddressFormatStyle::Postal);
365 param.replace(QLatin1Char(' '), QLatin1Char('+'));
366 param.replace(QLatin1Char('\n'), QLatin1Char(','));
367 query.addQueryItem(QStringLiteral("center"), param);
368 break;
369 case KABCGeo:
370 param = QString::number(d->locationGeo.latitude()) + QLatin1Char(',') + QString::number(d->locationGeo.longitude());
371 query.addQueryItem(QStringLiteral("center"), param);
372 break;
373 }
374 }
375
376 if (d->zoom != -1) {
377 query.addQueryItem(QStringLiteral("zoom"), QString::number(d->zoom));
378 }
379
380 if (!d->size.isEmpty()) {
381 QString size = QString::number(d->size.width()) + QLatin1Char('x') + QString::number(d->size.height());
382 query.addQueryItem(QStringLiteral("size"), size);
383 }
384
385 if (d->scale != Normal) {
386 query.addQueryItem(QStringLiteral("scale"), QString::number(2));
387 }
388 if (d->format != PNG) {
390
391 switch (d->format) {
392 case PNG:
393 case PNG32:
394 format = QStringLiteral("png32");
395 break;
396 case GIF:
397 format = QStringLiteral("gif");
398 break;
399 case JPG:
400 format = QStringLiteral("jpg");
401 break;
402 case JPGBaseline:
403 format = QStringLiteral("jpg-baseline");
404 break;
405 }
406
407 query.addQueryItem(QStringLiteral("format"), format);
408 }
409
410 if (d->maptype != Roadmap) {
411 QString maptype;
412
413 switch (d->maptype) {
414 case Roadmap:
415 case Satellite:
416 maptype = QStringLiteral("satellite");
417 break;
418 case Terrain:
419 maptype = QStringLiteral("terrain");
420 break;
421 case Hybrid:
422 maptype = QStringLiteral("hybrid");
423 break;
424 }
425
426 query.addQueryItem(QStringLiteral("maptype"), maptype);
427 }
428
429 for (const StaticMapMarker &marker : std::as_const(d->markers)) {
430 if (marker.isValid()) {
431 query.addQueryItem(QStringLiteral("markers"), marker.toString());
432 }
433 }
434
435 for (const StaticMapPath &path : std::as_const(d->paths)) {
436 if (path.isValid()) {
437 query.addQueryItem(QStringLiteral("path"), path.toString());
438 }
439 }
440
441 if (d->visibleLocationType != Undefined) {
442 QString param;
443
444 switch (d->visibleLocationType) {
445 case Undefined:
446 case String:
447 param = d->visibleString;
450 param.replace(QLatin1Char(' '), QLatin1Char('+'));
451 query.addQueryItem(QStringLiteral("visible"), param);
452 break;
453 case KABCAddress:
454 param = d->visibleAddress.formatted(KContacts::AddressFormatStyle::Postal);
457 param.replace(QLatin1Char(' '), QLatin1Char('+'));
458 param.replace(QLatin1Char('\n'), QLatin1Char(','));
459 query.addQueryItem(QStringLiteral("visible"), param);
460 break;
461 case KABCGeo:
462 param = QString::number(d->visibleGeo.latitude()) + QLatin1Char(',') + QString::number(d->visibleGeo.longitude());
463 query.addQueryItem(QStringLiteral("visible"), param);
464 break;
465 }
466 }
467
468 if (d->sensor) {
469 query.addQueryItem(QStringLiteral("sensor"), QStringLiteral("true"));
470 } else {
471 query.addQueryItem(QStringLiteral("sensor"), QStringLiteral("false"));
472 }
473
474 url.setQuery(query);
475 return url;
476}
Q_INVOKABLE QString formatted(KContacts::AddressFormatStyle style, const QString &realName=QString(), const QString &orgaName=QString()) const
void setLatitude(float latitude)
float longitude() const
float latitude() const
void setLongitude(float longitude)
Represents marker with defined label, color, size and markers.
Represents path with defined locations, weight, color and color for filled area.
A class to build a URL from StaticMapMarkers and StaticMapPaths to fetch a map tile.
LocationType locationType() const
Returns in which format the location is stored.
KContacts::Geo locationGeo() const
Returns map center in KContacts::Geo.
QSize size() const
Returns size of map tile.
void setVisibleLocation(const QString &location)
Sets visible location.
KContacts::Geo visibleLocationGeo() const
Returns visible area in KContacts::Geo.
void setScale(const Scale scale)
Sets scale of map (default is 1)
KContacts::Address locationAddress() const
Returns map center in KContacts::Address.
Scale scale() const
Returns scale of map.
QList< StaticMapPath > paths() const
Returns list paths.
void setMapType(const MapType type)
Sets type of map (roadmap by default)
QList< StaticMapMarker > markers() const
Returns list of markers.
MapType mapType() const
Returns type of map.
void setPaths(const QList< StaticMapPath > &paths)
Adds paths to map.
void setSize(const QSize &size)
Sets size of requested map tile.
QString locationString() const
Returns map center in QString.
StaticMapUrl & operator=(const StaticMapUrl &other)
Assignment operator.
void setMarker(const StaticMapMarker &marker)
Adds marker to map.
void setPath(const StaticMapPath &path)
Adds path to map.
StaticMapUrl()
Constructs an empty StaticMapUrl.
bool sensorUsed() const
Returns whether the application uses a sensor to determine user's location.
qint8 zoomLevel() const
Returns zoom level of map.
void setLocation(const QString &location)
Defines center of the map.
virtual ~StaticMapUrl()
Destructor.
QUrl url() const
Returns constructed url from all defined parameters.
KContacts::Address visibleLocationAddress() const
Returns visible area in KContacts::Address.
bool isValid() const
Returns whether map url is valid.
void setFormat(const ImageFormat format)
Sets map image format (default is PNG)
void setSensorUsed(const bool sensor)
Sets whether the application uses a sensor to determine the user's location.
void setZoomLevel(const quint32 zoom)
Sets zoom level of the map.
QString visibleLocationString() const
Returns visible area in QString.
LocationType visibleLocationType() const
Returns type of visible location.
void setMarkers(const QList< StaticMapMarker > &markers)
Adds markers to map.
ImageFormat format() const
Returns map image format.
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
QAction * zoom(const QObject *recvr, const char *slot, QObject *parent)
QCA_EXPORT void init()
int height() const const
bool isEmpty() const const
int width() const const
void clear()
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
void setQuery(const QString &query, ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.