2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the demonstration applications of the Qt Toolkit.
|
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-18 10:30:20 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-08 07:13:40 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QtNetwork>
|
|
|
|
#include "slippymap.h"
|
2017-02-21 16:14:37 +00:00
|
|
|
#include "qmath.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
uint qHash(const QPoint& p)
|
|
|
|
{
|
|
|
|
return p.x() * 17 ^ p.y();
|
|
|
|
}
|
|
|
|
|
|
|
|
// tile size in pixels
|
|
|
|
const int tdim = 256;
|
|
|
|
|
|
|
|
QPointF tileForCoordinate(qreal lat, qreal lng, int zoom)
|
|
|
|
{
|
2017-02-21 16:14:37 +00:00
|
|
|
qreal radianLat = qDegreesToRadians(lat);
|
2011-04-27 10:05:43 +00:00
|
|
|
qreal zn = static_cast<qreal>(1 << zoom);
|
|
|
|
qreal tx = (lng + 180.0) / 360.0;
|
2017-02-21 16:14:37 +00:00
|
|
|
qreal ty = 0.5 - log(tan(radianLat) + 1.0 / cos(radianLat)) / M_PI / 2.0;
|
2011-04-27 10:05:43 +00:00
|
|
|
return QPointF(tx * zn, ty * zn);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal longitudeFromTile(qreal tx, int zoom)
|
|
|
|
{
|
|
|
|
qreal zn = static_cast<qreal>(1 << zoom);
|
|
|
|
qreal lat = tx / zn * 360.0 - 180.0;
|
|
|
|
return lat;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal latitudeFromTile(qreal ty, int zoom)
|
|
|
|
{
|
|
|
|
qreal zn = static_cast<qreal>(1 << zoom);
|
|
|
|
qreal n = M_PI - 2 * M_PI * ty / zn;
|
2017-02-21 16:14:37 +00:00
|
|
|
return qRadiansToDegrees(atan(0.5 * (exp(n) - exp(-n))));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SlippyMap::SlippyMap(QObject *parent)
|
|
|
|
: QObject(parent), width(400), height(300), zoom(15),
|
|
|
|
latitude(59.9138204), longitude(10.7387413)
|
|
|
|
{
|
|
|
|
m_emptyTile = QPixmap(tdim, tdim);
|
|
|
|
m_emptyTile.fill(Qt::lightGray);
|
|
|
|
|
|
|
|
QNetworkDiskCache *cache = new QNetworkDiskCache;
|
2012-09-26 10:42:39 +00:00
|
|
|
cache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
2011-04-27 10:05:43 +00:00
|
|
|
m_manager.setCache(cache);
|
|
|
|
connect(&m_manager, SIGNAL(finished(QNetworkReply*)),
|
|
|
|
this, SLOT(handleNetworkData(QNetworkReply*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlippyMap::invalidate()
|
|
|
|
{
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QPointF ct = tileForCoordinate(latitude, longitude, zoom);
|
|
|
|
qreal tx = ct.x();
|
|
|
|
qreal ty = ct.y();
|
|
|
|
|
|
|
|
// top-left corner of the center tile
|
|
|
|
int xp = width / 2 - (tx - floor(tx)) * tdim;
|
|
|
|
int yp = height / 2 - (ty - floor(ty)) * tdim;
|
|
|
|
|
|
|
|
// first tile vertical and horizontal
|
|
|
|
int xa = (xp + tdim - 1) / tdim;
|
|
|
|
int ya = (yp + tdim - 1) / tdim;
|
|
|
|
int xs = static_cast<int>(tx) - xa;
|
|
|
|
int ys = static_cast<int>(ty) - ya;
|
|
|
|
|
|
|
|
// offset for top-left tile
|
|
|
|
m_offset = QPoint(xp - xa * tdim, yp - ya * tdim);
|
|
|
|
|
|
|
|
// last tile vertical and horizontal
|
|
|
|
int xe = static_cast<int>(tx) + (width - xp - 1) / tdim;
|
|
|
|
int ye = static_cast<int>(ty) + (height - yp - 1) / tdim;
|
|
|
|
|
|
|
|
// build a rect
|
|
|
|
m_tilesRect = QRect(xs, ys, xe - xs + 1, ye - ys + 1);
|
|
|
|
|
|
|
|
if (m_url.isEmpty())
|
|
|
|
download();
|
|
|
|
|
|
|
|
emit updated(QRect(0, 0, width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlippyMap::render(QPainter *p, const QRect &rect)
|
|
|
|
{
|
|
|
|
for (int x = 0; x <= m_tilesRect.width(); ++x)
|
|
|
|
for (int y = 0; y <= m_tilesRect.height(); ++y) {
|
|
|
|
QPoint tp(x + m_tilesRect.left(), y + m_tilesRect.top());
|
|
|
|
QRect box = tileRect(tp);
|
|
|
|
if (rect.intersects(box)) {
|
|
|
|
if (m_tilePixmaps.contains(tp))
|
|
|
|
p->drawPixmap(box, m_tilePixmaps.value(tp));
|
|
|
|
else
|
|
|
|
p->drawPixmap(box, m_emptyTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlippyMap::pan(const QPoint &delta)
|
|
|
|
{
|
|
|
|
QPointF dx = QPointF(delta) / qreal(tdim);
|
|
|
|
QPointF center = tileForCoordinate(latitude, longitude, zoom) - dx;
|
|
|
|
latitude = latitudeFromTile(center.y(), zoom);
|
|
|
|
longitude = longitudeFromTile(center.x(), zoom);
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlippyMap::handleNetworkData(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
QImage img;
|
|
|
|
QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint();
|
|
|
|
QUrl url = reply->url();
|
|
|
|
if (!reply->error())
|
|
|
|
if (!img.load(reply, 0))
|
|
|
|
img = QImage();
|
|
|
|
reply->deleteLater();
|
|
|
|
m_tilePixmaps[tp] = QPixmap::fromImage(img);
|
|
|
|
if (img.isNull())
|
|
|
|
m_tilePixmaps[tp] = m_emptyTile;
|
|
|
|
emit updated(tileRect(tp));
|
|
|
|
|
|
|
|
// purge unused spaces
|
|
|
|
QRect bound = m_tilesRect.adjusted(-2, -2, 2, 2);
|
|
|
|
foreach(QPoint tp, m_tilePixmaps.keys())
|
|
|
|
if (!bound.contains(tp))
|
|
|
|
m_tilePixmaps.remove(tp);
|
|
|
|
|
|
|
|
download();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlippyMap::download()
|
|
|
|
{
|
|
|
|
QPoint grab(0, 0);
|
|
|
|
for (int x = 0; x <= m_tilesRect.width(); ++x)
|
|
|
|
for (int y = 0; y <= m_tilesRect.height(); ++y) {
|
|
|
|
QPoint tp = m_tilesRect.topLeft() + QPoint(x, y);
|
|
|
|
if (!m_tilePixmaps.contains(tp)) {
|
|
|
|
grab = tp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (grab == QPoint(0, 0)) {
|
|
|
|
m_url = QUrl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = "http://tile.openstreetmap.org/%1/%2/%3.png";
|
|
|
|
m_url = QUrl(path.arg(zoom).arg(grab.x()).arg(grab.y()));
|
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(m_url);
|
2015-01-28 08:44:43 +00:00
|
|
|
request.setRawHeader("User-Agent", "The Qt Company (Qt) Graphics Dojo 1.0");
|
2011-04-27 10:05:43 +00:00
|
|
|
request.setAttribute(QNetworkRequest::User, QVariant(grab));
|
|
|
|
m_manager.get(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect SlippyMap::tileRect(const QPoint &tp)
|
|
|
|
{
|
|
|
|
QPoint t = tp - m_tilesRect.topLeft();
|
|
|
|
int x = t.x() * tdim + m_offset.x();
|
|
|
|
int y = t.y() * tdim + m_offset.y();
|
|
|
|
return QRect(x, y, tdim, tdim);
|
|
|
|
}
|