27 #include <QFileDialog>
28 #include <QMessageBox>
30 #include <QTextStream>
34 , mMinMaxWidget( NULL )
41 mColormapTreeWidget->setColumnWidth( 1, 50 );
47 QgsDebugMsg(
"defaultPalette = " + defaultPalette );
48 mColorRampComboBox->setCurrentIndex( mColorRampComboBox->findText( defaultPalette ) );
49 connect( mButtonEditRamp, SIGNAL( clicked() ), mColorRampComboBox, SLOT( editSourceRamp() ) );
70 mMinMaxContainerWidget->setLayout( layout );
72 connect( mMinMaxWidget, SIGNAL( load(
int,
double,
double,
int ) ),
73 this, SLOT(
loadMinMax(
int,
double,
double,
int ) ) );
78 for (
int i = 1; i <= nBands; ++i )
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 );
91 mNumberOfEntriesSpinBox->setValue( 5 );
96 if ( mMinLineEdit->text().isEmpty() || mMaxLineEdit->text().isEmpty() )
98 mMinMaxWidget->
load();
101 on_mClassificationModeComboBox_currentIndexChanged( 0 );
103 resetClassifyButton();
114 colorRampShader->
setClip( mClipCheckBox->isChecked() );
118 int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
120 for (
int i = 0; i < topLevelItemCount; ++i )
122 currentItem = mColormapTreeWidget->topLevelItem( i );
130 newColorRampItem.
label = currentItem->
text( 2 );
131 colorRampItems.
append( newColorRampItem );
134 qSort( colorRampItems );
137 if ( mColorInterpolationComboBox->currentText() ==
tr(
"Linear" ) )
141 else if ( mColorInterpolationComboBox->currentText() ==
tr(
"Discrete" ) )
151 int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
160 void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
165 newItem->
setText( 2,
tr(
"Custom color map entry" ) );
168 void QgsSingleBandPseudoColorRendererWidget::on_mDeleteEntryButton_clicked()
177 void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
179 bool inserted =
false;
180 int myCurrentIndex = 0;
181 int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
184 for (
int i = 0; i < myTopLevelItemCount; ++i )
186 myCurrentItem = mColormapTreeWidget->topLevelItem( i );
188 if ( !myCurrentItem || myCurrentItem->
text( 0 ) ==
"" )
197 myNewColorRampItem.
label = myCurrentItem->
text( 2 );
204 if ( 0 == myColorRampItems.
size() || myCurrentIndex == myColorRampItems.
size() )
206 myColorRampItems.
push_back( myNewColorRampItem );
209 else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.
value )
211 myColorRampItems.
insert( myCurrentIndex, myNewColorRampItem );
214 else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.
value && myCurrentIndex == myColorRampItems.
size() - 1 )
216 myColorRampItems.
push_back( myNewColorRampItem );
219 else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.
value && myColorRampItems[myCurrentIndex+1].
value > myNewColorRampItem.
value )
221 myColorRampItems.
insert( myCurrentIndex + 1, myNewColorRampItem );
227 populateColormapTreeWidget( myColorRampItems );
230 void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
232 int bandComboIndex = mBandComboBox->currentIndex();
240 int numberOfEntries = 0;
245 double min = lineEditValue( mMinLineEdit );
246 double max = lineEditValue( mMaxLineEdit );
250 if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() ==
Continuous )
254 numberOfEntries = colorRamp->
count();
256 for (
int i = 0; i < colorRamp->
count(); ++i )
258 double value = colorRamp->
value( i );
259 entryValues.
push_back( min + value * ( max - min ) );
265 numberOfEntries = mNumberOfEntriesSpinBox->value();
267 double currentValue =
min;
269 if ( numberOfEntries > 1 )
274 intervalDiff = ( max -
min ) / ( numberOfEntries - 1 );
279 intervalDiff = max -
min;
282 entryValues.
reserve( numberOfEntries );
283 for (
int i = 0; i < numberOfEntries; ++i )
286 currentValue += intervalDiff;
293 if ( numberOfEntries != 0 )
295 colorDiff = ( int )( 255 / numberOfEntries );
297 for (
int i = 0; i < numberOfEntries; ++i )
300 currentColor.
setRgb( colorDiff*i, 0, 255 - colorDiff * i );
309 if ( numberOfEntries != 0 )
311 colorDiff = ( int )( 255 / numberOfEntries );
314 entryColors.
reserve( numberOfEntries );
315 for (
int i = 0; i < numberOfEntries; ++i )
318 int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
319 currentColor.
setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
325 entryColors.
reserve( numberOfEntries );
326 for (
int i = 0; i < numberOfEntries; ++i )
328 int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
329 entryColors.
push_back( colorRamp->
color(((
double ) idx ) / ( numberOfEntries - 1 ) ) );
333 mColormapTreeWidget->clear();
338 for ( ; value_it != entryValues.
end(); ++value_it, ++color_it )
344 newItem->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
348 void QgsSingleBandPseudoColorRendererWidget::on_mClassificationModeComboBox_currentIndexChanged(
int index )
350 mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() ==
EqualInterval );
353 void QgsSingleBandPseudoColorRendererWidget::on_mColorRampComboBox_currentIndexChanged(
int index )
357 settings.
setValue(
"/Raster/defaultPalette", mColorRampComboBox->currentText() );
363 bool enableContinuous = ( ramp->
count() > 0 );
364 mClassificationModeComboBox->setEnabled( enableContinuous );
365 if ( !enableContinuous )
367 mClassificationModeComboBox->setCurrentIndex( mClassificationModeComboBox->findData(
EqualInterval ) );
373 mColormapTreeWidget->clear();
375 for ( ; it != colorRampItems.
constEnd(); ++it )
380 newItem->
setText( 2, it->label );
384 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
391 int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
395 if ( colorRampList.
size() > 0 )
397 populateColormapTreeWidget( colorRampList );
398 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
402 QMessageBox::warning(
this,
tr(
"Load Color Map" ),
tr(
"The color map for band %1 has no entries" ).arg( bandIndex ) );
406 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
409 bool importError =
false;
414 QFile inputFile( fileName );
415 if ( inputFile.open( QFile::ReadOnly ) )
418 mColormapTreeWidget->clear();
426 while ( !inputStream.atEnd() )
429 inputLine = inputStream.readLine();
434 if ( inputLine.
contains(
"INTERPOLATION", Qt::CaseInsensitive ) )
436 inputStringComponents = inputLine.
split(
":" );
437 if ( inputStringComponents.
size() == 2 )
439 if ( inputStringComponents[1].trimmed().toUpper().compare(
"INTERPOLATED", Qt::CaseInsensitive ) == 0 )
441 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
443 else if ( inputStringComponents[1].trimmed().toUpper().compare(
"DISCRETE", Qt::CaseInsensitive ) == 0 )
445 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Discrete" ) ) );
449 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Exact" ) ) );
455 badLines = badLines +
QString::number( lineCounter ) +
":\t[" + inputLine +
"]\n";
460 inputStringComponents = inputLine.
split(
"," );
461 if ( inputStringComponents.
size() == 6 )
464 QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
465 inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
466 inputStringComponents[5] );
472 badLines = badLines +
QString::number( lineCounter ) +
":\t[" + inputLine +
"]\n";
479 populateColormapTreeWidget( colorRampItems );
486 else if ( !fileName.
isEmpty() )
488 QMessageBox::warning(
this,
tr(
"Read access denied" ),
tr(
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
492 void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
499 if ( !fileName.
endsWith(
".txt", Qt::CaseInsensitive ) )
501 fileName = fileName +
".txt";
504 QFile outputFile( fileName );
505 if ( outputFile.open( QFile::WriteOnly ) )
508 outputStream <<
"# " <<
tr(
"QGIS Generated Color Map Export File" ) <<
"\n";
509 outputStream <<
"INTERPOLATION:";
510 if ( mColorInterpolationComboBox->currentText() ==
tr(
"Linear" ) )
512 outputStream <<
"INTERPOLATED\n";
514 else if ( mColorInterpolationComboBox->currentText() ==
tr(
"Discrete" ) )
516 outputStream <<
"DISCRETE\n";
520 outputStream <<
"EXACT\n";
523 int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
526 for (
int i = 0; i < topLevelItemCount; ++i )
528 currentItem = mColormapTreeWidget->topLevelItem( i );
534 outputStream << currentItem->
text( 0 ).
toDouble() <<
",";
535 outputStream << color.
red() <<
"," << color.
green() <<
"," << color.
blue() <<
"," << color.
alpha() <<
",";
536 if ( currentItem->
text( 2 ) ==
"" )
538 outputStream <<
"Color entry " << i + 1 <<
"\n";
542 outputStream << currentItem->
text( 2 ) <<
"\n";
545 outputStream.flush();
550 QMessageBox::warning(
this,
tr(
"Write access denied" ),
tr(
"Write access denied. Adjust the file permissions and try again.\n\n" ) );
555 void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked(
QTreeWidgetItem* item,
int column )
564 item->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
573 item->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
582 mBandComboBox->setCurrentIndex( mBandComboBox->findData( pr->
band() ) );
588 if ( colorRampShader )
592 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
596 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Discrete" ) ) );
600 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Exact" ) ) );
605 for ( ; it != colorRampItemList.
end(); ++it )
610 newItem->
setText( 2, it->label );
612 mClipCheckBox->setChecked( colorRampShader->
clip() );
622 void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged(
int index )
625 myBands.
append( mBandComboBox->itemData( index ).toInt() );
631 Q_UNUSED( theBandNo );
632 QgsDebugMsg(
QString(
"theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
634 if ( qIsNaN( theMin ) )
636 mMinLineEdit->clear();
643 if ( qIsNaN( theMax ) )
645 mMaxLineEdit->clear();
652 mMinMaxOrigin = theOrigin;
656 void QgsSingleBandPseudoColorRendererWidget::showMinMaxOrigin()
661 void QgsSingleBandPseudoColorRendererWidget::setLineEditValue(
QLineEdit * theLineEdit,
double theValue )
664 if ( !qIsNaN( theValue ) )
671 double QgsSingleBandPseudoColorRendererWidget::lineEditValue(
const QLineEdit * theLineEdit )
const
673 if ( theLineEdit->
text().isEmpty() )
675 return std::numeric_limits<double>::quiet_NaN();
678 return theLineEdit->
text().toDouble();
681 void QgsSingleBandPseudoColorRendererWidget::resetClassifyButton()
683 mClassifyButton->setEnabled(
true );
684 double min = lineEditValue( mMinLineEdit );
685 double max = lineEditValue( mMaxLineEdit );
686 if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
688 mClassifyButton->setEnabled(
false );
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Interface for all raster shaders.
void setContentsMargins(int left, int top, int right, int bottom)
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.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
int classificationMinMaxOrigin() const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
void setClassificationMinMaxOrigin(int origin)
QString simplified() const
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &theList)
Set custom colormap.
void setRgb(int r, int g, int b, int a)
double toDouble(bool *ok) const
QgsRasterShader * shader()
QString tr(const char *sourceText, const char *disambiguation, int n)
void setClassificationMax(double max)
const QColor & color() const
void setValue(const QString &key, const QVariant &value)
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Get the custom colormap.
QString number(int n, int base)
void append(const T &value)
QgsColorRampShader::ColorRamp_TYPE colorRampType() const
Get the color ramp type.
QgsRasterRenderer * renderer() const
virtual double value(int index) const =0
Returns relative value between [0,1] of color at specified index.
int band() const
Returns the band used by the renderer.
QgsRasterShaderFunction * rasterShaderFunction()
static QgsStyleV2 * defaultStyle()
return default application-wide style
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.
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
virtual QColor color(double value) const =0
bool contains(QChar ch, Qt::CaseSensitivity cs) const
double classificationMax() const
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 setClassificationMin(double min)
void insert(int i, const T &value)
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)
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 text(int column) const
double classificationMin() const
Raster renderer pipe that applies colors to a raster.
Base class for raster data providers.