QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
19#include "qgsapplication.h"
20#include <QAbstractNetworkCache>
21#include <QImage>
22#include <QUrl>
23
24QCache<QUrl, QImage> QgsTileCache::sTileCache( 256 );
25QMutex QgsTileCache::sTileCacheMutex;
26
27
28void QgsTileCache::insertTile( const QUrl &url, const QImage &image )
29{
30 const QMutexLocker locker( &sTileCacheMutex );
31 sTileCache.insert( url, new QImage( image ) );
32}
33
34bool QgsTileCache::tile( const QUrl &url, QImage &image )
35{
36 QNetworkRequest req( url );
37 //Preprocessing might alter the url, so we need to make sure we store/retrieve the url after preprocessing
39 const QUrl adjUrl = req.url();
40
41 const QMutexLocker locker( &sTileCacheMutex );
42 bool success = false;
43 if ( QImage *i = sTileCache.object( adjUrl ) )
44 {
45 image = *i;
46 success = true;
47 }
48 else if ( QgsNetworkAccessManager::instance()->cache()->metaData( adjUrl ).isValid() )
49 {
50 if ( QIODevice *data = QgsNetworkAccessManager::instance()->cache()->data( adjUrl ) )
51 {
52 const QByteArray imageData = data->readAll();
53 delete data;
54
55 image = QImage::fromData( imageData );
56
57 // cache it as well (mutex is already locked)
58 // Check for null because it could be a redirect (see: https://github.com/qgis/QGIS/issues/24336 )
59 if ( ! image.isNull( ) )
60 {
61 sTileCache.insert( adjUrl, new QImage( image ) );
62 success = true;
63 }
64 }
65 }
66 return success;
67}
68
70{
71 const QMutexLocker locker( &sTileCacheMutex );
72 return sTileCache.totalCost();
73}
74
76{
77 const QMutexLocker locker( &sTileCacheMutex );
78 return sTileCache.maxCost();
79}
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