QGIS API Documentation 4.1.0-Master (659fe69c07c)
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 <QString>
25
26#include "moc_qgsquickmapsettings.cpp"
27
28using namespace Qt::StringLiterals;
29
41
43{
44 if ( project == mProject )
45 return;
46
47 // If we have already something connected, disconnect it!
48 if ( mProject )
49 {
50 mProject->disconnect( this );
51 }
52
53 mProject = project;
54
55 // Connect all signals
56 if ( mProject )
57 {
58 connect( mProject, &QgsProject::readProject, this, &QgsQuickMapSettings::onReadProject );
59 connect( mProject, &QgsProject::crsChanged, this, &QgsQuickMapSettings::onCrsChanged );
60 setDestinationCrs( mProject->crs() );
61 mMapSettings.setTransformContext( mProject->transformContext() );
62 mMapSettings.setPathResolver( mProject->pathResolver() );
63 mMapSettings.setEllipsoid( mProject->ellipsoid() );
64 }
65 else
66 {
67 mMapSettings.setTransformContext( QgsCoordinateTransformContext() );
68 }
69
70 emit projectChanged();
71}
72
74{
75 return mProject;
76}
77
79{
80 return mMapSettings.transformContext();
81}
82
84{
85 return mMapSettings.extent();
86}
87
89{
90 if ( mMapSettings.extent() == extent )
91 return;
92
93 mMapSettings.setExtent( extent );
94 emit extentChanged();
95}
96
98{
99 return QgsPoint( extent().center() );
100}
101
103{
104 QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
105
106 QgsRectangle e = mMapSettings.extent();
107 e.setXMinimum( e.xMinimum() + delta.x() );
108 e.setXMaximum( e.xMaximum() + delta.x() );
109 e.setYMinimum( e.yMinimum() + delta.y() );
110 e.setYMaximum( e.yMaximum() + delta.y() );
111
112 setExtent( e );
113}
114
116{
117 return mMapSettings.mapUnitsPerPixel();
118}
119
121{
122 Q_ASSERT( layer );
123
124 const QgsRectangle extent = mapSettings().layerToMapCoordinates( layer, layer->extent() );
125
126 if ( !extent.isEmpty() )
127 {
128 if ( shouldZoom )
129 setExtent( extent );
130 else
131 setCenter( QgsPoint( extent.center() ) );
132 }
133}
134
136{
137 return mMapSettings.mapUnitsPerPixel() * devicePixelRatio();
138}
139
141{
142 return mMapSettings.visibleExtent();
143}
144
146{
147 QgsPointXY pt( point.x(), point.y() );
148 QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
149 pp.setX( pp.x() / devicePixelRatio() );
150 pp.setY( pp.y() / devicePixelRatio() );
151 return pp.toQPointF();
152}
153
155{
156 const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x() * devicePixelRatio(), point.y() * devicePixelRatio() );
157 return QgsPoint( pp );
158}
159
161{
162 mMapSettings.setTransformContext( ctx );
163}
164
166{
167 return mMapSettings;
168}
169
171{
172 return mMapSettings.outputSize();
173}
174
176{
177 outputSize.setWidth( outputSize.width() * devicePixelRatio() );
178 outputSize.setHeight( outputSize.height() * devicePixelRatio() );
179 if ( mMapSettings.outputSize() == outputSize )
180 return;
181
182 mMapSettings.setOutputSize( outputSize );
183 emit outputSizeChanged();
184}
185
187{
188 return mMapSettings.outputDpi();
189}
190
192{
194 if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
195 return;
196
197 mMapSettings.setOutputDpi( outputDpi );
198 emit outputDpiChanged();
199}
200
202{
203 return mMapSettings.destinationCrs();
204}
205
207{
208 if ( mMapSettings.destinationCrs() == destinationCrs )
209 return;
210
211 mMapSettings.setDestinationCrs( destinationCrs );
213}
214
215QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
216{
217 return mMapSettings.layers();
218}
219
220void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
221{
222 mMapSettings.setLayers( layers );
223 emit layersChanged();
224}
225
226void QgsQuickMapSettings::onCrsChanged()
227{
228 setDestinationCrs( mProject->crs() );
229 mMapSettings.setEllipsoid( mProject->ellipsoid() );
230}
231
232void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
233{
234 if ( mProject )
235 {
236 int red = mProject->readNumEntry( u"Gui"_s, u"/CanvasColorRedPart"_s, 255 );
237 int green = mProject->readNumEntry( u"Gui"_s, u"/CanvasColorGreenPart"_s, 255 );
238 int blue = mProject->readNumEntry( u"Gui"_s, u"/CanvasColorBluePart"_s, 255 );
239 mMapSettings.setBackgroundColor( QColor( red, green, blue ) );
240
241 const bool isTemporal = mProject->readNumEntry( u"TemporalControllerWidget"_s, u"/NavigationMode"_s, 0 ) != 0;
242 const QString startString = QgsProject::instance()->readEntry( u"TemporalControllerWidget"_s, u"/StartDateTime"_s );
243 const QString endString = QgsProject::instance()->readEntry( u"TemporalControllerWidget"_s, u"/EndDateTime"_s );
244 mMapSettings.setIsTemporal( isTemporal );
245 mMapSettings.setTemporalRange( QgsDateTimeRange( QDateTime::fromString( startString, Qt::ISODateWithMs ), QDateTime::fromString( endString, Qt::ISODateWithMs ) ) );
246 }
247
248 QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
249 bool foundTheMapCanvas = false;
250 for ( int i = 0; i < nodes.size(); i++ )
251 {
252 QDomNode node = nodes.item( 0 );
253 QDomElement element = node.toElement();
254
255 if ( element.hasAttribute( u"name"_s ) && element.attribute( u"name"_s ) == "theMapCanvas"_L1 )
256 {
257 foundTheMapCanvas = true;
258 mMapSettings.readXml( node );
259
260 if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
261 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
262 }
263 }
264 if ( !foundTheMapCanvas )
265 {
266 mMapSettings.setDestinationCrs( mProject->crs() );
267 mMapSettings.setExtent( mProject->viewSettings()->fullExtent() );
268 }
269
270 mMapSettings.setRotation( 0 );
271
272 mMapSettings.setTransformContext( mProject->transformContext() );
273 mMapSettings.setPathResolver( mProject->pathResolver() );
274 mMapSettings.setElevationShadingRenderer( mProject->elevationShadingRenderer() );
275
276 emit extentChanged();
278 emit outputSizeChanged();
279 emit outputDpiChanged();
280 emit layersChanged();
282 emit zRangeChanged();
283}
284
286{
287 return mMapSettings.rotation();
288}
289
291{
292 if ( !qgsDoubleNear( rotation, 0 ) )
293 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
294}
295
297{
298 return mMapSettings.backgroundColor();
299}
300
302{
303 if ( mMapSettings.backgroundColor() == color )
304 return;
305
306 mMapSettings.setBackgroundColor( color );
308}
309
311{
312 return mDevicePixelRatio;
313}
314
316{
317 mDevicePixelRatio = devicePixelRatio;
319}
320
322{
323 return mMapSettings.isTemporal();
324}
325
327{
328 mMapSettings.setIsTemporal( temporal );
330}
331
333{
334 return mMapSettings.temporalRange().begin();
335}
336
337void QgsQuickMapSettings::setTemporalBegin( const QDateTime &begin )
338{
339 const QgsDateTimeRange range = mMapSettings.temporalRange();
340 mMapSettings.setTemporalRange( QgsDateTimeRange( begin, range.end() ) );
342}
343
345{
346 return mMapSettings.temporalRange().end();
347}
348
349void QgsQuickMapSettings::setTemporalEnd( const QDateTime &end )
350{
351 const QgsDateTimeRange range = mMapSettings.temporalRange();
352 mMapSettings.setTemporalRange( QgsDateTimeRange( range.begin(), end ) );
354}
355
357{
358 const QgsDoubleRange zRange = mMapSettings.zRange();
359 return zRange.lower();
360}
361
362void QgsQuickMapSettings::setZRangeLower( const double &lower )
363{
364 const QgsDoubleRange zRange = mMapSettings.zRange();
365 if ( zRange.lower() == lower )
366 {
367 return;
368 }
369
370 mMapSettings.setZRange( QgsDoubleRange( lower, zRange.upper(), zRange.includeLower(), zRange.includeUpper() ) );
371 emit zRangeChanged();
372}
373
375{
376 const QgsDoubleRange zRange = mMapSettings.zRange();
377 return zRange.upper();
378}
379
380void QgsQuickMapSettings::setZRangeUpper( const double &upper )
381{
382 const QgsDoubleRange zRange = mMapSettings.zRange();
383 if ( zRange.upper() == upper )
384 {
385 return;
386 }
387
388 mMapSettings.setZRange( QgsDoubleRange( zRange.lower(), upper, zRange.includeLower(), zRange.includeUpper() ) );
389 emit zRangeChanged();
390}
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:217
Base class for all map layer types.
Definition qgsmaplayer.h:83
virtual Q_INVOKABLE 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
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Represents a 2D point.
Definition qgspointxy.h:62
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:132
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:122
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:168
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double x
Definition qgspoint.h:56
double y
Definition qgspoint.h:57
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString ellipsoid
Definition qgsproject.h:122
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:120
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:102
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:79
bool includeLower() const
Returns true if the lower bound is inclusive, or false if the lower bound is exclusive.
Definition qgsrange.h:94
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:86
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:408
T end() const
Returns the upper bound of the range.
Definition qgsrange.h:415
Represent a 2-dimensional vector.
Definition qgsvector.h:34
double y() const
Returns the vector's y-component.
Definition qgsvector.h:155
double x() const
Returns the vector's x-component.
Definition qgsvector.h:146
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7340
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.
Definition qgsrange.h:705