QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgspointcloudattributebyramprendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudattributebyramprendererwidget.cpp
3 ---------------------
4 begin : November 2020
5 copyright : (C) 2020 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 ***************************************************************************/
17
19
21#include "qgsdoublevalidator.h"
23#include "qgspointcloudlayer.h"
25#include "qgsstyle.h"
26
27#include <QString>
28
29#include "moc_qgspointcloudattributebyramprendererwidget.cpp"
30
31using namespace Qt::StringLiterals;
32
34
35QgsPointCloudAttributeByRampRendererWidget::QgsPointCloudAttributeByRampRendererWidget( QgsPointCloudLayer *layer, QgsStyle *style )
36 : QgsPointCloudRendererWidget( layer, style )
37{
38 setupUi( this );
39
40 mAttributeComboBox->setAllowEmptyAttributeName( false );
41 mAttributeComboBox->setFilters( QgsPointCloudAttributeProxyModel::AllTypes );
42
43 mMinSpin->setShowClearButton( false );
44 mMaxSpin->setShowClearButton( false );
45
46 if ( layer )
47 {
48 mAttributeComboBox->setLayer( layer );
49
50 setFromRenderer( layer->renderer() );
51 }
52
53 connect( mAttributeComboBox, &QgsPointCloudAttributeComboBox::attributeChanged, this, &QgsPointCloudAttributeByRampRendererWidget::attributeChanged );
54 connect( mMinSpin, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::minMaxChanged );
55 connect( mMaxSpin, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::minMaxChanged );
56
57 connect( mScalarColorRampShaderWidget, &QgsColorRampShaderWidget::widgetChanged, this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );
58 connect( mScalarRecalculateMinMaxButton, &QPushButton::clicked, this, &QgsPointCloudAttributeByRampRendererWidget::setMinMaxFromLayer );
59}
60
61QgsPointCloudRendererWidget *QgsPointCloudAttributeByRampRendererWidget::create( QgsPointCloudLayer *layer, QgsStyle *style, QgsPointCloudRenderer * )
62{
63 return new QgsPointCloudAttributeByRampRendererWidget( layer, style );
64}
65
66QgsPointCloudRenderer *QgsPointCloudAttributeByRampRendererWidget::renderer()
67{
68 if ( !mLayer )
69 {
70 return nullptr;
71 }
72
73 auto renderer = std::make_unique<QgsPointCloudAttributeByRampRenderer>();
74 renderer->setAttribute( mAttributeComboBox->currentAttribute() );
75
76 renderer->setMinimum( mMinSpin->value() );
77 renderer->setMaximum( mMaxSpin->value() );
78
79 renderer->setColorRampShader( mScalarColorRampShaderWidget->shader() );
80
81 return renderer.release();
82}
83
84void QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged()
85{
86 if ( !mBlockChangedSignal )
87 emit widgetChanged();
88}
89
90void QgsPointCloudAttributeByRampRendererWidget::minMaxChanged()
91{
92 if ( mBlockMinMaxChanged )
93 return;
94
95 mScalarColorRampShaderWidget->setMinimumMaximumAndClassify( mMinSpin->value(), mMaxSpin->value() );
96}
97
98void QgsPointCloudAttributeByRampRendererWidget::attributeChanged()
99{
100 if ( mLayer && mLayer->dataProvider() )
101 {
102 const QgsPointCloudStatistics stats = mLayer->statistics();
103 const double min = stats.minimum( mAttributeComboBox->currentAttribute() );
104 const double max = stats.maximum( mAttributeComboBox->currentAttribute() );
105 if ( !std::isnan( min ) && !std::isnan( max ) )
106 {
107 mProviderMin = min;
108 mProviderMax = max;
109 }
110 else
111 {
112 mProviderMin = std::numeric_limits<double>::quiet_NaN();
113 mProviderMax = std::numeric_limits<double>::quiet_NaN();
114 }
115
116 if ( mAttributeComboBox->currentAttribute().compare( 'z'_L1, Qt::CaseInsensitive ) == 0 )
117 {
118 const double zScale = static_cast<const QgsPointCloudLayerElevationProperties *>( mLayer->elevationProperties() )->zScale();
119 const double zOffset = static_cast<const QgsPointCloudLayerElevationProperties *>( mLayer->elevationProperties() )->zOffset();
120 mProviderMin = mProviderMin * zScale + zOffset;
121 mProviderMax = mProviderMax * zScale + zOffset;
122 }
123 }
124 if ( !mBlockSetMinMaxFromLayer )
125 setMinMaxFromLayer();
126
127 mScalarRecalculateMinMaxButton->setEnabled( !std::isnan( mProviderMin ) && !std::isnan( mProviderMax ) );
128 emitWidgetChanged();
129}
130
131void QgsPointCloudAttributeByRampRendererWidget::setMinMaxFromLayer()
132{
133 if ( std::isnan( mProviderMin ) || std::isnan( mProviderMax ) )
134 return;
135
136 mBlockMinMaxChanged = true;
137 mMinSpin->setValue( mProviderMin );
138 mMaxSpin->setValue( mProviderMax );
139 mBlockMinMaxChanged = false;
140
141 minMaxChanged();
142}
143
144void QgsPointCloudAttributeByRampRendererWidget::setFromRenderer( const QgsPointCloudRenderer *r )
145{
146 mBlockChangedSignal = true;
147 const QgsPointCloudAttributeByRampRenderer *mbcr = dynamic_cast<const QgsPointCloudAttributeByRampRenderer *>( r );
148 if ( mbcr )
149 {
150 // we will be restoring the existing ramp classes -- we don't want to regenerate any automatically!
151 mBlockSetMinMaxFromLayer = true;
152
153 mAttributeComboBox->setAttribute( mbcr->attribute() );
154
155 mMinSpin->setValue( mbcr->minimum() );
156 mMaxSpin->setValue( mbcr->maximum() );
157
158 whileBlocking( mScalarColorRampShaderWidget )->setFromShader( mbcr->colorRampShader() );
159 whileBlocking( mScalarColorRampShaderWidget )->setMinimumMaximum( mbcr->minimum(), mbcr->maximum() );
160 }
161 else
162 {
163 if ( mAttributeComboBox->findText( u"Intensity"_s ) > -1 )
164 {
165 mAttributeComboBox->setAttribute( u"Intensity"_s );
166 }
167 else
168 {
169 mAttributeComboBox->setCurrentIndex( mAttributeComboBox->count() > 1 ? 1 : 0 );
170 }
171 }
172 attributeChanged();
173 mBlockChangedSignal = false;
174 mBlockSetMinMaxFromLayer = false;
175}
176
void widgetChanged()
Widget changed.
double zScale() const
Returns the z scale, which is a scaling factor which should be applied to z values from the layer.
double zOffset() const
Returns the z offset, which is a fixed offset amount which should be added to z values from the layer...
An RGB renderer for 2d visualisation of point clouds using embedded red, green and blue attributes.
double maximum() const
Returns the maximum value for attributes which will be used by the color ramp shader.
QgsColorRampShader colorRampShader() const
Returns the color ramp shader function used to visualize the attribute.
double minimum() const
Returns the minimum value for attributes which will be used by the color ramp shader.
QString attribute() const
Returns the attribute to use for the renderer.
void attributeChanged(const QString &name)
Emitted when the currently selected attribute changes.
Point cloud layer specific subclass of QgsMapLayerElevationProperties.
Represents a map layer supporting display of point clouds.
QgsPointCloudRenderer * renderer()
Returns the 2D renderer for the point cloud.
Base class for point cloud 2D renderer settings widgets.
Abstract base class for 2d point cloud renderers.
Used to store statistics of a point cloud dataset.
double maximum(const QString &attribute) const
Returns the maximum value for the attribute attribute If no matching statistic is available then NaN ...
double minimum(const QString &attribute) const
Returns the minimum value for the attribute attribute If no matching statistic is available then NaN ...
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:89
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6804