QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgsmaplayer.h"
19 #include "qgsmessagelog.h"
20 #include "qgsproject.h"
21 #include "qgis.h"
22 
23 #include "qgsquickmapsettings.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  setDestinationCrs( mProject->crs() );
55  mMapSettings.setTransformContext( mProject->transformContext() );
56  }
57  else
58  {
60  }
61 
62  emit projectChanged();
63 }
64 
66 {
67  return mProject;
68 }
69 
71 {
72  return mMapSettings.transformContext();
73 }
74 
76 {
77  return mMapSettings.extent();
78 }
79 
81 {
82  if ( mMapSettings.extent() == extent )
83  return;
84 
85  mMapSettings.setExtent( extent );
86  emit extentChanged();
87 }
88 
90 {
91  QgsVector delta = QgsPointXY( center ) - mMapSettings.extent().center();
92 
93  QgsRectangle e = mMapSettings.extent();
94  e.setXMinimum( e.xMinimum() + delta.x() );
95  e.setXMaximum( e.xMaximum() + delta.x() );
96  e.setYMinimum( e.yMinimum() + delta.y() );
97  e.setYMaximum( e.yMaximum() + delta.y() );
98 
99  setExtent( e );
100 }
101 
103 {
104  return mMapSettings.mapUnitsPerPixel();
105 }
106 
108 {
109  return mMapSettings.visibleExtent();
110 }
111 
113 {
114  QgsPointXY pt( point.x(), point.y() );
115  QgsPointXY pp = mMapSettings.mapToPixel().transform( pt );
116  return pp.toQPointF();
117 }
118 
120 {
121  // use floating point precision with mapToCoordinates (i.e. do not use QPointF::toPoint)
122  // this is to avoid rounding errors with an odd screen width or height
123  // and the point being set to the exact center of it
124  const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x(), point.y() );
125  return QgsPoint( pp );
126 }
127 
129 {
130  return mMapSettings;
131 }
132 
134 {
135  return mMapSettings.outputSize();
136 }
137 
138 void QgsQuickMapSettings::setOutputSize( const QSize &outputSize )
139 {
140  if ( mMapSettings.outputSize() == outputSize )
141  return;
142 
143  mMapSettings.setOutputSize( outputSize );
144  emit outputSizeChanged();
145 }
146 
147 double QgsQuickMapSettings::outputDpi() const
148 {
149  return mMapSettings.outputDpi();
150 }
151 
152 void QgsQuickMapSettings::setOutputDpi( double outputDpi )
153 {
154  if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
155  return;
156 
157  mMapSettings.setOutputDpi( outputDpi );
158  emit outputDpiChanged();
159 }
160 
162 {
163  return mMapSettings.destinationCrs();
164 }
165 
167 {
168  if ( mMapSettings.destinationCrs() == destinationCrs )
169  return;
170 
171  mMapSettings.setDestinationCrs( destinationCrs );
172  emit destinationCrsChanged();
173 }
174 
175 QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
176 {
177  return mMapSettings.layers();
178 }
179 
180 void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
181 {
182  mMapSettings.setLayers( layers );
183  emit layersChanged();
184 }
185 
186 void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
187 {
188  if ( mProject )
189  {
190  mMapSettings.setBackgroundColor( mProject->backgroundColor() );
191  }
192 
193  QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
194  if ( nodes.count() )
195  {
196  QDomNode node = nodes.item( 0 );
197 
198  mMapSettings.readXml( node );
199 
200  if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
201  QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
202 
203  mMapSettings.setRotation( 0 );
204 
205  emit extentChanged();
206  emit destinationCrsChanged();
207  emit outputSizeChanged();
208  emit outputDpiChanged();
209  emit layersChanged();
210  }
211 }
212 
213 double QgsQuickMapSettings::rotation() const
214 {
215  return mMapSettings.rotation();
216 }
217 
218 void QgsQuickMapSettings::setRotation( double rotation )
219 {
220  if ( !qgsDoubleNear( rotation, 0 ) )
221  QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
222 }
223 
225 {
226  return mMapSettings.backgroundColor();
227 }
228 
229 void QgsQuickMapSettings::setBackgroundColor( const QColor &color )
230 {
231  if ( mMapSettings.backgroundColor() == color )
232  return;
233 
234  mMapSettings.setBackgroundColor( color );
235  emit backgroundColorChanged();
236 }
QgsMapSettings::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
sets destination coordinate reference system
Definition: qgsmapsettings.cpp:309
qgsmaplayerstylemanager.h
QgsQuickMapSettings::setExtent
void setExtent(const QgsRectangle &extent)
Set coordinates of the rectangle which should be rendered.
Definition: qgsquickmapsettings.cpp:80
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
QgsQuickMapSettings::extentChanged
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
QgsMapSettings::readXml
void readXml(QDomNode &node)
Definition: qgsmapsettings.cpp:610
QgsQuickMapSettings::destinationCrsChanged
void destinationCrsChanged()
CRS of destination coordinate reference system.
QgsMapSettings::outputSize
QSize outputSize() const
Returns the size of the resulting map image.
Definition: qgsmapsettings.cpp:234
QgsMapSettings::setRotation
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
Definition: qgsmapsettings.cpp:106
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsQuickMapSettings::visibleExtent
QgsRectangle visibleExtent
Returns the actual extent derived from requested extent that takes takes output image size into accou...
Definition: qgsquickmapsettings.h:69
QgsRectangle::setXMinimum
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:130
QgsQuickMapSettings::outputDpi
double outputDpi
Output DPI used for conversion between real world units (e.g.
Definition: qgsquickmapsettings.h:98
QgsQuickMapSettings::mapUnitsPerPixel
double mapUnitsPerPixel
Returns the distance in geographical coordinates that equals to one pixel in the map.
Definition: qgsquickmapsettings.h:71
QgsQuickMapSettings::project
QgsProject project
A project property should be used as a primary source of project all other components in the applicat...
Definition: qgsquickmapsettings.h:57
QgsQuickMapSettings::projectChanged
void projectChanged()
A project property should be used as a primary source of project all other components in the applicat...
QgsRectangle::xMaximum
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:99
QgsMapSettings::mapUnitsPerPixel
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
Definition: qgsmapsettings.cpp:390
qgis.h
QgsPointXY::toQPointF
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:154
QgsProject::readProject
void readProject(const QDomDocument &)
Emitted when a project is being read.
QgsQuickMapSettings::QgsQuickMapSettings
QgsQuickMapSettings(QObject *parent=nullptr)
Create new map settings.
Definition: qgsquickmapsettings.cpp:25
QgsQuickMapSettings::setRotation
void setRotation(double rotation)
The rotation of the resulting map image, in degrees clockwise.
Definition: qgsquickmapsettings.cpp:218
QgsMapSettings::setOutputDpi
void setOutputDpi(double dpi)
Sets DPI used for conversion between real world units (e.g. mm) and pixels.
Definition: qgsmapsettings.cpp:267
QgsQuickMapSettings::coordinateToScreen
Q_INVOKABLE QPointF coordinateToScreen(const QgsPoint &point) const
Convert a map coordinate to screen pixel coordinates.
Definition: qgsquickmapsettings.cpp:112
QgsMapToPixel::toMapCoordinates
QgsPointXY toMapCoordinates(int x, int y) const
Transform device coordinates to map (world) coordinates.
Definition: qgsmaptopixel.cpp:108
QgsQuickMapSettings::mapSettings
QgsMapSettings mapSettings() const
Clone map settings.
Definition: qgsquickmapsettings.cpp:128
QgsQuickMapSettings::layersChanged
void layersChanged()
Set list of layers for map rendering.
QgsRectangle
Definition: qgsrectangle.h:41
QgsQuickMapSettings::screenToCoordinate
Q_INVOKABLE QgsPoint screenToCoordinate(const QPointF &point) const
Convert a screen coordinate to a map coordinate.
Definition: qgsquickmapsettings.cpp:119
QgsProject
Definition: qgsproject.h:92
QgsQuickMapSettings::setLayers
void setLayers(const QList< QgsMapLayer * > &layers)
Set list of layers for map rendering.
Definition: qgsquickmapsettings.cpp:180
QgsMapSettings::extent
QgsRectangle extent() const
Returns geographical coordinates of the rectangle that should be rendered.
Definition: qgsmapsettings.cpp:74
QgsQuickMapSettings::setOutputDpi
void setOutputDpi(double outputDpi)
Sets DPI used for conversion between real world units (e.g. mm) and pixels.
Definition: qgsquickmapsettings.cpp:152
QgsPoint::y
double y
Definition: qgspoint.h:59
QgsQuickMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
Definition: qgsquickmapsettings.h:105
QgsQuickMapSettings::outputSize
QSize outputSize
The size of the resulting map image.
Definition: qgsquickmapsettings.h:91
QgsMapSettings::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets the background color of the map.
Definition: qgsmapsettings.h:290
QgsQuickMapSettings::setOutputSize
void setOutputSize(const QSize &outputSize)
Sets the size of the resulting map image.
Definition: qgsquickmapsettings.cpp:138
QgsQuickMapSettings::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
sets destination coordinate reference system
Definition: qgsquickmapsettings.cpp:166
QgsMapSettings::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgsmapsettings.cpp:400
QgsMapSettings::rotation
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
Definition: qgsmapsettings.cpp:101
QgsVector::x
double x() const
Returns the vector's x-component.
Definition: qgsvector.cpp:76
QgsQuickMapSettings::outputDpiChanged
void outputDpiChanged()
Output DPI used for conversion between real world units (e.g.
QgsQuickMapSettings::rotation
double rotation
The rotation of the resulting map image, in degrees clockwise.
Definition: qgsquickmapsettings.h:77
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
qgsmaplayer.h
QgsQuickMapSettings::layers
QList< QgsMapLayer * > layers
Set list of layers for map rendering.
Definition: qgsquickmapsettings.h:115
QgsMapSettings::backgroundColor
QColor backgroundColor() const
Gets the background color of the map.
Definition: qgsmapsettings.h:292
QgsRectangle::setXMaximum
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:135
QgsQuickMapSettings::setProject
void setProject(QgsProject *project)
A project property should be used as a primary source of project all other components in the applicat...
Definition: qgsquickmapsettings.cpp:37
QgsMapSettings::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
Definition: qgsmapsettings.cpp:410
QgsQuickMapSettings::mapUnitsPerPixelChanged
void mapUnitsPerPixelChanged()
Returns the distance in geographical coordinates that equals to one pixel in the map.
QgsQuickMapSettings::rotationChanged
void rotationChanged()
The rotation of the resulting map image, in degrees clockwise.
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsMapToPixel::transform
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.cpp:217
QgsRectangle::yMaximum
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
QgsMapSettings::setLayers
void setLayers(const QList< QgsMapLayer * > &layers)
Set list of layers for map rendering.
Definition: qgsmapsettings.cpp:286
QgsPointXY
Definition: qgspointxy.h:43
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
Definition: qgsmapsettings.cpp:317
QgsQuickMapSettings::transformContext
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgsquickmapsettings.cpp:70
QgsQuickMapSettings::outputSizeChanged
void outputSizeChanged()
The size of the resulting map image.
QgsQuickMapSettings::backgroundColorChanged
void backgroundColorChanged()
The background color used to render the map.
QgsRectangle::center
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:230
QgsVector::y
double y() const
Returns the vector's y-component.
Definition: qgsvector.cpp:81
QgsQuickMapSettings::backgroundColor
QColor backgroundColor
The background color used to render the map.
Definition: qgsquickmapsettings.h:84
QgsVector
Definition: qgsvector.h:29
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsRectangle::yMinimum
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
QgsMapSettings::layers
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
Definition: qgsmapsettings.cpp:281
QgsMapSettings::setOutputSize
void setOutputSize(QSize size)
Sets the size of the resulting map image.
Definition: qgsmapsettings.cpp:239
QgsMapSettings::outputDpi
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
Definition: qgsmapsettings.cpp:262
QgsMapSettings
Definition: qgsmapsettings.h:86
QgsMapSettings::visibleExtent
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
Definition: qgsmapsettings.cpp:370
QgsQuickMapSettings::extent
QgsRectangle extent
Geographical coordinates of the rectangle that should be rendered.
Definition: qgsquickmapsettings.h:67
QgsMapSettings::setExtent
void setExtent(const QgsRectangle &rect, bool magnified=true)
Set coordinates of the rectangle which should be rendered.
Definition: qgsmapsettings.cpp:79
qgsquickmapsettings.h
QgsQuickMapSettings::visibleExtentChanged
void visibleExtentChanged()
Returns the actual extent derived from requested extent that takes takes output image size into accou...
QgsProject::crs
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:98
QgsMapSettings::mapToPixel
const QgsMapToPixel & mapToPixel() const
Definition: qgsmapsettings.h:433
QgsPoint::x
double x
Definition: qgspoint.h:58
qgsproject.h
QgsQuickMapSettings::setBackgroundColor
void setBackgroundColor(const QColor &color)
The background color used to render the map.
Definition: qgsquickmapsettings.cpp:229
QgsQuickMapSettings::setCenter
Q_INVOKABLE void setCenter(const QgsPoint &center)
Move current map extent to have center point defined by center.
Definition: qgsquickmapsettings.cpp:89
QgsRectangle::setYMinimum
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:140
QgsRectangle::setYMaximum
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:145
QgsRectangle::xMinimum
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
QgsProject::backgroundColor
QColor backgroundColor
Definition: qgsproject.h:107
qgsmessagelog.h