QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
23 QCache<QUrl, QImage> QgsTileCache::sTileCache( 256 );
24 QMutex QgsTileCache::sTileCacheMutex;
25 
26 
27 void QgsTileCache::insertTile( const QUrl &url, const QImage &image )
28 {
29  QMutexLocker locker( &sTileCacheMutex );
30  sTileCache.insert( url, new QImage( image ) );
31 }
32 
33 bool QgsTileCache::tile( const QUrl &url, QImage &image )
34 {
35  QMutexLocker locker( &sTileCacheMutex );
36  bool success = false;
37  if ( QImage *i = sTileCache.object( url ) )
38  {
39  image = *i;
40  success = true;
41  }
42  else if ( QgsNetworkAccessManager::instance()->cache()->metaData( url ).isValid() )
43  {
44  if ( QIODevice *data = QgsNetworkAccessManager::instance()->cache()->data( url ) )
45  {
46  QByteArray imageData = data->readAll();
47  delete data;
48 
49  image = QImage::fromData( imageData );
50 
51  // cache it as well (mutex is already locked)
52  // Check for null because it could be a redirect (see: https://github.com/qgis/QGIS/issues/24336 )
53  if ( ! image.isNull( ) )
54  {
55  sTileCache.insert( url, new QImage( image ) );
56  success = true;
57  }
58  }
59  }
60  return success;
61 }
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
static void insertTile(const QUrl &url, const QImage &image)
Add a tile image with given URL to the cache.
static bool tile(const QUrl &url, QImage &image)
Try to access a tile and load it into "image" argument.