QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  for ( int i = 0; i < colorRamp->count(); ++i )
256  {
257  double value = colorRamp->value( i );
258  entryValues.push_back( min + value * ( max - min ) );
259  }
260  }
261  }
262  else // EqualInterval
263  {
264  numberOfEntries = mNumberOfEntriesSpinBox->value();
265  //double currentValue = myRasterBandStats.minimumValue;
266  double currentValue = min;
267  double intervalDiff;
268  if ( numberOfEntries > 1 )
269  {
270  //because the highest value is also an entry, there are (numberOfEntries - 1)
271  //intervals
272  //intervalDiff = ( myRasterBandStats.maximumValue - myRasterBandStats.minimumValue ) /
273  intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
274  }
275  else
276  {
277  //intervalDiff = myRasterBandStats.maximumValue - myRasterBandStats.minimumValue;
278  intervalDiff = max - min;
279  }
280 
281  for ( int i = 0; i < numberOfEntries; ++i )
282  {
283  entryValues.push_back( currentValue );
284  currentValue += intervalDiff;
285  }
286  }
287 
288 #if 0
289  //hard code color range from blue -> red for now. Allow choice of ramps in future
290  int colorDiff = 0;
291  if ( numberOfEntries != 0 )
292  {
293  colorDiff = ( int )( 255 / numberOfEntries );
294  }
295  for ( int i = 0; i < numberOfEntries; ++i )
296  {
297  QColor currentColor;
298  currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
299  entryColors.push_back( currentColor );
300  }
301 #endif
302 
303  if ( ! colorRamp )
304  {
305  //hard code color range from blue -> red (previous default)
306  int colorDiff = 0;
307  if ( numberOfEntries != 0 )
308  {
309  colorDiff = ( int )( 255 / numberOfEntries );
310  }
311 
312  for ( int i = 0; i < numberOfEntries; ++i )
313  {
314  QColor currentColor;
315  int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
316  currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
317  entryColors.push_back( currentColor );
318  }
319  }
320  else
321  {
322  for ( int i = 0; i < numberOfEntries; ++i )
323  {
324  int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
325  entryColors.push_back( colorRamp->color((( double ) idx ) / ( numberOfEntries - 1 ) ) );
326  }
327  }
328 
329  mColormapTreeWidget->clear();
330 
331  QList<double>::const_iterator value_it = entryValues.begin();
332  QList<QColor>::const_iterator color_it = entryColors.begin();
333 
334  for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
335  {
336  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
337  newItem->setText( 0, QString::number( *value_it, 'f' ) );
338  newItem->setBackground( 1, QBrush( *color_it ) );
339  newItem->setText( 2, QString::number( *value_it, 'f' ) );
340  newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
341  }
342 }
343 
344 void QgsSingleBandPseudoColorRendererWidget::on_mClassificationModeComboBox_currentIndexChanged( int index )
345 {
346  mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() == EqualInterval );
347 }
348 
349 void QgsSingleBandPseudoColorRendererWidget::on_mColorRampComboBox_currentIndexChanged( int index )
350 {
351  Q_UNUSED( index );
352  QSettings settings;
353  settings.setValue( "/Raster/defaultPalette", mColorRampComboBox->currentText() );
354 
355  QgsVectorColorRampV2* ramp = mColorRampComboBox->currentColorRamp();
356  if ( !ramp )
357  return;
358 
359  bool enableContinuous = ( ramp->count() > 0 );
360  mClassificationModeComboBox->setEnabled( enableContinuous );
361  if ( !enableContinuous )
362  {
363  mClassificationModeComboBox->setCurrentIndex( mClassificationModeComboBox->findData( EqualInterval ) );
364  }
365 }
366 
367 void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems )
368 {
369  mColormapTreeWidget->clear();
371  for ( ; it != colorRampItems.constEnd(); ++it )
372  {
373  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
374  newItem->setText( 0, QString::number( it->value, 'f' ) );
375  newItem->setBackground( 1, QBrush( it->color ) );
376  newItem->setText( 2, it->label );
377  }
378 }
379 
380 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
381 {
382  if ( !mRasterLayer || !mRasterLayer->dataProvider() )
383  {
384  return;
385  }
386 
387  int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
388 
389 
391  if ( colorRampList.size() > 0 )
392  {
393  populateColormapTreeWidget( colorRampList );
394  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
395  }
396  else
397  {
398  QMessageBox::warning( this, tr( "Load Color Map" ), tr( "The color map for band %1 has no entries" ).arg( bandIndex ) );
399  }
400 }
401 
402 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
403 {
404  int lineCounter = 0;
405  bool importError = false;
406  QString badLines;
407  QSettings settings;
408  QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
409  QString fileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), lastDir, tr( "Textfile (*.txt)" ) );
410  QFile inputFile( fileName );
411  if ( inputFile.open( QFile::ReadOnly ) )
412  {
413  //clear the current tree
414  mColormapTreeWidget->clear();
415 
416  QTextStream inputStream( &inputFile );
417  QString inputLine;
418  QStringList inputStringComponents;
420 
421  //read through the input looking for valid data
422  while ( !inputStream.atEnd() )
423  {
424  lineCounter++;
425  inputLine = inputStream.readLine();
426  if ( !inputLine.isEmpty() )
427  {
428  if ( !inputLine.simplified().startsWith( "#" ) )
429  {
430  if ( inputLine.contains( "INTERPOLATION", Qt::CaseInsensitive ) )
431  {
432  inputStringComponents = inputLine.split( ":" );
433  if ( inputStringComponents.size() == 2 )
434  {
435  if ( inputStringComponents[1].trimmed().toUpper().compare( "INTERPOLATED", Qt::CaseInsensitive ) == 0 )
436  {
437  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
438  }
439  else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE", Qt::CaseInsensitive ) == 0 )
440  {
441  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
442  }
443  else
444  {
445  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
446  }
447  }
448  else
449  {
450  importError = true;
451  badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
452  }
453  }
454  else
455  {
456  inputStringComponents = inputLine.split( "," );
457  if ( inputStringComponents.size() == 6 )
458  {
459  QgsColorRampShader::ColorRampItem currentItem( inputStringComponents[0].toDouble(),
460  QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
461  inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
462  inputStringComponents[5] );
463  colorRampItems.push_back( currentItem );
464  }
465  else
466  {
467  importError = true;
468  badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
469  }
470  }
471  }
472  }
473  lineCounter++;
474  }
475  populateColormapTreeWidget( colorRampItems );
476 
477  if ( importError )
478  {
479  QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n" ) + badLines );
480  }
481  }
482  else if ( !fileName.isEmpty() )
483  {
484  QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
485  }
486 }
487 
488 void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
489 {
490  QSettings settings;
491  QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
492  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), lastDir, tr( "Textfile (*.txt)" ) );
493  if ( !fileName.isEmpty() )
494  {
495  if ( !fileName.endsWith( ".txt", Qt::CaseInsensitive ) )
496  {
497  fileName = fileName + ".txt";
498  }
499 
500  QFile outputFile( fileName );
501  if ( outputFile.open( QFile::WriteOnly ) )
502  {
503  QTextStream outputStream( &outputFile );
504  outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << "\n";
505  outputStream << "INTERPOLATION:";
506  if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
507  {
508  outputStream << "INTERPOLATED\n";
509  }
510  else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
511  {
512  outputStream << "DISCRETE\n";
513  }
514  else
515  {
516  outputStream << "EXACT\n";
517  }
518 
519  int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
520  QTreeWidgetItem* currentItem;
521  QColor color;
522  for ( int i = 0; i < topLevelItemCount; ++i )
523  {
524  currentItem = mColormapTreeWidget->topLevelItem( i );
525  if ( !currentItem )
526  {
527  continue;
528  }
529  color = currentItem->background( 1 ).color();
530  outputStream << currentItem->text( 0 ).toDouble() << ",";
531  outputStream << color.red() << "," << color.green() << "," << color.blue() << "," << color.alpha() << ",";
532  if ( currentItem->text( 2 ) == "" )
533  {
534  outputStream << "Color entry " << i + 1 << "\n";
535  }
536  else
537  {
538  outputStream << currentItem->text( 2 ) << "\n";
539  }
540  }
541  outputStream.flush();
542  outputFile.close();
543  }
544  else
545  {
546  QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
547  }
548  }
549 }
550 
551 void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column )
552 {
553  if ( !item )
554  {
555  return;
556  }
557 
558  if ( column == 1 ) //change item color
559  {
560  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
561  QColor newColor = QgsColorDialogV2::getColor( item->background( column ).color(), this, "Change color", true );
562  if ( newColor.isValid() )
563  {
564  item->setBackground( 1, QBrush( newColor ) );
565  }
566  }
567  else
568  {
569  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
570  }
571 }
572 
574 {
575  const QgsSingleBandPseudoColorRenderer* pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer*>( r );
576  if ( pr )
577  {
578  mBandComboBox->setCurrentIndex( mBandComboBox->findData( pr->band() ) );
579 
580  const QgsRasterShader* rasterShader = pr->shader();
581  if ( rasterShader )
582  {
583  const QgsColorRampShader* colorRampShader = dynamic_cast<const QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
584  if ( colorRampShader )
585  {
586  if ( colorRampShader->colorRampType() == QgsColorRampShader::INTERPOLATED )
587  {
588  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
589  }
590  else if ( colorRampShader->colorRampType() == QgsColorRampShader::DISCRETE )
591  {
592  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
593  }
594  else
595  {
596  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
597  }
598 
599  const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
601  for ( ; it != colorRampItemList.end(); ++it )
602  {
603  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
604  newItem->setText( 0, QString::number( it->value, 'f' ) );
605  newItem->setBackground( 1, QBrush( it->color ) );
606  newItem->setText( 2, it->label );
607  }
608  mClipCheckBox->setChecked( colorRampShader->clip() );
609  }
610  }
611  setLineEditValue( mMinLineEdit, pr->classificationMin() );
612  setLineEditValue( mMaxLineEdit, pr->classificationMax() );
613  mMinMaxOrigin = pr->classificationMinMaxOrigin();
614  showMinMaxOrigin();
615  }
616 }
617 
618 void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged( int index )
619 {
620  QList<int> myBands;
621  myBands.append( mBandComboBox->itemData( index ).toInt() );
622  mMinMaxWidget->setBands( myBands );
623 }
624 
625 void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
626 {
627  Q_UNUSED( theBandNo );
628  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
629 
630  if ( qIsNaN( theMin ) )
631  {
632  mMinLineEdit->clear();
633  }
634  else
635  {
636  mMinLineEdit->setText( QString::number( theMin ) );
637  }
638 
639  if ( qIsNaN( theMax ) )
640  {
641  mMaxLineEdit->clear();
642  }
643  else
644  {
645  mMaxLineEdit->setText( QString::number( theMax ) );
646  }
647 
648  mMinMaxOrigin = theOrigin;
649  showMinMaxOrigin();
650 }
651 
652 void QgsSingleBandPseudoColorRendererWidget::showMinMaxOrigin()
653 {
654  mMinMaxOriginLabel->setText( QgsRasterRenderer::minMaxOriginLabel( mMinMaxOrigin ) );
655 }
656 
657 void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit * theLineEdit, double theValue )
658 {
659  QString s;
660  if ( !qIsNaN( theValue ) )
661  {
662  s = QString::number( theValue );
663  }
664  theLineEdit->setText( s );
665 }
666 
667 double QgsSingleBandPseudoColorRendererWidget::lineEditValue( const QLineEdit * theLineEdit ) const
668 {
669  if ( theLineEdit->text().isEmpty() )
670  {
671  return std::numeric_limits<double>::quiet_NaN();
672  }
673 
674  return theLineEdit->text().toDouble();
675 }
676 
677 void QgsSingleBandPseudoColorRendererWidget::resetClassifyButton()
678 {
679  mClassifyButton->setEnabled( true );
680  double min = lineEditValue( mMinLineEdit );
681  double max = lineEditValue( mMaxLineEdit );
682  if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
683  {
684  mClassifyButton->setEnabled( false );
685  }
686 }
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
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.