QGIS API Documentation  2.15.0-Master (af20121)
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  connect( mClassificationModeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( on_mClassifyButton_clicked() ) );
106  connect( mMinLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( on_mClassifyButton_clicked() ) );
107  connect( mMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( on_mClassifyButton_clicked() ) );
108  connect( mColorRampComboBox, SIGNAL( sourceRampEdited() ), this, SLOT( on_mClassifyButton_clicked() ) );
109  connect( mColorRampComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( on_mClassifyButton_clicked() ) );
110  connect( mInvertCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( on_mClassifyButton_clicked() ) );
111  connect( mNumberOfEntriesSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( on_mClassifyButton_clicked() ) );
112  connect( mBandComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( on_mClassifyButton_clicked() ) );
113 }
114 
116 {
117 }
118 
120 {
121  QgsRasterShader* rasterShader = new QgsRasterShader();
122  QgsColorRampShader* colorRampShader = new QgsColorRampShader();
123  colorRampShader->setClip( mClipCheckBox->isChecked() );
124 
125  //iterate through mColormapTreeWidget and set colormap info of layer
127  int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
128  QTreeWidgetItem* currentItem;
129  for ( int i = 0; i < topLevelItemCount; ++i )
130  {
131  currentItem = mColormapTreeWidget->topLevelItem( i );
132  if ( !currentItem )
133  {
134  continue;
135  }
136  QgsColorRampShader::ColorRampItem newColorRampItem;
137  newColorRampItem.value = currentItem->text( 0 ).toDouble();
138  newColorRampItem.color = currentItem->background( 1 ).color();
139  newColorRampItem.label = currentItem->text( 2 );
140  colorRampItems.append( newColorRampItem );
141  }
142  // sort the shader items
143  qSort( colorRampItems );
144  colorRampShader->setColorRampItemList( colorRampItems );
145 
146  if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
147  {
149  }
150  else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
151  {
153  }
154  else
155  {
156  colorRampShader->setColorRampType( QgsColorRampShader::EXACT );
157  }
158  rasterShader->setRasterShaderFunction( colorRampShader );
159 
160  int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
162 
163  renderer->setClassificationMin( lineEditValue( mMinLineEdit ) );
164  renderer->setClassificationMax( lineEditValue( mMaxLineEdit ) );
165  renderer->setClassificationMinMaxOrigin( mMinMaxOrigin );
166  return renderer;
167 }
168 
169 void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
170 {
171  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
172  newItem->setText( 0, "0.0" );
173  newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
174  newItem->setText( 2, tr( "Custom color map entry" ) );
175  emit widgetChanged();
176 }
177 
178 void QgsSingleBandPseudoColorRendererWidget::on_mDeleteEntryButton_clicked()
179 {
180  QTreeWidgetItem* currentItem = mColormapTreeWidget->currentItem();
181  if ( currentItem )
182  {
183  delete currentItem;
184  }
185  emit widgetChanged();
186 }
187 
188 void QgsSingleBandPseudoColorRendererWidget::on_mNumberOfEntriesSpinBox_valueChanged()
189 {
190 }
191 
192 void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
193 {
194  bool inserted = false;
195  int myCurrentIndex = 0;
196  int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
197  QTreeWidgetItem* myCurrentItem;
199  for ( int i = 0; i < myTopLevelItemCount; ++i )
200  {
201  myCurrentItem = mColormapTreeWidget->topLevelItem( i );
202  //If the item is null or does not have a pixel values set, skip
203  if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" )
204  {
205  continue;
206  }
207 
208  //Create a copy of the new Color ramp Item
209  QgsColorRampShader::ColorRampItem myNewColorRampItem;
210  myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble();
211  myNewColorRampItem.color = myCurrentItem->background( 1 ).color();
212  myNewColorRampItem.label = myCurrentItem->text( 2 );
213 
214  //Simple insertion sort - speed is not a huge factor here
215  inserted = false;
216  myCurrentIndex = 0;
217  while ( !inserted )
218  {
219  if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
220  {
221  myColorRampItems.push_back( myNewColorRampItem );
222  inserted = true;
223  }
224  else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value )
225  {
226  myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
227  inserted = true;
228  }
229  else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 )
230  {
231  myColorRampItems.push_back( myNewColorRampItem );
232  inserted = true;
233  }
234  else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value )
235  {
236  myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
237  inserted = true;
238  }
239  myCurrentIndex++;
240  }
241  }
242  populateColormapTreeWidget( myColorRampItems );
243  emit widgetChanged();
244 }
245 
246 void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
247 {
248  int bandComboIndex = mBandComboBox->currentIndex();
249  if ( bandComboIndex == -1 || !mRasterLayer )
250  {
251  return;
252  }
253 
254  //int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
255  //QgsRasterBandStats myRasterBandStats = mRasterLayer->dataProvider()->bandStatistics( bandNr );
256  int numberOfEntries = 0;
257 
258  QList<double> entryValues;
259  QVector<QColor> entryColors;
260 
261  double min = lineEditValue( mMinLineEdit );
262  double max = lineEditValue( mMaxLineEdit );
263 
264  QgsVectorColorRampV2* colorRamp = mColorRampComboBox->currentColorRamp();
265 
266  if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() == Continuous )
267  {
268  if ( colorRamp )
269  {
270  numberOfEntries = colorRamp->count();
271  entryValues.reserve( colorRamp->count() );
272  for ( int i = 0; i < colorRamp->count(); ++i )
273  {
274  double value = colorRamp->value( i );
275  entryValues.push_back( min + value * ( max - min ) );
276  }
277  }
278  }
279  else // EqualInterval
280  {
281  numberOfEntries = mNumberOfEntriesSpinBox->value();
282  //double currentValue = myRasterBandStats.minimumValue;
283  double currentValue = min;
284  double intervalDiff;
285  if ( numberOfEntries > 1 )
286  {
287  //because the highest value is also an entry, there are (numberOfEntries - 1)
288  //intervals
289  //intervalDiff = ( myRasterBandStats.maximumValue - myRasterBandStats.minimumValue ) /
290  intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
291  }
292  else
293  {
294  //intervalDiff = myRasterBandStats.maximumValue - myRasterBandStats.minimumValue;
295  intervalDiff = max - min;
296  }
297 
298  entryValues.reserve( numberOfEntries );
299  for ( int i = 0; i < numberOfEntries; ++i )
300  {
301  entryValues.push_back( currentValue );
302  currentValue += intervalDiff;
303  }
304  }
305 
306 #if 0
307  //hard code color range from blue -> red for now. Allow choice of ramps in future
308  int colorDiff = 0;
309  if ( numberOfEntries != 0 )
310  {
311  colorDiff = ( int )( 255 / numberOfEntries );
312  }
313  for ( int i = 0; i < numberOfEntries; ++i )
314  {
315  QColor currentColor;
316  currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
317  entryColors.push_back( currentColor );
318  }
319 #endif
320 
321  if ( ! colorRamp )
322  {
323  //hard code color range from blue -> red (previous default)
324  int colorDiff = 0;
325  if ( numberOfEntries != 0 )
326  {
327  colorDiff = ( int )( 255 / numberOfEntries );
328  }
329 
330  entryColors.reserve( numberOfEntries );
331  for ( int i = 0; i < numberOfEntries; ++i )
332  {
333  QColor currentColor;
334  int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
335  currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
336  entryColors.push_back( currentColor );
337  }
338  }
339  else
340  {
341  entryColors.reserve( numberOfEntries );
342  for ( int i = 0; i < numberOfEntries; ++i )
343  {
344  int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
345  entryColors.push_back( colorRamp->color((( double ) idx ) / ( numberOfEntries - 1 ) ) );
346  }
347  }
348 
349  mColormapTreeWidget->clear();
350 
351  QList<double>::const_iterator value_it = entryValues.begin();
352  QVector<QColor>::const_iterator color_it = entryColors.begin();
353 
354  for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
355  {
356  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
357  newItem->setText( 0, QString::number( *value_it, 'f' ) );
358  newItem->setBackground( 1, QBrush( *color_it ) );
359  newItem->setText( 2, QString::number( *value_it, 'f' ) );
360  newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
361  }
362  emit widgetChanged();
363 }
364 
365 void QgsSingleBandPseudoColorRendererWidget::on_mClassificationModeComboBox_currentIndexChanged( int index )
366 {
367  mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() == EqualInterval );
368 }
369 
370 void QgsSingleBandPseudoColorRendererWidget::on_mColorRampComboBox_currentIndexChanged( int index )
371 {
372  Q_UNUSED( index );
373  QSettings settings;
374  settings.setValue( "/Raster/defaultPalette", mColorRampComboBox->currentText() );
375 
376  QgsVectorColorRampV2* ramp = mColorRampComboBox->currentColorRamp();
377  if ( !ramp )
378  return;
379 
380  bool enableContinuous = ( ramp->count() > 0 );
381  mClassificationModeComboBox->setEnabled( enableContinuous );
382  if ( !enableContinuous )
383  {
384  mClassificationModeComboBox->setCurrentIndex( mClassificationModeComboBox->findData( EqualInterval ) );
385  }
386 }
387 
388 void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems )
389 {
390  mColormapTreeWidget->clear();
392  for ( ; it != colorRampItems.constEnd(); ++it )
393  {
394  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
395  newItem->setText( 0, QString::number( it->value, 'f' ) );
396  newItem->setBackground( 1, QBrush( it->color ) );
397  newItem->setText( 2, it->label );
398  }
399 }
400 
401 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
402 {
403  if ( !mRasterLayer || !mRasterLayer->dataProvider() )
404  {
405  return;
406  }
407 
408  int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
409 
410 
412  if ( !colorRampList.isEmpty() )
413  {
414  populateColormapTreeWidget( colorRampList );
415  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
416  }
417  else
418  {
419  QMessageBox::warning( this, tr( "Load Color Map" ), tr( "The color map for band %1 has no entries" ).arg( bandIndex ) );
420  }
421  emit widgetChanged();
422 }
423 
424 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
425 {
426  int lineCounter = 0;
427  bool importError = false;
428  QString badLines;
429  QSettings settings;
430  QString lastDir = settings.value( "lastColorMapDir", QDir::homePath() ).toString();
431  QString fileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), lastDir, tr( "Textfile (*.txt)" ) );
432  QFile inputFile( fileName );
433  if ( inputFile.open( QFile::ReadOnly ) )
434  {
435  //clear the current tree
436  mColormapTreeWidget->clear();
437 
438  QTextStream inputStream( &inputFile );
439  QString inputLine;
440  QStringList inputStringComponents;
442 
443  //read through the input looking for valid data
444  while ( !inputStream.atEnd() )
445  {
446  lineCounter++;
447  inputLine = inputStream.readLine();
448  if ( !inputLine.isEmpty() )
449  {
450  if ( !inputLine.simplified().startsWith( '#' ) )
451  {
452  if ( inputLine.contains( "INTERPOLATION", Qt::CaseInsensitive ) )
453  {
454  inputStringComponents = inputLine.split( ':' );
455  if ( inputStringComponents.size() == 2 )
456  {
457  if ( inputStringComponents[1].trimmed().toUpper().compare( "INTERPOLATED", Qt::CaseInsensitive ) == 0 )
458  {
459  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
460  }
461  else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE", Qt::CaseInsensitive ) == 0 )
462  {
463  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
464  }
465  else
466  {
467  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
468  }
469  }
470  else
471  {
472  importError = true;
473  badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
474  }
475  }
476  else
477  {
478  inputStringComponents = inputLine.split( ',' );
479  if ( inputStringComponents.size() == 6 )
480  {
481  QgsColorRampShader::ColorRampItem currentItem( inputStringComponents[0].toDouble(),
482  QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
483  inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
484  inputStringComponents[5] );
485  colorRampItems.push_back( currentItem );
486  }
487  else
488  {
489  importError = true;
490  badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
491  }
492  }
493  }
494  }
495  lineCounter++;
496  }
497  populateColormapTreeWidget( colorRampItems );
498 
499  QFileInfo fileInfo( fileName );
500  settings.setValue( "lastColorMapDir", fileInfo.absoluteDir().absolutePath() );
501 
502  if ( importError )
503  {
504  QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n" ) + badLines );
505  }
506  }
507  else if ( !fileName.isEmpty() )
508  {
509  QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
510  }
511  emit widgetChanged();
512 }
513 
514 void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
515 {
516  QSettings settings;
517  QString lastDir = settings.value( "lastColorMapDir", QDir::homePath() ).toString();
518  QString fileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), lastDir, tr( "Textfile (*.txt)" ) );
519  if ( !fileName.isEmpty() )
520  {
521  if ( !fileName.endsWith( ".txt", Qt::CaseInsensitive ) )
522  {
523  fileName = fileName + ".txt";
524  }
525 
526  QFile outputFile( fileName );
527  if ( outputFile.open( QFile::WriteOnly ) )
528  {
529  QTextStream outputStream( &outputFile );
530  outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << '\n';
531  outputStream << "INTERPOLATION:";
532  if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
533  {
534  outputStream << "INTERPOLATED\n";
535  }
536  else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
537  {
538  outputStream << "DISCRETE\n";
539  }
540  else
541  {
542  outputStream << "EXACT\n";
543  }
544 
545  int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
546  QTreeWidgetItem* currentItem;
547  QColor color;
548  for ( int i = 0; i < topLevelItemCount; ++i )
549  {
550  currentItem = mColormapTreeWidget->topLevelItem( i );
551  if ( !currentItem )
552  {
553  continue;
554  }
555  color = currentItem->background( 1 ).color();
556  outputStream << currentItem->text( 0 ).toDouble() << ',';
557  outputStream << color.red() << ',' << color.green() << ',' << color.blue() << ',' << color.alpha() << ',';
558  if ( currentItem->text( 2 ) == "" )
559  {
560  outputStream << "Color entry " << i + 1 << '\n';
561  }
562  else
563  {
564  outputStream << currentItem->text( 2 ) << '\n';
565  }
566  }
567  outputStream.flush();
568  outputFile.close();
569 
570  QFileInfo fileInfo( fileName );
571  settings.setValue( "lastColorMapDir", fileInfo.absoluteDir().absolutePath() );
572  }
573  else
574  {
575  QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
576  }
577  }
578 }
579 
580 void QgsSingleBandPseudoColorRendererWidget::on_mColorInterpolationComboBox_currentIndexChanged()
581 {
582 }
583 
584 void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column )
585 {
586  if ( !item )
587  {
588  return;
589  }
590 
591  if ( column == 1 ) //change item color
592  {
593  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
594  QColor newColor = QgsColorDialogV2::getColor( item->background( column ).color(), this, "Change color", true );
595  if ( newColor.isValid() )
596  {
597  item->setBackground( 1, QBrush( newColor ) );
598  emit widgetChanged();
599  }
600  }
601  else
602  {
603  item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
604  }
605 }
606 
608 {
609  const QgsSingleBandPseudoColorRenderer* pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer*>( r );
610  if ( pr )
611  {
612  mBandComboBox->setCurrentIndex( mBandComboBox->findData( pr->band() ) );
613 
614  const QgsRasterShader* rasterShader = pr->shader();
615  if ( rasterShader )
616  {
617  const QgsColorRampShader* colorRampShader = dynamic_cast<const QgsColorRampShader*>( rasterShader->rasterShaderFunction() );
618  if ( colorRampShader )
619  {
620  if ( colorRampShader->colorRampType() == QgsColorRampShader::INTERPOLATED )
621  {
622  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
623  }
624  else if ( colorRampShader->colorRampType() == QgsColorRampShader::DISCRETE )
625  {
626  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
627  }
628  else
629  {
630  mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
631  }
632 
633  const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->colorRampItemList();
635  for ( ; it != colorRampItemList.end(); ++it )
636  {
637  QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
638  newItem->setText( 0, QString::number( it->value, 'f' ) );
639  newItem->setBackground( 1, QBrush( it->color ) );
640  newItem->setText( 2, it->label );
641  }
642  mClipCheckBox->setChecked( colorRampShader->clip() );
643  }
644  }
645  setLineEditValue( mMinLineEdit, pr->classificationMin() );
646  setLineEditValue( mMaxLineEdit, pr->classificationMax() );
647  mMinMaxOrigin = pr->classificationMinMaxOrigin();
648  showMinMaxOrigin();
649  }
650 }
651 
652 void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged( int index )
653 {
654  QList<int> myBands;
655  myBands.append( mBandComboBox->itemData( index ).toInt() );
656  mMinMaxWidget->setBands( myBands );
657 }
658 
659 void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
660 {
661  Q_UNUSED( theBandNo );
662  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
663 
664  if ( qIsNaN( theMin ) )
665  {
666  mMinLineEdit->clear();
667  }
668  else
669  {
670  mMinLineEdit->setText( QString::number( theMin ) );
671  }
672 
673  if ( qIsNaN( theMax ) )
674  {
675  mMaxLineEdit->clear();
676  }
677  else
678  {
679  mMaxLineEdit->setText( QString::number( theMax ) );
680  }
681 
682  mMinMaxOrigin = theOrigin;
683  showMinMaxOrigin();
684 }
685 
686 void QgsSingleBandPseudoColorRendererWidget::showMinMaxOrigin()
687 {
688  mMinMaxOriginLabel->setText( QgsRasterRenderer::minMaxOriginLabel( mMinMaxOrigin ) );
689 }
690 
691 void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit * theLineEdit, double theValue )
692 {
693  QString s;
694  if ( !qIsNaN( theValue ) )
695  {
696  s = QString::number( theValue );
697  }
698  theLineEdit->setText( s );
699 }
700 
701 double QgsSingleBandPseudoColorRendererWidget::lineEditValue( const QLineEdit * theLineEdit ) const
702 {
703  if ( theLineEdit->text().isEmpty() )
704  {
705  return std::numeric_limits<double>::quiet_NaN();
706  }
707 
708  return theLineEdit->text().toDouble();
709 }
710 
711 void QgsSingleBandPseudoColorRendererWidget::resetClassifyButton()
712 {
713  mClassifyButton->setEnabled( true );
714  double min = lineEditValue( mMinLineEdit );
715  double max = lineEditValue( mMaxLineEdit );
716  if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
717  {
718  mClassifyButton->setEnabled( false );
719  }
720 }
QLayout * layout() const
Assigns the color of the exact matching value in the color ramp item list.
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)
Sets whether the shader should not render values out of range.
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)
bool clip() const
Returns whether the shader will clip values which are out of range.
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
Returns the color corresponding to a specified value.
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)
Interpolates the color between two class breaks linearly.
Assigns the color of the higher class for every pixel between two class breaks.
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
void widgetChanged()
Emmited when something on the widget has changed.
Abstract base class for color ramps.
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.