QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 "qgsquickmapsettings.h"
18
19#include "qgis.h"
20#include "qgsmaplayer.h"
21#include "qgsmessagelog.h"
23
24#include "moc_qgsquickmapsettings.cpp"
25
37
39{
40 if ( project == mProject )
41 return;
42
43 // If we have already something connected, disconnect it!
44 if ( mProject )
45 {
46 mProject->disconnect( this );
47 }
48
49 mProject = project;
50
51 // Connect all signals
52 if ( mProject )
53 {
54 connect( mProject, &QgsProject::readProject, this, &QgsQuickMapSettings::onReadProject );
55 connect( mProject, &QgsProject::crsChanged, this, &QgsQuickMapSettings::onCrsChanged );
56 setDestinationCrs( mProject->crs() );
57 mMapSettings.setTransformContext( mProject->transformContext() );
58 mMapSettings.setPathResolver( mProject->pathResolver() );
59 }
60 else
61 {
62 mMapSettings.setTransformContext( QgsCoordinateTransformContext() );
63 }
64
65 emit projectChanged();
66}
67
69{
70 return mProject;
71}
72
74{
75 return mMapSettings.transformContext();
76}
77
79{
80 return mMapSettings.extent();
81}
82
84{
85 if ( mMapSettings.extent() == extent )
86 return;
87
88 mMapSettings.setExtent( extent );
89 emit extentChanged();
90}
91
93{
94 return QgsPoint( extent().center() );
95}
96
98{
99 QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
100
101 QgsRectangle e = mMapSettings.extent();
102 e.setXMinimum( e.xMinimum() + delta.x() );
103 e.setXMaximum( e.xMaximum() + delta.x() );
104 e.setYMinimum( e.yMinimum() + delta.y() );
105 e.setYMaximum( e.yMaximum() + delta.y() );
106
107 setExtent( e );
108}
109
111{
112 return mMapSettings.mapUnitsPerPixel();
113}
114
116{
117 Q_ASSERT( layer );
118
119 const QgsRectangle extent = mapSettings().layerToMapCoordinates( layer, layer->extent() );
120
121 if ( !extent.isEmpty() )
122 {
123 if ( shouldZoom )
124 setExtent( extent );
125 else
126 setCenter( QgsPoint( extent.center() ) );
127 }
128}
129
131{
132 return mMapSettings.mapUnitsPerPixel() * devicePixelRatio();
133}
134
136{
137 return mMapSettings.visibleExtent();
138}
139
141{
142 QgsPointXY pt( point.x(), point.y() );
143 QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
144 pp.setX( pp.x() / devicePixelRatio() );
145 pp.setY( pp.y() / devicePixelRatio() );
146 return pp.toQPointF();
147}
148
150{
151 const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x() * devicePixelRatio(), point.y() * devicePixelRatio() );
152 return QgsPoint( pp );
153}
154
156{
157 mMapSettings.setTransformContext( ctx );
158}
159
161{
162 return mMapSettings;
163}
164
166{
167 return mMapSettings.outputSize();
168}
169
171{
172 outputSize.setWidth( outputSize.width() * devicePixelRatio() );
173 outputSize.setHeight( outputSize.height() * devicePixelRatio() );
174 if ( mMapSettings.outputSize() == outputSize )
175 return;
176
177 mMapSettings.setOutputSize( outputSize );
178 emit outputSizeChanged();
179}
180
182{
183 return mMapSettings.outputDpi();
184}
185
187{
189 if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
190 return;
191
192 mMapSettings.setOutputDpi( outputDpi );
193 emit outputDpiChanged();
194}
195
197{
198 return mMapSettings.destinationCrs();
199}
200
202{
203 if ( mMapSettings.destinationCrs() == destinationCrs )
204 return;
205
206 mMapSettings.setDestinationCrs( destinationCrs );
208}
209
210QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
211{
212 return mMapSettings.layers();
213}
214
215void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
216{
217 mMapSettings.setLayers( layers );
218 emit layersChanged();
219}
220
221void QgsQuickMapSettings::onCrsChanged()
222{
223 setDestinationCrs( mProject->crs() );
224}
225
226void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
227{
228 if ( mProject )
229 {
230 int red = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorRedPart" ), 255 );
231 int green = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorGreenPart" ), 255 );
232 int blue = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorBluePart" ), 255 );
233 mMapSettings.setBackgroundColor( QColor( red, green, blue ) );
234
235 const bool isTemporal = mProject->readNumEntry( QStringLiteral( "TemporalControllerWidget" ), QStringLiteral( "/NavigationMode" ), 0 ) != 0;
236 const QString startString = QgsProject::instance()->readEntry( QStringLiteral( "TemporalControllerWidget" ), QStringLiteral( "/StartDateTime" ) );
237 const QString endString = QgsProject::instance()->readEntry( QStringLiteral( "TemporalControllerWidget" ), QStringLiteral( "/EndDateTime" ) );
238 mMapSettings.setIsTemporal( isTemporal );
239 mMapSettings.setTemporalRange( QgsDateTimeRange( QDateTime::fromString( startString, Qt::ISODateWithMs ), QDateTime::fromString( endString, Qt::ISODateWithMs ) ) );
240 }
241
242 QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
243 bool foundTheMapCanvas = false;
244 for ( int i = 0; i < nodes.size(); i++ )
245 {
246 QDomNode node = nodes.item( 0 );
247 QDomElement element = node.toElement();
248
249 if ( element.hasAttribute( QStringLiteral( "name" ) ) && element.attribute( QStringLiteral( "name" ) ) == QLatin1String( "theMapCanvas" ) )
250 {
251 foundTheMapCanvas = true;
252 mMapSettings.readXml( node );
253
254 if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
255 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
256 }
257 }
258 if ( !foundTheMapCanvas )
259 {
260 mMapSettings.setDestinationCrs( mProject->crs() );
261 mMapSettings.setExtent( mProject->viewSettings()->fullExtent() );
262 }
263
264 mMapSettings.setRotation( 0 );
265
266 mMapSettings.setTransformContext( mProject->transformContext() );
267 mMapSettings.setPathResolver( mProject->pathResolver() );
268 mMapSettings.setElevationShadingRenderer( mProject->elevationShadingRenderer() );
269
270 emit extentChanged();
272 emit outputSizeChanged();
273 emit outputDpiChanged();
274 emit layersChanged();
276 emit zRangeChanged();
277}
278
280{
281 return mMapSettings.rotation();
282}
283
285{
286 if ( !qgsDoubleNear( rotation, 0 ) )
287 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
288}
289
291{
292 return mMapSettings.backgroundColor();
293}
294
296{
297 if ( mMapSettings.backgroundColor() == color )
298 return;
299
300 mMapSettings.setBackgroundColor( color );
302}
303
305{
306 return mDevicePixelRatio;
307}
308
310{
311 mDevicePixelRatio = devicePixelRatio;
313}
314
316{
317 return mMapSettings.isTemporal();
318}
319
321{
322 mMapSettings.setIsTemporal( temporal );
324}
325
327{
328 return mMapSettings.temporalRange().begin();
329}
330
331void QgsQuickMapSettings::setTemporalBegin( const QDateTime &begin )
332{
333 const QgsDateTimeRange range = mMapSettings.temporalRange();
334 mMapSettings.setTemporalRange( QgsDateTimeRange( begin, range.end() ) );
336}
337
339{
340 return mMapSettings.temporalRange().end();
341}
342
343void QgsQuickMapSettings::setTemporalEnd( const QDateTime &end )
344{
345 const QgsDateTimeRange range = mMapSettings.temporalRange();
346 mMapSettings.setTemporalRange( QgsDateTimeRange( range.begin(), end ) );
348}
349
351{
352 const QgsDoubleRange zRange = mMapSettings.zRange();
353 return zRange.lower();
354}
355
356void QgsQuickMapSettings::setZRangeLower( const double &lower )
357{
358 const QgsDoubleRange zRange = mMapSettings.zRange();
359 if ( zRange.lower() == lower )
360 {
361 return;
362 }
363
364 mMapSettings.setZRange( QgsDoubleRange( lower, zRange.upper(), zRange.includeLower(), zRange.includeUpper() ) );
365 emit zRangeChanged();
366}
367
369{
370 const QgsDoubleRange zRange = mMapSettings.zRange();
371 return zRange.upper();
372}
373
374void QgsQuickMapSettings::setZRangeUpper( const double &upper )
375{
376 const QgsDoubleRange zRange = mMapSettings.zRange();
377 if ( zRange.upper() == upper )
378 {
379 return;
380 }
381
382 mMapSettings.setZRange( QgsDoubleRange( zRange.lower(), upper, zRange.includeLower(), zRange.includeUpper() ) );
383 emit zRangeChanged();
384}
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
QgsRange which stores a range of double values.
Definition qgsrange.h:233
Base class for all map layer types.
Definition qgsmaplayer.h:80
virtual QgsRectangle extent() const
Returns the extent of the layer.
Contains configuration for rendering maps.
QgsPointXY layerToMapCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from layer's CRS to output CRS
QSize outputSize() const
Returns the size of the resulting map image, in pixels.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Represents a 2D point.
Definition qgspointxy.h:60
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:129
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:119
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:165
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
static QgsProject * instance()
Returns the QgsProject singleton instance.
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.
QgsCoordinateReferenceSystem crs
Definition qgsproject.h:115
void readProject(const QDomDocument &document)
Emitted when a project is being read.
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.
double zRangeLower
The Z range's lower value (since QGIS 3.38).
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.
double zRangeUpper
The Z range's upper value (since QGIS 3.38).
void setZRangeUpper(const double &upper)
The Z range's lower value (since QGIS 3.38).
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 setZRangeLower(const double &lower)
The Z range's lower value (since QGIS 3.38).
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 zRangeChanged()
Emitted when the Z range has changed.
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.
bool includeUpper() const
Returns true if the upper bound is inclusive, or false if the upper bound is exclusive.
Definition qgsrange.h:101
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:78
bool includeLower() const
Returns true if the lower bound is inclusive, or false if the lower bound is exclusive.
Definition qgsrange.h:93
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:85
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
T begin() const
Returns the beginning of the range.
Definition qgsrange.h:446
T end() const
Returns the upper bound of the range.
Definition qgsrange.h:453
Represent a 2-dimensional vector.
Definition qgsvector.h:31
double y() const
Returns the vector's y-component.
Definition qgsvector.h:153
double x() const
Returns the vector's x-component.
Definition qgsvector.h:144
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.
Definition qgsrange.h:761