QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsprojectdisplaysettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectdisplaysettings.cpp
3 -----------------------------
4 begin : January 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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
17#include "moc_qgsprojectdisplaysettings.cpp"
18#include "qgis.h"
22#include "qgsapplication.h"
24#include "qgsproject.h"
25
26#include <QDomElement>
27
29 : QObject( parent )
30 , mBearingFormat( std::make_unique< QgsBearingNumericFormat >() )
31 , mGeographicCoordinateFormat( std::make_unique< QgsGeographicCoordinateNumericFormat >() )
32{
33 if ( QgsProject *project = qobject_cast< QgsProject * >( parent ) )
34 {
35 connect( project, &QgsProject::crsChanged, this, &QgsProjectDisplaySettings::updateCoordinateCrs );
36 }
37}
38
40
42{
43 // inherit local default settings
44 mBearingFormat.reset( QgsLocalDefaultSettings::bearingFormat() );
45 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
46
48 mCoordinateCustomCrs = QgsCoordinateReferenceSystem( "EPSG:4326" );
49
50 mCoordinateAxisOrder = Qgis::CoordinateOrder::Default;
51
52 mCoordinateCrs = QgsCoordinateReferenceSystem();
53 updateCoordinateCrs();
54
60}
61
63{
64 mBearingFormat.reset( format );
66}
67
69{
70 return mBearingFormat.get();
71}
72
74{
75 mGeographicCoordinateFormat.reset( format );
77}
78
80{
81 return mGeographicCoordinateFormat.get();
82}
83
85{
86 if ( mCoordinateType == type )
87 return;
88
89 mCoordinateType = type;
90 updateCoordinateCrs();
91
93}
94
96{
97 if ( mCoordinateAxisOrder == order )
98 return;
99
100 mCoordinateAxisOrder = order;
102}
103
105{
106 if ( mCoordinateCustomCrs == crs )
107 return;
108
109 mCoordinateCustomCrs = crs;
110
111 if ( mCoordinateType == Qgis::CoordinateDisplayType::CustomCrs )
112 updateCoordinateCrs();
113
115}
116
117void QgsProjectDisplaySettings::updateCoordinateCrs()
118{
119 if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
120 {
121 const QgsCoordinateReferenceSystem projectCrs = project->crs();
123 switch ( mCoordinateType )
124 {
126 crs = projectCrs;
127 break;
128
130 crs = !projectCrs.isGeographic() ? projectCrs.toGeographicCrs() : projectCrs;
131 break;
132
134 crs = mCoordinateCustomCrs;
135 break;
136 }
137
138 if ( mCoordinateCrs != crs )
139 {
140 mCoordinateCrs = crs;
142 }
143 }
144}
145
146bool QgsProjectDisplaySettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
147{
148 {
149 const QDomElement bearingElement = element.firstChildElement( QStringLiteral( "BearingFormat" ) );
150 mBearingFormat.reset( static_cast< QgsBearingNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( bearingElement, context ) ) );
152 }
153
154 QgsProject *project = qobject_cast< QgsProject * >( parent() );
155
156 {
157 const QDomElement geographicElement = element.firstChildElement( QStringLiteral( "GeographicCoordinateFormat" ) );
158 if ( !geographicElement.isNull() )
159 {
160 mGeographicCoordinateFormat.reset( static_cast< QgsGeographicCoordinateNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( geographicElement, context ) ) );
161 }
162 else if ( project )
163 {
164 // upgrade old project setting
165 bool ok = false;
166 const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString(), &ok );
167 if ( ok )
168 {
169 mGeographicCoordinateFormat = std::make_unique< QgsGeographicCoordinateNumericFormat >();
170 mGeographicCoordinateFormat->setShowDirectionalSuffix( true );
171 if ( format == QLatin1String( "DM" ) )
172 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutes );
173 else if ( format == QLatin1String( "DMS" ) )
174 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutesSeconds );
175 else
176 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DecimalDegrees );
177 }
178 else
179 {
180 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
181 }
182 }
183 else
184 {
185 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
186 }
188 }
189
190 {
191 if ( element.hasAttribute( QStringLiteral( "CoordinateType" ) ) )
192 {
194 }
195 else if ( project )
196 {
197 const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString() );
198 if ( !format.isEmpty() )
199 {
200 if ( format != QLatin1String( "MU" ) && !project->crs().isGeographic() )
201 {
203 }
204 else
205 {
207 }
208 }
209 }
210
211 QDomNodeList crsNodeList = element.elementsByTagName( QStringLiteral( "CoordinateCustomCrs" ) );
212 if ( !crsNodeList.isEmpty() )
213 {
214 QDomElement crsElem = crsNodeList.at( 0 ).toElement();
215 mCoordinateCustomCrs.readXml( crsElem );
216 }
218 }
219
220
221 if ( element.hasAttribute( QStringLiteral( "CoordinateAxisOrder" ) ) )
222 {
223 setCoordinateAxisOrder( qgsEnumKeyToValue( element.attribute( QStringLiteral( "CoordinateAxisOrder" ) ), Qgis::CoordinateOrder::Default ) );
224 }
225 else if ( project )
226 {
227 setCoordinateAxisOrder( qgsEnumKeyToValue( QgsProject::instance()->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/CoordinateOrder" ) ), Qgis::CoordinateOrder::Default ) ); // skip-keyword-check
228 }
229
230 return true;
231}
232
233QDomElement QgsProjectDisplaySettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
234{
235 QDomElement element = doc.createElement( QStringLiteral( "ProjectDisplaySettings" ) );
236
237 {
238 QDomElement bearingElement = doc.createElement( QStringLiteral( "BearingFormat" ) );
239 mBearingFormat->writeXml( bearingElement, doc, context );
240 element.appendChild( bearingElement );
241 }
242
243 {
244 QDomElement geographicElement = doc.createElement( QStringLiteral( "GeographicCoordinateFormat" ) );
245 mGeographicCoordinateFormat->writeXml( geographicElement, doc, context );
246 element.appendChild( geographicElement );
247 }
248
249 element.setAttribute( QStringLiteral( "CoordinateType" ), qgsEnumValueToKey( mCoordinateType ) );
250 if ( mCoordinateCustomCrs.isValid() )
251 {
252 QDomElement crsElem = doc.createElement( QStringLiteral( "CoordinateCustomCrs" ) );
253 mCoordinateCustomCrs.writeXml( crsElem, doc );
254 element.appendChild( crsElem );
255 }
256
257 element.setAttribute( QStringLiteral( "CoordinateAxisOrder" ), qgsEnumValueToKey( mCoordinateAxisOrder ) );
258
259 return element;
260}
CoordinateDisplayType
Formats for displaying coordinates.
Definition qgis.h:4155
@ MapGeographic
Map Geographic CRS equivalent (stays unchanged if the map CRS is geographic)
CoordinateOrder
Order of coordinates.
Definition qgis.h:2234
@ Default
Respect the default axis ordering for the CRS, as defined in the CRS's parameters.
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
A numeric formatter which returns a text representation of a direction/bearing.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
QgsCoordinateReferenceSystem toGeographicCrs() const
Returns the geographic CRS associated with this CRS object.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
A numeric formatter which returns a text representation of a geographic coordinate (latitude or longi...
@ DegreesMinutes
Degrees and decimal minutes, eg 30 degrees 45.55'.
@ DecimalDegrees
Decimal degrees, eg 30.7555 degrees.
@ DegreesMinutesSeconds
Degrees, minutes and seconds, eg 30 degrees 45'30.
static QgsGeographicCoordinateNumericFormat * geographicCoordinateFormat()
Returns the default geographic coordinate format, which controls how geographic coordinates are displ...
static QgsBearingNumericFormat * bearingFormat()
Returns the default bearing format, which controls how angular bearings are displayed.
const QgsGeographicCoordinateNumericFormat * geographicCoordinateFormat() const
Returns the project's geographic coordinate format, which controls how geographic coordinates associa...
void geographicCoordinateFormatChanged()
Emitted when the geographic coordinate format changes.
void setCoordinateAxisOrder(Qgis::CoordinateOrder order)
Sets the default coordinate axis order to use when displaying coordinates for the project.
void setCoordinateCustomCrs(const QgsCoordinateReferenceSystem &crs)
Sets the coordinate custom CRS used when the project coordinate type is set to Qgis....
void coordinateAxisOrderChanged()
Emitted when the default coordinate axis order changes.
void reset()
Resets the settings to a default state.
const QgsBearingNumericFormat * bearingFormat() const
Returns the project bearing's format, which controls how bearings associated with the project are dis...
void bearingFormatChanged()
Emitted when the bearing format changes.
void coordinateCrsChanged()
Emitted when the coordinate CRS changes.
void coordinateTypeChanged()
Emitted when the default coordinate format changes.
QgsProjectDisplaySettings(QObject *parent=nullptr)
Constructor for QgsProjectDisplaySettings with the specified parent object.
void coordinateCustomCrsChanged()
Emitted when the coordinate custom CRS changes.
void setCoordinateType(Qgis::CoordinateDisplayType type)
Sets the default coordinate type for the project.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
void setGeographicCoordinateFormat(QgsGeographicCoordinateNumericFormat *format)
Sets the project geographic coordinate format, which controls how geographic coordinates associated w...
~QgsProjectDisplaySettings() override
void setBearingFormat(QgsBearingNumericFormat *format)
Sets the project bearing format, which controls how bearings associated with the project are displaye...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
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:112
The class is used as a container of context for various read/write operations on other objects.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6168
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6149
const QgsCoordinateReferenceSystem & crs