QGIS API Documentation  2.2.0-Valmiera
 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 {
24  setupUi( this );
26 
27  if ( mRasterLayer )
28  {
30  if ( !provider )
31  {
32  return;
33  }
34 
35  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
36  mMinMaxWidget->setExtent( extent );
37  layout()->addWidget( mMinMaxWidget );
38  connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
39  this, SLOT( loadMinMax( int, double, double, int ) ) );
40 
41  connect( mRedBandComboBox, SIGNAL( currentIndexChanged( int ) ),
42  this, SLOT( onBandChanged( int ) ) );
43  connect( mGreenBandComboBox, SIGNAL( currentIndexChanged( int ) ),
44  this, SLOT( onBandChanged( int ) ) );
45  connect( mBlueBandComboBox, SIGNAL( currentIndexChanged( int ) ),
46  this, SLOT( onBandChanged( int ) ) );
47 
48  //fill available bands into combo boxes
49  mRedBandComboBox->addItem( tr( "Not set" ), -1 );
50  mGreenBandComboBox->addItem( tr( "Not set" ), -1 );
51  mBlueBandComboBox->addItem( tr( "Not set" ), -1 );
52 
53  //contrast enhancement algorithms
54  mContrastEnhancementAlgorithmComboBox->addItem( tr( "No enhancement" ), 0 );
55  mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch to MinMax" ), 1 );
56  mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch and clip to MinMax" ), 2 );
57  mContrastEnhancementAlgorithmComboBox->addItem( tr( "Clip to MinMax" ), 3 );
58 
59  int nBands = provider->bandCount();
60  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
61  {
62  QString bandName = displayBandName( i );
63  mRedBandComboBox->addItem( bandName, i );
64  mGreenBandComboBox->addItem( bandName, i );
65  mBlueBandComboBox->addItem( bandName, i );
66  }
67 
69  onBandChanged( 0 ); // reset mMinMaxWidget bands
70  }
71 }
72 
74 {
75 }
76 
78 {
79  if ( !mRasterLayer )
80  {
81  return 0;
82  }
84  if ( !provider )
85  {
86  return 0;
87  }
88 
89  int redBand = mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt();
90  int greenBand = mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt();
91  int blueBand = mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt();
92 
93  QgsMultiBandColorRenderer* r = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
94  setCustomMinMaxValues( r, provider, redBand, greenBand, blueBand );
95  return r;
96 }
97 
99 {
100  mRedMinLineEdit->setValidator( new QDoubleValidator( mRedMinLineEdit ) );
101  mRedMaxLineEdit->setValidator( new QDoubleValidator( mRedMinLineEdit ) );
102  mGreenMinLineEdit->setValidator( new QDoubleValidator( mGreenMinLineEdit ) );
103  mGreenMaxLineEdit->setValidator( new QDoubleValidator( mGreenMinLineEdit ) );
104  mBlueMinLineEdit->setValidator( new QDoubleValidator( mBlueMinLineEdit ) );
105  mBlueMaxLineEdit->setValidator( new QDoubleValidator( mBlueMinLineEdit ) );
106 }
107 
109  const QgsRasterDataProvider* provider,
110  int redBand, int greenBand, int blueBand )
111 {
112  if ( !r || !provider )
113  {
114  return;
115  }
116 
117  if ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ==
119  {
123  return;
124  }
125 
126  QgsContrastEnhancement* redEnhancement = 0;
127  QgsContrastEnhancement* greenEnhancement = 0;
128  QgsContrastEnhancement* blueEnhancement = 0;
129 
130  bool redMinOk, redMaxOk;
131  double redMin = mRedMinLineEdit->text().toDouble( &redMinOk );
132  double redMax = mRedMaxLineEdit->text().toDouble( &redMaxOk );
133  if ( redMinOk && redMaxOk )
134  {
135  redEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
136  provider->dataType( redBand ) ) );
137  redEnhancement->setMinimumValue( redMin );
138  redEnhancement->setMaximumValue( redMax );
139  }
140 
141  bool greenMinOk, greenMaxOk;
142  double greenMin = mGreenMinLineEdit->text().toDouble( &greenMinOk );
143  double greenMax = mGreenMaxLineEdit->text().toDouble( &greenMaxOk );
144  if ( greenMinOk && greenMaxOk )
145  {
146  greenEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
147  provider->dataType( greenBand ) ) );
148  greenEnhancement->setMinimumValue( greenMin );
149  greenEnhancement->setMaximumValue( greenMax );
150  }
151 
152  bool blueMinOk, blueMaxOk;
153  double blueMin = mBlueMinLineEdit->text().toDouble( &blueMinOk );
154  double blueMax = mBlueMaxLineEdit->text().toDouble( &blueMaxOk );
155  if ( blueMinOk && blueMaxOk )
156  {
157  blueEnhancement = new QgsContrastEnhancement(( QGis::DataType )(
158  provider->dataType( blueBand ) ) );
159  blueEnhancement->setMinimumValue( blueMin );
160  blueEnhancement->setMaximumValue( blueMax );
161  }
162 
163  if ( redEnhancement )
164  {
166  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
167  }
168  if ( greenEnhancement )
169  {
171  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
172  }
173  if ( blueEnhancement )
174  {
176  ( mContrastEnhancementAlgorithmComboBox->itemData( mContrastEnhancementAlgorithmComboBox->currentIndex() ).toInt() ) );
177  }
178  r->setRedContrastEnhancement( redEnhancement );
179  r->setGreenContrastEnhancement( greenEnhancement );
180  r->setBlueContrastEnhancement( blueEnhancement );
181 }
182 
184 {
185  Q_UNUSED( index );
186 
187  QList<int> myBands;
188  myBands.append( mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt() );
189  myBands.append( mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt() );
190  myBands.append( mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt() );
191  mMinMaxWidget->setBands( myBands );
192 }
193 
194 void QgsMultiBandColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
195 {
196  Q_UNUSED( theOrigin );
197  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
198 
199  QLineEdit *myMinLineEdit, *myMaxLineEdit;
200 
201  if ( mRedBandComboBox->itemData( mRedBandComboBox->currentIndex() ).toInt() == theBandNo )
202  {
203  myMinLineEdit = mRedMinLineEdit;
204  myMaxLineEdit = mRedMaxLineEdit;
205  }
206  else if ( mGreenBandComboBox->itemData( mGreenBandComboBox->currentIndex() ).toInt() == theBandNo )
207  {
208  myMinLineEdit = mGreenMinLineEdit;
209  myMaxLineEdit = mGreenMaxLineEdit;
210  }
211  else if ( mBlueBandComboBox->itemData( mBlueBandComboBox->currentIndex() ).toInt() == theBandNo )
212  {
213  myMinLineEdit = mBlueMinLineEdit;
214  myMaxLineEdit = mBlueMaxLineEdit;
215  }
216  else // should not happen
217  {
218  QgsDebugMsg( "Band not found" );
219  return;
220  }
221 
222  if ( qIsNaN( theMin ) )
223  {
224  myMinLineEdit->clear();
225  }
226  else
227  {
228  myMinLineEdit->setText( QString::number( theMin ) );
229  }
230 
231  if ( qIsNaN( theMax ) )
232  {
233  myMaxLineEdit->clear();
234  }
235  else
236  {
237  myMaxLineEdit->setText( QString::number( theMax ) );
238  }
239 }
240 
241 void QgsMultiBandColorRendererWidget::setMinMaxValue( const QgsContrastEnhancement* ce, QLineEdit* minEdit, QLineEdit* maxEdit )
242 {
243  if ( !minEdit || !maxEdit )
244  {
245  return;
246  }
247 
248  if ( !ce )
249  {
250  minEdit->clear();
251  maxEdit->clear();
252  return;
253  }
254 
255  minEdit->setText( QString::number( ce->minimumValue() ) );
256  maxEdit->setText( QString::number( ce->maximumValue() ) );
257 
258  // QgsMultiBandColorRenderer is using individual contrast enhancements for each
259  // band, but this widget GUI has one for all
260  mContrastEnhancementAlgorithmComboBox->setCurrentIndex( mContrastEnhancementAlgorithmComboBox->findData(
261  ( int )( ce->contrastEnhancementAlgorithm() ) ) );
262 }
263 
265 {
266  const QgsMultiBandColorRenderer* mbcr = dynamic_cast<const QgsMultiBandColorRenderer*>( r );
267  if ( mbcr )
268  {
269  mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findData( mbcr->redBand() ) );
270  mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findData( mbcr->greenBand() ) );
271  mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findData( mbcr->blueBand() ) );
272 
273  setMinMaxValue( mbcr->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
274  setMinMaxValue( mbcr->greenContrastEnhancement(), mGreenMinLineEdit, mGreenMaxLineEdit );
275  setMinMaxValue( mbcr->blueContrastEnhancement(), mBlueMinLineEdit, mBlueMaxLineEdit );
276  }
277  else
278  {
279  mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findText( tr( "Red" ) ) );
280  mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findText( tr( "Green" ) ) );
281  mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findText( tr( "Blue" ) ) );
282  }
283 }
284 
286 {
287  switch ( index )
288  {
289  case 0:
290  return mRedMinLineEdit->text();
291  break;
292  case 1:
293  return mGreenMinLineEdit->text();
294  break;
295  case 2:
296  return mBlueMinLineEdit->text();
297  break;
298  default:
299  break;
300  }
301  return QString( );
302 }
303 
305 {
306  switch ( index )
307  {
308  case 0:
309  return mRedMaxLineEdit->text();
310  break;
311  case 1:
312  return mGreenMaxLineEdit->text();
313  break;
314  case 2:
315  return mBlueMaxLineEdit->text();
316  break;
317  default:
318  break;
319  }
320  return QString( );
321 }
322 
324 {
325  switch ( index )
326  {
327  case 0:
328  mRedMinLineEdit->setText( value );
329  break;
330  case 1:
331  mGreenMinLineEdit->setText( value );
332  break;
333  case 2:
334  mBlueMinLineEdit->setText( value );
335  break;
336  default:
337  break;
338  }
339 }
340 
342 {
343  switch ( index )
344  {
345  case 0:
346  mRedMaxLineEdit->setText( value );
347  break;
348  case 1:
349  mGreenMaxLineEdit->setText( value );
350  break;
351  case 2:
352  mBlueMaxLineEdit->setText( value );
353  break;
354  default:
355  break;
356  }
357 }
358 
360 {
361  switch ( index )
362  {
363  case 0:
364  return mRedBandComboBox->currentIndex();
365  break;
366  case 1:
367  return mGreenBandComboBox->currentIndex();
368  break;
369  case 2:
370  return mBlueBandComboBox->currentIndex();
371  break;
372  default:
373  break;
374  }
375  return -1;
376 }