QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  foreach ( 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  }
46  mCachedImages.clear();
47 }
48 
49 bool QgsMapRendererCache::init( QgsRectangle extent, double scale )
50 {
51  QMutexLocker lock( &mMutex );
52 
53  // check whether the params are the same
54  if ( extent == mExtent &&
55  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( 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 
80 QImage QgsMapRendererCache::cacheImage( QString layerId )
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 
93 void QgsMapRendererCache::clearCacheImage( QString layerId )
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:48
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
static QgsMapLayerRegistry * instance()
Definition: qgssingleton.h:23
void setCacheImage(QString layerId, const QImage &img)
set cached image for the specified layer ID
void clearCacheImage(QString layerId)
remove layer from the cache
QImage cacheImage(QString layerId)
get cached image for the specified layer ID. Returns null image if it is not cached.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:95
void clear()
invalidate the cache contents
void clearInternal()
invalidate cache contents (without locking)
QMap< QString, QImage > mCachedImages
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
bool init(QgsRectangle extent, double scale)
initialize cache: set new parameters and erase cache if parameters have changed
void layerRequestedRepaint()
remove layer (that emitted the signal) from the cache