QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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"
20
21#include <QAbstractNetworkCache>
22#include <QImage>
23#include <QUrl>
24
25QCache<QUrl, QImage> QgsTileCache::sTileCache( 256 );
26QMutex QgsTileCache::sTileCacheMutex;
27
28
29void QgsTileCache::insertTile( const QUrl &url, const QImage &image )
30{
31 const QMutexLocker locker( &sTileCacheMutex );
32 sTileCache.insert( url, new QImage( image ) );
33}
34
35bool 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
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
71{
72 const QMutexLocker locker( &sTileCacheMutex );
73 return sTileCache.totalCost();
74}
75
77{
78 const QMutexLocker locker( &sTileCacheMutex );
79 return sTileCache.maxCost();
80}
void preprocessRequest(QNetworkRequest *req) const
Preprocesses request.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
static bool tile(const QUrl &url, QImage &image)
Try to access a tile and load it into "image" argument.
static int totalCost()
how many tiles are stored in the in-memory cache
static void insertTile(const QUrl &url, const QImage &image)
Add a tile image with given URL to the cache.
static int maxCost()
how many tiles can be stored in the in-memory cache