16 #include <QQuickWindow>
17 #include <QSGSimpleTextureNode>
35 : QQuickItem( parent )
39 connect(
this, &QQuickItem::windowChanged,
this, &QgsQuickMapCanvasMap::onWindowChanged );
40 connect( &mRefreshTimer, &QTimer::timeout,
this, &QgsQuickMapCanvasMap::refreshMap );
41 connect( &mMapUpdateTimer, &QTimer::timeout,
this, &QgsQuickMapCanvasMap::renderJobUpdated );
49 mMapUpdateTimer.setSingleShot(
false );
50 mMapUpdateTimer.setInterval( 250 );
51 mRefreshTimer.setSingleShot(
true );
52 setTransformOrigin( QQuickItem::TopLeft );
53 setFlags( QQuickItem::ItemHasContents );
60 return mMapSettings.get();
67 QgsPoint mousePos( mMapSettings->screenToCoordinate( center ) );
69 QgsPointXY newCenter( mousePos.
x() + ( ( oldCenter.
x() - mousePos.
x() ) * scale ),
70 mousePos.
y() + ( ( oldCenter.
y() - mousePos.
y() ) * scale ) );
73 extent.
scale( scale, &newCenter );
74 mMapSettings->setExtent( extent );
79 QgsPoint start = mMapSettings->screenToCoordinate( oldPos.toPoint() );
80 QgsPoint end = mMapSettings->screenToCoordinate( newPos.toPoint() );
82 double dx = end.
x() - start.
x();
83 double dy = end.
y() - start.
y();
93 mMapSettings->setExtent( extent );
96 void QgsQuickMapCanvasMap::refreshMap()
107 QgsProject *project = mMapSettings->project();
115 mapSettings.setExpressionContext( expressionContext );
126 if ( mIncrementalRendering )
127 mMapUpdateTimer.start();
138 void QgsQuickMapCanvasMap::renderJobUpdated()
156 void QgsQuickMapCanvasMap::renderJobFinished()
169 delete mLabelingResults;
180 mMapUpdateTimer.stop();
192 void QgsQuickMapCanvasMap::onWindowChanged( QQuickWindow *window )
194 if ( mWindow == window )
198 disconnect( mWindow, &QQuickWindow::screenChanged,
this, &QgsQuickMapCanvasMap::onScreenChanged );
202 connect( window, &QQuickWindow::screenChanged,
this, &QgsQuickMapCanvasMap::onScreenChanged );
203 onScreenChanged( window->screen() );
209 void QgsQuickMapCanvasMap::onScreenChanged( QScreen *screen )
213 if ( screen->devicePixelRatio() > 0 )
215 mMapSettings->setDevicePixelRatio( screen->devicePixelRatio() );
217 mMapSettings->setOutputDpi( screen->physicalDotsPerInch() );
221 void QgsQuickMapCanvasMap::onExtentChanged()
229 void QgsQuickMapCanvasMap::updateTransform()
232 QgsRectangle newExtent = mMapSettings->mapSettings().visibleExtent();
233 setScale( imageExtent.
width() / newExtent.
width() );
242 return mMapUpdateTimer.interval();
257 return mIncrementalRendering;
303 QSGSimpleTextureNode *node =
static_cast<QSGSimpleTextureNode *
>( oldNode );
306 node =
new QSGSimpleTextureNode();
307 QSGTexture *texture = window()->createTextureFromImage( mImage );
308 node->setTexture( texture );
309 node->setOwnsTexture(
true );
312 QRectF rect( boundingRect() );
313 QSizeF size = mImage.size();
314 if ( !size.isEmpty() )
315 size /= mMapSettings->devicePixelRatio();
318 if ( !rect.isEmpty() && !size.isEmpty() && !
qgsDoubleNear( rect.width() / rect.height(), ( size.width() ) /
static_cast<double>( size.height() ), 3 ) )
322 rect.setHeight( rect.width() / size.width() * size.height() );
326 rect.setWidth( rect.height() / size.height() * size.width() );
330 node->setRect( rect );
337 QQuickItem::geometryChanged( newGeometry, oldGeometry );
338 if ( newGeometry.size() != oldGeometry.size() )
340 mMapSettings->setOutputSize( newGeometry.size().toSize() );
345 void QgsQuickMapCanvasMap::onLayersChanged()
347 if ( mMapSettings->extent().isEmpty() )
350 for (
const QMetaObject::Connection &conn : std::as_const( mLayerConnections ) )
354 mLayerConnections.clear();
356 const QList<QgsMapLayer *> layers = mMapSettings->layers();
383 void QgsQuickMapCanvasMap::zoomToFullExtent()
386 const QList<QgsMapLayer *> layers = mMapSettings->layers();
389 if ( mMapSettings->destinationCrs() != layer->crs() )
391 QgsCoordinateTransform transform( layer->crs(), mMapSettings->destinationCrs(), mMapSettings->transformContext() );
406 mMapSettings->setExtent( extent );
413 if ( mMapSettings->outputSize().isNull() )
417 mRefreshTimer.start( 1 );
Custom exception class for Coordinate Reference System related exceptions.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Base class for all map layer types.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
This class is responsible for keeping cache of rendered images resulting from a map rendering job.
Abstract base class for map rendering implementations.
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
void renderingLayersFinished()
Emitted when the layers are rendered.
const QgsMapSettings & mapSettings() const
Returns map settings with which this job was started.
void finished()
emitted when asynchronous rendering is finished (or canceled).
void start()
Start the rendering job and immediately return.
QList< QgsMapRendererJob::Error > Errors
virtual void cancel()=0
Stop the rendering job - does not return until the job has terminated.
Job implementation that renders all layers in parallel.
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
QImage renderedImage() override
Gets a preview/resulting image.
The QgsMapSettings class contains configuration for rendering of the map.
@ RenderPartialOutput
Whether to make extra effort to update map image with partially rendered layers (better for interacti...
@ UseRenderingOptimization
Enable vector simplification and other rendering optimizations.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A class to represent a 2D point.
Point geometry type, with support for z-dimension and m-values.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns project's global labeling engine settings.
void freezeChanged()
When freeze property is set to true, the map canvas does not refresh.
bool isRendering
The isRendering property is set to true while a rendering job is pending for this map canvas map.
void mapCanvasRefreshed()
Signal is emitted when a canvas is refreshed.
void incrementalRenderingChanged()
When the incrementalRendering property is set to true, the automatic refresh of map canvas during ren...
int mapUpdateInterval
Interval in milliseconds after which the map canvas will be updated while a rendering job is ongoing.
void setMapUpdateInterval(int mapUpdateInterval)
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.
void renderStarting()
Signal is emitted when a rendering is starting.
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override
void stopRendering()
Stop map rendering.
void zoom(QPointF center, qreal scale)
Set map setting's extent (zoom the map) on the center by given scale.
void setIncrementalRendering(bool incrementalRendering)
When the incrementalRendering property is set to true, the automatic refresh of map canvas during ren...
void setFreeze(bool freeze)
When freeze property is set to true, the map canvas does not refresh.
QgsQuickMapSettings * mapSettings
The mapSettings property contains configuration for rendering of the map.
void refresh()
Refresh the map canvas.
QSGNode * updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) override
void mapUpdateIntervalChanged()
Interval in milliseconds after which the map canvas will be updated while a rendering job is ongoing.
bool incrementalRendering
When the incrementalRendering property is set to true, the automatic refresh of map canvas during ren...
bool freeze
When freeze property is set to true, the map canvas does not refresh.
QgsQuickMapCanvasMap(QQuickItem *parent=nullptr)
Create map canvas map.
void isRenderingChanged()
The isRendering property is set to true while a rendering job is pending for this map canvas map.
The QgsQuickMapSettings class encapsulates QgsMapSettings class to offer settings of configuration of...
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
void layersChanged()
Set list of layers for map rendering.
A rectangle specified with double values.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)