16 #include <QQuickWindow> 18 #include <QSGSimpleTextureNode> 19 #include <QtConcurrent> 33 : QQuickItem( parent )
36 connect(
this, &QQuickItem::windowChanged,
this, &QgsQuickMapCanvasMap::onWindowChanged );
37 connect( &mRefreshTimer, &QTimer::timeout,
this, &QgsQuickMapCanvasMap::refreshMap );
38 connect( &mMapUpdateTimer, &QTimer::timeout,
this, &QgsQuickMapCanvasMap::renderJobUpdated );
46 mMapUpdateTimer.setSingleShot(
false );
47 mMapUpdateTimer.setInterval( 250 );
48 mRefreshTimer.setSingleShot(
true );
49 setTransformOrigin( QQuickItem::TopLeft );
50 setFlags( QQuickItem::ItemHasContents );
55 return mMapSettings.get();
62 QgsPoint mousePos( mMapSettings->screenToCoordinate( center ) );
63 QgsPointXY newCenter( mousePos.x() + ( ( oldCenter.x() - mousePos.x() ) * scale ),
64 mousePos.y() + ( ( oldCenter.y() - mousePos.y() ) * scale ) );
67 extent.
scale( scale, &newCenter );
68 mMapSettings->setExtent( extent );
73 QgsPoint start = mMapSettings->screenToCoordinate( oldPos.toPoint() );
74 QgsPoint end = mMapSettings->screenToCoordinate( newPos.toPoint() );
76 double dx = end.
x() - start.
x();
77 double dy = end.
y() - start.
y();
87 mMapSettings->setExtent( extent );
90 void QgsQuickMapCanvasMap::refreshMap()
101 QgsProject *project = mMapSettings->project();
113 if ( mIncrementalRendering )
114 mMapUpdateTimer.start();
125 void QgsQuickMapCanvasMap::renderJobUpdated()
140 void QgsQuickMapCanvasMap::renderJobFinished()
150 delete mLabelingResults;
161 mMapUpdateTimer.stop();
173 void QgsQuickMapCanvasMap::onWindowChanged( QQuickWindow *window )
175 disconnect( window, &QQuickWindow::screenChanged,
this, &QgsQuickMapCanvasMap::onScreenChanged );
178 connect( window, &QQuickWindow::screenChanged,
this, &QgsQuickMapCanvasMap::onScreenChanged );
179 onScreenChanged( window->screen() );
183 void QgsQuickMapCanvasMap::onScreenChanged( QScreen *screen )
186 mMapSettings->setOutputDpi( screen->physicalDotsPerInch() );
189 void QgsQuickMapCanvasMap::onExtentChanged()
197 void QgsQuickMapCanvasMap::updateTransform()
205 setScale( imageExtent.
width() / newExtent.
width() );
213 return mMapUpdateTimer.interval();
221 mMapUpdateTimer.setInterval( mapUpdateInterval );
228 return mIncrementalRendering;
233 if ( incrementalRendering == mIncrementalRendering )
247 if ( freeze == mFreeze )
272 QSGSimpleTextureNode *node =
static_cast<QSGSimpleTextureNode *
>( oldNode );
275 node =
new QSGSimpleTextureNode();
276 QSGTexture *texture = window()->createTextureFromImage( mImage );
277 node->setTexture( texture );
278 node->setOwnsTexture(
true );
281 QRectF rect( boundingRect() );
284 if ( !rect.isEmpty() &&
285 !mImage.size().isEmpty() &&
286 !
qgsDoubleNear( rect.width() / rect.height(), mImage.width() / mImage.height() ) )
290 rect.setHeight( rect.width() / mImage.width() * mImage.height() );
294 rect.setWidth( rect.height() / mImage.height() * mImage.width() );
298 node->setRect( rect );
305 Q_UNUSED( oldGeometry )
312 mMapSettings->setOutputSize( newGeometry.size().toSize() );
316 void QgsQuickMapCanvasMap::onLayersChanged()
318 if ( mMapSettings->extent().isEmpty() )
321 for (
const QMetaObject::Connection &conn : qgis::as_const( mLayerConnections ) )
325 mLayerConnections.clear();
327 const QList<QgsMapLayer *> layers = mMapSettings->layers();
354 void QgsQuickMapCanvasMap::zoomToFullExtent()
357 const QList<QgsMapLayer *> layers = mMapSettings->layers();
360 if ( mMapSettings->destinationCrs() != layer->crs() )
362 QgsCoordinateTransform transform( layer->crs(), mMapSettings->destinationCrs(), mMapSettings->transformContext() );
370 mMapSettings->setExtent( extent );
377 if ( mMapSettings->outputSize().isNull() )
381 mRefreshTimer.start( 1 );
void finished()
emitted when asynchronous rendering is finished (or canceled).
A rectangle specified with double values.
Base class for all map layer types.
void zoom(QPointF center, qreal scale)
Set map setting's extent (zoom the map) on the center by given scale.
int mapUpdateInterval() const
Interval in milliseconds after which the map canvas will be updated while a rendering job is ongoing...
Abstract base class for map rendering implementations.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setXMaximum(double x)
Set the maximum x value.
bool incrementalRendering() const
When the incrementalRendering property is set to true, the automatic refresh of map canvas during ren...
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
double yMaximum() const
Returns the y maximum value (top side of rectangle).
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
A class to represent a 2D point.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QgsQuickMapSettings * mapSettings() const
The mapSettings property contains configuration for rendering of the map.
void setFreeze(bool freeze)
When freeze property is set to true, the map canvas does not refresh.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
const QgsMapToPixel & mapToPixel() const
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
bool freeze() const
When freeze property is set to true, the map canvas does not refresh.
void renderingLayersFinished()
Emitted when the layers are rendered.
The QgsQuickMapSettings class encapsulates QgsMapSettings class to offer settings of configuration of...
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
QSGNode * updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) override
void incrementalRenderingChanged()
When the incrementalRendering property is set to true, the automatic refresh of map canvas during ren...
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Job implementation that renders all layers in parallel.
void setYMinimum(double y)
Set the minimum y value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QgsPointXY center() const
Returns the center point of the rectangle.
Reads and writes project states.
void renderStarting()
Signal is emitted when a rendering is starting.
void setIncrementalRendering(bool incrementalRendering)
When the incrementalRendering property is set to true, the automatic refresh of map canvas during ren...
Point geometry type, with support for z-dimension and m-values.
void mapCanvasRefreshed()
Signal is emitted when a canvas is refreshed.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
void stopRendering()
Stop map rendering.
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override
void freezeChanged()
When freeze property is set to true, the map canvas does not refresh.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
void refresh()
Refresh the map canvas.
QList< QgsMapRendererJob::Error > Errors
void isRenderingChanged()
The isRendering property is set to true while a rendering job is pending for this map canvas map...
void setYMaximum(double y)
Set the maximum y value.
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
void start() override
Start the rendering job and immediately return.
bool isRendering() const
The isRendering property is set to true while a rendering job is pending for this map canvas map...
QgsQuickMapCanvasMap(QQuickItem *parent=nullptr)
Create map canvas map.
virtual void cancel()=0
Stop the rendering job - does not return until the job has terminated.
void setMapUpdateInterval(int mapUpdateInterval)
Interval in milliseconds after which the map canvas will be updated while a rendering job is ongoing...
void mapUpdateIntervalChanged()
Interval in milliseconds after which the map canvas will be updated while a rendering job is ongoing...
void pan(QPointF oldPos, QPointF newPos)
Set map setting's extent (pan the map) based on the difference of positions.
const QgsMapSettings & mapSettings() const
Returns map settings with which this job was started.
double width() const
Returns the width of the rectangle.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
void setXMinimum(double x)
Set the minimum x value.
void layersChanged()
Set list of layers for map rendering.
QImage renderedImage() override
Gets a preview/resulting image.
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results). ...