16 #include <QQuickWindow>
17 #include <QSGSimpleTextureNode>
36 : QQuickItem( parent )
40 connect(
this, &QQuickItem::windowChanged,
this, &QgsQuickMapCanvasMap::onWindowChanged );
41 connect( &mRefreshTimer, &QTimer::timeout,
this, [ = ] { refreshMap(); } );
42 connect( &mMapUpdateTimer, &QTimer::timeout,
this, &QgsQuickMapCanvasMap::renderJobUpdated );
50 mMapUpdateTimer.setSingleShot(
false );
51 mMapUpdateTimer.setInterval( 250 );
52 mRefreshTimer.setSingleShot(
true );
53 setTransformOrigin( QQuickItem::TopLeft );
54 setFlags( QQuickItem::ItemHasContents );
61 return mMapSettings.get();
68 QgsPoint mousePos( mMapSettings->screenToCoordinate( center ) );
70 QgsPointXY newCenter( mousePos.
x() + ( ( oldCenter.
x() - mousePos.
x() ) * scale ),
71 mousePos.
y() + ( ( oldCenter.
y() - mousePos.
y() ) * scale ) );
74 extent.
scale( scale, &newCenter );
75 mMapSettings->setExtent( extent );
80 QgsPoint start = mMapSettings->screenToCoordinate( oldPos.toPoint() );
81 QgsPoint end = mMapSettings->screenToCoordinate( newPos.toPoint() );
83 double dx = end.
x() - start.
x();
84 double dy = end.
y() - start.
y();
94 mMapSettings->setExtent( extent );
97 void QgsQuickMapCanvasMap::refreshMap()
110 QgsProject *project = mMapSettings->project();
123 mapSettings.setExpressionContext( expressionContext );
134 if ( mIncrementalRendering )
135 mMapUpdateTimer.start();
143 if ( !mSilentRefresh )
149 mSilentRefresh =
false;
153 void QgsQuickMapCanvasMap::renderJobUpdated()
171 void QgsQuickMapCanvasMap::renderJobFinished()
184 delete mLabelingResults;
195 mMapUpdateTimer.stop();
206 if ( mDeferredRefreshPending )
208 mDeferredRefreshPending =
false;
209 mSilentRefresh =
true;
214 void QgsQuickMapCanvasMap::layerRepaintRequested(
bool deferred )
216 if ( mMapSettings->outputSize().isNull() )
225 mSilentRefresh =
true;
230 mDeferredRefreshPending =
true;
240 void QgsQuickMapCanvasMap::onWindowChanged( QQuickWindow *window )
242 if ( mWindow == window )
246 disconnect( mWindow, &QQuickWindow::screenChanged,
this, &QgsQuickMapCanvasMap::onScreenChanged );
250 connect( window, &QQuickWindow::screenChanged,
this, &QgsQuickMapCanvasMap::onScreenChanged );
251 onScreenChanged( window->screen() );
257 void QgsQuickMapCanvasMap::onScreenChanged( QScreen *screen )
261 if ( screen->devicePixelRatio() > 0 )
263 mMapSettings->setDevicePixelRatio( screen->devicePixelRatio() );
265 mMapSettings->setOutputDpi( screen->physicalDotsPerInch() );
269 void QgsQuickMapCanvasMap::onExtentChanged()
277 void QgsQuickMapCanvasMap::updateTransform()
280 QgsRectangle newExtent = mMapSettings->mapSettings().visibleExtent();
281 setScale( imageExtent.
width() / newExtent.
width() );
290 return mMapUpdateTimer.interval();
305 return mIncrementalRendering;
351 QSGSimpleTextureNode *node =
static_cast<QSGSimpleTextureNode *
>( oldNode );
354 node =
new QSGSimpleTextureNode();
355 QSGTexture *texture = window()->createTextureFromImage( mImage );
356 node->setTexture( texture );
357 node->setOwnsTexture(
true );
360 QRectF rect( boundingRect() );
361 QSizeF size = mImage.size();
362 if ( !size.isEmpty() )
363 size /= mMapSettings->devicePixelRatio();
366 if ( !rect.isEmpty() && !size.isEmpty() && !
qgsDoubleNear( rect.width() / rect.height(), ( size.width() ) /
static_cast<double>( size.height() ), 3 ) )
370 rect.setHeight( rect.width() / size.width() * size.height() );
374 rect.setWidth( rect.height() / size.height() * size.width() );
378 node->setRect( rect );
385 QQuickItem::geometryChanged( newGeometry, oldGeometry );
386 if ( newGeometry.size() != oldGeometry.size() )
388 mMapSettings->setOutputSize( newGeometry.size().toSize() );
393 void QgsQuickMapCanvasMap::onLayersChanged()
395 if ( mMapSettings->extent().isEmpty() )
398 for (
const QMetaObject::Connection &conn : std::as_const( mLayerConnections ) )
402 mLayerConnections.clear();
404 const QList<QgsMapLayer *> layers = mMapSettings->layers();
431 void QgsQuickMapCanvasMap::zoomToFullExtent()
434 const QList<QgsMapLayer *> layers = mMapSettings->layers();
437 if ( mMapSettings->destinationCrs() != layer->crs() )
439 QgsCoordinateTransform transform( layer->crs(), mMapSettings->destinationCrs(), mMapSettings->transformContext() );
454 mMapSettings->setExtent( extent );
461 if ( mMapSettings->outputSize().isNull() )
465 mRefreshTimer.start( 1 );