QGIS API Documentation  2.13.0-Master
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
38  Q_FOREACH ( const QString& layerId, mCachedImages.keys() )
39  {
40  QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
41  if ( layer )
42  {
43  disconnect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
44  }
45  }
47 }
48 
49 bool QgsMapRendererCache::init( const QgsRectangle& extent, double scale )
50 {
51  QMutexLocker lock( &mMutex );
52 
53  // check whether the params are the same
54  if ( extent == mExtent &&
55  qgsDoubleNear( scale, mScale ) )
56  return true;
57 
58  clearInternal();
59 
60  // set new params
61  mExtent = extent;
62  mScale = scale;
63 
64  return false;
65 }
66 
67 void QgsMapRendererCache::setCacheImage( const QString& layerId, const QImage& img )
68 {
69  QMutexLocker lock( &mMutex );
70  mCachedImages[layerId] = img;
71 
72  // connect to the layer to listen to layer's repaintRequested() signals
73  QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
74  if ( layer )
75  {
76  connect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
77  }
78 }
79 
81 {
82  QMutexLocker lock( &mMutex );
83  return mCachedImages.value( layerId );
84 }
85 
87 {
88  QgsMapLayer* layer = qobject_cast<QgsMapLayer*>( sender() );
89  if ( layer )
90  clearCacheImage( layer->id() );
91 }
92 
94 {
95  QMutexLocker lock( &mMutex );
96 
97  mCachedImages.remove( layerId );
98 
99  QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
100  if ( layer )
101  {
102  disconnect( layer, SIGNAL( repaintRequested() ), this, SLOT( layerRequestedRepaint() ) );
103  }
104 }
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
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)
Definition: qgis.h:285
QgsMapLayer * mapLayer(const QString &theLayerId)
Retrieve a pointer to a loaded layer by id.
QList< Key > keys() const
void setCacheImage(const QString &layerId, const QImage &img)
set cached image for the specified layer ID
bool init(const QgsRectangle &extent, double scale)
initialize cache: set new parameters and erase cache if parameters have changed
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
void clear()
invalidate the cache contents
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)