QGIS API Documentation  2.12.0-Lyon
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 
20 #include "qgsrasterlayer.h"
21 
22 // for color ramps - todo add rasterStyle and refactor raster vs. vector ramps
23 #include "qgsstylev2.h"
24 #include "qgsvectorcolorrampv2.h"
25 #include "qgscolordialog.h"
26 
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QSettings>
30 #include <QTextStream>
31 
33  : QgsRasterRendererWidget( layer, extent )
34  , mMinMaxWidget( NULL )
35  , mMinMaxOrigin( 0 )
36 {
37  QSettings settings;
38 
39  setupUi( this );
40 
41  mColormapTreeWidget->setColumnWidth( 1, 50 );
42 
43  QString defaultPalette = settings.value( "/Raster/defaultPalette", "Spectral" ).toString();
44 
45  mColorRampComboBox->populate( QgsStyleV2::defaultStyle() );
46 
47  QgsDebugMsg( "defaultPalette = " + defaultPalette );
48  mColorRampComboBox->setCurrentIndex( mColorRampComboBox->findText( defaultPalette ) );
49  connect( mButtonEditRamp, SIGNAL( clicked() ), mColorRampComboBox, SLOT( editSourceRamp() ) );
50 
51  if ( !mRasterLayer )
52  {
53  return;
54  }
55 
57  if ( !provider )
58  {
59  return;
60  }
61 
62  // Must be before adding items to mBandComboBox (signal)
63  mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
64  mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );
65 
66  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
67  mMinMaxWidget->setExtent( extent );
69  layout->setContentsMargins( 0, 0, 0, 0 );
70  mMinMaxContainerWidget->setLayout( layout );
71  layout->addWidget( mMinMaxWidget );
72  connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
73  this, SLOT( loadMinMax( int, double, double, int ) ) );
74 
75 
76  //fill available bands into combo box
77  int nBands = provider->bandCount();
78  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
79  {
80  mBandComboBox->addItem( displayBandName( i ), i );
81  }
82 
83  mColorInterpolationComboBox->addItem( tr( "Discrete" ), 0 );
84  mColorInterpolationComboBox->addItem( tr( "Linear" ), 1 );
85  mColorInterpolationComboBox->addItem( tr( "Exact" ), 2 );
86  mColorInterpolationComboBox->setCurrentIndex( 1 );
87  mClassificationModeComboBox->addItem( tr( "Continuous" ), Continuous );
88  mClassificationModeComboBox->addItem( tr( "Equal interval" ), EqualInterval );
89  //quantile would be nice as well
90 
91  mNumberOfEntriesSpinBox->setValue( 5 ); // some default
92 
93  setFromRenderer( layer->renderer() );
94 
95  // If there is currently no min/max, load default with user current default options
96  if ( mMinLineEdit->text().isEmpty() || mMaxLineEdit->text().isEmpty() )
97  {
98  mMinMaxWidget->load();
99  }
100 
101  on_mClassificationModeComboBox_currentIndexChanged( 0 );
102 
103  resetClassifyButton();
104 }
105 
107 {
108 }
109 
111 {
112  QgsRasterShader* rasterShader = new QgsRasterShader();
113  QgsColorRampShader* colorRampShader = new QgsColorRampShader();
114  colorRampShader->setClip( mClipCheckBox->isChecked() );
115 
116  //iterate through mColormapTreeWidget and set colormap info of layer
118  int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
119  QTreeWidgetItem* currentItem;
120  for ( int i = 0; i < topLevelItemCount; ++i )
121  {
122  currentItem = mColormapTreeWidget->topLevelItem( i );
123  if ( !currentItem )
124  {
125  continue;
126  }
127  QgsColorRampShader::ColorRampItem newColorRampItem;
128  newColorRampItem.value = currentItem->text( 0 ).toDouble();
129  newColorRampItem.color = currentItem->background( 1 ).color();
130  newColorRampItem.label = currentItem->text( 2 );
131  colorRampItems.append( newColorRampItem );
132  }
133  // sort the shader items
134  qSort( colorRampItems );
135  colorRampShader->setColorRampItemList( colorRampItems );
136 
137  if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
138  {
140  }
141  else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
142  {
144  }
145  else
146  {
147  colorRampShader->setColorRampType( QgsColorRampShader::EXACT );
148  }
149  rasterShader->setRasterShaderFunction( colorRampShader );
150 
151  int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
153 
154  renderer->setClassificationMin( lineEditValue( mMinLineEdit ) );
155  renderer->setClassificationMax( lineEditValue( mMaxLineEdit ) );
156  renderer->setClassificationMinMaxOrigin( mMinMaxOrigin );
157  return renderer;
158 }
159 
160 void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
161 {
162  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
163  newItem->setText( 0, "0.0" );
164  newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
165  newItem->setText( 2, tr( "Custom color map entry" ) );
166 }
167 
168 void QgsSingleBandPseudoColorRendererWidget::on_mDeleteEntryButton_clicked()
169 {
170  QTreeWidgetItem* currentItem = mColormapTreeWidget->currentItem();
171  if ( currentItem )
172  {
173  delete currentItem;
174  }
175 }
176 
177 void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
178 {
179  bool inserted = false;
180  int myCurrentIndex = 0;
181  int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
182  QTreeWidgetItem* myCurrentItem;
184  for ( int i = 0; i < myTopLevelItemCount; ++i )
185  {
186  myCurrentItem = mColormapTreeWidget->topLevelItem( i );
187  //If the item is null or does not have a pixel values set, skip
188  if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
189  {
190  continue;
191  }
192 
193  //Create a copy of the new Color ramp Item
194  QgsColorRampShader::ColorRampItem myNewColorRampItem;
195  myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble();
196  myNewColorRampItem.color = myCurrentItem->background( 1 ).color();
197  myNewColorRampItem.label = myCurrentItem->text( 2 );
198 
199  //Simple insertion sort - speed is not a huge factor here
200  inserted = false;
201  myCurrentIndex = 0;
202  while ( !inserted )
203  {
204  if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
205  {
206  myColorRampItems.push_back( myNewColorRampItem );
207  inserted = true;
208  }
209  else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
210  {
211  myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
212  inserted = true;
213  }
214  else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 )
215  {
216  myColorRampItems.push_back( myNewColorRampItem );
217  inserted = true;
218  }
219  else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value )
220  {
221  myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
222  inserted = true;
223  }
224  myCurrentIndex++;
225  }
226  }
227  populateColormapTreeWidget( myColorRampItems );
228 }
229 
230 void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
231 {
232  int bandComboIndex = mBandComboBox->currentIndex();
233  if ( bandComboIndex == -1 || !mRasterLayer )
234  {
235  return;
236  }
237 
238  //int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
239  //QgsRasterBandStats myRasterBandStats = mRasterLayer->dataProvider()->bandStatistics( bandNr );
240  int numberOfEntries = 0;
241 
242  QList<double> entryValues;
243  QList<QColor> entryColors;
244 
245  double min = lineEditValue( mMinLineEdit );
246  double max = lineEditValue( mMaxLineEdit );
247 
248  QgsVectorColorRampV2* colorRamp = mColorRampComboBox->currentColorRamp();
249 
250  if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() == Continuous )
251  {
252  if ( colorRamp )
253  {
254  numberOfEntries = colorRamp->count();
255  entryValues.reserve( colorRamp->count() );
256  for ( int i = 0; i < colorRamp->count(); ++i )
257  {
258  double value = colorRamp->value( i );
259  entryValues.push_back( min + value * ( max - min ) );
260  }
261  }
262  }
263  else // EqualInterval
264  {
265  numberOfEntries = mNumberOfEntriesSpinBox->value();
266  //double currentValue = myRasterBandStats.minimumValue;
267  double currentValue = min;
268  double intervalDiff;
269  if ( numberOfEntries > 1 )
270  {
271  //because the highest value is also an entry, there are (numberOfEntries - 1)
272  //intervals
273  //intervalDiff = ( myRasterBandStats.maximumValue - myRasterBandStats.minimumValue ) /
274  intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
275  }
276  else
277  {
278  //intervalDiff = myRasterBandStats.maximumValue - myRasterBandStats.minimumValue;
279  intervalDiff = max - min;
280  }
281 
282  entryValues.reserve( numberOfEntries );
283  for ( int i = 0; i < numberOfEntries; ++i )
284  {
285  entryValues.push_back( currentValue );
286  currentValue += intervalDiff;
287  }
288  }
289 
290 #if 0
291  //hard code color range from blue -> red for now. Allow choice of ramps in future
292  int colorDiff = 0;
293  if ( numberOfEntries != 0 )
294  {
295  colorDiff = ( int )( 255 / numberOfEntries );
296  }
297  for ( int i = 0; i < numberOfEntries; ++i )
298  {
299  QColor currentColor;
300  currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
301  entryColors.push_back( currentColor );
302  }
303 #endif
304 
305  if ( ! colorRamp )
306  {
307  //hard code color range from blue -> red (previous default)
308  int colorDiff = 0;
309  if ( numberOfEntries != 0 )
310  {
311  colorDiff = ( int )( 255 / numberOfEntries );
312  }
313 
314  entryColors.reserve( numberOfEntries );
315  for ( int i = 0; i < numberOfEntries; ++i )
316  {
317  QColor currentColor;
318  int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
319  currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
320  entryColors.push_back( currentColor );
321  }
322  }
323  else
324  {
325  entryColors.reserve( numberOfEntries );
326  for ( int i = 0; i < numberOfEntries; ++i )
327  {
328  int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
329  entryColors.push_back( colorRamp->color((( double ) idx ) / ( numberOfEntries - 1 ) ) );
330  }
331  }
332 
333  mColormapTreeWidget->clear();
334 
335  QList<double>::const_iterator value_it = entryValues.begin();
336  QList<QColor>::const_iterator color_it = entryColors.begin();
337 
338  for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
339  {
340  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
341  newItem->setText( 0, QString::number( *value_it, 'f' ) );
342  newItem->setBackground( 1, QBrush( *color_it ) );
343  newItem->setText( 2, QString::number( *value_it, 'f' ) );
344  newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
345  }
346 }
347 
348 void QgsSingleBandPseudoColorRendererWidget::on_mClassificationModeComboBox_currentIndexChanged( int index )
349 {
350  mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() == EqualInterval );
351 }
352 
353 void QgsSingleBandPseudoColorRendererWidget::on_mColorRampComboBox_currentIndexChanged( int index )
354 {
355  Q_UNUSED( index );
356  QSettings settings;
357  settings.setValue( "/Raster/defaultPalette", mColorRampComboBox->currentText() );
358 
359  QgsVectorColorRampV2* ramp = mColorRampComboBox->currentColorRamp();
360  if ( !ramp )
361  return;
362 
363  bool enableContinuous = ( ramp->count() > 0 );
364  mClassificationModeComboBox->setEnabled( enableContinuous );
365  if ( !enableContinuous )
366  {
367  mClassificationModeComboBox->setCurrentIndex( mClassificationModeComboBox->findData( EqualInterval ) );
368  }
369 }
370 
371 void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems )
372 {
373  mColormapTreeWidget->clear();
375  for ( ; it != colorRampItems.constEnd(); ++it )
376  {
377  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
378  newItem->setText( 0, QString::number( it->value, 'f' ) );
379  newItem->setBackground( 1, QBrush( it->color ) );
380  newItem->setText( 2, it->label );
381  }
382 }
383 
384 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
385 {
386  if ( !mRasterLayer || !mRasterLayer->dataProvider() )
387  {
388  return;
389  }
390 
391  int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
392 
393 
395  if ( colorRampList.size() > 0 )
396  {
397  populateColormapTreeWidget( colorRampList );
398  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
399  }
400  else
401  {
402  QMessageBox::warning( this, tr( "Load Color Map" ), tr( "The color map for band %1 has no entries" ).arg( bandIndex ) );
403  }
404 }
405 
406 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
407 {
408  int lineCounter = 0;
409  bool importError = false;
410  QString badLines;
411  QSettings settings;
412  QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
413  QString fileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), lastDir, tr( "Textfile (*.txt)" ) );
414  QFile inputFile( fileName );
415  if ( inputFile.open( QFile::ReadOnly ) )
416  {
417  //clear the current tree
418  mColormapTreeWidget->clear();
419 
420  QTextStream inputStream( &inputFile );
421  QString inputLine;
422  QStringList inputStringComponents;
424 
425  //read through the input looking for valid data
426  while ( !inputStream.atEnd() )
427  {
428  lineCounter++;
429  inputLine = inputStream.readLine();
430  if ( !inputLine.isEmpty() )
431  {
432  if ( !inputLine.simplified().startsWith( "#" ) )
433  {
434  if ( inputLine.contains( "INTERPOLATION", Qt::CaseInsensitive ) )
435  {
436  inputStringComponents = inputLine.split( ":" );
437  if ( inputStringComponents.size() == 2 )
438  {
439  if ( inputStringComponents[1].trimmed().toUpper().compare( "INTERPOLATED", Qt::CaseInsensitive ) == 0 )
440  {
441  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
442  }
443  else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE", Qt::CaseInsensitive ) == 0 )
444  {
445  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
446  }
447  else
448  {
449  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
450  }
451  }
452  else
453  {
454  importError = true;
455  badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
456  }
457  }
458  else
459  {
460  inputStringComponents = inputLine.split( "," );
461  if ( inputStringComponents.size() == 6 )
462  {
463  QgsColorRampShader::ColorRampItem currentItem( inputStringComponents[0].toDouble(),
464  QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
465  inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
466  inputStringComponents[5] );
467  colorRampItems.push_back( currentItem );
468  }
469  else
470  {
471  importError = true;
472  badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
473  }
474  }
475  }
476  }
477  lineCounter++;
478  }
479  populateColormapTreeWidget( colorRampItems );
480 
481  if ( importError )
482  {
483  QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n" ) + badLines );
484  }
485  }
486  else if ( !fileName.isEmpty() )
487  {
488  QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
489  }
490 }
491 
492 void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
493 {
494  QSettings settings;
495  QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
496  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), lastDir, tr( "Textfile (*.txt)" ) );
497  if ( !fileName.isEmpty() )
498  {
499  if ( !fileName.endsWith( ".txt", Qt::CaseInsensitive ) )
500  {
501  fileName = fileName + ".txt";
502  }
503 
504  QFile outputFile( fileName );
505  if ( outputFile.open( QFile::WriteOnly ) )
506  {
507  QTextStream outputStream( &outputFile );
508  outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << "\n";
509  outputStream << "INTERPOLATION:";
510  if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
511  {
512  outputStream << "INTERPOLATED\n";
513  }
514  else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
515  {
516  outputStream << "DISCRETE\n";
517  }
518  else
519  {
520  outputStream << "EXACT\n";
521  }
522 
523  int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
524  QTreeWidgetItem* currentItem;
525  QColor color;
526  for ( int i = 0; i < topLevelItemCount; ++i )
527  {
528  currentItem = mColormapTreeWidget->topLevelItem( i );
529  if ( !currentItem )
530  {
531  continue;
532  }
533  color = currentItem->background( 1 ).color();
534  outputStream << currentItem->text( 0 ).toDouble() << ",";
535  outputStream << color.red() << "," << color.green() << "," << color.blue() << "," << color.alpha() << ",";
536  if ( currentItem->text( 2 ) == "" )
537  {
538  outputStream << "Color entry " << i + 1 << "\n";
539  }
540  else
541  {
542  outputStream << currentItem->text( 2 ) << "\n";
543  }
544  }
545  outputStream.flush();
546  outputFile.close();
547  }
548  else
549  {
550  QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
551  }
552  }
553 }
554 
555 void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column )
556 {
557  if ( !item )
558  {
559  return;
560  }
561 
562  if ( column == 1 ) //change item color
563  {
564  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
565  QColor newColor = QgsColorDialogV2::getColor( item->background( column ).color(), this, "Change color", true );
566  if ( newColor.isValid() )
567  {
568  item->setBackground( 1, QBrush( newColor ) );
569  }
570  }
571  else
572  {
573  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
574  }
575 }
576 
578 {
579  const QgsSingleBandPseudoColorRenderer* pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer*>( r );
580  if ( pr )
581  {
582  mBandComboBox->setCurrentIndex( mBandComboBox->findData( pr->band() ) );
583 
584  const QgsRasterShader* rasterShader = pr->shader();
585  if ( rasterShader )
586  {
587  const QgsColorRampShader* colorRampShader = dynamic_cast<const QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
588  if ( colorRampShader )
589  {
590  if ( colorRampShader->colorRampType() == QgsColorRampShader::INTERPOLATED )
591  {
592  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
593  }
594  else if ( colorRampShader->colorRampType() == QgsColorRampShader::DISCRETE )
595  {
596  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
597  }
598  else
599  {
600  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
601  }
602 
603  const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
605  for ( ; it != colorRampItemList.end(); ++it )
606  {
607  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
608  newItem->setText( 0, QString::number( it->value, 'f' ) );
609  newItem->setBackground( 1, QBrush( it->color ) );
610  newItem->setText( 2, it->label );
611  }
612  mClipCheckBox->setChecked( colorRampShader->clip() );
613  }
614  }
615  setLineEditValue( mMinLineEdit, pr->classificationMin() );
616  setLineEditValue( mMaxLineEdit, pr->classificationMax() );
617  mMinMaxOrigin = pr->classificationMinMaxOrigin();
618  showMinMaxOrigin();
619  }
620 }
621 
622 void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged( int index )
623 {
624  QList<int> myBands;
625  myBands.append( mBandComboBox->itemData( index ).toInt() );
626  mMinMaxWidget->setBands( myBands );
627 }
628 
629 void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
630 {
631  Q_UNUSED( theBandNo );
632  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
633 
634  if ( qIsNaN( theMin ) )
635  {
636  mMinLineEdit->clear();
637  }
638  else
639  {
640  mMinLineEdit->setText( QString::number( theMin ) );
641  }
642 
643  if ( qIsNaN( theMax ) )
644  {
645  mMaxLineEdit->clear();
646  }
647  else
648  {
649  mMaxLineEdit->setText( QString::number( theMax ) );
650  }
651 
652  mMinMaxOrigin = theOrigin;
653  showMinMaxOrigin();
654 }
655 
656 void QgsSingleBandPseudoColorRendererWidget::showMinMaxOrigin()
657 {
658  mMinMaxOriginLabel->setText( QgsRasterRenderer::minMaxOriginLabel( mMinMaxOrigin ) );
659 }
660 
661 void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit * theLineEdit, double theValue )
662 {
663  QString s;
664  if ( !qIsNaN( theValue ) )
665  {
666  s = QString::number( theValue );
667  }
668  theLineEdit->setText( s );
669 }
670 
671 double QgsSingleBandPseudoColorRendererWidget::lineEditValue( const QLineEdit * theLineEdit ) const
672 {
673  if ( theLineEdit->text().isEmpty() )
674  {
675  return std::numeric_limits<double>::quiet_NaN();
676  }
677 
678  return theLineEdit->text().toDouble();
679 }
680 
681 void QgsSingleBandPseudoColorRendererWidget::resetClassifyButton()
682 {
683  mClassifyButton->setEnabled( true );
684  double min = lineEditValue( mMinLineEdit );
685  double max = lineEditValue( mMaxLineEdit );
686  if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
687  {
688  mClassifyButton->setEnabled( false );
689  }
690 }
QLayout * layout() const
virtual int bandCount() const =0
Get number of bands.
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Interface for all raster shaders.
void setBackground(int column, const QBrush &brush)
void setContentsMargins(int left, int top, int right, int bottom)
void setupUi(QWidget *widget)
void push_back(const T &value)
void setText(const QString &)
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
#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)
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
void reserve(int alloc)
QString simplified() const
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &theList)
Set custom colormap.
void setClip(bool clip)
void setRgb(int r, int g, int b, int a)
double toDouble(bool *ok) const
QString tr(const char *sourceText, const char *disambiguation, int n)
void loadMinMax(int theBandNo, double theMin, double theMax, int theOrigin)
int size() const
T value(int i) const
const QColor & color() const
void setValue(const QString &key, const QVariant &value)
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Get the custom colormap.
void setFlags(QFlags< Qt::ItemFlag > flags)
QColor fromRgb(QRgb rgb)
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString number(int n, int base)
void append(const T &value)
QgsColorRampShader::ColorRamp_TYPE colorRampType() const
Get the color ramp type.
virtual QString min(int index=0)
QgsRasterRenderer * renderer() const
void setExtent(const QgsRectangle &theExtent)
virtual double value(int index) const =0
Returns relative value between [0,1] of color at specified index.
int red() const
int band() const
Returns the band used by the renderer.
QgsRasterShaderFunction * rasterShaderFunction()
static QgsStyleV2 * defaultStyle()
return default application-wide style
Definition: qgsstylev2.cpp:51
bool isEmpty() const
static QString minMaxOriginLabel(int theOrigin)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Raster renderer pipe for single band pseudocolor.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setRasterShaderFunction(QgsRasterShaderFunction *)
A public method that allows the user to set their own shader function.
int alpha() const
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
virtual QColor color(double value) const =0
int green() const
iterator end()
bool contains(QChar ch, Qt::CaseSensitivity cs) const
int blue() const
QString displayBandName(int band) const
Returns a band name for display.
QVariant value(const QString &key, const QVariant &defaultValue) const
virtual int count() const =0
Returns number of defined colors, or -1 if undefined.
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
void insert(int i, const T &value)
QgsSingleBandPseudoColorRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
void setText(int column, const QString &text)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
virtual QString max(int index=0)
QgsRasterDataProvider * dataProvider()
Returns the data provider.
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
QString toString() const
QString text(int column) const
iterator begin()
Raster renderer pipe that applies colors to a raster.
QBrush background(int column) const
bool isValid() const
Base class for raster data providers.