QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsrasterbandcombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterbandcombobox.cpp
3 -------------------------
4 begin : May 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsrasterbandcombobox.cpp"
18#include "qgsrasterlayer.h"
20
22 : QComboBox( parent )
23 , mNotSetString( tr( "Not set" ) )
24{
25 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
26 {
27 if ( mLayer && mLayer->isValid() )
28 {
29 const int newBand = currentIndex() >= 0 ? currentData().toInt() : -1 ;
30 if ( newBand != mPrevBand )
31 {
32 emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
33 mPrevBand = newBand;
34 }
35 }
36 } );
37
38 connect( this, &QComboBox::currentTextChanged, this, [ = ]( const QString & value )
39 {
40 if ( !mLayer || !mLayer->isValid() )
41 {
42 bool ok = false;
43 const int band = value.toInt( &ok );
44 if ( ok && band != mPrevBand )
45 {
46 emit bandChanged( band );
47 mPrevBand = band;
48 }
49 else if ( mShowNotSet && mPrevBand != -1 )
50 {
51 emit bandChanged( -1 );
52 mPrevBand = -1;
53 }
54 }
55 } );
56
57 // default to editable, until a layer is set
58 setEditable( true );
59}
60
62{
63 return mLayer;
64}
65
67{
68 if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
69 {
70 bool ok = false;
71 const int band = currentText().toInt( &ok );
72 if ( ok )
73 return band;
74 return -1;
75 }
76 else
77 {
78 if ( currentIndex() < 0 )
79 return -1;
80
81 return currentData().toInt();
82 }
83}
84
86{
87 const int oldBand = currentBand();
88
89 QgsRasterLayer *rl = qobject_cast< QgsRasterLayer * >( layer );
90 mLayer = rl;
91
92 blockSignals( true );
93 clear();
94
95 if ( mShowNotSet )
96 addItem( mNotSetString, -1 );
97
98 if ( mLayer )
99 {
100 QgsRasterDataProvider *provider = mLayer->dataProvider();
101 if ( provider && mLayer->isValid() )
102 {
103 setEditable( false );
104 //fill available bands into combo box
105 const int nBands = provider->bandCount();
106 for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
107 {
108 addItem( displayBandName( provider, i ), i );
109 }
110 }
111 else
112 {
113 setEditable( true );
114 }
115 }
116 else
117 {
118 setEditable( true );
119 }
120
121 if ( oldBand >= 0 )
122 setBand( oldBand );
123 else
124 setCurrentIndex( 0 );
125
126 blockSignals( false );
127 const int newBand = currentBand();
128 //if ( newBand != oldBand )
129 // emit bandChanged( newBand );
130 mPrevBand = newBand;
131}
132
134{
135 if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
136 {
137 if ( band < 0 )
138 {
139 setCurrentIndex( -1 );
140 if ( mPrevBand != -1 )
141 emit bandChanged( -1 );
142 }
143 else
144 setCurrentText( QString::number( band ) );
145 }
146 else
147 {
148 setCurrentIndex( findData( band ) );
149 }
150 mPrevBand = band;
151}
152
154{
155 return mShowNotSet;
156}
157
158void QgsRasterBandComboBox::setShowNotSetOption( bool show, const QString &string )
159{
160 mShowNotSet = show;
161 mNotSetString = string.isEmpty() ? tr( "Not set" ) : string;
162 setLayer( mLayer );
163}
164
166{
167 if ( !provider )
168 return QString();
169
170 QString name { provider->displayBandName( band ) };
171 const QString description { provider->bandDescription( band ) };
172 // displayBandName() includes band description and this description can be the same
173 // as a band description from the metadata, so let's not append description to the band
174 // name if it is already there
175 if ( !description.isEmpty() )
176 {
177 return name.contains( description, Qt::CaseInsensitive ) ? name : QStringLiteral( "%1 - %2" ).arg( name, description );
178 }
179 return name;
180}
Base class for all map layer types.
Definition qgsmaplayer.h:76
QgsRasterLayer * layer() const
Returns the layer currently associated with the combobox.
void bandChanged(int band)
Emitted when the currently selected band changes.
void setShowNotSetOption(bool show, const QString &string=QString())
Sets whether the combo box should show the "not set" option.
int currentBand() const
Returns the current band number selected in the combobox, or -1 if no band is selected.
QgsRasterBandComboBox(QWidget *parent=nullptr)
Constructor for QgsRasterBandComboBox.
void setLayer(QgsMapLayer *layer)
Sets the raster layer for which the bands are listed in the combobox.
static QString displayBandName(QgsRasterDataProvider *provider, int band)
Returns a user-friendly band name for the specified band.
bool isShowingNotSetOption() const
Returns true if the combo box is showing the "not set" option.
void setBand(int band)
Sets the current band number selected in the combobox.
Base class for raster data providers.
virtual QString bandDescription(int bandNumber)
Returns the description for band bandNumber, or an empty string if the band is not valid or has not d...
virtual int bandCount() const =0
Gets number of bands.
QString displayBandName(int bandNumber) const
Generates a friendly, descriptive name for the specified bandNumber.
Represents a raster layer.