QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
qgssinglebandpseudocolorrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssinglebandpseudocolorrendererwidget.cpp
3 ------------------------------------------
4 begin : February 2012
5 copyright : (C) 2012 by Marco Hugentobler
6 email : marco at sourcepole dot ch
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
20#include "qgsdoublevalidator.h"
21#include "qgsguiutils.h"
22#include "qgsmapcanvas.h"
24#include "qgsrasterlayer.h"
26#include "qgsrastershader.h"
27#include "qgssettings.h"
29
30#include <QCursor>
31#include <QFileDialog>
32#include <QInputDialog>
33#include <QMenu>
34#include <QMessageBox>
35#include <QPushButton>
36#include <QTextStream>
37#include <QTreeView>
38
39#include "moc_qgssinglebandpseudocolorrendererwidget.cpp"
40
42 : QgsRasterRendererWidget( layer, extent )
43{
44 const QgsSettings settings;
45
46 setupUi( this );
47
48 mColorRampShaderWidget->initializeForUseWithRasterLayer();
49
50 connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textChanged );
51 connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textChanged );
52 connect( mMinLineEdit, &QLineEdit::textEdited, this, &QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textEdited );
53 connect( mMaxLineEdit, &QLineEdit::textEdited, this, &QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textEdited );
54
55 if ( !mRasterLayer )
56 {
57 return;
58 }
59
60 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
61 if ( !provider )
62 {
63 return;
64 }
65
66 // Must be before adding items to mBandComboBox (signal)
67 mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
68 mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
69
70 mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
71 mMinMaxWidget->setExtent( extent );
72 mMinMaxWidget->setMapCanvas( mCanvas );
73
74 QHBoxLayout *layout = new QHBoxLayout();
75 layout->setContentsMargins( 0, 0, 0, 0 );
76 mMinMaxContainerWidget->setLayout( layout );
77 layout->addWidget( mMinMaxWidget );
78
79 mColorRampShaderWidget->setRasterDataProvider( provider );
80 mBandComboBox->setLayer( mRasterLayer );
81
82 setFromRenderer( layer->renderer() );
83
86
87 // If there is currently no min/max, load default with user current default options
88 if ( mMinLineEdit->text().isEmpty() || mMaxLineEdit->text().isEmpty() )
89 {
90 QgsRasterMinMaxOrigin minMaxOrigin = mMinMaxWidget->minMaxOrigin();
91 if ( minMaxOrigin.limits() == Qgis::RasterRangeLimit::NotSet )
92 {
94 mMinMaxWidget->setFromMinMaxOrigin( minMaxOrigin );
95 }
96 mMinMaxWidget->doComputations();
97 }
98
99 whileBlocking( mColorRampShaderWidget )->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
100
101 connect( mBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandPseudoColorRendererWidget::bandChanged );
104}
105
107{
108 QgsRasterShader *rasterShader = new QgsRasterShader();
109
110 mColorRampShaderWidget->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
111 mColorRampShaderWidget->setExtent( mMinMaxWidget->extent() );
112
113 QgsColorRampShader *fcn = new QgsColorRampShader( mColorRampShaderWidget->shader() );
114 rasterShader->setRasterShaderFunction( fcn );
115
116 const int bandNumber = mBandComboBox->currentBand();
117 QgsSingleBandPseudoColorRenderer *renderer = new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), bandNumber, rasterShader );
118 renderer->setClassificationMin( lineEditValue( mMinLineEdit ) );
119 renderer->setClassificationMax( lineEditValue( mMaxLineEdit ) );
120 renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
121 return renderer;
122}
123
125{
126 mMinMaxWidget->doComputations();
127}
128
130
132{
134 mMinMaxWidget->setMapCanvas( canvas );
135 mColorRampShaderWidget->setExtent( mMinMaxWidget->extent() );
136}
137
139{
140 const QgsSingleBandPseudoColorRenderer *pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer *>( r );
141 if ( pr )
142 {
143 mBandComboBox->setBand( pr->inputBand() );
144 mMinMaxWidget->setBands( QList<int>() << pr->inputBand() );
145 mColorRampShaderWidget->setRasterBand( pr->inputBand() );
146
147 // need to set min/max properties here because if we use the raster shader below,
148 // we may set a new color ramp which needs to have min/max values defined.
149 setLineEditValue( mMinLineEdit, pr->classificationMin() );
150 setLineEditValue( mMaxLineEdit, pr->classificationMax() );
151 mMinMaxWidget->setFromMinMaxOrigin( pr->minMaxOrigin() );
152
153 const QgsRasterShader *rasterShader = pr->shader();
154 if ( rasterShader )
155 {
156 const QgsColorRampShader *colorRampShader = dynamic_cast<const QgsColorRampShader *>( rasterShader->rasterShaderFunction() );
157 if ( colorRampShader )
158 {
159 mColorRampShaderWidget->setFromShader( *colorRampShader );
160 }
161 }
162 }
163 else
164 {
165 mMinMaxWidget->setBands( QList<int>() << mBandComboBox->currentBand() );
166 mColorRampShaderWidget->setRasterBand( mBandComboBox->currentBand() );
167 }
168}
169
170void QgsSingleBandPseudoColorRendererWidget::bandChanged()
171{
172 QList<int> bands;
173 bands.append( mBandComboBox->currentBand() );
174 mMinMaxWidget->setBands( bands );
175 mColorRampShaderWidget->setRasterBand( mBandComboBox->currentBand() );
176 mColorRampShaderWidget->classify();
177}
178
180{
181 QgsDebugMsgLevel( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ), 2 );
182
183 const QString oldMinTextvalue = mMinLineEdit->text();
184 const QString oldMaxTextvalue = mMaxLineEdit->text();
185
186 if ( std::isnan( min ) )
187 {
188 whileBlocking( mMinLineEdit )->clear();
189 }
190 else
191 {
192 whileBlocking( mMinLineEdit )->setText( displayValueWithMaxPrecision( min ) );
193 }
194
195 if ( std::isnan( max ) )
196 {
197 whileBlocking( mMaxLineEdit )->clear();
198 }
199 else
200 {
201 whileBlocking( mMaxLineEdit )->setText( displayValueWithMaxPrecision( max ) );
202 }
203
204 // We compare old min and new min as text because QString::number keeps a fixed number of significant
205 // digits (default 6) and so loaded min/max will always differ from current one, which triggers a
206 // classification, and wipe out every user modification (see https://github.com/qgis/QGIS/issues/36172)
207 if ( mMinLineEdit->text() != oldMinTextvalue || mMaxLineEdit->text() != oldMaxTextvalue )
208 {
209 whileBlocking( mColorRampShaderWidget )->setRasterBand( bandNo );
210 whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( min, max );
211 }
212}
213
214
216{
217 whileBlocking( mMinLineEdit )->setText( displayValueWithMaxPrecision( min ) );
218 whileBlocking( mMaxLineEdit )->setText( displayValueWithMaxPrecision( max ) );
219 minMaxModified();
220}
221
222
223void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit *lineEdit, double value )
224{
225 QString s;
226 if ( !std::isnan( value ) )
227 {
228 s = displayValueWithMaxPrecision( value );
229 }
230 lineEdit->setText( s );
231}
232
233double QgsSingleBandPseudoColorRendererWidget::lineEditValue( const QLineEdit *lineEdit ) const
234{
235 if ( lineEdit->text().isEmpty() )
236 {
237 return std::numeric_limits<double>::quiet_NaN();
238 }
239
240 return QgsDoubleValidator::toDouble( lineEdit->text() );
241}
242
243void QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textEdited( const QString & )
244{
245 minMaxModified();
246 whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
247 emit widgetChanged();
248}
249
250void QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textEdited( const QString & )
251{
252 minMaxModified();
253 whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
254 emit widgetChanged();
255}
256
257void QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textChanged( const QString & )
258{
259 whileBlocking( mColorRampShaderWidget )->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
260 emit widgetChanged();
261}
262
263void QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textChanged( const QString & )
264{
265 whileBlocking( mColorRampShaderWidget )->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
266 emit widgetChanged();
267}
268
269
270void QgsSingleBandPseudoColorRendererWidget::minMaxModified()
271{
272 mMinMaxWidget->userHasSetManualMinMaxValues();
273}
274
275QString QgsSingleBandPseudoColorRendererWidget::displayValueWithMaxPrecision( const double value )
276{
277 if ( mRasterLayer->dataProvider() )
278 {
279 return QgsGuiUtils::displayValueWithMaximumDecimals( mRasterLayer->dataProvider()->dataType( mBandComboBox->currentBand() ), value );
280 }
281 else
282 {
283 // Use QLocale default
284 return QLocale().toString( value, 'g' );
285 }
286}
287
288void QgsSingleBandPseudoColorRendererWidget::setMin( const QString &value, int )
289{
290 mMinLineEdit->setText( value );
291 minMaxModified();
292 mColorRampShaderWidget->classify();
293}
294
295void QgsSingleBandPseudoColorRendererWidget::setMax( const QString &value, int )
296{
297 mMaxLineEdit->setText( value );
298 minMaxModified();
299 mColorRampShaderWidget->classify();
300}
@ MinimumMaximum
Real min-max values.
Definition qgis.h:1546
@ NotSet
User defined.
Definition qgis.h:1545
void minimumMaximumChangedFromTree(double minimum, double maximum)
Color ramp tree has changed.
void widgetChanged()
Widget changed.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
A custom validator which allows entry of doubles in a locale-tolerant way.
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
Map canvas is a class for displaying all GIS data types on a canvas.
void bandChanged(int band)
Emitted when the currently selected band changes.
Base class for raster data providers.
Represents a raster layer.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
Describes the origin of minimum and maximum values in a raster.
void setLimits(Qgis::RasterRangeLimit limits)
Sets the limits.
Qgis::RasterRangeLimit limits() const
Returns the raster limits.
A widget for configuring how the minimum and maximum value of a raster layer is determined.
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
void setBands(const QList< int > &bands)
void widgetChanged()
Emitted when something on the widget has changed.
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
QgsMapCanvas * mCanvas
Associated map canvas.
QgsRasterRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent)
Constructor for QgsRasterRendererWidget.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
Interface for all raster shaders.
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
QgsRasterShaderFunction * rasterShaderFunction()
A rectangle specified with double values.
Stores settings for use within QGIS.
Definition qgssettings.h:65
void setMax(const QString &value, int index=0) override
QgsSingleBandPseudoColorRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Constructor for QgsSingleBandPseudoColorRendererWidget.
void doComputations() override
Load programmatically with current values.
QgsRasterMinMaxWidget * minMaxWidget() override
Returns min/max widget when it exists.
void setMin(const QString &value, int index=0) override
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
QgsRasterRenderer * renderer() override
Creates a new renderer, using the properties defined in the widget.
void setFromRenderer(const QgsRasterRenderer *r)
Sets the widget state from the specified renderer.
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
void loadMinMaxFromTree(double min, double max)
called when the color ramp tree has changed
Raster renderer pipe for single band pseudocolor.
QgsRasterShader * shader()
Returns the raster shader.
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
QString displayValueWithMaximumDecimals(const Qgis::DataType dataType, const double value, bool displayTrailingZeroes)
Returns a localized string representation of the value with the appropriate number of decimals suppor...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61