QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
qgsnumericformatregistry.h
QgsProjectDisplaySettings::setBearingFormat
void setBearingFormat(QgsBearingNumericFormat *format)
Sets the project bearing format, which controls how bearings associated with the project are displaye...
Definition: qgsprojectdisplaysettings.cpp:47
QgsProjectDisplaySettings::QgsProjectDisplaySettings
QgsProjectDisplaySettings(QObject *parent=nullptr)
Constructor for QgsProjectDisplaySettings with the specified parent object.
Definition: qgsprojectdisplaysettings.cpp:27
qgslocaldefaultsettings.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsGeographicCoordinateNumericFormat
A numeric formatter which returns a text representation of a geographic coordinate (latitude or longi...
Definition: qgscoordinatenumericformat.h:28
qgis.h
QgsGeographicCoordinateNumericFormat::AngleFormat::DecimalDegrees
@ DecimalDegrees
Decimal degrees, eg 30.7555 degrees.
qgsbearingnumericformat.h
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsProjectDisplaySettings::geographicCoordinateFormatChanged
void geographicCoordinateFormatChanged()
Emitted when the geographic coordinate format changes.
QgsProjectDisplaySettings::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
Definition: qgsprojectdisplaysettings.cpp:114
qgsapplication.h
QgsProjectDisplaySettings::geographicCoordinateFormat
const QgsGeographicCoordinateNumericFormat * geographicCoordinateFormat() const
Returns the project's geographic coordinate format, which controls how geographic coordinates associa...
Definition: qgsprojectdisplaysettings.cpp:64
QgsLocalDefaultSettings::geographicCoordinateFormat
static QgsGeographicCoordinateNumericFormat * geographicCoordinateFormat()
Returns the default geographic coordinate format, which controls how geographic coordinates are displ...
Definition: qgslocaldefaultsettings.cpp:71
QgsProjectDisplaySettings::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
Definition: qgsprojectdisplaysettings.cpp:69
QgsProjectDisplaySettings::reset
void reset()
Resets the settings to a default state.
Definition: qgsprojectdisplaysettings.cpp:37
QgsProjectDisplaySettings::bearingFormat
const QgsBearingNumericFormat * bearingFormat() const
Returns the project bearing's format, which controls how bearings associated with the project are dis...
Definition: qgsprojectdisplaysettings.cpp:53
QgsProjectDisplaySettings::bearingFormatChanged
void bearingFormatChanged()
Emitted when the bearing format changes.
qgscoordinatenumericformat.h
QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutesSeconds
@ DegreesMinutesSeconds
Degrees, minutes and seconds, eg 30 degrees 45'30.
QgsApplication::numericFormatRegistry
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
Definition: qgsapplication.cpp:2475
QgsProjectDisplaySettings::~QgsProjectDisplaySettings
~QgsProjectDisplaySettings() override
QgsGeographicCoordinateNumericFormat::AngleFormat::DegreesMinutes
@ DegreesMinutes
Degrees and decimal minutes, eg 30 degrees 45.55'.
QgsBearingNumericFormat
A numeric formatter which returns a text representation of a direction/bearing.
Definition: qgsbearingnumericformat.h:28
QgsProjectDisplaySettings::setGeographicCoordinateFormat
void setGeographicCoordinateFormat(QgsGeographicCoordinateNumericFormat *format)
Sets the project geographic coordinate format, which controls how geographic coordinates associated w...
Definition: qgsprojectdisplaysettings.cpp:58
qgsprojectdisplaysettings.h
QgsLocalDefaultSettings::bearingFormat
static QgsBearingNumericFormat * bearingFormat()
Returns the default bearing format, which controls how angular bearings are displayed.
Definition: qgslocaldefaultsettings.cpp:40
qgsproject.h