Skip to content
Permalink
Browse files
Add a new QgsRasterBandComboBox widget
And use wherever custom widgets were being created. Reduces
duplicate code and allows specialised handling for raster
band display/selection to be centralized.
  • Loading branch information
nyalldawson committed May 9, 2017
1 parent 3cd3e5d commit a32314d8b5f8c55510a0a095bf00999812da6059
@@ -1766,11 +1766,18 @@ QgsRasterProjector {#qgis_api_break_3_0_QgsRasterProjector}

- extentSize() now takes a QgsCoordinateTransform reference, not a pointer. An invalid QgsCoordinateTransform should be used instead of a null pointer if no transformation is required.

QgsRasterRenderer
QgsRasterRenderer {#qgis_api_break_3_0_QgsRasterRenderer}
-----------------

- MinMaxOrigin enum, minMaxOriginName(), minMaxOriginLabel(), minMaxOriginFromName() removed. Use minMaxOrigin() instead


QgsRasterRendererWidget {#qgis_api_break_3_0_QgsRasterRendererWidget}
-----------------

- displayBandName() was removed.


QgsRectangle {#qgis_api_break_3_0_QgsRectangle}
------------

@@ -212,6 +212,7 @@

%Include raster/qgsmultibandcolorrendererwidget.sip
%Include raster/qgspalettedrendererwidget.sip
%Include raster/qgsrasterbandcombobox.sip
%Include raster/qgsrasterhistogramwidget.sip
%Include raster/qgsrasterminmaxwidget.sip
%Include raster/qgsrasterrendererwidget.sip
@@ -0,0 +1,74 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/raster/qgsrasterbandcombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsRasterBandComboBox : QComboBox
{
%Docstring
A combobox widget which displays the bands present in a raster layer.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsrasterbandcombobox.h"
%End
public:

QgsRasterBandComboBox( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsRasterBandComboBox.
%End

QgsRasterLayer *layer() const;
%Docstring
Returns the layer currently associated with the combobox.
.. seealso:: setLayer()
:rtype: QgsRasterLayer
%End

int currentBand() const;
%Docstring
Returns the current band number selected in the combobox, or -1
if no band is selected.
.. seealso:: setBand()
:rtype: int
%End

public slots:

void setLayer( QgsMapLayer *layer );
%Docstring
Sets the raster ``layer`` for which the bands are listed in the combobox. If no layer is set
or a non-raster layer is set then the combobox will be empty.
.. seealso:: layer()
%End

void setBand( int band );
%Docstring
Sets the current ``band`` number selected in the combobox.
.. seealso:: band()
%End

signals:

void bandChanged( int band );
%Docstring
This signal is emitted when the currently selected band changes.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/raster/qgsrasterbandcombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -44,8 +44,4 @@ class QgsRasterRendererWidget: QWidget
*/
void widgetChanged();


protected:
/** Returns a band name for display. First choice is color name, otherwise band number*/
QString displayBandName( int band ) const;
};
@@ -1,6 +1,7 @@
SET(QGIS_GUI_SRCS
raster/qgsmultibandcolorrendererwidget.cpp
raster/qgspalettedrendererwidget.cpp
raster/qgsrasterbandcombobox.cpp
raster/qgsrasterhistogramwidget.cpp
raster/qgsrasterminmaxwidget.cpp
raster/qgsrasterrendererwidget.cpp
@@ -460,6 +461,7 @@ SET(QGIS_GUI_MOC_HDRS

raster/qgsmultibandcolorrendererwidget.h
raster/qgspalettedrendererwidget.h
raster/qgsrasterbandcombobox.h
raster/qgsrasterhistogramwidget.h
raster/qgsrasterminmaxwidget.h
raster/qgsrasterrendererwidget.h
@@ -41,23 +41,7 @@ QgsHillshadeRendererWidget::QgsHillshadeRendererWidget( QgsRasterLayer *layer, c
mZFactor->setClearValue( 1 );

mMultiDirection->setChecked( false );

if ( mRasterLayer )
{
QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
if ( !provider )
{
return;
}

//fill available bands into combo box
int nBands = provider->bandCount();
for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
{
mBandsCombo->addItem( displayBandName( i ), i );
}

}
mBandsCombo->setLayer( mRasterLayer );

setFromRenderer( layer->renderer() );

@@ -66,6 +50,7 @@ QgsHillshadeRendererWidget::QgsHillshadeRendererWidget( QgsRasterLayer *layer, c
connect( mLightAzimuthDial, &QAbstractSlider::valueChanged, this, &QgsHillshadeRendererWidget::on_mLightAzimuthDail_updated );
connect( mZFactor, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsRasterRendererWidget::widgetChanged );
connect( mMultiDirection, &QAbstractButton::toggled, this, &QgsRasterRendererWidget::widgetChanged );
connect( mBandsCombo, &QgsRasterBandComboBox::bandChanged, this, &QgsHillshadeRendererWidget::widgetChanged );
}

QgsRasterRenderer *QgsHillshadeRendererWidget::renderer()
@@ -81,7 +66,7 @@ QgsRasterRenderer *QgsHillshadeRendererWidget::renderer()
return nullptr;
}

int band = mBandsCombo->currentData().toInt();
int band = mBandsCombo->currentBand();
QgsHillshadeRenderer *renderer = new QgsHillshadeRenderer( provider, band, mLightAzimuth->value(), mLightAngle->value() );
double value = mZFactor->value();
renderer->setZFactor( value );
@@ -94,7 +79,7 @@ void QgsHillshadeRendererWidget::setFromRenderer( const QgsRasterRenderer *rende
const QgsHillshadeRenderer *r = dynamic_cast<const QgsHillshadeRenderer *>( renderer );
if ( r )
{
mBandsCombo->setCurrentIndex( mBandsCombo->findData( r->band() ) );
mBandsCombo->setBand( r->band() );
mLightAngle->setValue( r->altitude() );
mLightAzimuth->setValue( r->azimuth() );
mZFactor->setValue( r->zFactor() );
@@ -50,33 +50,28 @@ QgsMultiBandColorRendererWidget::QgsMultiBandColorRendererWidget( QgsRasterLayer
connect( mMinMaxWidget, &QgsRasterMinMaxWidget::load,
this, &QgsMultiBandColorRendererWidget::loadMinMax );

connect( mRedBandComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
connect( mRedBandComboBox, &QgsRasterBandComboBox::bandChanged,
this, &QgsMultiBandColorRendererWidget::onBandChanged );
connect( mGreenBandComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
connect( mGreenBandComboBox, &QgsRasterBandComboBox::bandChanged,
this, &QgsMultiBandColorRendererWidget::onBandChanged );
connect( mBlueBandComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
connect( mBlueBandComboBox, &QgsRasterBandComboBox::bandChanged,
this, &QgsMultiBandColorRendererWidget::onBandChanged );

mRedBandComboBox->setLayer( mRasterLayer );
mGreenBandComboBox->setLayer( mRasterLayer );
mBlueBandComboBox->setLayer( mRasterLayer );

//fill available bands into combo boxes
mRedBandComboBox->addItem( tr( "Not set" ), -1 );
mGreenBandComboBox->addItem( tr( "Not set" ), -1 );
mBlueBandComboBox->addItem( tr( "Not set" ), -1 );
mRedBandComboBox->insertItem( 0, tr( "Not set" ), -1 );
mGreenBandComboBox->insertItem( 0, tr( "Not set" ), -1 );
mBlueBandComboBox->insertItem( 0, tr( "Not set" ), -1 );

//contrast enhancement algorithms
mContrastEnhancementAlgorithmComboBox->addItem( tr( "No enhancement" ), QgsContrastEnhancement::NoEnhancement );
mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch and clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
mContrastEnhancementAlgorithmComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );

int nBands = provider->bandCount();
for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
{
QString bandName = displayBandName( i );
mRedBandComboBox->addItem( bandName, i );
mGreenBandComboBox->addItem( bandName, i );
mBlueBandComboBox->addItem( bandName, i );
}

setFromRenderer( mRasterLayer->renderer() );
onBandChanged( 0 ); // reset mMinMaxWidget bands

@@ -96,9 +91,9 @@ QgsRasterRenderer *QgsMultiBandColorRendererWidget::renderer()
return nullptr;
}

int redBand = mRedBandComboBox->currentData().toInt();
int greenBand = mGreenBandComboBox->currentData().toInt();
int blueBand = mBlueBandComboBox->currentData().toInt();
int redBand = mRedBandComboBox->currentBand();
int greenBand = mGreenBandComboBox->currentBand();
int blueBand = mBlueBandComboBox->currentBand();

QgsMultiBandColorRenderer *r = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
setCustomMinMaxValues( r, provider, redBand, greenBand, blueBand );
@@ -209,9 +204,9 @@ void QgsMultiBandColorRendererWidget::onBandChanged( int index )
Q_UNUSED( index );

QList<int> myBands;
myBands.append( mRedBandComboBox->currentData().toInt() );
myBands.append( mGreenBandComboBox->currentData().toInt() );
myBands.append( mBlueBandComboBox->currentData().toInt() );
myBands.append( mRedBandComboBox->currentBand() );
myBands.append( mGreenBandComboBox->currentBand() );
myBands.append( mBlueBandComboBox->currentBand() );
mMinMaxWidget->setBands( myBands );
emit widgetChanged();
}
@@ -266,17 +261,17 @@ void QgsMultiBandColorRendererWidget::loadMinMax( int bandNo, double min, double

QLineEdit *myMinLineEdit, *myMaxLineEdit;

if ( mRedBandComboBox->currentData().toInt() == bandNo )
if ( mRedBandComboBox->currentBand() == bandNo )
{
myMinLineEdit = mRedMinLineEdit;
myMaxLineEdit = mRedMaxLineEdit;
}
else if ( mGreenBandComboBox->currentData().toInt() == bandNo )
else if ( mGreenBandComboBox->currentBand() == bandNo )
{
myMinLineEdit = mGreenMinLineEdit;
myMaxLineEdit = mGreenMaxLineEdit;
}
else if ( mBlueBandComboBox->currentData().toInt() == bandNo )
else if ( mBlueBandComboBox->currentBand() == bandNo )
{
myMinLineEdit = mBlueMinLineEdit;
myMaxLineEdit = mBlueMaxLineEdit;
@@ -336,9 +331,9 @@ void QgsMultiBandColorRendererWidget::setFromRenderer( const QgsRasterRenderer *
const QgsMultiBandColorRenderer *mbcr = dynamic_cast<const QgsMultiBandColorRenderer *>( r );
if ( mbcr )
{
mRedBandComboBox->setCurrentIndex( mRedBandComboBox->findData( mbcr->redBand() ) );
mGreenBandComboBox->setCurrentIndex( mGreenBandComboBox->findData( mbcr->greenBand() ) );
mBlueBandComboBox->setCurrentIndex( mBlueBandComboBox->findData( mbcr->blueBand() ) );
mRedBandComboBox->setBand( mbcr->redBand() );
mGreenBandComboBox->setBand( mbcr->greenBand() );
mBlueBandComboBox->setBand( mbcr->blueBand() );

mDisableMinMaxWidgetRefresh = true;
setMinMaxValue( mbcr->redContrastEnhancement(), mRedMinLineEdit, mRedMaxLineEdit );
@@ -443,11 +438,11 @@ int QgsMultiBandColorRendererWidget::selectedBand( int index )
switch ( index )
{
case 0:
return mRedBandComboBox->currentIndex();
return mRedBandComboBox->currentBand();
case 1:
return mGreenBandComboBox->currentIndex();
return mGreenBandComboBox->currentBand();
case 2:
return mBlueBandComboBox->currentIndex();
return mBlueBandComboBox->currentBand();
default:
break;
}
@@ -72,25 +72,19 @@ QgsPalettedRendererWidget::QgsPalettedRendererWidget( QgsRasterLayer *layer, con

connect( btnColorRamp, &QgsColorRampButton::colorRampChanged, this, &QgsPalettedRendererWidget::applyColorRamp );

mBandComboBox->setLayer( mRasterLayer );

if ( mRasterLayer )
{
QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
if ( !provider )
{
return;
}

//fill available bands into combo box
int nBands = provider->bandCount();
for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
{
mBandComboBox->addItem( displayBandName( i ), i );
}

setFromRenderer( mRasterLayer->renderer() );
connect( mBandComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
}

connect( mBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsRasterRendererWidget::widgetChanged );
connect( mModel, &QgsPalettedRendererModel::classesChanged, this, &QgsPalettedRendererWidget::widgetChanged );
connect( mDeleteEntryButton, &QPushButton::clicked, this, &QgsPalettedRendererWidget::deleteEntry );
connect( mAddEntryButton, &QPushButton::clicked, this, &QgsPalettedRendererWidget::addEntry );
@@ -102,15 +96,15 @@ QgsPalettedRendererWidget::QgsPalettedRendererWidget( QgsRasterLayer *layer, con
QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
if ( provider )
{
mButtonLoadFromLayer->setEnabled( !provider->colorTable( mBandComboBox->currentData().toInt() ).isEmpty() );
mButtonLoadFromLayer->setEnabled( !provider->colorTable( mBandComboBox->currentBand() ).isEmpty() );
}
else
{
mButtonLoadFromLayer->setEnabled( false );
}

connect( QgsProject::instance(), static_cast < void ( QgsProject::* )( QgsMapLayer * ) >( &QgsProject::layerWillBeRemoved ), this, &QgsPalettedRendererWidget::layerWillBeRemoved );
connect( mBandComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPalettedRendererWidget::loadFromLayer );
connect( mBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsPalettedRendererWidget::loadFromLayer );
}

QgsPalettedRendererWidget::~QgsPalettedRendererWidget()
@@ -125,7 +119,7 @@ QgsPalettedRendererWidget::~QgsPalettedRendererWidget()
QgsRasterRenderer *QgsPalettedRendererWidget::renderer()
{
QgsPalettedRasterRenderer::ClassData classes = mModel->classData();
int bandNumber = mBandComboBox->currentData().toInt();
int bandNumber = mBandComboBox->currentBand();

QgsPalettedRasterRenderer *r = new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, classes );
if ( !btnColorRamp->isNull() )
@@ -406,7 +400,7 @@ void QgsPalettedRendererWidget::classify()
return;
}

mGatherer = new QgsPalettedRendererClassGatherer( mRasterLayer, mBandComboBox->currentData().toInt(), mModel->classData(), btnColorRamp->colorRamp() );
mGatherer = new QgsPalettedRendererClassGatherer( mRasterLayer, mBandComboBox->currentBand(), btnColorRamp->colorRamp() );

connect( mGatherer, &QgsPalettedRendererClassGatherer::progressChanged, mCalculatingProgressBar, &QProgressBar::setValue );
mCalculatingProgressBar->show();
@@ -427,10 +421,10 @@ void QgsPalettedRendererWidget::loadFromLayer()
QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
if ( provider )
{
QList<QgsColorRampShader::ColorRampItem> table = provider->colorTable( mBandComboBox->currentData().toInt() );
QList<QgsColorRampShader::ColorRampItem> table = provider->colorTable( mBandComboBox->currentBand() );
if ( !table.isEmpty() )
{
QgsPalettedRasterRenderer::ClassData classes = QgsPalettedRasterRenderer::colorTableToClassData( provider->colorTable( mBandComboBox->currentData().toInt() ) );
QgsPalettedRasterRenderer::ClassData classes = QgsPalettedRasterRenderer::colorTableToClassData( provider->colorTable( mBandComboBox->currentBand() ) );
mModel->setClassData( classes );
emit widgetChanged();
}
@@ -450,7 +444,7 @@ void QgsPalettedRendererWidget::gathererThreadFinished()
{
mGatherer->deleteLater();
mGatherer = nullptr;
mClassifyButton->setText( tr( "Populate Values" ) );
mClassifyButton->setText( tr( "Add Unique Values" ) );
mClassifyButton->setEnabled( true );
mCalculatingProgressBar->hide();
mCancelButton->hide();

0 comments on commit a32314d

Please sign in to comment.