QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( NULL )
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 }
74 
76 {
77 }
78 
80 {
81  if ( !mRasterLayer )
82  {
83  return 0;
84  }
86  if ( !provider )
87  {
88  return 0;
89  }
90 
91  int redBand = mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt();
92  int greenBand = mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt();
93  int blueBand = mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt();
94 
95  QgsMultiBandColorRenderer* r = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
96  setCustomMinMaxValues( r, provider, redBand, greenBand, blueBand );
97  return r;
98 }
99 
100 void QgsMultiBandColorRendererWidget::createValidators()
101 {
102  mRedMinLineEdit->setValidator( new QDoubleValidator( mRedMinLineEdit ) );
103  mRedMaxLineEdit->setValidator( new QDoubleValidator( mRedMinLineEdit ) );
104  mGreenMinLineEdit->setValidator( new QDoubleValidator( mGreenMinLineEdit ) );
105  mGreenMaxLineEdit->setValidator( new QDoubleValidator( mGreenMinLineEdit ) );
106  mBlueMinLineEdit->setValidator( new QDoubleValidator( mBlueMinLineEdit ) );
107  mBlueMaxLineEdit->setValidator( new QDoubleValidator( mBlueMinLineEdit ) );
108 }
109 
110 void QgsMultiBandColorRendererWidget::setCustomMinMaxValues( QgsMultiBandColorRenderer* r,
111  const QgsRasterDataProvider* provider,
112  int redBand, int greenBand, int blueBand )
113 {
114  if ( !r || !provider )
115  {
116  return;
117  }
118 
119  if ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ==
121  {
125  return;
126  }
127 
128  QgsContrastEnhancement* redEnhancement = 0;
129  QgsContrastEnhancement* greenEnhancement = 0;
130  QgsContrastEnhancement* blueEnhancement = 0;
131 
132  bool redMinOk, redMaxOk;
133  double redMin = mRedMinLineEdit->text().toDouble( &redMinOk );
134  double redMax = mRedMaxLineEdit->text().toDouble( &redMaxOk );
135  if ( redMinOk && redMaxOk && redBand != -1 )
136  {
137  redEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
138  provider->dataType( redBand ) ) );
139  redEnhancement->setMinimumValue( redMin );
140  redEnhancement->setMaximumValue( redMax );
141  }
142 
143  bool greenMinOk, greenMaxOk;
144  double greenMin = mGreenMinLineEdit->text().toDouble( &greenMinOk );
145  double greenMax = mGreenMaxLineEdit->text().toDouble( &greenMaxOk );
146  if ( greenMinOk && greenMaxOk && greenBand != -1 )
147  {
148  greenEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
149  provider->dataType( greenBand ) ) );
150  greenEnhancement->setMinimumValue( greenMin );
151  greenEnhancement->setMaximumValue( greenMax );
152  }
153 
154  bool blueMinOk, blueMaxOk;
155  double blueMin = mBlueMinLineEdit->text().toDouble( &blueMinOk );
156  double blueMax = mBlueMaxLineEdit->text().toDouble( &blueMaxOk );
157  if ( blueMinOk && blueMaxOk && blueBand != -1 )
158  {
159  blueEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
160  provider->dataType( blueBand ) ) );
161  blueEnhancement->setMinimumValue( blueMin );
162  blueEnhancement->setMaximumValue( blueMax );
163  }
164 
165  if ( redEnhancement )
166  {
168  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
169  }
170  if ( greenEnhancement )
171  {
173  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
174  }
175  if ( blueEnhancement )
176  {
178  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
179  }
180  r->setRedContrastEnhancement( redEnhancement );
181  r->setGreenContrastEnhancement( greenEnhancement );
182  r->setBlueContrastEnhancement( blueEnhancement );
183 }
184 
185 void QgsMultiBandColorRendererWidget::onBandChanged( int index )
186 {
187  Q_UNUSED( index );
188 
189  QList<int> myBands;
190  myBands.append( mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt() );
191  myBands.append( mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt() );
192  myBands.append( mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt() );
193  mMinMaxWidget->setBands( myBands );
194 }
195 
196 void QgsMultiBandColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
197 {
198  Q_UNUSED( theOrigin );
199  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
200 
201  QLineEdit *myMinLineEdit, *myMaxLineEdit;
202 
203  if ( mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt() == theBandNo )
204  {
205  myMinLineEdit = mRedMinLineEdit;
206  myMaxLineEdit = mRedMaxLineEdit;
207  }
208  else if ( mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt() == theBandNo )
209  {
210  myMinLineEdit = mGreenMinLineEdit;
211  myMaxLineEdit = mGreenMaxLineEdit;
212  }
213  else if ( mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt() == theBandNo )
214  {
215  myMinLineEdit = mBlueMinLineEdit;
216  myMaxLineEdit = mBlueMaxLineEdit;
217  }
218  else // should not happen
219  {
220  QgsDebugMsg( "Band not found" );
221  return;
222  }
223 
224  if ( qIsNaN( theMin ) )
225  {
226  myMinLineEdit->clear();
227  }
228  else
229  {
230  myMinLineEdit->setText( QString::number( theMin ) );
231  }
232 
233  if ( qIsNaN( theMax ) )
234  {
235  myMaxLineEdit->clear();
236  }
237  else
238  {
239  myMaxLineEdit->setText( QString::number( theMax ) );
240  }
241 }
242 
243 void QgsMultiBandColorRendererWidget::setMinMaxValue( const QgsContrastEnhancement* ce, QLineEdit* minEdit, QLineEdit* maxEdit )
244 {
245  if ( !minEdit || !maxEdit )
246  {
247  return;
248  }
249 
250  if ( !ce )
251  {
252  minEdit->clear();
253  maxEdit->clear();
254  return;
255  }
256 
257  minEdit->setText( QString::number( ce->minimumValue() ) );
258  maxEdit->setText( QString::number( ce->maximumValue() ) );
259 
260  // QgsMultiBandColorRenderer is using individual contrast enhancements for each
261  // band, but this widget GUI has one for all
262  mContrastEnhancementAlgorithmComboBox->setCurrentIndex( mContrastEnhancementAlgorithmComboBox->findData(
263  ( int )( ce->contrastEnhancementAlgorithm() ) ) );
264 }
265 
267 {
268  const QgsMultiBandColorRenderer* mbcr = dynamic_cast<const QgsMultiBandColorRenderer*>( r );
269  if ( mbcr )
270  {
271  mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findData( mbcr->redBand() ) );
272  mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findData( mbcr->greenBand() ) );
273  mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findData( mbcr->blueBand() ) );
274 
275  setMinMaxValue( mbcr->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
276  setMinMaxValue( mbcr->greenContrastEnhancement(), mGreenMinLineEdit, mGreenMaxLineEdit );
277  setMinMaxValue( mbcr->blueContrastEnhancement(), mBlueMinLineEdit, mBlueMaxLineEdit );
278  }
279  else
280  {
281  mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findText( tr( "Red" ) ) );
282  mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findText( tr( "Green" ) ) );
283  mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findText( tr( "Blue" ) ) );
284  }
285 }
286 
288 {
289  switch ( index )
290  {
291  case 0:
292  return mRedMinLineEdit->text();
293  break;
294  case 1:
295  return mGreenMinLineEdit->text();
296  break;
297  case 2:
298  return mBlueMinLineEdit->text();
299  break;
300  default:
301  break;
302  }
303  return QString();
304 }
305 
307 {
308  switch ( index )
309  {
310  case 0:
311  return mRedMaxLineEdit->text();
312  break;
313  case 1:
314  return mGreenMaxLineEdit->text();
315  break;
316  case 2:
317  return mBlueMaxLineEdit->text();
318  break;
319  default:
320  break;
321  }
322  return QString();
323 }
324 
325 void QgsMultiBandColorRendererWidget::setMin( QString value, int index )
326 {
327  switch ( index )
328  {
329  case 0:
330  mRedMinLineEdit->setText( value );
331  break;
332  case 1:
333  mGreenMinLineEdit->setText( value );
334  break;
335  case 2:
336  mBlueMinLineEdit->setText( value );
337  break;
338  default:
339  break;
340  }
341 }
342 
343 void QgsMultiBandColorRendererWidget::setMax( QString value, int index )
344 {
345  switch ( index )
346  {
347  case 0:
348  mRedMaxLineEdit->setText( value );
349  break;
350  case 1:
351  mGreenMaxLineEdit->setText( value );
352  break;
353  case 2:
354  mBlueMaxLineEdit->setText( value );
355  break;
356  default:
357  break;
358  }
359 }
360 
362 {
363  switch ( index )
364  {
365  case 0:
366  return mRedBandComboBox->currentIndex();
367  break;
368  case 1:
369  return mGreenBandComboBox->currentIndex();
370  break;
371  case 2:
372  return mBlueBandComboBox->currentIndex();
373  break;
374  default:
375  break;
376  }
377  return -1;
378 }