QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsprojectelevationproperties.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectelevationproperties.cpp
3 ---------------
4 begin : February 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18#include "moc_qgsprojectelevationproperties.cpp"
19#include "qgis.h"
20#include "qgsterrainprovider.h"
21
22#include <QDomElement>
23
25 : QObject( parent )
26 , mTerrainProvider( std::make_unique< QgsFlatTerrainProvider >() )
27{
28
29}
30
32
34{
35 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
36 mElevationRange = QgsDoubleRange();
37 emit changed();
38 emit elevationRangeChanged( mElevationRange );
39}
40
42{
43 if ( mTerrainProvider )
44 mTerrainProvider->resolveReferences( project );
45}
46
47bool QgsProjectElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
48{
49 const QDomElement providerElement = element.firstChildElement( QStringLiteral( "terrainProvider" ) );
50 if ( !providerElement.isNull() )
51 {
52 const QString type = providerElement.attribute( QStringLiteral( "type" ) );
53 if ( type.compare( QLatin1String( "flat" ) ) == 0 )
54 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
55 else if ( type.compare( QLatin1String( "raster" ) ) == 0 )
56 mTerrainProvider = std::make_unique< QgsRasterDemTerrainProvider >();
57 else if ( type.compare( QLatin1String( "mesh" ) ) == 0 )
58 mTerrainProvider = std::make_unique< QgsMeshTerrainProvider >();
59 else
60 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
61
62 mTerrainProvider->readXml( providerElement, context );
63 }
64 else
65 {
66 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
67 }
68
69 bool ok = false;
70 double rangeLower = std::numeric_limits< double >::lowest();
71 const double storedRangeLower = element.attribute( QStringLiteral( "RangeLower" ) ).toDouble( &ok );
72 if ( ok )
73 rangeLower = storedRangeLower;
74 double rangeUpper = std::numeric_limits< double >::max();
75 const double storedRangeUpper = element.attribute( QStringLiteral( "RangeUpper" ) ).toDouble( &ok );
76 if ( ok )
77 rangeUpper = storedRangeUpper;
78 mElevationRange = QgsDoubleRange( rangeLower, rangeUpper );
79
80 mElevationFilterRangeSize = element.attribute( QStringLiteral( "FilterRangeSize" ) ).toDouble( &ok );
81 if ( !ok )
82 mElevationFilterRangeSize = -1;
83
84 mInvertElevationFilter = element.attribute( QStringLiteral( "FilterInvertSlider" ), QStringLiteral( "0" ) ).toInt();
85
86 emit changed();
87 emit elevationRangeChanged( mElevationRange );
88 return true;
89}
90
91QDomElement QgsProjectElevationProperties::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
92{
93 QDomElement element = document.createElement( QStringLiteral( "ElevationProperties" ) );
94
95 if ( mTerrainProvider )
96 {
97 QDomElement providerElement = document.createElement( QStringLiteral( "terrainProvider" ) );
98 providerElement.setAttribute( QStringLiteral( "type" ), mTerrainProvider->type() );
99 providerElement.appendChild( mTerrainProvider->writeXml( document, context ) );
100 element.appendChild( providerElement );
101 }
102
103 if ( mElevationRange.lower() != std::numeric_limits< double >::lowest() )
104 element.setAttribute( QStringLiteral( "RangeLower" ), qgsDoubleToString( mElevationRange.lower() ) );
105 if ( mElevationRange.upper() != std::numeric_limits< double >::max() )
106 element.setAttribute( QStringLiteral( "RangeUpper" ), qgsDoubleToString( mElevationRange.upper() ) );
107
108 if ( mElevationFilterRangeSize >= 0 )
109 {
110 element.setAttribute( QStringLiteral( "FilterRangeSize" ), mElevationFilterRangeSize );
111 }
112 element.setAttribute( QStringLiteral( "FilterInvertSlider" ), mInvertElevationFilter ? "1" : "0" );
113
114 return element;
115}
116
121
123{
124 if ( mTerrainProvider.get() == provider )
125 return;
126
127 const bool hasChanged = ( provider && mTerrainProvider ) ? !mTerrainProvider->equals( provider ) : ( static_cast< bool >( provider ) != static_cast< bool >( mTerrainProvider.get() ) );
128
129 mTerrainProvider.reset( provider );
130 if ( hasChanged )
131 emit changed();
132}
133
135{
136 if ( mElevationFilterRangeSize == size )
137 return;
138
139 mElevationFilterRangeSize = size;
140 emit changed();
141}
142
144{
145 if ( mInvertElevationFilter == invert )
146 return;
147
148 mInvertElevationFilter = invert;
149 emit changed();
150}
151
153{
154 if ( mElevationRange == range )
155 return;
156
157 mElevationRange = range;
158 emit changed();
159 emit elevationRangeChanged( mElevationRange );
160}
Abstract base class for terrain providers.
QgsRange which stores a range of double values.
Definition qgsrange.h:231
A terrain provider where the terrain is a simple flat surface.
void setElevationFilterRangeSize(double size)
Sets the fixed size for elevation range filtering in the project, used when interactively filtering b...
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the property state from a DOM element.
void reset()
Resets the properties to a default state.
QgsProjectElevationProperties(QObject *parent=nullptr)
Constructor for QgsProjectElevationProperties with the specified parent object.
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns a DOM element representing the properties.
~QgsProjectElevationProperties() override
void setElevationRange(const QgsDoubleRange &range)
Sets the project's elevation range, which indicates the upper and lower elevation limits associated w...
QgsAbstractTerrainProvider * terrainProvider()
Returns the project's terrain provider.
void elevationRangeChanged(const QgsDoubleRange &range)
Emitted when the project's elevation is changed.
void changed()
Emitted when the elevation properties change.
void resolveReferences(const QgsProject *project)
Resolves reference to layers from stored layer ID.
void setInvertElevationFilter(bool invert)
Sets whether the elevation range filter slider should be inverted for this project.
void setTerrainProvider(QgsAbstractTerrainProvider *provider)
Sets the project's terrain provider.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
T lower() const
Returns the lower bound of the range.
Definition qgsrange.h:78
T upper() const
Returns the upper bound of the range.
Definition qgsrange.h:85
The class is used as a container of context for various read/write operations on other objects.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5834