QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmeshlayerelevationproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmeshlayerelevationproperties.cpp
3  ---------------
4  begin : February 2022
5  copyright : (C) 2022 by Nyall Dawson
6  email : nyall dot dawson 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  ***************************************************************************/
17 
19 #include "qgsmeshlayer.h"
20 #include "qgslinesymbol.h"
21 #include "qgsfillsymbol.h"
22 #include "qgssymbollayerutils.h"
23 #include "qgslinesymbollayer.h"
24 #include "qgsfillsymbollayer.h"
25 #include "qgsapplication.h"
26 #include "qgscolorschemeregistry.h"
27 
30 {
32  setDefaultProfileLineSymbol( color );
33  setDefaultProfileFillSymbol( color );
34 }
35 
37 
39 {
40  return true;
41 }
42 
43 QDomElement QgsMeshLayerElevationProperties::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context )
44 {
45  QDomElement element = document.createElement( QStringLiteral( "elevation" ) );
46  element.setAttribute( QStringLiteral( "symbology" ), qgsEnumValueToKey( mSymbology ) );
47  writeCommonProperties( element, document, context );
48 
49  QDomElement profileLineSymbolElement = document.createElement( QStringLiteral( "profileLineSymbol" ) );
50  profileLineSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileLineSymbol.get(), document, context ) );
51  element.appendChild( profileLineSymbolElement );
52 
53  QDomElement profileFillSymbolElement = document.createElement( QStringLiteral( "profileFillSymbol" ) );
54  profileFillSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mProfileFillSymbol.get(), document, context ) );
55  element.appendChild( profileFillSymbolElement );
56 
57  parentElement.appendChild( element );
58  return element;
59 }
60 
61 bool QgsMeshLayerElevationProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
62 {
63  const QDomElement elevationElement = element.firstChildElement( QStringLiteral( "elevation" ) ).toElement();
64  mSymbology = qgsEnumKeyToValue( elevationElement.attribute( QStringLiteral( "symbology" ) ), Qgis::ProfileSurfaceSymbology::Line );
65 
66  readCommonProperties( elevationElement, context );
67 
68  const QColor defaultColor = QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
69 
70  const QDomElement profileLineSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileLineSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
71  mProfileLineSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsLineSymbol >( profileLineSymbolElement, context ) );
72  if ( !mProfileLineSymbol )
73  setDefaultProfileLineSymbol( defaultColor );
74 
75  const QDomElement profileFillSymbolElement = elevationElement.firstChildElement( QStringLiteral( "profileFillSymbol" ) ).firstChildElement( QStringLiteral( "symbol" ) );
76  mProfileFillSymbol.reset( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( profileFillSymbolElement, context ) );
77  if ( !mProfileFillSymbol )
78  setDefaultProfileFillSymbol( defaultColor );
79 
80  return true;
81 }
82 
84 {
85  QStringList properties;
86  properties << tr( "Scale: %1" ).arg( mZScale );
87  properties << tr( "Offset: %1" ).arg( mZOffset );
88  return QStringLiteral( "<li>%1</li>" ).arg( properties.join( QLatin1String( "</li><li>" ) ) );
89 }
90 
92 {
93  std::unique_ptr< QgsMeshLayerElevationProperties > res = std::make_unique< QgsMeshLayerElevationProperties >( nullptr );
94  res->setProfileLineSymbol( mProfileLineSymbol->clone() );
95  res->setProfileFillSymbol( mProfileFillSymbol->clone() );
96  res->setProfileSymbology( mSymbology );
97  res->copyCommonProperties( this );
98  return res.release();
99 }
100 
102 {
103  // TODO -- test actual raster z range
104  return true;
105 }
106 
108 {
109  // TODO -- determine actual z range from raster statistics
110  return QgsDoubleRange();
111 }
112 
114 {
115  return true;
116 }
117 
119 {
120  return mProfileLineSymbol.get();
121 }
122 
124 {
125  mProfileLineSymbol.reset( symbol );
126  emit changed();
128 }
129 
131 {
132  return mProfileFillSymbol.get();
133 }
134 
136 {
137  mProfileFillSymbol.reset( symbol );
138 }
139 
141 {
142  mSymbology = symbology;
143 }
144 
145 void QgsMeshLayerElevationProperties::setDefaultProfileLineSymbol( const QColor &color )
146 {
147  std::unique_ptr< QgsSimpleLineSymbolLayer > profileLineLayer = std::make_unique< QgsSimpleLineSymbolLayer >( color, 0.6 );
148  mProfileLineSymbol = std::make_unique< QgsLineSymbol>( QgsSymbolLayerList( { profileLineLayer.release() } ) );
149 }
150 
151 void QgsMeshLayerElevationProperties::setDefaultProfileFillSymbol( const QColor &color )
152 {
153  std::unique_ptr< QgsSimpleFillSymbolLayer > profileFillLayer = std::make_unique< QgsSimpleFillSymbolLayer >( color );
154  profileFillLayer->setStrokeStyle( Qt::NoPen );
155  mProfileFillSymbol = std::make_unique< QgsFillSymbol>( QgsSymbolLayerList( { profileFillLayer.release() } ) );
156 }
QgsMeshLayerElevationProperties::QgsMeshLayerElevationProperties
QgsMeshLayerElevationProperties(QObject *parent)
Constructor for QgsMeshLayerElevationProperties, with the specified parent object.
Definition: qgsmeshlayerelevationproperties.cpp:28
QgsMeshLayerElevationProperties::showByDefaultInElevationProfilePlots
bool showByDefaultInElevationProfilePlots() const override
Returns true if the layer should be visible by default in newly created elevation profile plots.
Definition: qgsmeshlayerelevationproperties.cpp:113
qgsEnumValueToKey
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:2440
QgsColorSchemeRegistry::fetchRandomStyleColor
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
Definition: qgscolorschemeregistry.cpp:141
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsMapLayerElevationProperties
Base class for storage of map layer elevation properties.
Definition: qgsmaplayerelevationproperties.h:41
QgsMeshLayerElevationProperties::isVisibleInZRange
bool isVisibleInZRange(const QgsDoubleRange &range) const override
Returns true if the layer should be visible and rendered for the specified z range.
Definition: qgsmeshlayerelevationproperties.cpp:101
QgsMeshLayerElevationProperties
Mesh layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgsmeshlayerelevationproperties.h:37
Qgis::ProfileSurfaceSymbology
ProfileSurfaceSymbology
Surface symbology type for elevation profile plots.
Definition: qgis.h:1849
QgsMeshLayerElevationProperties::setProfileSymbology
void setProfileSymbology(Qgis::ProfileSurfaceSymbology symbology)
Sets the symbology option used to render the mesh profile in elevation profile plots.
Definition: qgsmeshlayerelevationproperties.cpp:140
qgssymbollayerutils.h
QgsMeshLayerElevationProperties::hasElevation
bool hasElevation() const override
Returns true if the layer has an elevation or z component.
Definition: qgsmeshlayerelevationproperties.cpp:38
QgsApplication::colorSchemeRegistry
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
Definition: qgsapplication.cpp:2310
QgsMeshLayerElevationProperties::~QgsMeshLayerElevationProperties
~QgsMeshLayerElevationProperties() override
qgsmeshlayerelevationproperties.h
QgsMeshLayerElevationProperties::htmlSummary
QString htmlSummary() const override
Returns a HTML formatted summary of the properties.
Definition: qgsmeshlayerelevationproperties.cpp:83
QgsMapLayerElevationProperties::readCommonProperties
void readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common class properties from a DOM element previously written by writeXml().
Definition: qgsmaplayerelevationproperties.cpp:54
Qgis::ProfileSurfaceSymbology::Line
@ Line
The elevation surface will be rendered using a line symbol.
qgsapplication.h
QgsMeshLayerElevationProperties::profileFillSymbol
QgsFillSymbol * profileFillSymbol() const
Returns the fill symbol used to render the mesh profile in elevation profile plots.
Definition: qgsmeshlayerelevationproperties.cpp:130
QgsMeshLayerElevationProperties::writeXml
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
Definition: qgsmeshlayerelevationproperties.cpp:43
QgsMeshLayerElevationProperties::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the elevation properties from a DOM element previously written by writeXml().
Definition: qgsmeshlayerelevationproperties.cpp:61
QgsLineSymbol
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:29
QgsMeshLayerElevationProperties::profileLineSymbol
QgsLineSymbol * profileLineSymbol() const
Returns the line symbol used to render the mesh profile in elevation profile plots.
Definition: qgsmeshlayerelevationproperties.cpp:118
QgsMapLayerElevationProperties::mZOffset
double mZOffset
Z offset.
Definition: qgsmaplayerelevationproperties.h:299
qgsfillsymbollayer.h
QgsMeshLayerElevationProperties::clone
QgsMeshLayerElevationProperties * clone() const override
Creates a clone of the properties.
Definition: qgsmeshlayerelevationproperties.cpp:91
qgsmeshlayer.h
QgsMeshLayerElevationProperties::calculateZRange
QgsDoubleRange calculateZRange(QgsMapLayer *layer) const override
Attempts to calculate the overall elevation or z range for the specified layer, using the settings de...
Definition: qgsmeshlayerelevationproperties.cpp:107
QgsMapLayerElevationProperties::profileRenderingPropertyChanged
void profileRenderingPropertyChanged()
Emitted when any of the elevation properties which relate solely to presentation of elevation results...
QgsDoubleRange
QgsRange which stores a range of double values.
Definition: qgsrange.h:202
qgslinesymbollayer.h
QgsMapLayerElevationProperties::mZScale
double mZScale
Z scale.
Definition: qgsmaplayerelevationproperties.h:297
QgsSymbolLayerList
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:27
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgsEnumKeyToValue
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:2459
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:29
qgscolorschemeregistry.h
QgsMapLayerElevationProperties::changed
void changed()
Emitted when any of the elevation properties have changed.
QgsMeshLayerElevationProperties::setProfileLineSymbol
void setProfileLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the mesh profile in elevation profile plots.
Definition: qgsmeshlayerelevationproperties.cpp:123
QgsMapLayerElevationProperties::writeCommonProperties
void writeCommonProperties(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context)
Writes common class properties to a DOM element, to be used later with readXml().
Definition: qgsmaplayerelevationproperties.cpp:44
qgsfillsymbol.h
QgsSymbolLayerUtils::saveSymbol
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Definition: qgssymbollayerutils.cpp:1397
QgsMeshLayerElevationProperties::setProfileFillSymbol
void setProfileFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to render the mesh profile in elevation profile plots.
Definition: qgsmeshlayerelevationproperties.cpp:135
qgslinesymbol.h