QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsmaprenderercache.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaprenderercache.cpp
3  --------------------------------------
4  Date : December 2013
5  Copyright : (C) 2013 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 "qgsmaprenderercache.h"
17 
18 #include "qgsmaplayerregistry.h"
19 #include "qgsmaplayer.h"
20 
22 {
23  clear();
24 }
25 
27 {
28  QMutexLocker lock( &mMutex );
29  clearInternal();
30 }
31 
33 {
35  mScale = 0;
36 
37  // make sure we are disconnected from all layers
39  for ( ; it != mCachedImages.constEnd(); ++it )
40  {
42  if ( layer )
43  {
44  disconnect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
45  }
46  }
48 }
49 
50 bool QgsMapRendererCache::init( const QgsRectangle& extent, double scale )
51 {
52  QMutexLocker lock( &mMutex );
53 
54  // check whether the params are the same
55  if ( extent == mExtent &&
56  qgsDoubleNear( scale, mScale ) )
57  return true;
58 
59  clearInternal();
60 
61  // set new params
62  mExtent = extent;
63  mScale = scale;
64 
65  return false;
66 }
67 
68 void QgsMapRendererCache::setCacheImage( const QString& layerId, const QImage& img )
69 {
70  QMutexLocker lock( &mMutex );
71  mCachedImages[layerId] = img;
72 
73  // connect to the layer to listen to layer's repaintRequested() signals
74  QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
75  if ( layer )
76  {
77  connect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
78  }
79 }
80 
82 {
83  QMutexLocker lock( &mMutex );
84  return mCachedImages.value( layerId );
85 }
86 
88 {
89  QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( sender() );
90  if ( layer )
91  clearCacheImage( layer->id() );
92 }
93 
95 {
96  QMutexLocker lock( &mMutex );
97 
98  mCachedImages.remove( layerId );
99 
100  QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
101  if ( layer )
102  {
103  disconnect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
104  }
105 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
QObject * sender() const
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
const_iterator constBegin() const
void clearCacheImage(const QString &layerId)
remove layer from the cache
void clear()
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:353
void setCacheImage(const QString &layerId, const QImage &img)
set cached image for the specified layer ID
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
bool init(const QgsRectangle &extent, double scale)
initialize cache: set new parameters and erase cache if parameters have changed
const_iterator constEnd() const
void clear()
invalidate the cache contents
const Key key(const T &value) const
void clearInternal()
invalidate cache contents (without locking)
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QMap< QString, QImage > mCachedImages
QImage cacheImage(const QString &layerId)
get cached image for the specified layer ID. Returns null image if it is not cached.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void layerRequestedRepaint()
remove layer (that emitted the signal) from the cache
const T value(const Key &key) const
int remove(const Key &key)