QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
22 #include "qgsmessagelog.h"
23 #include "qgsproject.h"
24 
26  : QObject( parent )
27 {
28  // Connect signals for derived values
35 }
36 
38 {
39  if ( project == mProject )
40  return;
41 
42  // If we have already something connected, disconnect it!
43  if ( mProject )
44  {
45  mProject->disconnect( this );
46  }
47 
48  mProject = project;
49 
50  // Connect all signals
51  if ( mProject )
52  {
53  connect( mProject, &QgsProject::readProject, this, &QgsQuickMapSettings::onReadProject );
54  connect( mProject, &QgsProject::crsChanged, this, &QgsQuickMapSettings::onCrsChanged );
55  setDestinationCrs( mProject->crs() );
56  mMapSettings.setTransformContext( mProject->transformContext() );
57  mMapSettings.setPathResolver( mProject->pathResolver() );
58  }
59  else
60  {
62  }
63 
64  emit projectChanged();
65 }
66 
68 {
69  return mProject;
70 }
71 
73 {
74  return mMapSettings.transformContext();
75 }
76 
78 {
79  return mMapSettings.extent();
80 }
81 
83 {
84  if ( mMapSettings.extent() == extent )
85  return;
86 
87  mMapSettings.setExtent( extent );
88  emit extentChanged();
89 }
90 
92 {
93  QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
94 
95  QgsRectangle e = mMapSettings.extent();
96  e.setXMinimum( e.xMinimum() + delta.x() );
97  e.setXMaximum( e.xMaximum() + delta.x() );
98  e.setYMinimum( e.yMinimum() + delta.y() );
99  e.setYMaximum( e.yMaximum() + delta.y() );
100 
101  setExtent( e );
102 }
103 
105 {
106  return mMapSettings.mapUnitsPerPixel();
107 }
108 
109 void QgsQuickMapSettings::setCenterToLayer( QgsMapLayer *layer, bool shouldZoom )
110 {
111  Q_ASSERT( layer );
112 
113  const QgsRectangle extent = mapSettings().layerToMapCoordinates( layer, layer->extent() );
114 
115  if ( !extent.isEmpty() )
116  {
117  if ( shouldZoom )
118  setExtent( extent );
119  else
121  }
122 }
123 
125 {
126  return mMapSettings.mapUnitsPerPixel() * devicePixelRatio();
127 }
128 
130 {
131  return mMapSettings.visibleExtent();
132 }
133 
135 {
136  QgsPointXY pt( point.x(), point.y() );
137  QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
138  pp.setX( pp.x() / devicePixelRatio() );
139  pp.setY( pp.y() / devicePixelRatio() );
140  return pp.toQPointF();
141 }
142 
144 {
145  const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x() * devicePixelRatio(), point.y() * devicePixelRatio() );
146  return QgsPoint( pp );
147 }
148 
150 {
151  return mMapSettings;
152 }
153 
155 {
156  return mMapSettings.outputSize();
157 }
158 
159 void QgsQuickMapSettings::setOutputSize( QSize outputSize )
160 {
161  outputSize.setWidth( outputSize.width() * devicePixelRatio() );
162  outputSize.setHeight( outputSize.height() * devicePixelRatio() );
163  if ( mMapSettings.outputSize() == outputSize )
164  return;
165 
166  mMapSettings.setOutputSize( outputSize );
167  emit outputSizeChanged();
168 }
169 
171 {
172  return mMapSettings.outputDpi();
173 }
174 
175 void QgsQuickMapSettings::setOutputDpi( double outputDpi )
176 {
178  if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
179  return;
180 
181  mMapSettings.setOutputDpi( outputDpi );
182  emit outputDpiChanged();
183 }
184 
186 {
187  return mMapSettings.destinationCrs();
188 }
189 
191 {
192  if ( mMapSettings.destinationCrs() == destinationCrs )
193  return;
194 
195  mMapSettings.setDestinationCrs( destinationCrs );
196  emit destinationCrsChanged();
197 }
198 
199 QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
200 {
201  return mMapSettings.layers();
202 }
203 
204 void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
205 {
206  mMapSettings.setLayers( layers );
207  emit layersChanged();
208 }
209 
210 void QgsQuickMapSettings::onCrsChanged()
211 {
212  setDestinationCrs( mProject->crs() );
213 }
214 
215 void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
216 {
217  if ( mProject )
218  {
219  int red = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorRedPart" ), 255 );
220  int green = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorGreenPart" ), 255 );
221  int blue = mProject->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorBluePart" ), 255 );
222  mMapSettings.setBackgroundColor( QColor( red, green, blue ) );
223  }
224 
225  QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
226  bool foundTheMapCanvas = false;
227  for ( int i = 0; i < nodes.size(); i++ )
228  {
229  QDomNode node = nodes.item( 0 );
230  QDomElement element = node.toElement();
231 
232  if ( element.hasAttribute( QStringLiteral( "name" ) ) && element.attribute( QStringLiteral( "name" ) ) == QStringLiteral( "theMapCanvas" ) )
233  {
234  foundTheMapCanvas = true;
235  mMapSettings.readXml( node );
236 
237  if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
238  QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
239  }
240  }
241  if ( !foundTheMapCanvas )
242  {
243  mMapSettings.setDestinationCrs( mProject->crs() );
244  mMapSettings.setExtent( mMapSettings.fullExtent() );
245  }
246 
247  mMapSettings.setRotation( 0 );
248 
249  mMapSettings.setTransformContext( mProject->transformContext() );
250  mMapSettings.setPathResolver( mProject->pathResolver() );
251 
252  emit extentChanged();
253  emit destinationCrsChanged();
254  emit outputSizeChanged();
255  emit outputDpiChanged();
256  emit layersChanged();
257 }
258 
260 {
261  return mMapSettings.rotation();
262 }
263 
264 void QgsQuickMapSettings::setRotation( double rotation )
265 {
266  if ( !qgsDoubleNear( rotation, 0 ) )
267  QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
268 }
269 
271 {
272  return mMapSettings.backgroundColor();
273 }
274 
275 void QgsQuickMapSettings::setBackgroundColor( const QColor &color )
276 {
277  if ( mMapSettings.backgroundColor() == color )
278  return;
279 
280  mMapSettings.setBackgroundColor( color );
281  emit backgroundColorChanged();
282 }
283 
285 {
286  return mDevicePixelRatio;
287 }
288 
289 void QgsQuickMapSettings::setDevicePixelRatio( const qreal &devicePixelRatio )
290 {
291  mDevicePixelRatio = devicePixelRatio;
292 }
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:70
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
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.
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.
const QgsMapToPixel & mapToPixel() const
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
QgsRectangle fullExtent() const
returns current extent of layer set
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.
QList< QgsMapLayer * > layers() const
Returns the list of layers which will be rendered in the map.
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
Transform device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:82
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
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
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.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:105
void readProject(const QDomDocument &)
Emitted when a project is being read.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:104
double outputDpi
Output DPI used for conversion between real world units (e.g.
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 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.
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...
QgsRectangle visibleExtent
Returns the actual extent derived from requested extent that takes takes output image size into accou...
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 takes output image size into accou...
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.
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.
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 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.
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
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:598