QGeoMapReplyQGC.cpp 9.27 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/****************************************************************************
**
** Copyright (C) 2013 Aaron McCarthy <mccarthy.aaron@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
** 2015.4.4
** Adapted for use with QGroundControl
**
Gus Grubba's avatar
Gus Grubba committed
43
** Gus Grubba <gus@auterion.com>
dogmaphobic's avatar
dogmaphobic committed
44 45 46
**
****************************************************************************/

dogmaphobic's avatar
dogmaphobic committed
47 48
#include "QGCMapEngine.h"
#include "QGeoMapReplyQGC.h"
49
#include "QGeoTileFetcherQGC.h"
dogmaphobic's avatar
dogmaphobic committed
50

dogmaphobic's avatar
dogmaphobic committed
51 52 53
#include <QtLocation/private/qgeotilespec_p.h>
#include <QtNetwork/QNetworkAccessManager>
#include <QFile>
54
#include "TerrainTile.h"
dogmaphobic's avatar
dogmaphobic committed
55

56 57
int         QGeoTiledMapReplyQGC::_requestCount = 0;
QByteArray  QGeoTiledMapReplyQGC::_bingNoTileImage;
58

dogmaphobic's avatar
dogmaphobic committed
59 60 61
//-----------------------------------------------------------------------------
QGeoTiledMapReplyQGC::QGeoTiledMapReplyQGC(QNetworkAccessManager *networkManager, const QNetworkRequest &request, const QGeoTileSpec &spec, QObject *parent)
    : QGeoTiledMapReply(spec, parent)
62
    , _reply(nullptr)
dogmaphobic's avatar
dogmaphobic committed
63 64 65
    , _request(request)
    , _networkManager(networkManager)
{
66 67 68 69 70 71
    if (_bingNoTileImage.count() == 0) {
        QFile file(":/res/BingNoTileBytes.dat");
        file.open(QFile::ReadOnly);
        _bingNoTileImage = file.readAll();
        file.close();
    }
dogmaphobic's avatar
dogmaphobic committed
72
    if(_request.url().isEmpty()) {
Gus Grubba's avatar
Gus Grubba committed
73
        if(!_badMapbox.size()) {
74 75
            QFile b(":/res/notile.png");
            if(b.open(QFile::ReadOnly))
Gus Grubba's avatar
Gus Grubba committed
76
                _badMapbox = b.readAll();
77
        }
Gus Grubba's avatar
Gus Grubba committed
78
        setMapImageData(_badMapbox);
79 80 81
        setMapImageFormat("png");
        setFinished(true);
        setCached(false);
dogmaphobic's avatar
dogmaphobic committed
82
    } else {
83
        QGCFetchTileTask* task = getQGCMapEngine()->createFetchTileTask(getQGCMapEngine()->urlFactory()->getTypeFromId(spec.mapId()), spec.x(), spec.y(), spec.zoom());
dogmaphobic's avatar
dogmaphobic committed
84 85 86 87 88 89 90 91
        connect(task, &QGCFetchTileTask::tileFetched, this, &QGeoTiledMapReplyQGC::cacheReply);
        connect(task, &QGCMapTask::error, this, &QGeoTiledMapReplyQGC::cacheError);
        getQGCMapEngine()->addTask(task);
    }
}

//-----------------------------------------------------------------------------
QGeoTiledMapReplyQGC::~QGeoTiledMapReplyQGC()
92 93 94 95 96 97 98
{
    _clearReply();
}

//-----------------------------------------------------------------------------
void
QGeoTiledMapReplyQGC::_clearReply()
dogmaphobic's avatar
dogmaphobic committed
99
{
100
    _timer.stop();
dogmaphobic's avatar
dogmaphobic committed
101 102
    if (_reply) {
        _reply->deleteLater();
103
        _reply = nullptr;
104
        _requestCount--;
dogmaphobic's avatar
dogmaphobic committed
105 106
    }
}
107

dogmaphobic's avatar
dogmaphobic committed
108 109
//-----------------------------------------------------------------------------
void
110
QGeoTiledMapReplyQGC::abort()
dogmaphobic's avatar
dogmaphobic committed
111
{
112
    _timer.stop();
113 114
    if (_reply)
        _reply->abort();
Andreas Bircher's avatar
Andreas Bircher committed
115
    emit aborted();
dogmaphobic's avatar
dogmaphobic committed
116 117 118 119 120 121
}

//-----------------------------------------------------------------------------
void
QGeoTiledMapReplyQGC::networkReplyFinished()
{
122
    _timer.stop();
dogmaphobic's avatar
dogmaphobic committed
123
    if (!_reply) {
Andreas Bircher's avatar
Andreas Bircher committed
124
        emit aborted();
dogmaphobic's avatar
dogmaphobic committed
125 126 127
        return;
    }
    if (_reply->error() != QNetworkReply::NoError) {
Andreas Bircher's avatar
Andreas Bircher committed
128
        emit aborted();
dogmaphobic's avatar
dogmaphobic committed
129 130 131
        return;
    }
    QByteArray a = _reply->readAll();
132 133
    UrlFactory* urlFactory = getQGCMapEngine()->urlFactory();
    QString format = urlFactory->getImageFormat(tileSpec().mapId(), a);
134
    //-- Test for a specialized, elevation data (not map tile)
135
    if( getQGCMapEngine()->urlFactory()->isElevation(tileSpec().mapId())){
136
        a = TerrainTile::serialize(a);
137 138
        //-- Cache it if valid
        if(!a.isEmpty()) {
139 140 141 142
            getQGCMapEngine()->cacheTile(
                getQGCMapEngine()->urlFactory()->getTypeFromId(
                    tileSpec().mapId()),
                tileSpec().x(), tileSpec().y(), tileSpec().zoom(), a, format);
143
        }
144 145
        emit terrainDone(a, QNetworkReply::NoError);
    } else {
146 147
        MapProvider* mapProvider = urlFactory->getMapProviderFromId(tileSpec().mapId());
        if (mapProvider && mapProvider->_isBingProvider() && a.size() && _bingNoTileImage.size() && a == _bingNoTileImage) {
148 149 150 151 152 153 154 155 156 157 158 159
            // Bing doesn't return an error if you request a tile above supported zoom level
            // It instead returns an image of a missing tile graphic. We need to detect that
            // and error out so Qt will deal with zooming correctly even if it doesn't have the tile.
            // This allows us to zoom up to level 23 even though the tiles don't actually exist
            setError(QGeoTiledMapReply::CommunicationError, "Bing tile above zoom level");
        } else {
            //-- This is a map tile. Process and cache it if valid.
            setMapImageData(a);
            if(!format.isEmpty()) {
                setMapImageFormat(format);
                getQGCMapEngine()->cacheTile(getQGCMapEngine()->urlFactory()->getTypeFromId(tileSpec().mapId()), tileSpec().x(), tileSpec().y(), tileSpec().zoom(), a, format);
            }
160 161
        }
        setFinished(true);
dogmaphobic's avatar
dogmaphobic committed
162
    }
163
    _clearReply();
dogmaphobic's avatar
dogmaphobic committed
164 165 166 167 168 169
}

//-----------------------------------------------------------------------------
void
QGeoTiledMapReplyQGC::networkReplyError(QNetworkReply::NetworkError error)
{
170
    _timer.stop();
dogmaphobic's avatar
dogmaphobic committed
171 172 173
    if (!_reply) {
        return;
    }
174
    //-- Test for a specialized, elevation data (not map tile)
175
    if( getQGCMapEngine()->urlFactory()->isElevation(tileSpec().mapId())){
176 177 178 179 180 181 182 183
        emit terrainDone(QByteArray(), error);
    } else {
        //-- Regular map tile
        if (error != QNetworkReply::OperationCanceledError) {
            qWarning() << "Fetch tile error:" << _reply->errorString();
            setError(QGeoTiledMapReply::CommunicationError, _reply->errorString());
        }
        setFinished(true);
dogmaphobic's avatar
dogmaphobic committed
184
    }
185
    _clearReply();
dogmaphobic's avatar
dogmaphobic committed
186 187 188 189 190 191
}

//-----------------------------------------------------------------------------
void
QGeoTiledMapReplyQGC::cacheError(QGCMapTask::TaskType type, QString /*errorString*/)
{
192
    if(!getQGCMapEngine()->isInternetActive()) {
193
        if( getQGCMapEngine()->urlFactory()->isElevation(tileSpec().mapId())){
194 195 196 197 198
            emit terrainDone(QByteArray(), QNetworkReply::NetworkSessionFailedError);
        } else {
            setError(QGeoTiledMapReply::CommunicationError, "Network not available");
            setFinished(true);
        }
199 200 201 202 203 204 205 206 207 208 209 210
    } else {
        if(type != QGCMapTask::taskFetchTile) {
            qWarning() << "QGeoTiledMapReplyQGC::cacheError() for wrong task";
        }
        //-- Tile not in cache. Get it off the Internet.
#if !defined(__mobile__)
        QNetworkProxy proxy = _networkManager->proxy();
        QNetworkProxy tProxy;
        tProxy.setType(QNetworkProxy::DefaultProxy);
        _networkManager->setProxy(tProxy);
#endif
        _reply = _networkManager->get(_request);
211 212
        _reply->setParent(nullptr);
        connect(_reply, &QNetworkReply::finished, this, &QGeoTiledMapReplyQGC::networkReplyFinished);
213 214 215 216 217 218 219 220 221
        connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
#if !defined(__mobile__)
        _networkManager->setProxy(proxy);
#endif
        //- Wait for an answer up to 10 seconds
        connect(&_timer, &QTimer::timeout, this, &QGeoTiledMapReplyQGC::timeout);
        _timer.setSingleShot(true);
        _timer.start(10000);
        _requestCount++;
dogmaphobic's avatar
dogmaphobic committed
222 223 224 225 226 227 228
    }
}

//-----------------------------------------------------------------------------
void
QGeoTiledMapReplyQGC::cacheReply(QGCCacheTile* tile)
{
229
    //-- Test for a specialized, elevation data (not map tile)
230
    if( getQGCMapEngine()->urlFactory()->isElevation(tileSpec().mapId())){
231 232 233 234 235 236 237 238
        emit terrainDone(tile->img(), QNetworkReply::NoError);
    } else {
        //-- Regular map tile
        setMapImageData(tile->img());
        setMapImageFormat(tile->format());
        setFinished(true);
        setCached(true);
    }
dogmaphobic's avatar
dogmaphobic committed
239 240
    tile->deleteLater();
}
241 242 243 244 245 246 247 248

//-----------------------------------------------------------------------------
void
QGeoTiledMapReplyQGC::timeout()
{
    if(_reply) {
        _reply->abort();
    }
Andreas Bircher's avatar
Andreas Bircher committed
249
    emit aborted();
250
}