26#include "moc_qgsmaprenderercache.cpp"
35 QMutexLocker lock( &mMutex );
39void QgsMapRendererCache::clearInternal()
53 mCachedImages.clear();
54 mConnectedLayers.clear();
57void QgsMapRendererCache::dropUnusedConnections()
60 const QSet< QgsWeakMapLayerPointer > disconnects = mConnectedLayers.subtract( stillDepends );
70 mConnectedLayers = stillDepends;
75 QSet< QgsWeakMapLayerPointer > result;
76 QMap<QString, CacheParameters>::const_iterator it = mCachedImages.constBegin();
77 for ( ; it != mCachedImages.constEnd(); ++it )
91 QMutexLocker lock( &mMutex );
94 if ( extent == mExtent &&
110 QMutexLocker lock( &mMutex );
113 if ( extent == mExtent &&
128 QMutexLocker lock( &mMutex );
139 QMutexLocker lock( &mMutex );
141 if ( extent != mExtent || mapToPixel != mMtp )
143 auto it = mCachedImages.constFind( cacheKey );
144 if ( it != mCachedImages.constEnd() )
149 if ( it->cachedExtent == mExtent && it->cachedMtp == mMtp )
154 CacheParameters params;
155 params.cachedImage = image;
156 params.cachedExtent = extent;
157 params.cachedMtp = mapToPixel;
164 params.dependentLayers << layer;
169 mConnectedLayers << layer;
174 mCachedImages[cacheKey] = params;
179 QMutexLocker lock( &mMutex );
181 auto it = mCachedImages.constFind( cacheKey );
182 if ( it != mCachedImages.constEnd() )
184 const CacheParameters ¶ms = it.value();
185 return ( params.cachedExtent == mExtent &&
186 params.cachedMtp.
transform() == mMtp.transform() );
196 auto it = mCachedImages.constFind( cacheKey );
197 if ( it != mCachedImages.constEnd() )
199 const CacheParameters ¶ms = it.value();
202 if ( minimumScaleThreshold != 0 && mMtp.mapUnitsPerPixel() < params.cachedMtp.
mapUnitsPerPixel() * minimumScaleThreshold )
204 if ( maximumScaleThreshold != 0 && mMtp.mapUnitsPerPixel() > params.cachedMtp.
mapUnitsPerPixel() * maximumScaleThreshold )
217 QMutexLocker lock( &mMutex );
218 return mCachedImages.value( cacheKey ).cachedImage;
223 qreal x = point.
x(), y = point.
y();
225 return QPointF( x, y ) * scale;
230 QMutexLocker lock( &mMutex );
231 const CacheParameters params = mCachedImages.value( cacheKey );
233 if ( params.cachedExtent == mExtent &&
236 return params.cachedImage;
246 if ( intersection.
isNull() )
252 const QRectF targetRect( ulT.x(), ulT.y(), lrT.x() - ulT.x(), lrT.y() - ulT.y() );
255 const QPointF ulS = _transform( params.cachedMtp,
QgsPointXY( intersection.
xMinimum(), intersection.
yMaximum() ), params.cachedImage.devicePixelRatio() );
256 const QPointF lrS = _transform( params.cachedMtp,
QgsPointXY( intersection.
xMaximum(), intersection.
yMinimum() ), params.cachedImage.devicePixelRatio() );
257 const QRectF sourceRect( ulS.x(), ulS.y(), lrS.x() - ulS.x(), lrS.y() - ulS.y() );
260 QImage ret( params.cachedImage.size(), params.cachedImage.format() );
261 ret.setDevicePixelRatio( params.cachedImage.devicePixelRatio() );
262 ret.setDotsPerMeterX( params.cachedImage.dotsPerMeterX() );
263 ret.setDotsPerMeterY( params.cachedImage.dotsPerMeterY() );
264 ret.fill( Qt::transparent );
266 painter.begin( &ret );
267 painter.drawImage( targetRect, params.cachedImage, sourceRect );
275 auto it = mCachedImages.constFind( cacheKey );
276 if ( it != mCachedImages.constEnd() )
278 return _qgis_listQPointerToRaw( ( *it ).dependentLayers );
280 return QList< QgsMapLayer * >();
284void QgsMapRendererCache::layerRequestedRepaint()
286 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sender() );
295 QMutexLocker lock( &mMutex );
298 QMap<QString, CacheParameters>::iterator it = mCachedImages.begin();
299 for ( ; it != mCachedImages.end(); )
301 if ( !it.value().dependentLayers.contains( layer ) )
307 it = mCachedImages.erase( it );
309 dropUnusedConnections();
314 QMutexLocker lock( &mMutex );
316 mCachedImages.remove( cacheKey );
317 dropUnusedConnections();
@ Unknown
Unknown distance unit.
Base class for all map layer types.
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
bool updateParameters(const QgsRectangle &extent, const QgsMapToPixel &mtp)
Sets extent and scale parameters.
QList< QgsMapLayer * > dependentLayers(const QString &cacheKey) const
Returns a list of map layers on which an image in the cache depends.
void clear()
Invalidates the cache contents, clearing all cached images.
bool hasCacheImage(const QString &cacheKey) const
Returns true if the cache contains an image with the specified cacheKey that has the same extent and ...
QImage cacheImage(const QString &cacheKey) const
Returns the cached image for the specified cacheKey.
bool hasAnyCacheImage(const QString &cacheKey, double minimumScaleThreshold=0, double maximumScaleThreshold=0) const
Returns true if the cache contains an image with the specified cacheKey with any cache's parameters (...
void setCacheImageWithParameters(const QString &cacheKey, const QImage &image, const QgsRectangle &extent, const QgsMapToPixel &mapToPixel, const QList< QgsMapLayer * > &dependentLayers=QList< QgsMapLayer * >())
Set the cached image for a particular cacheKey, using a specific extent and mapToPixel (which may dif...
void setCacheImage(const QString &cacheKey, const QImage &image, const QList< QgsMapLayer * > &dependentLayers=QList< QgsMapLayer * >())
Set the cached image for a particular cacheKey, using the current cache parameters.
void invalidateCacheForLayer(QgsMapLayer *layer)
Invalidates cached images which relate to the specified map layer.
void clearCacheImage(const QString &cacheKey)
Removes an image from the cache with matching cacheKey.
QImage transformedCacheImage(const QString &cacheKey, const QgsMapToPixel &mtp) const
Returns the cached image for the specified cacheKey transformed to the particular extent and scale.
Q_DECL_DEPRECATED bool init(const QgsRectangle &extent, double scale)
Initialize cache: sets extent and scale parameters and clears the cache if any parameters have change...
Perform transforms between map coordinates and device coordinates.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
static QgsMapToPixel fromScale(double scale, Qgis::DistanceUnit mapUnits, double dpi=96)
Returns a new QgsMapToPixel created using a specified scale and distance unit.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
double mapRotation() const
Returns the current map rotation in degrees (clockwise).
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
A rectangle specified with double values.
void setNull()
Mark a rectangle as being null (holding no spatial information).
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.