QGIS API Documentation  2.15.0-Master (972fc9f)
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 
20 #include "qgsrasterlayer.h"
21 
23  : QgsRasterRendererWidget( layer, extent )
24  , mMinMaxWidget( nullptr )
25 {
26  setupUi( this );
27  createValidators();
28 
29  if ( mRasterLayer )
30  {
32  if ( !provider )
33  {
34  return;
35  }
36 
37  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
38  mMinMaxWidget->setExtent( extent );
39  layout()->addWidget( mMinMaxWidget );
40  connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
41  this, SLOT( loadMinMax( int, double, double, int ) ) );
42 
43  connect( mRedBandComboBox, SIGNAL( currentIndexChanged( int ) ),
44  this, SLOT( onBandChanged( int ) ) );
45  connect( mGreenBandComboBox, SIGNAL( currentIndexChanged( int ) ),
46  this, SLOT( onBandChanged( int ) ) );
47  connect( mBlueBandComboBox, SIGNAL( currentIndexChanged( int ) ),
48  this, SLOT( onBandChanged( int ) ) );
49 
50  //fill available bands into combo boxes
51  mRedBandComboBox->addItem( tr( "Not set" ), -1 );
52  mGreenBandComboBox->addItem( tr( "Not set" ), -1 );
53  mBlueBandComboBox->addItem( tr( "Not set" ), -1 );
54 
55  //contrast enhancement algorithms
56  mContrastEnhancementAlgorithmComboBox->addItem( tr( "No enhancement" ), 0 );
57  mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch to MinMax" ), 1 );
58  mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch and clip to MinMax" ), 2 );
59  mContrastEnhancementAlgorithmComboBox->addItem( tr( "Clip to MinMax" ), 3 );
60 
61  int nBands = provider->bandCount();
62  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
63  {
64  QString bandName = displayBandName( i );
65  mRedBandComboBox->addItem( bandName, i );
66  mGreenBandComboBox->addItem( bandName, i );
67  mBlueBandComboBox->addItem( bandName, i );
68  }
69 
71  onBandChanged( 0 ); // reset mMinMaxWidget bands
72 
73  connect( mRedMinLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
74  connect( mRedMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
75  connect( mGreenMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
76  connect( mGreenMinLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
77  connect( mBlueMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
78  connect( mBlueMinLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
79 
80  }
81 }
82 
84 {
85 }
86 
88 {
89  if ( !mRasterLayer )
90  {
91  return nullptr;
92  }
94  if ( !provider )
95  {
96  return nullptr;
97  }
98 
99  int redBand = mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt();
100  int greenBand = mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt();
101  int blueBand = mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt();
102 
103  QgsMultiBandColorRenderer* r = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
104  setCustomMinMaxValues( r, provider, redBand, greenBand, blueBand );
105  return r;
106 }
107 
108 void QgsMultiBandColorRendererWidget::createValidators()
109 {
110  mRedMinLineEdit->setValidator( new QDoubleValidator( mRedMinLineEdit ) );
111  mRedMaxLineEdit->setValidator( new QDoubleValidator( mRedMinLineEdit ) );
112  mGreenMinLineEdit->setValidator( new QDoubleValidator( mGreenMinLineEdit ) );
113  mGreenMaxLineEdit->setValidator( new QDoubleValidator( mGreenMinLineEdit ) );
114  mBlueMinLineEdit->setValidator( new QDoubleValidator( mBlueMinLineEdit ) );
115  mBlueMaxLineEdit->setValidator( new QDoubleValidator( mBlueMinLineEdit ) );
116 }
117 
118 void QgsMultiBandColorRendererWidget::setCustomMinMaxValues( QgsMultiBandColorRenderer* r,
119  const QgsRasterDataProvider* provider,
120  int redBand, int greenBand, int blueBand )
121 {
122  if ( !r || !provider )
123  {
124  return;
125  }
126 
127  if ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ==
129  {
130  r->setRedContrastEnhancement( nullptr );
131  r->setGreenContrastEnhancement( nullptr );
132  r->setBlueContrastEnhancement( nullptr );
133  return;
134  }
135 
136  QgsContrastEnhancement* redEnhancement = nullptr;
137  QgsContrastEnhancement* greenEnhancement = nullptr;
138  QgsContrastEnhancement* blueEnhancement = nullptr;
139 
140  bool redMinOk, redMaxOk;
141  double redMin = mRedMinLineEdit->text().toDouble( &redMinOk );
142  double redMax = mRedMaxLineEdit->text().toDouble( &redMaxOk );
143  if ( redMinOk && redMaxOk && redBand != -1 )
144  {
145  redEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
146  provider->dataType( redBand ) ) );
147  redEnhancement->setMinimumValue( redMin );
148  redEnhancement->setMaximumValue( redMax );
149  }
150 
151  bool greenMinOk, greenMaxOk;
152  double greenMin = mGreenMinLineEdit->text().toDouble( &greenMinOk );
153  double greenMax = mGreenMaxLineEdit->text().toDouble( &greenMaxOk );
154  if ( greenMinOk && greenMaxOk && greenBand != -1 )
155  {
156  greenEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
157  provider->dataType( greenBand ) ) );
158  greenEnhancement->setMinimumValue( greenMin );
159  greenEnhancement->setMaximumValue( greenMax );
160  }
161 
162  bool blueMinOk, blueMaxOk;
163  double blueMin = mBlueMinLineEdit->text().toDouble( &blueMinOk );
164  double blueMax = mBlueMaxLineEdit->text().toDouble( &blueMaxOk );
165  if ( blueMinOk && blueMaxOk && blueBand != -1 )
166  {
167  blueEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
168  provider->dataType( blueBand ) ) );
169  blueEnhancement->setMinimumValue( blueMin );
170  blueEnhancement->setMaximumValue( blueMax );
171  }
172 
173  if ( redEnhancement )
174  {
176  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
177  }
178  if ( greenEnhancement )
179  {
181  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
182  }
183  if ( blueEnhancement )
184  {
186  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
187  }
188  r->setRedContrastEnhancement( redEnhancement );
189  r->setGreenContrastEnhancement( greenEnhancement );
190  r->setBlueContrastEnhancement( blueEnhancement );
191 }
192 
193 void QgsMultiBandColorRendererWidget::onBandChanged( int index )
194 {
195  Q_UNUSED( index );
196 
197  QList<int> myBands;
198  myBands.append( mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt() );
199  myBands.append( mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt() );
200  myBands.append( mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt() );
201  mMinMaxWidget->setBands( myBands );
202  emit widgetChanged();
203 }
204 
205 void QgsMultiBandColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
206 {
207  Q_UNUSED( theOrigin );
208  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
209 
210  QLineEdit *myMinLineEdit, *myMaxLineEdit;
211 
212  if ( mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt() == theBandNo )
213  {
214  myMinLineEdit = mRedMinLineEdit;
215  myMaxLineEdit = mRedMaxLineEdit;
216  }
217  else if ( mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt() == theBandNo )
218  {
219  myMinLineEdit = mGreenMinLineEdit;
220  myMaxLineEdit = mGreenMaxLineEdit;
221  }
222  else if ( mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt() == theBandNo )
223  {
224  myMinLineEdit = mBlueMinLineEdit;
225  myMaxLineEdit = mBlueMaxLineEdit;
226  }
227  else // should not happen
228  {
229  QgsDebugMsg( "Band not found" );
230  return;
231  }
232 
233  if ( qIsNaN( theMin ) )
234  {
235  myMinLineEdit->clear();
236  }
237  else
238  {
239  myMinLineEdit->setText( QString::number( theMin ) );
240  }
241 
242  if ( qIsNaN( theMax ) )
243  {
244  myMaxLineEdit->clear();
245  }
246  else
247  {
248  myMaxLineEdit->setText( QString::number( theMax ) );
249  }
250 }
251 
252 void QgsMultiBandColorRendererWidget::setMinMaxValue( const QgsContrastEnhancement* ce, QLineEdit* minEdit, QLineEdit* maxEdit )
253 {
254  if ( !minEdit || !maxEdit )
255  {
256  return;
257  }
258 
259  if ( !ce )
260  {
261  minEdit->clear();
262  maxEdit->clear();
263  return;
264  }
265 
266  minEdit->setText( QString::number( ce->minimumValue() ) );
267  maxEdit->setText( QString::number( ce->maximumValue() ) );
268 
269  // QgsMultiBandColorRenderer is using individual contrast enhancements for each
270  // band, but this widget GUI has one for all
271  mContrastEnhancementAlgorithmComboBox->setCurrentIndex( mContrastEnhancementAlgorithmComboBox->findData(
272  ( int )( ce->contrastEnhancementAlgorithm() ) ) );
273 }
274 
276 {
277  const QgsMultiBandColorRenderer* mbcr = dynamic_cast<const QgsMultiBandColorRenderer*>( r );
278  if ( mbcr )
279  {
280  mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findData( mbcr->redBand() ) );
281  mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findData( mbcr->greenBand() ) );
282  mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findData( mbcr->blueBand() ) );
283 
284  setMinMaxValue( mbcr->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
285  setMinMaxValue( mbcr->greenContrastEnhancement(), mGreenMinLineEdit, mGreenMaxLineEdit );
286  setMinMaxValue( mbcr->blueContrastEnhancement(), mBlueMinLineEdit, mBlueMaxLineEdit );
287  }
288  else
289  {
290  mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findText( tr( "Red" ) ) );
291  mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findText( tr( "Green" ) ) );
292  mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findText( tr( "Blue" ) ) );
293  }
294 }
295 
297 {
298  switch ( index )
299  {
300  case 0:
301  return mRedMinLineEdit->text();
302  case 1:
303  return mGreenMinLineEdit->text();
304  case 2:
305  return mBlueMinLineEdit->text();
306  default:
307  break;
308  }
309  return QString();
310 }
311 
313 {
314  switch ( index )
315  {
316  case 0:
317  return mRedMaxLineEdit->text();
318  case 1:
319  return mGreenMaxLineEdit->text();
320  case 2:
321  return mBlueMaxLineEdit->text();
322  default:
323  break;
324  }
325  return QString();
326 }
327 
329 {
330  switch ( index )
331  {
332  case 0:
333  mRedMinLineEdit->setText( value );
334  break;
335  case 1:
336  mGreenMinLineEdit->setText( value );
337  break;
338  case 2:
339  mBlueMinLineEdit->setText( value );
340  break;
341  default:
342  break;
343  }
344 }
345 
347 {
348  switch ( index )
349  {
350  case 0:
351  mRedMaxLineEdit->setText( value );
352  break;
353  case 1:
354  mGreenMaxLineEdit->setText( value );
355  break;
356  case 2:
357  mBlueMaxLineEdit->setText( value );
358  break;
359  default:
360  break;
361  }
362 }
363 
365 {
366  switch ( index )
367  {
368  case 0:
369  return mRedBandComboBox->currentIndex();
370  case 1:
371  return mGreenBandComboBox->currentIndex();
372  case 2:
373  return mBlueBandComboBox->currentIndex();
374  default:
375  break;
376  }
377  return -1;
378 }
QLayout * layout() const
virtual int bandCount() const =0
Get number of bands.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm, bool generateTable=true)
Set the contrast enhancement algorithm.
static unsigned index
void setFromRenderer(const QgsRasterRenderer *r)
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void loadMinMax(int theBandNo, double theMin, double theMax, int theOrigin)
void setupUi(QWidget *widget)
const QgsContrastEnhancement * greenContrastEnhancement() const
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
QgsMultiBandColorRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
void clear()
void setText(const QString &)
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void setBands(const QList< int > &theBands)
const QgsContrastEnhancement * redContrastEnhancement() const
QString tr(const char *sourceText, const char *disambiguation, int n)
void setGreenContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
void setMax(const QString &value, int index=0) override
void setMin(const QString &value, int index=0) override
QString number(int n, int base)
void append(const T &value)
void setRedContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
QgsRasterRenderer * renderer() const
void setExtent(const QgsRectangle &theExtent)
void setMinimumValue(double, bool generateTable=true)
Return the minimum value for the contrast enhancement range.
double minimumValue() const
Return the minimum value for the contrast enhancement range.
void addWidget(QWidget *w)
const QgsContrastEnhancement * blueContrastEnhancement() const
virtual QGis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used...
QString displayBandName(int band) const
Returns a band name for display.
double maximumValue() const
Return the maximum value for the contrast enhancement range.
DataType
Raster data types.
Definition: qgis.h:133
void setBlueContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
Renderer for multiband images with the color components.
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
QgsRasterDataProvider * dataProvider()
Returns the data provider.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
void setMaximumValue(double, bool generateTable=true)
Set the maximum value for the contrast enhancement range.
Base class for raster data providers.