QGIS API Documentation 3.39.0-Master (be2050b798e)
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 "qgis.h"
19#include "qgsterrainprovider.h"
20
21#include <QDomElement>
22
24 : QObject( parent )
25 , mTerrainProvider( std::make_unique< QgsFlatTerrainProvider >() )
26{
27
28}
29
31
33{
34 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
35 mElevationRange = QgsDoubleRange();
36 emit changed();
37 emit elevationRangeChanged( mElevationRange );
38}
39
41{
42 if ( mTerrainProvider )
43 mTerrainProvider->resolveReferences( project );
44}
45
46bool QgsProjectElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
47{
48 const QDomElement providerElement = element.firstChildElement( QStringLiteral( "terrainProvider" ) );
49 if ( !providerElement.isNull() )
50 {
51 const QString type = providerElement.attribute( QStringLiteral( "type" ) );
52 if ( type.compare( QLatin1String( "flat" ) ) == 0 )
53 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
54 else if ( type.compare( QLatin1String( "raster" ) ) == 0 )
55 mTerrainProvider = std::make_unique< QgsRasterDemTerrainProvider >();
56 else if ( type.compare( QLatin1String( "mesh" ) ) == 0 )
57 mTerrainProvider = std::make_unique< QgsMeshTerrainProvider >();
58 else
59 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
60
61 mTerrainProvider->readXml( providerElement, context );
62 }
63 else
64 {
65 mTerrainProvider = std::make_unique< QgsFlatTerrainProvider >();
66 }
67
68 bool ok = false;
69 double rangeLower = std::numeric_limits< double >::lowest();
70 const double storedRangeLower = element.attribute( QStringLiteral( "RangeLower" ) ).toDouble( &ok );
71 if ( ok )
72 rangeLower = storedRangeLower;
73 double rangeUpper = std::numeric_limits< double >::max();
74 const double storedRangeUpper = element.attribute( QStringLiteral( "RangeUpper" ) ).toDouble( &ok );
75 if ( ok )
76 rangeUpper = storedRangeUpper;
77 mElevationRange = QgsDoubleRange( rangeLower, rangeUpper );
78
79 mElevationFilterRangeSize = element.attribute( QStringLiteral( "FilterRangeSize" ) ).toDouble( &ok );
80 if ( !ok )
81 mElevationFilterRangeSize = -1;
82
83 mInvertElevationFilter = element.attribute( QStringLiteral( "FilterInvertSlider" ), QStringLiteral( "0" ) ).toInt();
84
85 emit changed();
86 emit elevationRangeChanged( mElevationRange );
87 return true;
88}
89
90QDomElement QgsProjectElevationProperties::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
91{
92 QDomElement element = document.createElement( QStringLiteral( "ElevationProperties" ) );
93
94 if ( mTerrainProvider )
95 {
96 QDomElement providerElement = document.createElement( QStringLiteral( "terrainProvider" ) );
97 providerElement.setAttribute( QStringLiteral( "type" ), mTerrainProvider->type() );
98 providerElement.appendChild( mTerrainProvider->writeXml( document, context ) );
99 element.appendChild( providerElement );
100 }
101
102 if ( mElevationRange.lower() != std::numeric_limits< double >::lowest() )
103 element.setAttribute( QStringLiteral( "RangeLower" ), qgsDoubleToString( mElevationRange.lower() ) );
104 if ( mElevationRange.upper() != std::numeric_limits< double >::max() )
105 element.setAttribute( QStringLiteral( "RangeUpper" ), qgsDoubleToString( mElevationRange.upper() ) );
106
107 if ( mElevationFilterRangeSize >= 0 )
108 {
109 element.setAttribute( QStringLiteral( "FilterRangeSize" ), mElevationFilterRangeSize );
110 }
111 element.setAttribute( QStringLiteral( "FilterInvertSlider" ), mInvertElevationFilter ? "1" : "0" );
112
113 return element;
114}
115
120
122{
123 if ( mTerrainProvider.get() == provider )
124 return;
125
126 const bool hasChanged = ( provider && mTerrainProvider ) ? !mTerrainProvider->equals( provider ) : ( static_cast< bool >( provider ) != static_cast< bool >( mTerrainProvider.get() ) );
127
128 mTerrainProvider.reset( provider );
129 if ( hasChanged )
130 emit changed();
131}
132
134{
135 if ( mElevationFilterRangeSize == size )
136 return;
137
138 mElevationFilterRangeSize = size;
139 emit changed();
140}
141
143{
144 if ( mInvertElevationFilter == invert )
145 return;
146
147 mInvertElevationFilter = invert;
148 emit changed();
149}
150
152{
153 if ( mElevationRange == range )
154 return;
155
156 mElevationRange = range;
157 emit changed();
158 emit elevationRangeChanged( mElevationRange );
159}
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:5569