QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 "moc_qgspointcloudattributebyramprendererwidget.cpp"
28
30
31QgsPointCloudAttributeByRampRendererWidget::QgsPointCloudAttributeByRampRendererWidget( QgsPointCloudLayer *layer, QgsStyle *style )
32 : QgsPointCloudRendererWidget( layer, style )
33{
34 setupUi( this );
35
36 mAttributeComboBox->setAllowEmptyAttributeName( false );
37 mAttributeComboBox->setFilters( QgsPointCloudAttributeProxyModel::AllTypes );
38
39 mMinSpin->setShowClearButton( false );
40 mMaxSpin->setShowClearButton( false );
41
42 if ( layer )
43 {
44 mAttributeComboBox->setLayer( layer );
45
46 setFromRenderer( layer->renderer() );
47 }
48
49 connect( mAttributeComboBox, &QgsPointCloudAttributeComboBox::attributeChanged, this, &QgsPointCloudAttributeByRampRendererWidget::attributeChanged );
50 connect( mMinSpin, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::minMaxChanged );
51 connect( mMaxSpin, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, &QgsPointCloudAttributeByRampRendererWidget::minMaxChanged );
52
53 connect( mScalarColorRampShaderWidget, &QgsColorRampShaderWidget::widgetChanged, this, &QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged );
54 connect( mScalarRecalculateMinMaxButton, &QPushButton::clicked, this, &QgsPointCloudAttributeByRampRendererWidget::setMinMaxFromLayer );
55}
56
57QgsPointCloudRendererWidget *QgsPointCloudAttributeByRampRendererWidget::create( QgsPointCloudLayer *layer, QgsStyle *style, QgsPointCloudRenderer * )
58{
59 return new QgsPointCloudAttributeByRampRendererWidget( layer, style );
60}
61
62QgsPointCloudRenderer *QgsPointCloudAttributeByRampRendererWidget::renderer()
63{
64 if ( !mLayer )
65 {
66 return nullptr;
67 }
68
69 auto renderer = std::make_unique<QgsPointCloudAttributeByRampRenderer>();
70 renderer->setAttribute( mAttributeComboBox->currentAttribute() );
71
72 renderer->setMinimum( mMinSpin->value() );
73 renderer->setMaximum( mMaxSpin->value() );
74
75 renderer->setColorRampShader( mScalarColorRampShaderWidget->shader() );
76
77 return renderer.release();
78}
79
80void QgsPointCloudAttributeByRampRendererWidget::emitWidgetChanged()
81{
82 if ( !mBlockChangedSignal )
83 emit widgetChanged();
84}
85
86void QgsPointCloudAttributeByRampRendererWidget::minMaxChanged()
87{
88 if ( mBlockMinMaxChanged )
89 return;
90
91 mScalarColorRampShaderWidget->setMinimumMaximumAndClassify( mMinSpin->value(), mMaxSpin->value() );
92}
93
94void QgsPointCloudAttributeByRampRendererWidget::attributeChanged()
95{
96 if ( mLayer && mLayer->dataProvider() )
97 {
98 const QgsPointCloudStatistics stats = mLayer->statistics();
99 const double min = stats.minimum( mAttributeComboBox->currentAttribute() );
100 const double max = stats.maximum( mAttributeComboBox->currentAttribute() );
101 if ( !std::isnan( min ) && !std::isnan( max ) )
102 {
103 mProviderMin = min;
104 mProviderMax = max;
105 }
106 else
107 {
108 mProviderMin = std::numeric_limits<double>::quiet_NaN();
109 mProviderMax = std::numeric_limits<double>::quiet_NaN();
110 }
111
112 if ( mAttributeComboBox->currentAttribute().compare( QLatin1String( "z" ), Qt::CaseInsensitive ) == 0 )
113 {
114 const double zScale = static_cast<const QgsPointCloudLayerElevationProperties *>( mLayer->elevationProperties() )->zScale();
115 const double zOffset = static_cast<const QgsPointCloudLayerElevationProperties *>( mLayer->elevationProperties() )->zOffset();
116 mProviderMin = mProviderMin * zScale + zOffset;
117 mProviderMax = mProviderMax * zScale + zOffset;
118 }
119 }
120 if ( !mBlockSetMinMaxFromLayer )
121 setMinMaxFromLayer();
122
123 mScalarRecalculateMinMaxButton->setEnabled( !std::isnan( mProviderMin ) && !std::isnan( mProviderMax ) );
124 emitWidgetChanged();
125}
126
127void QgsPointCloudAttributeByRampRendererWidget::setMinMaxFromLayer()
128{
129 if ( std::isnan( mProviderMin ) || std::isnan( mProviderMax ) )
130 return;
131
132 mBlockMinMaxChanged = true;
133 mMinSpin->setValue( mProviderMin );
134 mMaxSpin->setValue( mProviderMax );
135 mBlockMinMaxChanged = false;
136
137 minMaxChanged();
138}
139
140void QgsPointCloudAttributeByRampRendererWidget::setFromRenderer( const QgsPointCloudRenderer *r )
141{
142 mBlockChangedSignal = true;
143 const QgsPointCloudAttributeByRampRenderer *mbcr = dynamic_cast<const QgsPointCloudAttributeByRampRenderer *>( r );
144 if ( mbcr )
145 {
146 // we will be restoring the existing ramp classes -- we don't want to regenerate any automatically!
147 mBlockSetMinMaxFromLayer = true;
148
149 mAttributeComboBox->setAttribute( mbcr->attribute() );
150
151 mMinSpin->setValue( mbcr->minimum() );
152 mMaxSpin->setValue( mbcr->maximum() );
153
154 whileBlocking( mScalarColorRampShaderWidget )->setFromShader( mbcr->colorRampShader() );
155 whileBlocking( mScalarColorRampShaderWidget )->setMinimumMaximum( mbcr->minimum(), mbcr->maximum() );
156 }
157 else
158 {
159 if ( mAttributeComboBox->findText( QStringLiteral( "Intensity" ) ) > -1 )
160 {
161 mAttributeComboBox->setAttribute( QStringLiteral( "Intensity" ) );
162 }
163 else
164 {
165 mAttributeComboBox->setCurrentIndex( mAttributeComboBox->count() > 1 ? 1 : 0 );
166 }
167 }
168 attributeChanged();
169 mBlockChangedSignal = false;
170 mBlockSetMinMaxFromLayer = false;
171}
172
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:88
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511