Skip to content

Commit c9bfb9f

Browse files
committed
Add a "not set" option to QgsRasterBandComboBox
1 parent ddf8cc8 commit c9bfb9f

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

python/gui/raster/qgsrasterbandcombobox.sip

+15
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ class QgsRasterBandComboBox : QComboBox
4141
:rtype: int
4242
%End
4343

44+
bool isShowingNotSetOption() const;
45+
%Docstring
46+
Returns true if the combo box is showing the "not set" option.
47+
.. seealso:: setShowNotSetOption()
48+
:rtype: bool
49+
%End
50+
51+
void setShowNotSetOption( bool show, const QString &string = QString() );
52+
%Docstring
53+
Sets whether the combo box should show the "not set" option.
54+
Optionally the built in "not set" text can be overridden by specifying
55+
a ``string``.
56+
.. seealso:: setShowNotSetOption()
57+
%End
58+
4459
public slots:
4560

4661
void setLayer( QgsMapLayer *layer );

src/gui/raster/qgsmultibandcolorrendererwidget.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ QgsMultiBandColorRendererWidget::QgsMultiBandColorRendererWidget( QgsRasterLayer
5757
connect( mBlueBandComboBox, &QgsRasterBandComboBox::bandChanged,
5858
this, &QgsMultiBandColorRendererWidget::onBandChanged );
5959

60+
mRedBandComboBox->setShowNotSetOption( true );
61+
mGreenBandComboBox->setShowNotSetOption( true );
62+
mBlueBandComboBox->setShowNotSetOption( true );
6063
mRedBandComboBox->setLayer( mRasterLayer );
6164
mGreenBandComboBox->setLayer( mRasterLayer );
6265
mBlueBandComboBox->setLayer( mRasterLayer );
6366

64-
//fill available bands into combo boxes
65-
mRedBandComboBox->insertItem( 0, tr( "Not set" ), -1 );
66-
mGreenBandComboBox->insertItem( 0, tr( "Not set" ), -1 );
67-
mBlueBandComboBox->insertItem( 0, tr( "Not set" ), -1 );
68-
6967
//contrast enhancement algorithms
7068
mContrastEnhancementAlgorithmComboBox->addItem( tr( "No enhancement" ), QgsContrastEnhancement::NoEnhancement );
7169
mContrastEnhancementAlgorithmComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );

src/gui/raster/qgsrasterbandcombobox.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
QgsRasterBandComboBox::QgsRasterBandComboBox( QWidget *parent )
2121
: QComboBox( parent )
22+
, mNotSetString( tr( "Not set" ) )
2223
{
2324
connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
2425
{
@@ -43,6 +44,9 @@ void QgsRasterBandComboBox::setLayer( QgsMapLayer *layer )
4344
blockSignals( true );
4445
clear();
4546

47+
if ( mShowNotSet )
48+
addItem( mNotSetString, -1 );
49+
4650
if ( mLayer )
4751
{
4852
QgsRasterDataProvider *provider = mLayer->dataProvider();
@@ -70,6 +74,18 @@ void QgsRasterBandComboBox::setBand( int band )
7074
setCurrentIndex( findData( band ) );
7175
}
7276

77+
bool QgsRasterBandComboBox::isShowingNotSetOption() const
78+
{
79+
return mShowNotSet;
80+
}
81+
82+
void QgsRasterBandComboBox::setShowNotSetOption( bool show, const QString &string )
83+
{
84+
mShowNotSet = show;
85+
mNotSetString = string.isEmpty() ? tr( "Not set" ) : string;
86+
setLayer( mLayer );
87+
}
88+
7389
QString QgsRasterBandComboBox::displayBandName( QgsRasterDataProvider *provider, int band ) const
7490
{
7591
if ( !provider )

src/gui/raster/qgsrasterbandcombobox.h

+17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ class GUI_EXPORT QgsRasterBandComboBox : public QComboBox
5555
*/
5656
int currentBand() const;
5757

58+
/**
59+
* Returns true if the combo box is showing the "not set" option.
60+
* \see setShowNotSetOption()
61+
*/
62+
bool isShowingNotSetOption() const;
63+
64+
/**
65+
* Sets whether the combo box should show the "not set" option.
66+
* Optionally the built in "not set" text can be overridden by specifying
67+
* a \a string.
68+
* \see setShowNotSetOption()
69+
*/
70+
void setShowNotSetOption( bool show, const QString &string = QString() );
71+
5872
public slots:
5973

6074
/**
@@ -81,6 +95,9 @@ class GUI_EXPORT QgsRasterBandComboBox : public QComboBox
8195

8296
QPointer< QgsRasterLayer > mLayer;
8397

98+
bool mShowNotSet = false;
99+
QString mNotSetString;
100+
84101
QString displayBandName( QgsRasterDataProvider *provider, int band ) const;
85102

86103

0 commit comments

Comments
 (0)