32 QMutexLocker lock( &mMutex );
36void QgsMapRendererCache::clearInternal()
50 mCachedImages.clear();
51 mConnectedLayers.clear();
54void QgsMapRendererCache::dropUnusedConnections()
57 const QSet< QgsWeakMapLayerPointer > disconnects = mConnectedLayers.subtract( stillDepends );
67 mConnectedLayers = stillDepends;
72 QSet< QgsWeakMapLayerPointer > result;
73 QMap<QString, CacheParameters>::const_iterator it = mCachedImages.constBegin();
74 for ( ; it != mCachedImages.constEnd(); ++it )
88 QMutexLocker lock( &mMutex );
91 if ( extent == mExtent &&
107 QMutexLocker lock( &mMutex );
110 if ( extent == mExtent &&
125 QMutexLocker lock( &mMutex );
136 QMutexLocker lock( &mMutex );
138 if ( extent != mExtent || mapToPixel != mMtp )
140 auto it = mCachedImages.constFind( cacheKey );
141 if ( it != mCachedImages.constEnd() )
146 if ( it->cachedExtent == mExtent && it->cachedMtp == mMtp )
151 CacheParameters params;
152 params.cachedImage = image;
153 params.cachedExtent = extent;
154 params.cachedMtp = mapToPixel;
161 params.dependentLayers << layer;
166 mConnectedLayers << layer;
171 mCachedImages[cacheKey] = params;
176 QMutexLocker lock( &mMutex );
178 auto it = mCachedImages.constFind( cacheKey );
179 if ( it != mCachedImages.constEnd() )
181 const CacheParameters ¶ms = it.value();
182 return ( params.cachedExtent == mExtent &&
183 params.cachedMtp.transform() == mMtp.
transform() );
193 auto it = mCachedImages.constFind( cacheKey );
194 if ( it != mCachedImages.constEnd() )
196 const CacheParameters ¶ms = it.value();
199 if ( minimumScaleThreshold != 0 && mMtp.
mapUnitsPerPixel() < params.cachedMtp.mapUnitsPerPixel() * minimumScaleThreshold )
201 if ( maximumScaleThreshold != 0 && mMtp.
mapUnitsPerPixel() > params.cachedMtp.mapUnitsPerPixel() * maximumScaleThreshold )
214 QMutexLocker lock( &mMutex );
215 return mCachedImages.value( cacheKey ).cachedImage;
220 qreal x = point.
x(), y = point.
y();
222 return QPointF( x, y ) * scale;
227 QMutexLocker lock( &mMutex );
228 const CacheParameters params = mCachedImages.value( cacheKey );
230 if ( params.cachedExtent == mExtent &&
233 return params.cachedImage;
243 if ( intersection.
isNull() )
249 const QRectF targetRect( ulT.x(), ulT.y(), lrT.x() - ulT.x(), lrT.y() - ulT.y() );
252 const QPointF ulS = _transform( params.cachedMtp,
QgsPointXY( intersection.
xMinimum(), intersection.
yMaximum() ), params.cachedImage.devicePixelRatio() );
253 const QPointF lrS = _transform( params.cachedMtp,
QgsPointXY( intersection.
xMaximum(), intersection.
yMinimum() ), params.cachedImage.devicePixelRatio() );
254 const QRectF sourceRect( ulS.x(), ulS.y(), lrS.x() - ulS.x(), lrS.y() - ulS.y() );
257 QImage ret( params.cachedImage.size(), params.cachedImage.format() );
258 ret.setDevicePixelRatio( params.cachedImage.devicePixelRatio() );
259 ret.setDotsPerMeterX( params.cachedImage.dotsPerMeterX() );
260 ret.setDotsPerMeterY( params.cachedImage.dotsPerMeterY() );
261 ret.fill( Qt::transparent );
263 painter.begin( &ret );
264 painter.drawImage( targetRect, params.cachedImage, sourceRect );
272 auto it = mCachedImages.constFind( cacheKey );
273 if ( it != mCachedImages.constEnd() )
275 return _qgis_listQPointerToRaw( ( *it ).dependentLayers );
277 return QList< QgsMapLayer * >();
281void QgsMapRendererCache::layerRequestedRepaint()
283 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sender() );
292 QMutexLocker lock( &mMutex );
295 QMap<QString, CacheParameters>::iterator it = mCachedImages.begin();
296 for ( ; it != mCachedImages.end(); )
298 if ( !it.value().dependentLayers.contains( layer ) )
304 it = mCachedImages.erase( it );
306 dropUnusedConnections();
311 QMutexLocker lock( &mMutex );
313 mCachedImages.remove( cacheKey );
314 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 device coordinates to map coordinates.
A class to represent a 2D point.
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
bool isNull() const
Test if the rectangle is null (holding no spatial information).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
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.