QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
qgstilecache.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgstilecache.h
3
--------------------------------------
4
Date : September 2016
5
Copyright : (C) 2016 by Martin Dobias
6
Email : wonder dot sk at gmail dot com
7
***************************************************************************
8
* *
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
13
* *
14
***************************************************************************/
15
16
#include "
qgstilecache.h
"
17
18
#include "
qgsapplication.h
"
19
#include "
qgsnetworkaccessmanager.h
"
20
21
#include <QAbstractNetworkCache>
22
#include <QImage>
23
#include <QUrl>
24
25
QCache<QUrl, QImage> QgsTileCache::sTileCache( 256 );
26
QMutex QgsTileCache::sTileCacheMutex;
27
28
29
void
QgsTileCache::insertTile
(
const
QUrl &url,
const
QImage &image )
30
{
31
const
QMutexLocker locker( &sTileCacheMutex );
32
sTileCache.insert( url,
new
QImage( image ) );
33
}
34
35
bool
QgsTileCache::tile
(
const
QUrl &url, QImage &image )
36
{
37
QNetworkRequest req( url );
38
//Preprocessing might alter the url, so we need to make sure we store/retrieve the url after preprocessing
39
QgsNetworkAccessManager::instance
()->
preprocessRequest
( &req );
40
const
QUrl adjUrl = req.url();
41
42
const
QMutexLocker locker( &sTileCacheMutex );
43
bool
success =
false
;
44
if
( QImage *i = sTileCache.object( adjUrl ) )
45
{
46
image = *i;
47
success =
true
;
48
}
49
else
if
(
QgsNetworkAccessManager::instance
()->cache()->metaData( adjUrl ).isValid() )
50
{
51
if
( QIODevice *data =
QgsNetworkAccessManager::instance
()->cache()->data( adjUrl ) )
52
{
53
const
QByteArray imageData = data->readAll();
54
delete
data;
55
56
image = QImage::fromData( imageData );
57
58
// cache it as well (mutex is already locked)
59
// Check for null because it could be a redirect (see: https://github.com/qgis/QGIS/issues/24336 )
60
if
( !image.isNull() )
61
{
62
sTileCache.insert( adjUrl,
new
QImage( image ) );
63
success =
true
;
64
}
65
}
66
}
67
return
success;
68
}
69
70
int
QgsTileCache::totalCost
()
71
{
72
const
QMutexLocker locker( &sTileCacheMutex );
73
return
sTileCache.totalCost();
74
}
75
76
int
QgsTileCache::maxCost
()
77
{
78
const
QMutexLocker locker( &sTileCacheMutex );
79
return
sTileCache.maxCost();
80
}
QgsNetworkAccessManager::preprocessRequest
void preprocessRequest(QNetworkRequest *req) const
Preprocesses request.
Definition
qgsnetworkaccessmanager.cpp:972
QgsNetworkAccessManager::instance
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
Definition
qgsnetworkaccessmanager.cpp:212
QgsTileCache::tile
static bool tile(const QUrl &url, QImage &image)
Try to access a tile and load it into "image" argument.
Definition
qgstilecache.cpp:35
QgsTileCache::totalCost
static int totalCost()
how many tiles are stored in the in-memory cache
Definition
qgstilecache.cpp:70
QgsTileCache::insertTile
static void insertTile(const QUrl &url, const QImage &image)
Add a tile image with given URL to the cache.
Definition
qgstilecache.cpp:29
QgsTileCache::maxCost
static int maxCost()
how many tiles can be stored in the in-memory cache
Definition
qgstilecache.cpp:76
qgsapplication.h
qgsnetworkaccessmanager.h
qgstilecache.h
Generated on
for QGIS API Documentation by
1.17.0