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 ) );
69 mMinMaxContainerWidget->setLayout( layout );
71 connect( mMinMaxWidget, SIGNAL( load(
int,
double,
double,
int ) ),
72 this, SLOT(
loadMinMax(
int,
double,
double,
int ) ) );
77 for (
int i = 1; i <= nBands; ++i )
82 mColorInterpolationComboBox->addItem(
tr(
"Discrete" ), 0 );
83 mColorInterpolationComboBox->addItem(
tr(
"Linear" ), 1 );
84 mColorInterpolationComboBox->addItem(
tr(
"Exact" ), 2 );
85 mColorInterpolationComboBox->setCurrentIndex( 1 );
86 mClassificationModeComboBox->addItem(
tr(
"Continuous" ),
Continuous );
87 mClassificationModeComboBox->addItem(
tr(
"Equal interval" ),
EqualInterval );
90 mNumberOfEntriesSpinBox->setValue( 5 );
95 if ( mMinLineEdit->text().isEmpty() || mMaxLineEdit->text().isEmpty() )
97 mMinMaxWidget->
load();
100 on_mClassificationModeComboBox_currentIndexChanged( 0 );
102 resetClassifyButton();
113 colorRampShader->
setClip( mClipCheckBox->isChecked() );
117 int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
119 for (
int i = 0; i < topLevelItemCount; ++i )
121 currentItem = mColormapTreeWidget->topLevelItem( i );
129 newColorRampItem.
label = currentItem->
text( 2 );
130 colorRampItems.
append( newColorRampItem );
133 qSort( colorRampItems );
136 if ( mColorInterpolationComboBox->currentText() ==
tr(
"Linear" ) )
140 else if ( mColorInterpolationComboBox->currentText() ==
tr(
"Discrete" ) )
150 int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
159 void QgsSingleBandPseudoColorRendererWidget::on_mAddEntryButton_clicked()
164 newItem->
setText( 2,
tr(
"Custom color map entry" ) );
167 void QgsSingleBandPseudoColorRendererWidget::on_mDeleteEntryButton_clicked()
176 void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked()
178 bool inserted =
false;
179 int myCurrentIndex = 0;
180 int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount();
183 for (
int i = 0; i < myTopLevelItemCount; ++i )
185 myCurrentItem = mColormapTreeWidget->topLevelItem( i );
187 if ( !myCurrentItem || myCurrentItem->
text( 0 ) ==
"" )
196 myNewColorRampItem.
label = myCurrentItem->
text( 2 );
203 if ( 0 == myColorRampItems.
size() || myCurrentIndex == myColorRampItems.
size() )
205 myColorRampItems.
push_back( myNewColorRampItem );
208 else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.
value )
210 myColorRampItems.
insert( myCurrentIndex, myNewColorRampItem );
213 else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.
value && myCurrentIndex == myColorRampItems.
size() - 1 )
215 myColorRampItems.
push_back( myNewColorRampItem );
218 else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.
value && myColorRampItems[myCurrentIndex+1].
value > myNewColorRampItem.
value )
220 myColorRampItems.
insert( myCurrentIndex + 1, myNewColorRampItem );
226 populateColormapTreeWidget( myColorRampItems );
229 void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
231 int bandComboIndex = mBandComboBox->currentIndex();
239 int numberOfEntries = 0;
244 double min = lineEditValue( mMinLineEdit );
245 double max = lineEditValue( mMaxLineEdit );
249 if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() ==
Continuous )
253 numberOfEntries = colorRamp->
count();
254 for (
int i = 0; i < colorRamp->
count(); ++i )
256 double value = colorRamp->
value( i );
257 entryValues.
push_back( min + value * ( max - min ) );
263 numberOfEntries = mNumberOfEntriesSpinBox->value();
265 double currentValue =
min;
267 if ( numberOfEntries > 1 )
272 intervalDiff = ( max -
min ) / ( numberOfEntries - 1 );
277 intervalDiff = max -
min;
280 for (
int i = 0; i < numberOfEntries; ++i )
283 currentValue += intervalDiff;
290 if ( numberOfEntries != 0 )
292 colorDiff = ( int )( 255 / numberOfEntries );
294 for (
int i = 0; i < numberOfEntries; ++i )
297 currentColor.
setRgb( colorDiff*i, 0, 255 - colorDiff * i );
306 if ( numberOfEntries != 0 )
308 colorDiff = ( int )( 255 / numberOfEntries );
311 for (
int i = 0; i < numberOfEntries; ++i )
314 int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
315 currentColor.
setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
321 for (
int i = 0; i < numberOfEntries; ++i )
323 int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
324 entryColors.
push_back( colorRamp->
color(((
double ) idx ) / ( numberOfEntries - 1 ) ) );
328 mColormapTreeWidget->clear();
333 for ( ; value_it != entryValues.
end(); ++value_it, ++color_it )
339 newItem->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
343 void QgsSingleBandPseudoColorRendererWidget::on_mClassificationModeComboBox_currentIndexChanged(
int index )
345 mNumberOfEntriesSpinBox->setEnabled( mClassificationModeComboBox->itemData( index ).toInt() ==
EqualInterval );
348 void QgsSingleBandPseudoColorRendererWidget::on_mColorRampComboBox_currentIndexChanged(
int index )
352 settings.
setValue(
"/Raster/defaultPalette", mColorRampComboBox->currentText() );
358 bool enableContinuous = ( ramp->
count() > 0 );
359 mClassificationModeComboBox->setEnabled( enableContinuous );
360 if ( !enableContinuous )
362 mClassificationModeComboBox->setCurrentIndex( mClassificationModeComboBox->findData(
EqualInterval ) );
368 mColormapTreeWidget->clear();
370 for ( ; it != colorRampItems.
constEnd(); ++it )
375 newItem->
setText( 2, it->label );
379 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
386 int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
390 if ( colorRampList.
size() > 0 )
392 populateColormapTreeWidget( colorRampList );
393 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
397 QMessageBox::warning(
this,
tr(
"Load Color Map" ),
tr(
"The color map for band %1 has no entries" ).arg( bandIndex ) );
401 void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
404 bool importError =
false;
409 QFile inputFile( fileName );
410 if ( inputFile.open( QFile::ReadOnly ) )
413 mColormapTreeWidget->clear();
421 while ( !inputStream.atEnd() )
424 inputLine = inputStream.readLine();
429 if ( inputLine.
contains(
"INTERPOLATION", Qt::CaseInsensitive ) )
431 inputStringComponents = inputLine.
split(
":" );
432 if ( inputStringComponents.
size() == 2 )
434 if ( inputStringComponents[1].trimmed().toUpper().compare(
"INTERPOLATED", Qt::CaseInsensitive ) == 0 )
436 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
438 else if ( inputStringComponents[1].trimmed().toUpper().compare(
"DISCRETE", Qt::CaseInsensitive ) == 0 )
440 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Discrete" ) ) );
444 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Exact" ) ) );
450 badLines = badLines +
QString::number( lineCounter ) +
":\t[" + inputLine +
"]\n";
455 inputStringComponents = inputLine.
split(
"," );
456 if ( inputStringComponents.
size() == 6 )
459 QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
460 inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
461 inputStringComponents[5] );
467 badLines = badLines +
QString::number( lineCounter ) +
":\t[" + inputLine +
"]\n";
474 populateColormapTreeWidget( colorRampItems );
481 else if ( !fileName.
isEmpty() )
483 QMessageBox::warning(
this,
tr(
"Read access denied" ),
tr(
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
487 void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
494 if ( !fileName.
endsWith(
".txt", Qt::CaseInsensitive ) )
496 fileName = fileName +
".txt";
499 QFile outputFile( fileName );
500 if ( outputFile.open( QFile::WriteOnly ) )
503 outputStream <<
"# " <<
tr(
"QGIS Generated Color Map Export File" ) <<
"\n";
504 outputStream <<
"INTERPOLATION:";
505 if ( mColorInterpolationComboBox->currentText() ==
tr(
"Linear" ) )
507 outputStream <<
"INTERPOLATED\n";
509 else if ( mColorInterpolationComboBox->currentText() ==
tr(
"Discrete" ) )
511 outputStream <<
"DISCRETE\n";
515 outputStream <<
"EXACT\n";
518 int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
521 for (
int i = 0; i < topLevelItemCount; ++i )
523 currentItem = mColormapTreeWidget->topLevelItem( i );
529 outputStream << currentItem->
text( 0 ).
toDouble() <<
",";
530 outputStream << color.
red() <<
"," << color.
green() <<
"," << color.
blue() <<
"," << color.
alpha() <<
",";
531 if ( currentItem->
text( 2 ) ==
"" )
533 outputStream <<
"Color entry " << i + 1 <<
"\n";
537 outputStream << currentItem->
text( 2 ) <<
"\n";
540 outputStream.flush();
545 QMessageBox::warning(
this,
tr(
"Write access denied" ),
tr(
"Write access denied. Adjust the file permissions and try again.\n\n" ) );
550 void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked(
QTreeWidgetItem* item,
int column )
559 item->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
568 item->
setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
577 mBandComboBox->setCurrentIndex( mBandComboBox->findData( pr->
band() ) );
583 if ( colorRampShader )
587 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Linear" ) ) );
591 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Discrete" ) ) );
595 mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText(
tr(
"Exact" ) ) );
600 for ( ; it != colorRampItemList.
end(); ++it )
605 newItem->
setText( 2, it->label );
607 mClipCheckBox->setChecked( colorRampShader->
clip() );
617 void QgsSingleBandPseudoColorRendererWidget::on_mBandComboBox_currentIndexChanged(
int index )
620 myBands.
append( mBandComboBox->itemData( index ).toInt() );
626 Q_UNUSED( theBandNo );
627 QgsDebugMsg(
QString(
"theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
629 if ( qIsNaN( theMin ) )
631 mMinLineEdit->clear();
638 if ( qIsNaN( theMax ) )
640 mMaxLineEdit->clear();
647 mMinMaxOrigin = theOrigin;
651 void QgsSingleBandPseudoColorRendererWidget::showMinMaxOrigin()
656 void QgsSingleBandPseudoColorRendererWidget::setLineEditValue(
QLineEdit * theLineEdit,
double theValue )
659 if ( !qIsNaN( theValue ) )
666 double QgsSingleBandPseudoColorRendererWidget::lineEditValue(
const QLineEdit * theLineEdit )
const
668 if ( theLineEdit->
text().isEmpty() )
670 return std::numeric_limits<double>::quiet_NaN();
673 return theLineEdit->
text().toDouble();
676 void QgsSingleBandPseudoColorRendererWidget::resetClassifyButton()
678 mClassifyButton->setEnabled(
true );
679 double min = lineEditValue( mMinLineEdit );
680 double max = lineEditValue( mMaxLineEdit );
681 if ( qIsNaN( min ) || qIsNaN( max ) || min >= max )
683 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.