QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsmultibandcolorrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmultibandcolorrendererwidget.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#include "moc_qgsmultibandcolorrendererwidget.cpp"
21#include "qgsrasterlayer.h"
24#include "qgsdoublevalidator.h"
25
27 : QgsRasterRendererWidget( layer, extent )
28 , mDisableMinMaxWidgetRefresh( false )
29{
30 setupUi( this );
31 connect( mRedMinLineEdit, &QLineEdit::textChanged, this, &QgsMultiBandColorRendererWidget::mRedMinLineEdit_textChanged );
32 connect( mRedMaxLineEdit, &QLineEdit::textChanged, this, &QgsMultiBandColorRendererWidget::mRedMaxLineEdit_textChanged );
33 connect( mGreenMinLineEdit, &QLineEdit::textChanged, this, &QgsMultiBandColorRendererWidget::mGreenMinLineEdit_textChanged );
34 connect( mGreenMaxLineEdit, &QLineEdit::textChanged, this, &QgsMultiBandColorRendererWidget::mGreenMaxLineEdit_textChanged );
35 connect( mBlueMinLineEdit, &QLineEdit::textChanged, this, &QgsMultiBandColorRendererWidget::mBlueMinLineEdit_textChanged );
36 connect( mBlueMaxLineEdit, &QLineEdit::textChanged, this, &QgsMultiBandColorRendererWidget::mBlueMaxLineEdit_textChanged );
37 createValidators();
38
39 if ( mRasterLayer )
40 {
42 if ( !provider )
43 {
44 return;
45 }
46
47 mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
48 mMinMaxWidget->setExtent( extent );
49 mMinMaxWidget->setMapCanvas( mCanvas );
50 QHBoxLayout *layout = new QHBoxLayout();
51 layout->setContentsMargins( 0, 0, 0, 0 );
52 mMinMaxContainerWidget->setLayout( layout );
53 layout->addWidget( mMinMaxWidget );
54
57
58 connect( mRedBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsMultiBandColorRendererWidget::onBandChanged );
59 connect( mGreenBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsMultiBandColorRendererWidget::onBandChanged );
60 connect( mBlueBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsMultiBandColorRendererWidget::onBandChanged );
61
62 mRedBandComboBox->setShowNotSetOption( true );
63 mGreenBandComboBox->setShowNotSetOption( true );
64 mBlueBandComboBox->setShowNotSetOption( true );
65 mRedBandComboBox->setLayer( mRasterLayer );
66 mGreenBandComboBox->setLayer( mRasterLayer );
67 mBlueBandComboBox->setLayer( mRasterLayer );
68
69 //contrast enhancement algorithms
70 mContrastEnhancementAlgorithmComboBox->addItem( tr( "No Enhancement" ), QgsContrastEnhancement::NoEnhancement );
71 mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
72 mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch and Clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
73 mContrastEnhancementAlgorithmComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
74
76 onBandChanged( 0 ); // reset mMinMaxWidget bands
77
78 connect( mContrastEnhancementAlgorithmComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
79 }
80}
81
83{
84 if ( !mRasterLayer )
85 {
86 return nullptr;
87 }
89 if ( !provider )
90 {
91 return nullptr;
92 }
93
94 const int redBand = mRedBandComboBox->currentBand();
95 const int greenBand = mGreenBandComboBox->currentBand();
96 const int blueBand = mBlueBandComboBox->currentBand();
97
98 QgsMultiBandColorRenderer *r = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
99 setCustomMinMaxValues( r, provider, redBand, greenBand, blueBand );
100
101 r->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
102
103 return r;
104}
105
110
112{
114 mMinMaxWidget->setMapCanvas( canvas );
115}
116
117void QgsMultiBandColorRendererWidget::createValidators()
118{
119 mRedMinLineEdit->setValidator( new QgsDoubleValidator( mRedMinLineEdit ) );
120 mRedMaxLineEdit->setValidator( new QgsDoubleValidator( mRedMinLineEdit ) );
121 mGreenMinLineEdit->setValidator( new QgsDoubleValidator( mGreenMinLineEdit ) );
122 mGreenMaxLineEdit->setValidator( new QgsDoubleValidator( mGreenMinLineEdit ) );
123 mBlueMinLineEdit->setValidator( new QgsDoubleValidator( mBlueMinLineEdit ) );
124 mBlueMaxLineEdit->setValidator( new QgsDoubleValidator( mBlueMinLineEdit ) );
125}
126
127void QgsMultiBandColorRendererWidget::setCustomMinMaxValues( QgsMultiBandColorRenderer *r, const QgsRasterDataProvider *provider, int redBand, int greenBand, int blueBand )
128{
129 if ( !r || !provider )
130 {
131 return;
132 }
133
134 QgsContrastEnhancement *redEnhancement = nullptr;
135 QgsContrastEnhancement *greenEnhancement = nullptr;
136 QgsContrastEnhancement *blueEnhancement = nullptr;
137
138 bool redMinOk, redMaxOk;
139 const double redMin = QgsDoubleValidator::toDouble( mRedMinLineEdit->text(), &redMinOk );
140 const double redMax = QgsDoubleValidator::toDouble( mRedMaxLineEdit->text(), &redMaxOk );
141 if ( redMinOk && redMaxOk && redBand != -1 )
142 {
143 redEnhancement = new QgsContrastEnhancement( ( Qgis::DataType )(
144 provider->dataType( redBand )
145 ) );
146 redEnhancement->setMinimumValue( redMin );
147 redEnhancement->setMaximumValue( redMax );
148 }
149
150 bool greenMinOk, greenMaxOk;
151 const double greenMin = QgsDoubleValidator::toDouble( mGreenMinLineEdit->text(), &greenMinOk );
152 const double greenMax = QgsDoubleValidator::toDouble( mGreenMaxLineEdit->text(), &greenMaxOk );
153 if ( greenMinOk && greenMaxOk && greenBand != -1 )
154 {
155 greenEnhancement = new QgsContrastEnhancement( ( Qgis::DataType )(
156 provider->dataType( greenBand )
157 ) );
158 greenEnhancement->setMinimumValue( greenMin );
159 greenEnhancement->setMaximumValue( greenMax );
160 }
161
162 bool blueMinOk, blueMaxOk;
163 const double blueMin = QgsDoubleValidator::toDouble( mBlueMinLineEdit->text(), &blueMinOk );
164 const double blueMax = QgsDoubleValidator::toDouble( mBlueMaxLineEdit->text(), &blueMaxOk );
165 if ( blueMinOk && blueMaxOk && blueBand != -1 )
166 {
167 blueEnhancement = new QgsContrastEnhancement( ( Qgis::DataType )(
168 provider->dataType( blueBand )
169 ) );
170 blueEnhancement->setMinimumValue( blueMin );
171 blueEnhancement->setMaximumValue( blueMax );
172 }
173
174 if ( redEnhancement )
175 {
176 redEnhancement->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementAlgorithmComboBox->currentData().toInt() ) );
177 }
178 if ( greenEnhancement )
179 {
180 greenEnhancement->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementAlgorithmComboBox->currentData().toInt() ) );
181 }
182 if ( blueEnhancement )
183 {
184 blueEnhancement->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementAlgorithmComboBox->currentData().toInt() ) );
185 }
186 r->setRedContrastEnhancement( redEnhancement );
187 r->setGreenContrastEnhancement( greenEnhancement );
188 r->setBlueContrastEnhancement( blueEnhancement );
189}
190
191void QgsMultiBandColorRendererWidget::onBandChanged( int index )
192{
193 Q_UNUSED( index )
194
195 QList<int> myBands;
196 myBands.append( mRedBandComboBox->currentBand() );
197 myBands.append( mGreenBandComboBox->currentBand() );
198 myBands.append( mBlueBandComboBox->currentBand() );
199 mMinMaxWidget->setBands( myBands );
200 emit widgetChanged();
201}
202
203void QgsMultiBandColorRendererWidget::mRedMinLineEdit_textChanged( const QString & )
204{
205 minMaxModified();
206}
207
208void QgsMultiBandColorRendererWidget::mRedMaxLineEdit_textChanged( const QString & )
209{
210 minMaxModified();
211}
212
213void QgsMultiBandColorRendererWidget::mGreenMinLineEdit_textChanged( const QString & )
214{
215 minMaxModified();
216}
217
218void QgsMultiBandColorRendererWidget::mGreenMaxLineEdit_textChanged( const QString & )
219{
220 minMaxModified();
221}
222
223void QgsMultiBandColorRendererWidget::mBlueMinLineEdit_textChanged( const QString & )
224{
225 minMaxModified();
226}
227
228void QgsMultiBandColorRendererWidget::mBlueMaxLineEdit_textChanged( const QString & )
229{
230 minMaxModified();
231}
232
233void QgsMultiBandColorRendererWidget::minMaxModified()
234{
235 if ( !mDisableMinMaxWidgetRefresh )
236 {
237 if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementAlgorithmComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
238 {
239 mContrastEnhancementAlgorithmComboBox->setCurrentIndex(
240 mContrastEnhancementAlgorithmComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum )
241 );
242 }
243 mMinMaxWidget->userHasSetManualMinMaxValues();
244 emit widgetChanged();
245 }
246}
247
248void QgsMultiBandColorRendererWidget::loadMinMax( int bandNo, double min, double max )
249{
250 QgsDebugMsgLevel( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ), 2 );
251
252 QLineEdit *myMinLineEdit, *myMaxLineEdit;
253
254 if ( mRedBandComboBox->currentBand() == bandNo )
255 {
256 myMinLineEdit = mRedMinLineEdit;
257 myMaxLineEdit = mRedMaxLineEdit;
258 }
259 else if ( mGreenBandComboBox->currentBand() == bandNo )
260 {
261 myMinLineEdit = mGreenMinLineEdit;
262 myMaxLineEdit = mGreenMaxLineEdit;
263 }
264 else if ( mBlueBandComboBox->currentBand() == bandNo )
265 {
266 myMinLineEdit = mBlueMinLineEdit;
267 myMaxLineEdit = mBlueMaxLineEdit;
268 }
269 else // should not happen
270 {
271 QgsDebugError( QStringLiteral( "Band not found" ) );
272 return;
273 }
274
275 mDisableMinMaxWidgetRefresh = true;
276 if ( std::isnan( min ) )
277 {
278 myMinLineEdit->clear();
279 }
280 else
281 {
282 myMinLineEdit->setText( QLocale().toString( min ) );
283 }
284
285 if ( std::isnan( max ) )
286 {
287 myMaxLineEdit->clear();
288 }
289 else
290 {
291 myMaxLineEdit->setText( QLocale().toString( max ) );
292 }
293 mDisableMinMaxWidgetRefresh = false;
294}
295
296void QgsMultiBandColorRendererWidget::setMinMaxValue( const QgsContrastEnhancement *ce, QLineEdit *minEdit, QLineEdit *maxEdit )
297{
298 if ( !minEdit || !maxEdit )
299 {
300 return;
301 }
302
303 if ( !ce )
304 {
305 minEdit->clear();
306 maxEdit->clear();
307 return;
308 }
309
310 minEdit->setText( QLocale().toString( ce->minimumValue() ) );
311 maxEdit->setText( QLocale().toString( ce->maximumValue() ) );
312
313 // QgsMultiBandColorRenderer is using individual contrast enhancements for each
314 // band, but this widget GUI has one for all
315 mContrastEnhancementAlgorithmComboBox->setCurrentIndex( mContrastEnhancementAlgorithmComboBox->findData(
316 ( int ) ( ce->contrastEnhancementAlgorithm() )
317 ) );
318}
319
321{
322 const QgsMultiBandColorRenderer *mbcr = dynamic_cast<const QgsMultiBandColorRenderer *>( r );
323 if ( mbcr )
324 {
325 mRedBandComboBox->setBand( mbcr->redBand() );
326 mGreenBandComboBox->setBand( mbcr->greenBand() );
327 mBlueBandComboBox->setBand( mbcr->blueBand() );
328
329 mDisableMinMaxWidgetRefresh = true;
330 setMinMaxValue( mbcr->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
331 setMinMaxValue( mbcr->greenContrastEnhancement(), mGreenMinLineEdit, mGreenMaxLineEdit );
332 setMinMaxValue( mbcr->blueContrastEnhancement(), mBlueMinLineEdit, mBlueMaxLineEdit );
333 mDisableMinMaxWidgetRefresh = false;
334
335 mMinMaxWidget->setFromMinMaxOrigin( mbcr->minMaxOrigin() );
336 }
337 else
338 {
339 if ( mRedBandComboBox->findText( tr( "Red" ) ) > -1 && mRedBandComboBox->findText( tr( "Green" ) ) > -1 && mRedBandComboBox->findText( tr( "Blue" ) ) > -1 )
340 {
341 mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findText( tr( "Red" ) ) );
342 mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findText( tr( "Green" ) ) );
343 mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findText( tr( "Blue" ) ) );
344 }
345 else
346 {
347 mRedBandComboBox->setCurrentIndex( mRedBandComboBox->count() > 1 ? 1 : 0 );
348 mGreenBandComboBox->setCurrentIndex( mRedBandComboBox->count() > 2 ? 2 : 0 );
349 mBlueBandComboBox->setCurrentIndex( mRedBandComboBox->count() > 3 ? 3 : 0 );
350 }
351 }
352}
353
355{
356 switch ( index )
357 {
358 case 0:
359 return mRedMinLineEdit->text();
360 case 1:
361 return mGreenMinLineEdit->text();
362 case 2:
363 return mBlueMinLineEdit->text();
364 default:
365 break;
366 }
367 return QString();
368}
369
371{
372 switch ( index )
373 {
374 case 0:
375 return mRedMaxLineEdit->text();
376 case 1:
377 return mGreenMaxLineEdit->text();
378 case 2:
379 return mBlueMaxLineEdit->text();
380 default:
381 break;
382 }
383 return QString();
384}
385
386void QgsMultiBandColorRendererWidget::setMin( const QString &value, int index )
387{
388 mDisableMinMaxWidgetRefresh = true;
389 switch ( index )
390 {
391 case 0:
392 mRedMinLineEdit->setText( value );
393 break;
394 case 1:
395 mGreenMinLineEdit->setText( value );
396 break;
397 case 2:
398 mBlueMinLineEdit->setText( value );
399 break;
400 default:
401 break;
402 }
403 mDisableMinMaxWidgetRefresh = false;
404}
405
406void QgsMultiBandColorRendererWidget::setMax( const QString &value, int index )
407{
408 mDisableMinMaxWidgetRefresh = true;
409 switch ( index )
410 {
411 case 0:
412 mRedMaxLineEdit->setText( value );
413 break;
414 case 1:
415 mGreenMaxLineEdit->setText( value );
416 break;
417 case 2:
418 mBlueMaxLineEdit->setText( value );
419 break;
420 default:
421 break;
422 }
423 mDisableMinMaxWidgetRefresh = false;
424}
425
427{
428 switch ( index )
429 {
430 case 0:
431 return mRedBandComboBox->currentBand();
432 case 1:
433 return mGreenBandComboBox->currentBand();
434 case 2:
435 return mBlueBandComboBox->currentBand();
436 default:
437 break;
438 }
439 return -1;
440}
441
446
448{
449 mDisableMinMaxWidgetRefresh = true;
450 mContrastEnhancementAlgorithmComboBox->setCurrentIndex( mContrastEnhancementAlgorithmComboBox->findData( static_cast<int>( algorithm ) ) );
451 mDisableMinMaxWidgetRefresh = false;
452}
DataType
Raster data types.
Definition qgis.h:351
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
@ StretchToMinimumMaximum
Linear histogram.
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
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 loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
QgsMultiBandColorRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
void doComputations() override
Load programmatically with current values.
void setContrastEnhancementAlgorithm(QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm) override
Sets the contrast enhancement algorithm to be used by the raster renderer.
QgsContrastEnhancement::ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const override
Returns the contrast enhancement algorithm to be used by the raster renderer.
QgsRasterRenderer * renderer() override
Creates a new renderer, using the properties defined in the widget.
void setMin(const QString &value, int index=0) override
void setMax(const QString &value, int index=0) override
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
void setFromRenderer(const QgsRasterRenderer *r)
Sets the widget state from the specified renderer.
Renderer for multiband images with the color components.
const QgsContrastEnhancement * greenContrastEnhancement() const
Returns the contrast enhancement to use for the green channel.
const QgsContrastEnhancement * blueContrastEnhancement() const
Returns the contrast enhancement to use for the blue channel.
void setGreenContrastEnhancement(QgsContrastEnhancement *ce)
Sets the contrast enhancement to use for the green channel.
void setBlueContrastEnhancement(QgsContrastEnhancement *ce)
Sets the contrast enhancement to use for the blue channel.
void setRedContrastEnhancement(QgsContrastEnhancement *ce)
Sets the contrast enhancement to use for the red channel.
const QgsContrastEnhancement * redContrastEnhancement() const
Returns the contrast enhancement to use for the red channel.
void bandChanged(int band)
Emitted when the currently selected band changes.
Base class for raster data providers.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
Represents a raster layer.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
void doComputations()
Load programmatically with current values.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
void setFromMinMaxOrigin(const QgsRasterMinMaxOrigin &)
Sets the "source" of min/max values.
void setBands(const QList< int > &bands)
QgsRasterMinMaxOrigin minMaxOrigin()
Returns a QgsRasterMinMaxOrigin object with the widget values.
void widgetChanged()
Emitted when something on the widget has changed.
void userHasSetManualMinMaxValues()
Uncheck cumulative cut, min/max, std-dev radio buttons.
Abstract base class for widgets which configure a QgsRasterRenderer.
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
QgsMapCanvas * mCanvas
Associated map canvas.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
A rectangle specified with double values.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38