QGIS API Documentation  3.25.0-Master (dec16ba68b)
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 
33 }
34 
36 
38 {
39  // inherit local default settings
40  mBearingFormat.reset( QgsLocalDefaultSettings::bearingFormat() );
41  mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
42 
43  emit bearingFormatChanged();
45 }
46 
48 {
49  mBearingFormat.reset( format );
50  emit bearingFormatChanged();
51 }
52 
54 {
55  return mBearingFormat.get();
56 }
57 
59 {
60  mGeographicCoordinateFormat.reset( format );
62 }
63 
65 {
66  return mGeographicCoordinateFormat.get();
67 }
68 
69 bool QgsProjectDisplaySettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
70 {
71  {
72  const QDomElement bearingElement = element.firstChildElement( QStringLiteral( "BearingFormat" ) );
73  mBearingFormat.reset( static_cast< QgsBearingNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( bearingElement, context ) ) );
74  emit bearingFormatChanged();
75  }
76 
77  {
78  const QDomElement geographicElement = element.firstChildElement( QStringLiteral( "GeographicCoordinateFormat" ) );
79  if ( !geographicElement.isNull() )
80  {
81  mGeographicCoordinateFormat.reset( static_cast< QgsGeographicCoordinateNumericFormat * >( QgsApplication::numericFormatRegistry()->createFromXml( geographicElement, context ) ) );
82  }
83  else if ( QgsProject *project = qobject_cast< QgsProject * >( parent() ) )
84  {
85  // upgrade old project setting
86  bool ok = false;
87  const QString format = project->readEntry( QStringLiteral( "PositionPrecision" ), QStringLiteral( "/DegreeFormat" ), QString(), &ok );
88  if ( ok )
89  {
90  mGeographicCoordinateFormat = std::make_unique< QgsGeographicCoordinateNumericFormat >();
91  mGeographicCoordinateFormat->setShowDirectionalSuffix( true );
92  if ( format == QLatin1String( "DM" ) )
93  mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutes );
94  else if ( format == QLatin1String( "DMS" ) )
95  mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutesSeconds );
96  else
97  mGeographicCoordinateFormat->setAngleFormat( QgsGeographicCoordinateNumericFormat::AngleFormat::DecimalDegrees );
98  }
99  else
100  {
101  mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
102  }
103  }
104  else
105  {
106  mGeographicCoordinateFormat.reset( QgsLocalDefaultSettings::geographicCoordinateFormat() );
107  }
109  }
110 
111  return true;
112 }
113 
114 QDomElement QgsProjectDisplaySettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
115 {
116  QDomElement element = doc.createElement( QStringLiteral( "ProjectDisplaySettings" ) );
117 
118  {
119  QDomElement bearingElement = doc.createElement( QStringLiteral( "BearingFormat" ) );
120  mBearingFormat->writeXml( bearingElement, doc, context );
121  element.appendChild( bearingElement );
122  }
123 
124  {
125  QDomElement geographicElement = doc.createElement( QStringLiteral( "GeographicCoordinateFormat" ) );
126  mGeographicCoordinateFormat->writeXml( geographicElement, doc, context );
127  element.appendChild( geographicElement );
128  }
129 
130  return element;
131 }
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
A numeric formatter which returns a text representation of a direction/bearing.
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 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.
QgsProjectDisplaySettings(QObject *parent=nullptr)
Constructor for QgsProjectDisplaySettings with the specified parent object.
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:104
The class is used as a container of context for various read/write operations on other objects.