QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsquickmapsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsquickmapsettings.cpp
3 --------------------------------------
4 Date : 27.12.2014
5 Copyright : (C) 2014 by Matthias Kuhn
6 Email : matthias (at) opengis.ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17#include "qgis.h"
18#include "qgsquickmapsettings.h"
19
20#include "qgsmaplayer.h"
21#include "qgsmessagelog.h"
23
25 : QObject( parent )
26{
27 // Connect signals for derived values
34}
35
37{
38 if ( project == mProject )
39 return;
40
41 // If we have already something connected, disconnect it!
42 if ( mProject )
43 {
44 mProject->disconnect( this );
45 }
46
47 mProject = project;
48
49 // Connect all signals
50 if ( mProject )
51 {
52 connect( mProject, &QgsProject::readProject, this, &QgsQuickMapSettings::onReadProject );
53 connect( mProject, &QgsProject::crsChanged, this, &QgsQuickMapSettings::onCrsChanged );
54 setDestinationCrs( mProject->crs() );
55 mMapSettings.setTransformContext( mProject->transformContext() );
56 mMapSettings.setPathResolver( mProject->pathResolver() );
57 }
58 else
59 {
61 }
62
63 emit projectChanged();
64}
65
67{
68 return mProject;
69}
70
72{
73 return mMapSettings.transformContext();
74}
75
77{
78 return mMapSettings.extent();
79}
80
82{
83 if ( mMapSettings.extent() == extent )
84 return;
85
86 mMapSettings.setExtent( extent );
87 emit extentChanged();
88}
89
91{
92 return QgsPoint( extent().center() );
93}
94
96{
97 QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
98
99 QgsRectangle e = mMapSettings.extent();
100 e.setXMinimum( e.xMinimum() + delta.x() );
101 e.setXMaximum( e.xMaximum() + delta.x() );
102 e.setYMinimum( e.yMinimum() + delta.y() );
103 e.setYMaximum( e.yMaximum() + delta.y() );
104
105 setExtent( e );
106}
107
109{
110 return mMapSettings.mapUnitsPerPixel();
111}
112
114{
115 Q_ASSERT( layer );
116
117 const QgsRectangle extent = mapSettings().layerToMapCoordinates( layer, layer->extent() );
118
119 if ( !extent.isEmpty() )
120 {
121 if ( shouldZoom )
122 setExtent( extent );
123 else
125 }
126}
127
129{
130 return mMapSettings.mapUnitsPerPixel() * devicePixelRatio();
131}
132
134{
135 return mMapSettings.visibleExtent();
136}
137
139{
140 QgsPointXY pt( point.x(), point.y() );
141 QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
142 pp.setX( pp.x() / devicePixelRatio() );
143 pp.setY( pp.y() / devicePixelRatio() );
144 return pp.toQPointF();
145}
146
148{
149 const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x() * devicePixelRatio(), point.y() * devicePixelRatio() );
150 return QgsPoint( pp );
151}
152
154{
155 mMapSettings.setTransformContext( ctx );
156}
157
159{
160 return mMapSettings;
161}
162
164{
165 return mMapSettings.outputSize();
166}
167
169{
170 outputSize.setWidth( outputSize.width() * devicePixelRatio() );
171 outputSize.setHeight( outputSize.height() * devicePixelRatio() );
172 if ( mMapSettings.outputSize() == outputSize )
173 return;
174
175 mMapSettings.setOutputSize( outputSize );
176 emit outputSizeChanged();
177}
178
180{
181 return mMapSettings.outputDpi();
182}
183
184void QgsQuickMapSettings::setOutputDpi( double outputDpi )
185{
187 if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
188 return;
189
190 mMapSettings.setOutputDpi( outputDpi );
191 emit outputDpiChanged();
192}
193
195{
196 return mMapSettings.destinationCrs();
197}
198
200{
201 if ( mMapSettings.destinationCrs() == destinationCrs )
202 return;
203
204 mMapSettings.setDestinationCrs( destinationCrs );
206}
207
208QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
209{
210 return mMapSettings.layers();
211}
212
213void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
214{
215 mMapSettings.setLayers( layers );
216 emit layersChanged();
217}
218
219void QgsQuickMapSettings::onCrsChanged()
220{
221 setDestinationCrs( mProject->crs() );
222}
223
224void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
225{
226 if ( mProject )
227 {
228 int red = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorRedPart" ), 255 );
229 int green = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorGreenPart" ), 255 );
230 int blue = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorBluePart" ), 255 );
231 mMapSettings.setBackgroundColor( QColor( red, green, blue ) );
232
233 const bool isTemporal = mProject->readNumEntry( QStringLiteral( "TemporalControllerWidget" ), QStringLiteral( "/NavigationMode" ), 0 ) != 0;
234 const QString startString = QgsProject::instance()->readEntry( QStringLiteral( "TemporalControllerWidget" ), QStringLiteral( "/StartDateTime" ) );
235 const QString endString = QgsProject::instance()->readEntry( QStringLiteral( "TemporalControllerWidget" ), QStringLiteral( "/EndDateTime" ) );
236 mMapSettings.setIsTemporal( isTemporal );
237 mMapSettings.setTemporalRange( QgsDateTimeRange( QDateTime::fromString( startString, Qt::ISODateWithMs ),
238 QDateTime::fromString( endString, Qt::ISODateWithMs ) ) );
239 }
240
241 QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
242 bool foundTheMapCanvas = false;
243 for ( int i = 0; i < nodes.size(); i++ )
244 {
245 QDomNode node = nodes.item( 0 );
246 QDomElement element = node.toElement();
247
248 if ( element.hasAttribute( QStringLiteral( "name" ) ) && element.attribute( QStringLiteral( "name" ) ) == QLatin1String( "theMapCanvas" ) )
249 {
250 foundTheMapCanvas = true;
251 mMapSettings.readXml( node );
252
253 if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
254 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
255 }
256 }
257 if ( !foundTheMapCanvas )
258 {
259 mMapSettings.setDestinationCrs( mProject->crs() );
260 mMapSettings.setExtent( mProject->viewSettings()->fullExtent() );
261 }
262
263 mMapSettings.setRotation( 0 );
264
265 mMapSettings.setTransformContext( mProject->transformContext() );
266 mMapSettings.setPathResolver( mProject->pathResolver() );
267
268 emit extentChanged();
270 emit outputSizeChanged();
271 emit outputDpiChanged();
272 emit layersChanged();
274}
275
277{
278 return mMapSettings.rotation();
279}
280
281void QgsQuickMapSettings::setRotation( double rotation )
282{
283 if ( !qgsDoubleNear( rotation, 0 ) )
284 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
285}
286
288{
289 return mMapSettings.backgroundColor();
290}
291
293{
294 if ( mMapSettings.backgroundColor() == color )
295 return;
296
297 mMapSettings.setBackgroundColor( color );
299}
300
302{
303 return mDevicePixelRatio;
304}
305
306void QgsQuickMapSettings::setDevicePixelRatio( const qreal &devicePixelRatio )
307{
308 mDevicePixelRatio = devicePixelRatio;
310}
311
313{
314 return mMapSettings.isTemporal();
315}
316
318{
319 mMapSettings.setIsTemporal( temporal );
321}
322
324{
325 return mMapSettings.temporalRange().begin();
326}
327
328void QgsQuickMapSettings::setTemporalBegin( const QDateTime &begin )
329{
330 const QgsDateTimeRange range = mMapSettings.temporalRange();
331 mMapSettings.setTemporalRange( QgsDateTimeRange( begin, range.end() ) );
333}
334
336{
337 return mMapSettings.temporalRange().end();
338}
339
340void QgsQuickMapSettings::setTemporalEnd( const QDateTime &end )
341{
342 const QgsDateTimeRange range = mMapSettings.temporalRange();
343 mMapSettings.setTemporalRange( QgsDateTimeRange( range.begin(), end ) );
345}
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
virtual QgsRectangle extent() const
Returns the extent of the layer.
The QgsMapSettings class contains configuration for rendering of the map.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers which will be rendered in the map.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
QColor backgroundColor() const
Returns the background color of the map.
void setOutputDpi(double dpi)
Sets the dpi (dots per inch) used for conversion between real world units (e.g.
const QgsMapToPixel & mapToPixel() const
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
QSize outputSize() const
Returns the size of the resulting map image, in pixels.
QgsRectangle extent() const
Returns geographical coordinates of the rectangle that should be rendered.
void setExtent(const QgsRectangle &rect, bool magnified=true)
Sets the coordinates of the rectangle which should be rendered.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
void setBackgroundColor(const QColor &color)
Sets the background color of the map.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
void readXml(QDomNode &node)
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:90
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.
Definition: qgspointxy.h:59
void setX(double x) SIP_HOLDGIL
Sets the x value of the point.
Definition: qgspointxy.h:122
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:132
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:169
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Q_GADGET double x
Definition: qgspoint.h:52
double y
Definition: qgspoint.h:53
QgsReferencedRectangle fullExtent() const
Returns the full extent of the project, which represents the maximal limits of the project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:105
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
QgsPathResolver pathResolver() const
Returns path resolver object with considering whether the project uses absolute or relative paths and...
void crsChanged()
Emitted when the CRS of the project has changed.
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:111
void readProject(const QDomDocument &)
Emitted when a project is being read.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:110
const QgsProjectViewSettings * viewSettings() const
Returns the project's view settings, which contains settings and properties relating to how a QgsProj...
double outputDpi
Output DPI used for conversion between real world units (e.g.
void setIsTemporal(bool temporal)
Returns true if a temporal filtering is enabled.
QgsQuickMapSettings(QObject *parent=nullptr)
Create new map settings.
void setOutputSize(QSize outputSize)
Sets the size of the resulting map image, in pixels.
void setRotation(double rotation)
The rotation of the resulting map image, in degrees clockwise.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
double rotation
The rotation of the resulting map image, in degrees clockwise.
QgsRectangle extent
Geographical coordinates of the rectangle that should be rendered.
void outputSizeChanged()
The size of the resulting map image.
QDateTime temporalBegin
The temporal range's begin (i.e.
void projectChanged()
A project property should be used as a primary source of project all other components in the applicat...
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
bool isTemporal
Returns true if a temporal filtering is enabled.
QgsRectangle visibleExtent
Returns the actual extent derived from requested extent that takes output image size into account.
void outputDpiChanged()
Output DPI used for conversion between real world units (e.g.
void layersChanged()
Set list of layers for map rendering.
void destinationCrsChanged()
CRS of destination coordinate reference system.
void visibleExtentChanged()
Returns the actual extent derived from requested extent that takes output image size into account.
void temporalStateChanged()
Emitted when the temporal state has changed.
void setExtent(const QgsRectangle &extent)
Sets the coordinates of the rectangle which should be rendered.
Q_INVOKABLE QgsPoint screenToCoordinate(const QPointF &point) const
Convert a screen coordinate to a map coordinate.
Q_INVOKABLE void setCenter(const QgsPoint &center)
Move current map extent to have center point defined by center.
void devicePixelRatioChanged()
Returns the ratio between physical pixels and device-independent pixels.
QColor backgroundColor
The background color used to render the map.
double mapUnitsPerPixel
Returns the distance in geographical coordinates that equals to one pixel in the map.
void setDevicePixelRatio(const qreal &devicePixelRatio)
Sets the ratio between physical pixels and device-independent pixels.
double mapUnitsPerPoint
Returns the distance in geographical coordinates that equals to one point unit in the map.
QgsMapSettings mapSettings() const
Clone map settings.
void setProject(QgsProject *project)
A project property should be used as a primary source of project all other components in the applicat...
void mapUnitsPerPixelChanged()
Returns the distance in geographical coordinates that equals to one pixel in the map.
void setOutputDpi(double outputDpi)
Sets the dpi (dots per inch) used for conversion between real world units (e.g.
qreal devicePixelRatio() const
Returns the ratio between physical pixels and device-independent pixels.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
Q_INVOKABLE void setCenterToLayer(QgsMapLayer *layer, bool shouldZoom=true)
Move current map extent to have center point defined by layer. Optionally only pan to the layer if sh...
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
QDateTime temporalEnd
The temporal range's end (i.e.
QList< QgsMapLayer * > layers
Set list of layers for map rendering.
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
Sets the destination crs (coordinate reference system) for the map render.
QSize outputSize
The size of the resulting map image.
void backgroundColorChanged()
The background color used to render the map.
void setTemporalEnd(const QDateTime &end)
The temporal range's end (i.e.
void rotationChanged()
The rotation of the resulting map image, in degrees clockwise.
Q_INVOKABLE QPointF coordinateToScreen(const QgsPoint &point) const
Convert a map coordinate to screen pixel coordinates.
QgsPoint center
Geographical coordinate representing the center point of the current extent.
void setTemporalBegin(const QDateTime &begin)
The temporal range's begin (i.e.
QgsProject * project
A project property should be used as a primary source of project all other components in the applicat...
void setBackgroundColor(const QColor &color)
The background color used to render the map.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
Definition: qgsrectangle.h:161
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
Definition: qgsrectangle.h:156
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
Definition: qgsrectangle.h:151
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
Definition: qgsrectangle.h:166
bool isEmpty() const
Returns true if the rectangle is empty.
Definition: qgsrectangle.h:469
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
Definition: qgsrectangle.h:251
const QgsDateTimeRange & temporalRange() const
Returns the datetime range for the object.
bool isTemporal() const
Returns true if the object's temporal range is enabled, and the object will be filtered when renderin...
void setIsTemporal(bool enabled)
Sets whether the temporal range is enabled (i.e.
void setTemporalRange(const QgsDateTimeRange &range)
Sets the temporal range for the object.
A class to represent a vector.
Definition: qgsvector.h:30
double y() const SIP_HOLDGIL
Returns the vector's y-component.
Definition: qgsvector.h:156
double x() const SIP_HOLDGIL
Returns the vector's x-component.
Definition: qgsvector.h:147
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3509