17#include "moc_qgsmaprenderercache.cpp"
33 QMutexLocker lock( &mMutex );
37void QgsMapRendererCache::clearInternal()
51 mCachedImages.clear();
52 mConnectedLayers.clear();
55void QgsMapRendererCache::dropUnusedConnections()
58 const QSet< QgsWeakMapLayerPointer > disconnects = mConnectedLayers.subtract( stillDepends );
68 mConnectedLayers = stillDepends;
73 QSet< QgsWeakMapLayerPointer > result;
74 QMap<QString, CacheParameters>::const_iterator it = mCachedImages.constBegin();
75 for ( ; it != mCachedImages.constEnd(); ++it )
89 QMutexLocker lock( &mMutex );
92 if ( extent == mExtent &&
108 QMutexLocker lock( &mMutex );
111 if ( extent == mExtent &&
126 QMutexLocker lock( &mMutex );
137 QMutexLocker lock( &mMutex );
139 if ( extent != mExtent || mapToPixel != mMtp )
141 auto it = mCachedImages.constFind( cacheKey );
142 if ( it != mCachedImages.constEnd() )
147 if ( it->cachedExtent == mExtent && it->cachedMtp == mMtp )
152 CacheParameters params;
153 params.cachedImage = image;
154 params.cachedExtent = extent;
155 params.cachedMtp = mapToPixel;
162 params.dependentLayers << layer;
167 mConnectedLayers << layer;
172 mCachedImages[cacheKey] = params;
177 QMutexLocker lock( &mMutex );
179 auto it = mCachedImages.constFind( cacheKey );
180 if ( it != mCachedImages.constEnd() )
182 const CacheParameters ¶ms = it.value();
183 return ( params.cachedExtent == mExtent &&
184 params.cachedMtp.transform() == mMtp.
transform() );
194 auto it = mCachedImages.constFind( cacheKey );
195 if ( it != mCachedImages.constEnd() )
197 const CacheParameters ¶ms = it.value();
200 if ( minimumScaleThreshold != 0 && mMtp.
mapUnitsPerPixel() < params.cachedMtp.mapUnitsPerPixel() * minimumScaleThreshold )
202 if ( maximumScaleThreshold != 0 && mMtp.
mapUnitsPerPixel() > params.cachedMtp.mapUnitsPerPixel() * maximumScaleThreshold )
215 QMutexLocker lock( &mMutex );
216 return mCachedImages.value( cacheKey ).cachedImage;
221 qreal x = point.
x(), y = point.
y();
223 return QPointF( x, y ) * scale;
228 QMutexLocker lock( &mMutex );
229 const CacheParameters params = mCachedImages.value( cacheKey );
231 if ( params.cachedExtent == mExtent &&
234 return params.cachedImage;
244 if ( intersection.
isNull() )
250 const QRectF targetRect( ulT.x(), ulT.y(), lrT.x() - ulT.x(), lrT.y() - ulT.y() );
253 const QPointF ulS = _transform( params.cachedMtp,
QgsPointXY( intersection.
xMinimum(), intersection.
yMaximum() ), params.cachedImage.devicePixelRatio() );
254 const QPointF lrS = _transform( params.cachedMtp,
QgsPointXY( intersection.
xMaximum(), intersection.
yMinimum() ), params.cachedImage.devicePixelRatio() );
255 const QRectF sourceRect( ulS.x(), ulS.y(), lrS.x() - ulS.x(), lrS.y() - ulS.y() );
258 QImage ret( params.cachedImage.size(), params.cachedImage.format() );
259 ret.setDevicePixelRatio( params.cachedImage.devicePixelRatio() );
260 ret.setDotsPerMeterX( params.cachedImage.dotsPerMeterX() );
261 ret.setDotsPerMeterY( params.cachedImage.dotsPerMeterY() );
262 ret.fill( Qt::transparent );
264 painter.begin( &ret );
265 painter.drawImage( targetRect, params.cachedImage, sourceRect );
273 auto it = mCachedImages.constFind( cacheKey );
274 if ( it != mCachedImages.constEnd() )
276 return _qgis_listQPointerToRaw( ( *it ).dependentLayers );
278 return QList< QgsMapLayer * >();
282void QgsMapRendererCache::layerRequestedRepaint()
284 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sender() );
293 QMutexLocker lock( &mMutex );
296 QMap<QString, CacheParameters>::iterator it = mCachedImages.begin();
297 for ( ; it != mCachedImages.end(); )
299 if ( !it.value().dependentLayers.contains( layer ) )
305 it = mCachedImages.erase( it );
307 dropUnusedConnections();
312 QMutexLocker lock( &mMutex );
314 mCachedImages.remove( cacheKey );
315 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 class to represent a 2D point.
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.