QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgis.h"
19#include "qgsapplication.h"
24#include "qgsproject.h"
25
26#include <QDomElement>
27
28#include "moc_qgsprojectdisplaysettings.cpp"
29
31 : QObject( parent )
32 , mBearingFormat( std::make_unique< QgsBearingNumericFormat >() )
33 , mGeographicCoordinateFormat( std::make_unique< QgsGeographicCoordinateNumericFormat >() )
34{
35 if ( QgsProject *project = qobject_cast< QgsProject * >( parent ) )
36 {
37 connect( project, &QgsProject::crsChanged, this, &QgsProjectDisplaySettings::updateCoordinateCrs );
38 }
39}
40
42
44{
45 // inherit local default settings
46 mBearingFormat.reset( QgsLocalDefaultSettings::bearingFormat() );
47 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
48
50 mCoordinateCustomCrs = QgsCoordinateReferenceSystem( "EPSG:4326" );
51
52 mCoordinateAxisOrder = Qgis::CoordinateOrder::Default;
53
54 mCoordinateCrs = QgsCoordinateReferenceSystem();
55 updateCoordinateCrs();
56
62}
63
65{
66 mBearingFormat.reset( format );
68}
69
71{
72 return mBearingFormat.get();
73}
74
76{
77 mGeographicCoordinateFormat.reset( format );
79}
80
82{
83 return mGeographicCoordinateFormat.get();
84}
85
87{
88 if ( mCoordinateType == type )
89 return;
90
91 mCoordinateType = type;
92 updateCoordinateCrs();
93
95}
96
98{
99 if ( mCoordinateAxisOrder == order )
100 return;
101
102 mCoordinateAxisOrder = order;
104}
105
107{
108 if ( mCoordinateCustomCrs == crs )
109 return;
110
111 mCoordinateCustomCrs = crs;
112
113 if ( mCoordinateType == Qgis::CoordinateDisplayType::CustomCrs )
114 updateCoordinateCrs();
115
117}
118
119void QgsProjectDisplaySettings::updateCoordinateCrs()
120{
121 if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
122 {
123 const QgsCoordinateReferenceSystem projectCrs = project->crs();
125 switch ( mCoordinateType )
126 {
128 crs = projectCrs;
129 break;
130
132 crs = !projectCrs.isGeographic() ? projectCrs.toGeographicCrs() : projectCrs;
133 break;
134
136 crs = mCoordinateCustomCrs;
137 break;
138 }
139
140 if ( mCoordinateCrs != crs )
141 {
142 mCoordinateCrs = crs;
144 }
145 }
146}
147
148bool QgsProjectDisplaySettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
149{
150 {
151 const QDomElement bearingElement = element.firstChildElement( QStringLiteral( "BearingFormat" ) );
152 mBearingFormat.reset( static_cast< QgsBearingNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( bearingElement, context ) ) );
154 }
155
156 QgsProject *project = qobject_cast< QgsProject * >( parent() );
157
158 {
159 const QDomElement geographicElement = element.firstChildElement( QStringLiteral( "GeographicCoordinateFormat" ) );
160 if ( !geographicElement.isNull() )
161 {
162 mGeographicCoordinateFormat.reset( static_cast< QgsGeographicCoordinateNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( geographicElement, context ) ) );
163 }
164 else if ( project )
165 {
166 // upgrade old project setting
167 bool ok = false;
168 const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString(), &ok );
169 if ( ok )
170 {
171 mGeographicCoordinateFormat = std::make_unique< QgsGeographicCoordinateNumericFormat >();
172 mGeographicCoordinateFormat->setShowDirectionalSuffix( true );
173 if ( format == QLatin1String( "DM" ) )
174 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutes );
175 else if ( format == QLatin1String( "DMS" ) )
176 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutesSeconds );
177 else
178 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DecimalDegrees );
179 }
180 else
181 {
182 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
183 }
184 }
185 else
186 {
187 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
188 }
190 }
191
192 {
193 if ( element.hasAttribute( QStringLiteral( "CoordinateType" ) ) )
194 {
196 }
197 else if ( project )
198 {
199 const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString() );
200 if ( !format.isEmpty() )
201 {
202 if ( format != QLatin1String( "MU" ) && !project->crs().isGeographic() )
203 {
205 }
206 else
207 {
209 }
210 }
211 }
212
213 QDomNodeList crsNodeList = element.elementsByTagName( QStringLiteral( "CoordinateCustomCrs" ) );
214 if ( !crsNodeList.isEmpty() )
215 {
216 QDomElement crsElem = crsNodeList.at( 0 ).toElement();
217 mCoordinateCustomCrs.readXml( crsElem );
218 }
220 }
221
222
223 if ( element.hasAttribute( QStringLiteral( "CoordinateAxisOrder" ) ) )
224 {
225 setCoordinateAxisOrder( qgsEnumKeyToValue( element.attribute( QStringLiteral( "CoordinateAxisOrder" ) ), Qgis::CoordinateOrder::Default ) );
226 }
227 else if ( project )
228 {
229 setCoordinateAxisOrder( qgsEnumKeyToValue( QgsProject::instance()->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/CoordinateOrder" ) ), Qgis::CoordinateOrder::Default ) ); // skip-keyword-check
230 }
231
232 return true;
233}
234
235QDomElement QgsProjectDisplaySettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
236{
237 QDomElement element = doc.createElement( QStringLiteral( "ProjectDisplaySettings" ) );
238
239 {
240 QDomElement bearingElement = doc.createElement( QStringLiteral( "BearingFormat" ) );
241 mBearingFormat->writeXml( bearingElement, doc, context );
242 element.appendChild( bearingElement );
243 }
244
245 {
246 QDomElement geographicElement = doc.createElement( QStringLiteral( "GeographicCoordinateFormat" ) );
247 mGeographicCoordinateFormat->writeXml( geographicElement, doc, context );
248 element.appendChild( geographicElement );
249 }
250
251 element.setAttribute( QStringLiteral( "CoordinateType" ), qgsEnumValueToKey( mCoordinateType ) );
252 if ( mCoordinateCustomCrs.isValid() )
253 {
254 QDomElement crsElem = doc.createElement( QStringLiteral( "CoordinateCustomCrs" ) );
255 mCoordinateCustomCrs.writeXml( crsElem, doc );
256 element.appendChild( crsElem );
257 }
258
259 element.setAttribute( QStringLiteral( "CoordinateAxisOrder" ), qgsEnumValueToKey( mCoordinateAxisOrder ) );
260
261 return element;
262}
CoordinateDisplayType
Formats for displaying coordinates.
Definition qgis.h:4452
@ MapGeographic
Map Geographic CRS equivalent (stays unchanged if the map CRS is geographic).
Definition qgis.h:4454
@ CustomCrs
Custom CRS.
Definition qgis.h:4455
CoordinateOrder
Order of coordinates.
Definition qgis.h:2402
@ Default
Respect the default axis ordering for the CRS, as defined in the CRS's parameters.
Definition qgis.h:2403
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
A numeric formatter which returns a text representation of a direction/bearing.
Represents a coordinate reference system (CRS).
QgsCoordinateReferenceSystem toGeographicCrs() const
Returns the geographic CRS associated with this CRS object.
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'.
@ 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:109
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:115
A container for the context for various read/write operations on 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:6817
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6798