QGIS API Documentation  3.2.0-Bonn (bc43194)
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  const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.toPoint() );
122  return QgsPoint( pp );
123 }
124 
126 {
127  return mMapSettings;
128 }
129 
131 {
132  return mMapSettings.outputSize();
133 }
134 
136 {
137  if ( mMapSettings.outputSize() == outputSize )
138  return;
139 
140  mMapSettings.setOutputSize( outputSize );
141  emit outputSizeChanged();
142 }
143 
144 double QgsQuickMapSettings::outputDpi() const
145 {
146  return mMapSettings.outputDpi();
147 }
148 
150 {
151  if ( qgsDoubleNear( mMapSettings.outputDpi(), outputDpi ) )
152  return;
153 
154  mMapSettings.setOutputDpi( outputDpi );
155  emit outputDpiChanged();
156 }
157 
159 {
160  return mMapSettings.destinationCrs();
161 }
162 
164 {
165  if ( mMapSettings.destinationCrs() == destinationCrs )
166  return;
167 
168  mMapSettings.setDestinationCrs( destinationCrs );
169  emit destinationCrsChanged();
170 }
171 
172 QList<QgsMapLayer *> QgsQuickMapSettings::layers() const
173 {
174  return mMapSettings.layers();
175 }
176 
177 void QgsQuickMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
178 {
179  mMapSettings.setLayers( layers );
180  emit layersChanged();
181 }
182 
183 void QgsQuickMapSettings::onReadProject( const QDomDocument &doc )
184 {
185  QDomNodeList nodes = doc.elementsByTagName( "mapcanvas" );
186  if ( nodes.count() )
187  {
188  QDomNode node = nodes.item( 0 );
189 
190  mMapSettings.readXml( node );
191 
192  if ( !qgsDoubleNear( mMapSettings.rotation(), 0 ) )
193  QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( mMapSettings.rotation() ) );
194 
195  mMapSettings.setRotation( 0 );
196 
197  emit extentChanged();
198  emit destinationCrsChanged();
199  emit outputSizeChanged();
200  emit outputDpiChanged();
201  emit layersChanged();
202  }
203 }
204 
205 double QgsQuickMapSettings::rotation() const
206 {
207  return mMapSettings.rotation();
208 }
209 
211 {
212  if ( !qgsDoubleNear( rotation, 0 ) )
213  QgsMessageLog::logMessage( tr( "Map Canvas rotation is not supported. Resetting from %1 to 0." ).arg( rotation ) );
214 }
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
QSize outputSize() const
Returns the size of the resulting map image.
void visibleExtentChanged()
Returns the actual extent derived from requested extent that takes takes output image size into accou...
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
sets destination coordinate reference system
A rectangle specified with double values.
Definition: qgsrectangle.h:40
void setExtent(const QgsRectangle &rect, bool magnified=true)
Set coordinates of the rectangle which should be rendered.
double y
Definition: qgspoint.h:42
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:134
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
void rotationChanged()
The rotation of the resulting map image, in degrees clockwise.
void extentChanged()
Geographical coordinates of the rectangle that should be rendered.
A class to represent a 2D point.
Definition: qgspointxy.h:43
QgsMapSettings mapSettings() const
Clone map settings.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:251
void setOutputDpi(double dpi)
Sets DPI used for conversion between real world units (e.g. mm) and pixels.
QgsRectangle extent() const
Returns geographical coordinates of the rectangle that should be rendered.
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
void setLayers(const QList< QgsMapLayer *> &layers)
Set list of layers for map rendering.
void setOutputSize(const QSize &outputSize)
Sets the size of the resulting map image.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
QgsCoordinateTransformContext transformContext() const
Returns a copy of the project&#39;s coordinate transform context, which stores various information regard...
Definition: qgsproject.cpp:543
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
The QgsMapSettings class contains configuration for rendering of the map.
void outputDpiChanged()
Output DPI used for conversion between real world units (e.g.
Q_INVOKABLE QPointF coordinateToScreen(const QgsPoint &point) const
Convert a map coordinate to screen pixel coordinates.
void mapUnitsPerPixelChanged()
Returns the distance in geographical coordinates that equals to one pixel in the map.
QgsPointXY transform(const QgsPointXY &p) const
Transform the point from map (world) coordinates to device coordinates.
double rotation() const
The rotation of the resulting map image, in degrees clockwise.
Q_INVOKABLE QgsPoint screenToCoordinate(const QPointF &point) const
Convert a screen coordinate to a map coordinate.
QgsRectangle extent() const
Returns geographical coordinates of the rectangle that should be rendered.
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:148
void setOutputSize(QSize size)
Sets 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...
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:139
void setOutputDpi(double outputDpi)
Sets DPI used for conversion between real world units (e.g. mm) and pixels.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:91
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).
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
void readProject(const QDomDocument &)
Emitted when a project is being read.
void setRotation(double rotation)
The rotation of the resulting map image, in degrees clockwise.
Reads and writes project states.
Definition: qgsproject.h:85
void setDestinationCrs(const QgsCoordinateReferenceSystem &destinationCrs)
sets destination coordinate reference system
Contains information about the context in which a coordinate transform is executed.
void destinationCrsChanged()
CRS of destination coordinate reference system.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
const QgsMapToPixel & mapToPixel() const
double mapUnitsPerPixel() const
Returns the distance in geographical coordinates that equals to one pixel in the map.
A class to represent a vector.
Definition: qgsvector.h:28
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
Q_INVOKABLE void setCenter(const QgsPoint &center)
Move current map extent to have center point defined by center.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
void setProject(QgsProject *project)
A project property should be used as a primary source of project all other components in the applicat...
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:144
void setExtent(const QgsRectangle &extent)
Set coordinates of the rectangle which should be rendered.
void outputSizeChanged()
The size of the resulting map image.
This class represents a coordinate reference system (CRS).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
void setLayers(const QList< QgsMapLayer *> &layers)
Set list of layers for map rendering.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
QgsProject * project() const
A project property should be used as a primary source of project all other components in the applicat...
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:229
void readXml(QDomNode &node)
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QSize outputSize() const
Returns the size of the resulting map image.
QgsQuickMapSettings(QObject *parent=nullptr)
Create new map settings.
QgsPointXY toMapCoordinates(int x, int y) const
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:129
void layersChanged()
Set list of layers for map rendering.
double x
Definition: qgspoint.h:41