QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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 }
64 else
65 {
66 mMapSettings.setTransformContext( QgsCoordinateTransformContext() );
67 }
68
69 emit projectChanged();
70}
71
73{
74 return mProject;
75}
76
78{
79 return mMapSettings.transformContext();
80}
81
83{
84 return mMapSettings.extent();
85}
86
88{
89 if ( mMapSettings.extent() == extent )
90 return;
91
92 mMapSettings.setExtent( extent );
93 emit extentChanged();
94}
95
97{
98 return QgsPoint( extent().center() );
99}
100
102{
103 QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
104
105 QgsRectangle e = mMapSettings.extent();
106 e.setXMinimum( e.xMinimum() + delta.x() );
107 e.setXMaximum( e.xMaximum() + delta.x() );
108 e.setYMinimum( e.yMinimum() + delta.y() );
109 e.setYMaximum( e.yMaximum() + delta.y() );
110
111 setExtent( e );
112}
113
115{
116 return mMapSettings.mapUnitsPerPixel();
117}
118
120{
121 Q_ASSERT( layer );
122
123 const QgsRectangle extent = mapSettings().layerToMapCoordinates( layer, layer->extent() );
124
125 if ( !extent.isEmpty() )
126 {
127 if ( shouldZoom )
128 setExtent( extent );
129 else
130 setCenter( QgsPoint( extent.center() ) );
131 }
132}
133
135{
136 return mMapSettings.mapUnitsPerPixel() * devicePixelRatio();
137}
138
140{
141 return mMapSettings.visibleExtent();
142}
143
145{
146 QgsPointXY pt( point.x(), point.y() );
147 QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
148 pp.setX( pp.x() / devicePixelRatio() );
149 pp.setY( pp.y() / devicePixelRatio() );
150 return pp.toQPointF();
151}
152
154{
155 const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x() * devicePixelRatio(), point.y() * devicePixelRatio() );
156 return QgsPoint( pp );
157}
158
160{
161 mMapSettings.setTransformContext( ctx );
162}
163
165{
166 return mMapSettings;
167}
168
170{
171 return mMapSettings.outputSize();
172}
173
175{
176 outputSize.setWidth( outputSize.width() * devicePixelRatio() );
177 outputSize.setHeight( outputSize.height() * devicePixelRatio() );
178 if ( mMapSettings.outputSize() == outputSize )
179 return;
180
181 mMapSettings.setOutputSize( outputSize );
182 emit outputSizeChanged();
183}
184
186{
187 return mMapSettings.outputDpi();
188}
189
191{
193 if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
194 return;
195
196 mMapSettings.setOutputDpi( outputDpi );
197 emit outputDpiChanged();
198}
199
201{
202 return mMapSettings.destinationCrs();
203}
204
206{
207 if ( mMapSettings.destinationCrs() == destinationCrs )
208 return;
209
210 mMapSettings.setDestinationCrs( destinationCrs );
212}
213
214QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
215{
216 return mMapSettings.layers();
217}
218
219void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
220{
221 mMapSettings.setLayers( layers );
222 emit layersChanged();
223}
224
225void QgsQuickMapSettings::onCrsChanged()
226{
227 setDestinationCrs( mProject->crs() );
228}
229
230void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
231{
232 if ( mProject )
233 {
234 int red = mProject->readNumEntry( u"Gui"_s, u"/CanvasColorRedPart"_s, 255 );
235 int green = mProject->readNumEntry( u"Gui"_s, u"/CanvasColorGreenPart"_s, 255 );
236 int blue = mProject->readNumEntry( u"Gui"_s, u"/CanvasColorBluePart"_s, 255 );
237 mMapSettings.setBackgroundColor( QColor( red, green, blue ) );
238
239 const bool isTemporal = mProject->readNumEntry( u"TemporalControllerWidget"_s, u"/NavigationMode"_s, 0 ) != 0;
240 const QString startString = QgsProject::instance()->readEntry( u"TemporalControllerWidget"_s, u"/StartDateTime"_s );
241 const QString endString = QgsProject::instance()->readEntry( u"TemporalControllerWidget"_s, u"/EndDateTime"_s );
242 mMapSettings.setIsTemporal( isTemporal );
243 mMapSettings.setTemporalRange( QgsDateTimeRange( QDateTime::fromString( startString, Qt::ISODateWithMs ), QDateTime::fromString( endString, Qt::ISODateWithMs ) ) );
244 }
245
246 QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
247 bool foundTheMapCanvas = false;
248 for ( int i = 0; i < nodes.size(); i++ )
249 {
250 QDomNode node = nodes.item( 0 );
251 QDomElement element = node.toElement();
252
253 if ( element.hasAttribute( u"name"_s ) && element.attribute( u"name"_s ) == "theMapCanvas"_L1 )
254 {
255 foundTheMapCanvas = true;
256 mMapSettings.readXml( node );
257
258 if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
259 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
260 }
261 }
262 if ( !foundTheMapCanvas )
263 {
264 mMapSettings.setDestinationCrs( mProject->crs() );
265 mMapSettings.setExtent( mProject->viewSettings()->fullExtent() );
266 }
267
268 mMapSettings.setRotation( 0 );
269
270 mMapSettings.setTransformContext( mProject->transformContext() );
271 mMapSettings.setPathResolver( mProject->pathResolver() );
272 mMapSettings.setElevationShadingRenderer( mProject->elevationShadingRenderer() );
273
274 emit extentChanged();
276 emit outputSizeChanged();
277 emit outputDpiChanged();
278 emit layersChanged();
280 emit zRangeChanged();
281}
282
284{
285 return mMapSettings.rotation();
286}
287
289{
290 if ( !qgsDoubleNear( rotation, 0 ) )
291 QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
292}
293
295{
296 return mMapSettings.backgroundColor();
297}
298
300{
301 if ( mMapSettings.backgroundColor() == color )
302 return;
303
304 mMapSettings.setBackgroundColor( color );
306}
307
309{
310 return mDevicePixelRatio;
311}
312
314{
315 mDevicePixelRatio = devicePixelRatio;
317}
318
320{
321 return mMapSettings.isTemporal();
322}
323
325{
326 mMapSettings.setIsTemporal( temporal );
328}
329
331{
332 return mMapSettings.temporalRange().begin();
333}
334
335void QgsQuickMapSettings::setTemporalBegin( const QDateTime &begin )
336{
337 const QgsDateTimeRange range = mMapSettings.temporalRange();
338 mMapSettings.setTemporalRange( QgsDateTimeRange( begin, range.end() ) );
340}
341
343{
344 return mMapSettings.temporalRange().end();
345}
346
347void QgsQuickMapSettings::setTemporalEnd( const QDateTime &end )
348{
349 const QgsDateTimeRange range = mMapSettings.temporalRange();
350 mMapSettings.setTemporalRange( QgsDateTimeRange( range.begin(), end ) );
352}
353
355{
356 const QgsDoubleRange zRange = mMapSettings.zRange();
357 return zRange.lower();
358}
359
360void QgsQuickMapSettings::setZRangeLower( const double &lower )
361{
362 const QgsDoubleRange zRange = mMapSettings.zRange();
363 if ( zRange.lower() == lower )
364 {
365 return;
366 }
367
368 mMapSettings.setZRange( QgsDoubleRange( lower, zRange.upper(), zRange.includeLower(), zRange.includeUpper() ) );
369 emit zRangeChanged();
370}
371
373{
374 const QgsDoubleRange zRange = mMapSettings.zRange();
375 return zRange.upper();
376}
377
378void QgsQuickMapSettings::setZRangeUpper( const double &upper )
379{
380 const QgsDoubleRange zRange = mMapSettings.zRange();
381 if ( zRange.upper() == upper )
382 {
383 return;
384 }
385
386 mMapSettings.setZRange( QgsDoubleRange( zRange.lower(), upper, zRange.includeLower(), zRange.includeUpper() ) );
387 emit zRangeChanged();
388}
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:236
Base class for all map layer types.
Definition qgsmaplayer.h:83
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:62
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:131
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:121
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:167
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:113
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:119
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:104
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:81
bool includeLower() const
Returns true if the lower bound is inclusive, or false if the lower bound is exclusive.
Definition qgsrange.h:96
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:88
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:449
T end() const
Returns the upper bound of the range.
Definition qgsrange.h:456
Represent a 2-dimensional vector.
Definition qgsvector.h:34
double y() const
Returns the vector's y-component.
Definition qgsvector.h:156
double x() const
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:6924
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.
Definition qgsrange.h:764