26 #include <QColorDialog>
27 #include <QFileDialog>
28 #include <QMessageBox>
30 #include <QTextStream>
39 mColormapTreeWidget->setColumnWidth( 1, 50 );
41 QString defaultPalette = settings.value(
"/Raster/defaultPalette",
"Spectral" ).toString();
45 QgsDebugMsg(
"defaultPalette = " + defaultPalette );
46 mColorRampComboBox->setCurrentIndex( mColorRampComboBox->findText( defaultPalette ) );
60 mMinLineEdit->setValidator(
new QDoubleValidator( mMinLineEdit ) );
61 mMaxLineEdit->setValidator(
new QDoubleValidator( mMaxLineEdit ) );
65 QHBoxLayout *layout =
new QHBoxLayout();
66 layout->setContentsMargins( 0, 0, 0, 0 );
67 mMinMaxContainerWidget->setLayout( layout );
69 connect(
mMinMaxWidget, SIGNAL( load(
int,
double,
double,
int ) ),
70 this, SLOT(
loadMinMax(
int,
double,
double,
int ) ) );
75 for (
int i = 1; i <= nBands; ++i )
80 mColorInterpolationComboBox->addItem(
tr(
"Discrete" ), 0 );
81 mColorInterpolationComboBox->addItem(
tr(
"Linear" ), 1 );
82 mColorInterpolationComboBox->addItem(
tr(
"Exact" ), 2 );
83 mColorInterpolationComboBox->setCurrentIndex( 1 );
84 mClassificationModeComboBox->addItem(
tr(
"Continuous" ),
Continuous );
85 mClassificationModeComboBox->addItem(
tr(
"Equal interval" ),
EqualInterval );
88 mNumberOfEntriesSpinBox->setValue( 5 );
93 if ( mMinLineEdit->text().isEmpty() || mMaxLineEdit->text().isEmpty() )
111 colorRampShader->
setClip( mClipCheckBox->isChecked() );
114 QList<QgsColorRampShader::ColorRampItem> colorRampItems;
115 int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
116 QTreeWidgetItem* currentItem;
117 for (
int i = 0; i < topLevelItemCount; ++i )
119 currentItem = mColormapTreeWidget->topLevelItem( i );
125 newColorRampItem.
value = currentItem->text( 0 ).toDouble();
126 newColorRampItem.
color = currentItem->background( 1 ).color();
127 newColorRampItem.
label = currentItem->text( 2 );
128 colorRampItems.append( newColorRampItem );
131 qSort( colorRampItems );
134 if ( mColorInterpolationComboBox->currentText() ==
tr(
"Linear" ) )
138 else if ( mColorInterpolationComboBox->currentText() ==
tr(
"Discrete" ) )
148 int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
159 QTreeWidgetItem* newItem =
new QTreeWidgetItem( mColormapTreeWidget );
160 newItem->setText( 0,
"0.0" );
161 newItem->setBackground( 1, QBrush( QColor( Qt::magenta ) ) );
162 newItem->setText( 2,
tr(
"Custom color map entry" ) );
167 QTreeWidgetItem* currentItem = mColormapTreeWidget->currentItem();
176 bool inserted =
false;
177 int myCurrentIndex = 0;
178 int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
179 QTreeWidgetItem* myCurrentItem;
180 QList<QgsColorRampShader::ColorRampItem> myColorRampItems;
181 for (
int i = 0; i < myTopLevelItemCount; ++i )
183 myCurrentItem = mColormapTreeWidget->topLevelItem( i );
185 if ( !myCurrentItem || myCurrentItem->text( 0 ) ==
"" )
192 myNewColorRampItem.
value = myCurrentItem->text( 0 ).toDouble();
193 myNewColorRampItem.
color = myCurrentItem->background( 1 ).color();
194 myNewColorRampItem.
label = myCurrentItem->text( 2 );
201 if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() )
203 myColorRampItems.push_back( myNewColorRampItem );
206 else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.
value )
208 myColorRampItems.insert( myCurrentIndex, myNewColorRampItem );
211 else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.
value && myCurrentIndex == myColorRampItems.size() - 1 )
213 myColorRampItems.push_back( myNewColorRampItem );
216 else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.
value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.
value )
218 myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem );
229 int bandComboIndex = mBandComboBox->currentIndex();
237 int numberOfEntries = 0;
239 QList<double> entryValues;
240 QList<QColor> entryColors;
247 if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() ==
Continuous )
251 numberOfEntries = colorRamp->
count();
252 for (
int i = 0; i < colorRamp->
count(); ++i )
254 double value = colorRamp->
value( i );
255 entryValues.push_back( min + value * ( max - min ) );
261 numberOfEntries = mNumberOfEntriesSpinBox->value();
263 double currentValue =
min;
265 if ( numberOfEntries > 1 )
270 intervalDiff = ( max -
min ) / ( numberOfEntries - 1 );
275 intervalDiff = max -
min;
278 for (
int i = 0; i < numberOfEntries; ++i )
280 entryValues.push_back( currentValue );
281 currentValue += intervalDiff;
288 if ( numberOfEntries != 0 )
290 colorDiff = ( int )( 255 / numberOfEntries );
292 for (
int i = 0; i < numberOfEntries; ++i )
295 currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
296 entryColors.push_back( currentColor );
304 if ( numberOfEntries != 0 )
306 colorDiff = ( int )( 255 / numberOfEntries );
309 for (
int i = 0; i < numberOfEntries; ++i )
312 int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
313 currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
314 entryColors.push_back( currentColor );
319 for (
int i = 0; i < numberOfEntries; ++i )
321 int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
322 entryColors.push_back( colorRamp->
color(((
double ) idx ) / ( numberOfEntries - 1 ) ) );
326 mColormapTreeWidget->clear();
328 QList<double>::const_iterator value_it = entryValues.begin();
329 QList<QColor>::const_iterator color_it = entryColors.begin();
331 for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
333 QTreeWidgetItem* newItem =
new QTreeWidgetItem( mColormapTreeWidget );
334 newItem->setText( 0, QString::number( *value_it,
'f' ) );
335 newItem->setBackground( 1, QBrush( *color_it ) );
336 newItem->setText( 2, QString::number( *value_it,
'f' ) );
337 newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
343 mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() ==
EqualInterval );
350 settings.setValue(
"/Raster/defaultPalette", mColorRampComboBox->currentText() );
355 mColormapTreeWidget->clear();
356 QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItems.constBegin();
357 for ( ; it != colorRampItems.constEnd(); ++it )
359 QTreeWidgetItem* newItem =
new QTreeWidgetItem( mColormapTreeWidget );
360 newItem->setText( 0, QString::number( it->value,
'f' ) );
361 newItem->setBackground( 1, QBrush( it->color ) );
362 newItem->setText( 2, it->label );
373 int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
377 if ( colorRampList.size() > 0 )
380 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
384 QMessageBox::warning(
this,
tr(
"Load Color Map" ),
tr(
"The color map for band %1 has no entries" ).arg( bandIndex ) );
391 bool importError =
false;
394 QString lastDir = settings.value(
"lastRasterFileFilterDir",
"" ).toString();
395 QString fileName = QFileDialog::getOpenFileName(
this,
tr(
"Open file" ), lastDir,
tr(
"Textfile (*.txt)" ) );
396 QFile inputFile( fileName );
397 if ( inputFile.open( QFile::ReadOnly ) )
400 mColormapTreeWidget->clear();
402 QTextStream inputStream( &inputFile );
404 QStringList inputStringComponents;
405 QList<QgsColorRampShader::ColorRampItem> colorRampItems;
408 while ( !inputStream.atEnd() )
411 inputLine = inputStream.readLine();
412 if ( !inputLine.isEmpty() )
414 if ( !inputLine.simplified().startsWith(
"#" ) )
416 if ( inputLine.contains(
"INTERPOLATION", Qt::CaseInsensitive ) )
418 inputStringComponents = inputLine.split(
":" );
419 if ( inputStringComponents.size() == 2 )
421 if ( inputStringComponents[1].trimmed().toUpper().compare(
"INTERPOLATED", Qt::CaseInsensitive ) == 0 )
423 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
425 else if ( inputStringComponents[1].trimmed().toUpper().compare(
"DISCRETE", Qt::CaseInsensitive ) == 0 )
427 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Discrete" ) ) );
431 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Exact" ) ) );
437 badLines = badLines + QString::number( lineCounter ) +
":\t[" + inputLine +
"]\n";
442 inputStringComponents = inputLine.split(
"," );
443 if ( inputStringComponents.size() == 6 )
446 QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
447 inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
448 inputStringComponents[5] );
449 colorRampItems.push_back( currentItem );
454 badLines = badLines + QString::number( lineCounter ) +
":\t[" + inputLine +
"]\n";
465 QMessageBox::warning(
this,
tr(
"Import Error" ),
tr(
"The following lines contained errors\n\n" ) + badLines );
468 else if ( !fileName.isEmpty() )
470 QMessageBox::warning(
this,
tr(
"Read access denied" ),
tr(
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
477 QString lastDir = settings.value(
"lastRasterFileFilterDir",
"" ).toString();
478 QString fileName = QFileDialog::getSaveFileName(
this,
tr(
"Save file" ), lastDir,
tr(
"Textfile (*.txt)" ) );
479 if ( !fileName.isEmpty() )
481 if ( !fileName.endsWith(
".txt", Qt::CaseInsensitive ) )
483 fileName = fileName +
".txt";
486 QFile outputFile( fileName );
487 if ( outputFile.open( QFile::WriteOnly ) )
489 QTextStream outputStream( &outputFile );
490 outputStream <<
"# " <<
tr(
"QGIS Generated Color Map Export File" ) <<
"\n";
491 outputStream <<
"INTERPOLATION:";
492 if ( mColorInterpolationComboBox->currentText() ==
tr(
"Linear" ) )
494 outputStream <<
"INTERPOLATED\n";
496 else if ( mColorInterpolationComboBox->currentText() ==
tr(
"Discrete" ) )
498 outputStream <<
"DISCRETE\n";
502 outputStream <<
"EXACT\n";
505 int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
506 QTreeWidgetItem* currentItem;
508 for (
int i = 0; i < topLevelItemCount; ++i )
510 currentItem = mColormapTreeWidget->topLevelItem( i );
515 color = currentItem->background( 1 ).color();
516 outputStream << currentItem->text( 0 ).toDouble() <<
",";
517 outputStream << color.red() <<
"," << color.green() <<
"," << color.blue() <<
"," << color.alpha() <<
",";
518 if ( currentItem->text( 2 ) ==
"" )
520 outputStream <<
"Color entry " << i + 1 <<
"\n";
524 outputStream << currentItem->text( 2 ) <<
"\n";
527 outputStream.flush();
532 QMessageBox::warning(
this,
tr(
"Write access denied" ),
tr(
"Write access denied. Adjust the file permissions and try again.\n\n" ) );
546 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
547 QColor newColor = QColorDialog::getColor( item->background( column ).color(),
this,
"Change color", QColorDialog::ShowAlphaChannel );
548 if ( newColor.isValid() )
550 item->setBackground( 1, QBrush( newColor ) );
555 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
568 if ( colorRampShader )
572 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
576 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Discrete" ) ) );
580 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Exact" ) ) );
583 const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader->
colorRampItemList();
584 QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItemList.constBegin();
585 for ( ; it != colorRampItemList.end(); ++it )
587 QTreeWidgetItem* newItem =
new QTreeWidgetItem( mColormapTreeWidget );
588 newItem->setText( 0, QString::number( it->value,
'f' ) );
589 newItem->setBackground( 1, QBrush( it->color ) );
590 newItem->setText( 2, it->label );
592 mClipCheckBox->setChecked( colorRampShader->
clip() );
605 myBands.append( mBandComboBox->itemData( index ).toInt() );
611 Q_UNUSED( theBandNo );
612 QgsDebugMsg( QString(
"theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
614 if ( qIsNaN( theMin ) )
616 mMinLineEdit->clear();
620 mMinLineEdit->setText( QString::number( theMin ) );
623 if ( qIsNaN( theMax ) )
625 mMaxLineEdit->clear();
629 mMaxLineEdit->setText( QString::number( theMax ) );
644 if ( !qIsNaN( theValue ) )
646 s = QString::number( theValue );
648 theLineEdit->setText( s );
653 if ( theLineEdit->text().isEmpty() )
655 return std::numeric_limits<double>::quiet_NaN();
658 return theLineEdit->text().toDouble();
663 mClassifyButton->setEnabled(
true );
666 if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
668 mClassifyButton->setEnabled(
false );