QGIS API Documentation 3.99.0-Master (8e76e220402)
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#include <QString>
28
29#include "moc_qgsprojectdisplaysettings.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QObject( parent )
35 , mBearingFormat( std::make_unique< QgsBearingNumericFormat >() )
36 , mGeographicCoordinateFormat( std::make_unique< QgsGeographicCoordinateNumericFormat >() )
37{
38 if ( QgsProject *project = qobject_cast< QgsProject * >( parent ) )
39 {
40 connect( project, &QgsProject::crsChanged, this, &QgsProjectDisplaySettings::updateCoordinateCrs );
41 }
42}
43
45
47{
48 // inherit local default settings
49 mBearingFormat.reset( QgsLocalDefaultSettings::bearingFormat() );
50 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
51
53 mCoordinateCustomCrs = QgsCoordinateReferenceSystem( "EPSG:4326" );
54
55 mCoordinateAxisOrder = Qgis::CoordinateOrder::Default;
56
57 mCoordinateCrs = QgsCoordinateReferenceSystem();
58 updateCoordinateCrs();
59
65}
66
68{
69 mBearingFormat.reset( format );
71}
72
74{
75 return mBearingFormat.get();
76}
77
79{
80 mGeographicCoordinateFormat.reset( format );
82}
83
85{
86 return mGeographicCoordinateFormat.get();
87}
88
90{
91 if ( mCoordinateType == type )
92 return;
93
94 mCoordinateType = type;
95 updateCoordinateCrs();
96
98}
99
101{
102 if ( mCoordinateAxisOrder == order )
103 return;
104
105 mCoordinateAxisOrder = order;
107}
108
110{
111 if ( mCoordinateCustomCrs == crs )
112 return;
113
114 mCoordinateCustomCrs = crs;
115
116 if ( mCoordinateType == Qgis::CoordinateDisplayType::CustomCrs )
117 updateCoordinateCrs();
118
120}
121
122void QgsProjectDisplaySettings::updateCoordinateCrs()
123{
124 if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
125 {
126 const QgsCoordinateReferenceSystem projectCrs = project->crs();
128 switch ( mCoordinateType )
129 {
131 crs = projectCrs;
132 break;
133
135 crs = !projectCrs.isGeographic() ? projectCrs.toGeographicCrs() : projectCrs;
136 break;
137
139 crs = mCoordinateCustomCrs;
140 break;
141 }
142
143 if ( mCoordinateCrs != crs )
144 {
145 mCoordinateCrs = crs;
147 }
148 }
149}
150
151bool QgsProjectDisplaySettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
152{
153 {
154 const QDomElement bearingElement = element.firstChildElement( u"BearingFormat"_s );
155 mBearingFormat.reset( static_cast< QgsBearingNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( bearingElement, context ) ) );
157 }
158
159 QgsProject *project = qobject_cast< QgsProject * >( parent() );
160
161 {
162 const QDomElement geographicElement = element.firstChildElement( u"GeographicCoordinateFormat"_s );
163 if ( !geographicElement.isNull() )
164 {
165 mGeographicCoordinateFormat.reset( static_cast< QgsGeographicCoordinateNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( geographicElement, context ) ) );
166 }
167 else if ( project )
168 {
169 // upgrade old project setting
170 bool ok = false;
171 const QString format = project->readEntry( u"PositionPrecision"_s, u"/DegreeFormat"_s, QString(), &ok );
172 if ( ok )
173 {
174 mGeographicCoordinateFormat = std::make_unique< QgsGeographicCoordinateNumericFormat >();
175 mGeographicCoordinateFormat->setShowDirectionalSuffix( true );
176 if ( format == "DM"_L1 )
177 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutes );
178 else if ( format == "DMS"_L1 )
179 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutesSeconds );
180 else
181 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DecimalDegrees );
182 }
183 else
184 {
185 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
186 }
187 }
188 else
189 {
190 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
191 }
193 }
194
195 {
196 if ( element.hasAttribute( u"CoordinateType"_s ) )
197 {
199 }
200 else if ( project )
201 {
202 const QString format = project->readEntry( u"PositionPrecision"_s, u"/DegreeFormat"_s, QString() );
203 if ( !format.isEmpty() )
204 {
205 if ( format != "MU"_L1 && !project->crs().isGeographic() )
206 {
208 }
209 else
210 {
212 }
213 }
214 }
215
216 QDomNodeList crsNodeList = element.elementsByTagName( u"CoordinateCustomCrs"_s );
217 if ( !crsNodeList.isEmpty() )
218 {
219 QDomElement crsElem = crsNodeList.at( 0 ).toElement();
220 mCoordinateCustomCrs.readXml( crsElem );
221 }
223 }
224
225
226 if ( element.hasAttribute( u"CoordinateAxisOrder"_s ) )
227 {
228 setCoordinateAxisOrder( qgsEnumKeyToValue( element.attribute( u"CoordinateAxisOrder"_s ), Qgis::CoordinateOrder::Default ) );
229 }
230 else if ( project )
231 {
232 setCoordinateAxisOrder( qgsEnumKeyToValue( QgsProject::instance()->readEntry( u"PositionPrecision"_s, u"/CoordinateOrder"_s ), Qgis::CoordinateOrder::Default ) ); // skip-keyword-check
233 }
234
235 return true;
236}
237
238QDomElement QgsProjectDisplaySettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
239{
240 QDomElement element = doc.createElement( u"ProjectDisplaySettings"_s );
241
242 {
243 QDomElement bearingElement = doc.createElement( u"BearingFormat"_s );
244 mBearingFormat->writeXml( bearingElement, doc, context );
245 element.appendChild( bearingElement );
246 }
247
248 {
249 QDomElement geographicElement = doc.createElement( u"GeographicCoordinateFormat"_s );
250 mGeographicCoordinateFormat->writeXml( geographicElement, doc, context );
251 element.appendChild( geographicElement );
252 }
253
254 element.setAttribute( u"CoordinateType"_s, qgsEnumValueToKey( mCoordinateType ) );
255 if ( mCoordinateCustomCrs.isValid() )
256 {
257 QDomElement crsElem = doc.createElement( u"CoordinateCustomCrs"_s );
258 mCoordinateCustomCrs.writeXml( crsElem, doc );
259 element.appendChild( crsElem );
260 }
261
262 element.setAttribute( u"CoordinateAxisOrder"_s, qgsEnumValueToKey( mCoordinateAxisOrder ) );
263
264 return element;
265}
CoordinateDisplayType
Formats for displaying coordinates.
Definition qgis.h:4546
@ MapGeographic
Map Geographic CRS equivalent (stays unchanged if the map CRS is geographic).
Definition qgis.h:4548
@ CustomCrs
Custom CRS.
Definition qgis.h:4549
CoordinateOrder
Order of coordinates.
Definition qgis.h:2460
@ Default
Respect the default axis ordering for the CRS, as defined in the CRS's parameters.
Definition qgis.h:2461
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:113
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:119
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:7145
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7126