QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "qgis.h"
21#include "qgsapplication.h"
23#include "qgsproject.h"
24
25#include <QDomElement>
26
28 : QObject( parent )
29 , mBearingFormat( std::make_unique< QgsBearingNumericFormat >() )
30 , mGeographicCoordinateFormat( std::make_unique< QgsGeographicCoordinateNumericFormat >() )
31{
32 if ( QgsProject *project = qobject_cast< QgsProject * >( parent ) )
33 {
34 connect( project, &QgsProject::crsChanged, this, &QgsProjectDisplaySettings::updateCoordinateCrs );
35 }
36}
37
39
41{
42 // inherit local default settings
43 mBearingFormat.reset( QgsLocalDefaultSettings::bearingFormat() );
44 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
45
47 mCoordinateCustomCrs = QgsCoordinateReferenceSystem( "EPSG:4326" );
48
49 mCoordinateAxisOrder = Qgis::CoordinateOrder::Default;
50
51 mCoordinateCrs = QgsCoordinateReferenceSystem();
52 updateCoordinateCrs();
53
59}
60
62{
63 mBearingFormat.reset( format );
65}
66
68{
69 return mBearingFormat.get();
70}
71
73{
74 mGeographicCoordinateFormat.reset( format );
76}
77
79{
80 return mGeographicCoordinateFormat.get();
81}
82
84{
85 if ( mCoordinateType == type )
86 return;
87
88 mCoordinateType = type;
89 updateCoordinateCrs();
90
92}
93
95{
96 if ( mCoordinateAxisOrder == order )
97 return;
98
99 mCoordinateAxisOrder = order;
101}
102
104{
105 if ( mCoordinateCustomCrs == crs )
106 return;
107
108 mCoordinateCustomCrs = crs;
109
110 if ( mCoordinateType == Qgis::CoordinateDisplayType::CustomCrs )
111 updateCoordinateCrs();
112
114}
115
116void QgsProjectDisplaySettings::updateCoordinateCrs()
117{
118 if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
119 {
120 const QgsCoordinateReferenceSystem projectCrs = project->crs();
122 switch ( mCoordinateType )
123 {
125 crs = projectCrs;
126 break;
127
129 crs = !projectCrs.isGeographic() ? projectCrs.toGeographicCrs() : projectCrs;
130 break;
131
133 crs = mCoordinateCustomCrs;
134 break;
135 }
136
137 if ( mCoordinateCrs != crs )
138 {
139 mCoordinateCrs = crs;
141 }
142 }
143}
144
145bool QgsProjectDisplaySettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
146{
147 {
148 const QDomElement bearingElement = element.firstChildElement( QStringLiteral( "BearingFormat" ) );
149 mBearingFormat.reset( static_cast< QgsBearingNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( bearingElement, context ) ) );
151 }
152
153 QgsProject *project = qobject_cast< QgsProject * >( parent() );
154
155 {
156 const QDomElement geographicElement = element.firstChildElement( QStringLiteral( "GeographicCoordinateFormat" ) );
157 if ( !geographicElement.isNull() )
158 {
159 mGeographicCoordinateFormat.reset( static_cast< QgsGeographicCoordinateNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( geographicElement, context ) ) );
160 }
161 else if ( project )
162 {
163 // upgrade old project setting
164 bool ok = false;
165 const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString(), &ok );
166 if ( ok )
167 {
168 mGeographicCoordinateFormat = std::make_unique< QgsGeographicCoordinateNumericFormat >();
169 mGeographicCoordinateFormat->setShowDirectionalSuffix( true );
170 if ( format == QLatin1String( "DM" ) )
171 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutes );
172 else if ( format == QLatin1String( "DMS" ) )
173 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutesSeconds );
174 else
175 mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DecimalDegrees );
176 }
177 else
178 {
179 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
180 }
181 }
182 else
183 {
184 mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
185 }
187 }
188
189 {
190 if ( element.hasAttribute( QStringLiteral( "CoordinateType" ) ) )
191 {
193 }
194 else if ( project )
195 {
196 const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString() );
197 if ( !format.isEmpty() )
198 {
199 if ( format != QLatin1String( "MU" ) && !project->crs().isGeographic() )
200 {
202 }
203 else
204 {
206 }
207 }
208 }
209
210 QDomNodeList crsNodeList = element.elementsByTagName( QStringLiteral( "CoordinateCustomCrs" ) );
211 if ( !crsNodeList.isEmpty() )
212 {
213 QDomElement crsElem = crsNodeList.at( 0 ).toElement();
214 mCoordinateCustomCrs.readXml( crsElem );
215 }
217 }
218
219
220 if ( element.hasAttribute( QStringLiteral( "CoordinateAxisOrder" ) ) )
221 {
222 setCoordinateAxisOrder( qgsEnumKeyToValue( element.attribute( QStringLiteral( "CoordinateAxisOrder" ) ), Qgis::CoordinateOrder::Default ) );
223 }
224 else if ( project )
225 {
226 setCoordinateAxisOrder( qgsEnumKeyToValue( QgsProject::instance()->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/CoordinateOrder" ) ), Qgis::CoordinateOrder::Default ) );
227 }
228
229 return true;
230}
231
232QDomElement QgsProjectDisplaySettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
233{
234 QDomElement element = doc.createElement( QStringLiteral( "ProjectDisplaySettings" ) );
235
236 {
237 QDomElement bearingElement = doc.createElement( QStringLiteral( "BearingFormat" ) );
238 mBearingFormat->writeXml( bearingElement, doc, context );
239 element.appendChild( bearingElement );
240 }
241
242 {
243 QDomElement geographicElement = doc.createElement( QStringLiteral( "GeographicCoordinateFormat" ) );
244 mGeographicCoordinateFormat->writeXml( geographicElement, doc, context );
245 element.appendChild( geographicElement );
246 }
247
248 element.setAttribute( QStringLiteral( "CoordinateType" ), qgsEnumValueToKey( mCoordinateType ) );
249 if ( mCoordinateCustomCrs.isValid() )
250 {
251 QDomElement crsElem = doc.createElement( QStringLiteral( "CoordinateCustomCrs" ) );
252 mCoordinateCustomCrs.writeXml( crsElem, doc );
253 element.appendChild( crsElem );
254 }
255
256 element.setAttribute( QStringLiteral( "CoordinateAxisOrder" ), qgsEnumValueToKey( mCoordinateAxisOrder ) );
257
258 return element;
259}
CoordinateDisplayType
Relationship capabilities.
Definition qgis.h:3080
@ MapGeographic
Map Geographic CRS equivalent (stays unchanged if the map CRS is geographic)
CoordinateOrder
Order of coordinates.
Definition qgis.h:1771
@ 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:4555
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:4536
const QgsCoordinateReferenceSystem & crs