QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 }