QGIS API Documentation  2.13.0-Master
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( nullptr )
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  QVector<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  QVector<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.isEmpty() )
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( "lastColorMapDir", QDir::homePath() ).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  QFileInfo fileInfo( fileName );
482  settings.setValue( "lastColorMapDir", fileInfo.absoluteDir().absolutePath() );
483 
484  if ( importError )
485  {
486  QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n" ) + badLines );
487  }
488  }
489  else if ( !fileName.isEmpty() )
490  {
491  QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
492  }
493 }
494 
495 void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
496 {
497  QSettings settings;
498  QString lastDir = settings.value( "lastColorMapDir", QDir::homePath() ).toString();
499  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), lastDir, tr( "Textfile (*.txt)" ) );
500  if ( !fileName.isEmpty() )
501  {
502  if ( !fileName.endsWith( ".txt", Qt::CaseInsensitive ) )
503  {
504  fileName = fileName + ".txt";
505  }
506 
507  QFile outputFile( fileName );
508  if ( outputFile.open( QFile::WriteOnly ) )
509  {
510  QTextStream outputStream( &outputFile );
511  outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << '\n';
512  outputStream << "INTERPOLATION:";
513  if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
514  {
515  outputStream << "INTERPOLATED\n";
516  }
517  else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
518  {
519  outputStream << "DISCRETE\n";
520  }
521  else
522  {
523  outputStream << "EXACT\n";
524  }
525 
526  int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
527  QTreeWidgetItem* currentItem;
528  QColor color;
529  for ( int i = 0; i < topLevelItemCount; ++i )
530  {
531  currentItem = mColormapTreeWidget->topLevelItem( i );
532  if ( !currentItem )
533  {
534  continue;
535  }
536  color = currentItem->background( 1 ).color();
537  outputStream << currentItem->text( 0 ).toDouble() << ',';
538  outputStream << color.red() << ',' << color.green() << ',' << color.blue() << ',' << color.alpha() << ',';
539  if ( currentItem->text( 2 ) == "" )
540  {
541  outputStream << "Color entry " << i + 1 << '\n';
542  }
543  else
544  {
545  outputStream << currentItem->text( 2 ) << '\n';
546  }
547  }
548  outputStream.flush();
549  outputFile.close();
550 
551  QFileInfo fileInfo( fileName );
552  settings.setValue( "lastColorMapDir", fileInfo.absoluteDir().absolutePath() );
553  }
554  else
555  {
556  QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
557  }
558  }
559 }
560 
561 void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column )
562 {
563  if ( !item )
564  {
565  return;
566  }
567 
568  if ( column == 1 ) //change item color
569  {
570  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
571  QColor newColor = QgsColorDialogV2::getColor( item->background( column ).color(), this, "Change color", true );
572  if ( newColor.isValid() )
573  {
574  item->setBackground( 1, QBrush( newColor ) );
575  }
576  }
577  else
578  {
579  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
580  }
581 }
582 
584 {
585  const QgsSingleBandPseudoColorRenderer* pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer*>( r );
586  if ( pr )
587  {
588  mBandComboBox->setCurrentIndex( mBandComboBox->findData( pr->band() ) );
589 
590  const QgsRasterShader* rasterShader = pr->shader();
591  if ( rasterShader )
592  {
593  const QgsColorRampShader* colorRampShader = dynamic_cast<const QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
594  if ( colorRampShader )
595  {
596  if ( colorRampShader->colorRampType() == QgsColorRampShader::INTERPOLATED )
597  {
598  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
599  }
600  else if ( colorRampShader->colorRampType() == QgsColorRampShader::DISCRETE )
601  {
602  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
603  }
604  else
605  {
606  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
607  }
608 
609  const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
611  for ( ; it != colorRampItemList.end(); ++it )
612  {
613  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
614  newItem->setText( 0, QString::number( it->value, 'f' ) );
615  newItem->setBackground( 1, QBrush( it->color ) );
616  newItem->setText( 2, it->label );
617  }
618  mClipCheckBox->setChecked( colorRampShader->clip() );
619  }
620  }
621  setLineEditValue( mMinLineEdit, pr->classificationMin() );
622  setLineEditValue( mMaxLineEdit, pr->classificationMax() );
623  mMinMaxOrigin = pr->classificationMinMaxOrigin();
624  showMinMaxOrigin();
625  }
626 }
627 
628 void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged( int index )
629 {
630  QList<int> myBands;
631  myBands.append( mBandComboBox->itemData( index ).toInt() );
632  mMinMaxWidget->setBands( myBands );
633 }
634 
635 void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
636 {
637  Q_UNUSED( theBandNo );
638  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
639 
640  if ( qIsNaN( theMin ) )
641  {
642  mMinLineEdit->clear();
643  }
644  else
645  {
646  mMinLineEdit->setText( QString::number( theMin ) );
647  }
648 
649  if ( qIsNaN( theMax ) )
650  {
651  mMaxLineEdit->clear();
652  }
653  else
654  {
655  mMaxLineEdit->setText( QString::number( theMax ) );
656  }
657 
658  mMinMaxOrigin = theOrigin;
659  showMinMaxOrigin();
660 }
661 
662 void QgsSingleBandPseudoColorRendererWidget::showMinMaxOrigin()
663 {
664  mMinMaxOriginLabel->setText( QgsRasterRenderer::minMaxOriginLabel( mMinMaxOrigin ) );
665 }
666 
667 void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit * theLineEdit, double theValue )
668 {
669  QString s;
670  if ( !qIsNaN( theValue ) )
671  {
672  s = QString::number( theValue );
673  }
674  theLineEdit->setText( s );
675 }
676 
677 double QgsSingleBandPseudoColorRendererWidget::lineEditValue( const QLineEdit * theLineEdit ) const
678 {
679  if ( theLineEdit->text().isEmpty() )
680  {
681  return std::numeric_limits<double>::quiet_NaN();
682  }
683 
684  return theLineEdit->text().toDouble();
685 }
686 
687 void QgsSingleBandPseudoColorRendererWidget::resetClassifyButton()
688 {
689  mClassifyButton->setEnabled( true );
690  double min = lineEditValue( mMinLineEdit );
691  double max = lineEditValue( mMaxLineEdit );
692  if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
693  {
694  mClassifyButton->setEnabled( false );
695  }
696 }
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)
QString readLine(qint64 maxlen)
iterator begin()
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 homePath()
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)
bool atEnd() const
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()
bool isEmpty() const
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.
QDir absoluteDir() const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
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()
void reserve(int size)
bool contains(QChar ch, Qt::CaseSensitivity cs) const
virtual void close()
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 flush()
QString absolutePath() const
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)
void push_back(const T &value)
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.