QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
19#include "qgsrasterlayer.h"
20
21#include "moc_qgsrasterbandcombobox.cpp"
22
24 : QComboBox( parent )
25 , mNotSetString( tr( "Not set" ) )
26{
27 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [this] {
28 if ( mLayer && mLayer->isValid() )
29 {
30 const int newBand = currentIndex() >= 0 ? currentData().toInt() : -1;
31 if ( newBand != mPrevBand )
32 {
33 emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
34 mPrevBand = newBand;
35 }
36 }
37 } );
38
39 connect( this, &QComboBox::currentTextChanged, this, [this]( const QString &value ) {
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 setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
61}
62
64{
65 return mLayer;
66}
67
69{
70 if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
71 {
72 bool ok = false;
73 const int band = currentText().toInt( &ok );
74 if ( ok )
75 return band;
76 return -1;
77 }
78 else
79 {
80 if ( currentIndex() < 0 )
81 return -1;
82
83 return currentData().toInt();
84 }
85}
86
88{
89 const int oldBand = currentBand();
90
91 QgsRasterLayer *rl = qobject_cast<QgsRasterLayer *>( layer );
92 mLayer = rl;
93
94 blockSignals( true );
95 clear();
96
97 if ( mShowNotSet )
98 addItem( mNotSetString, -1 );
99
100 if ( mLayer )
101 {
102 QgsRasterDataProvider *provider = mLayer->dataProvider();
103 if ( provider && mLayer->isValid() )
104 {
105 setEditable( false );
106 //fill available bands into combo box
107 const int nBands = provider->bandCount();
108 for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
109 {
110 addItem( displayBandName( provider, i ), i );
111 }
112 }
113 else
114 {
115 setEditable( true );
116 }
117 }
118 else
119 {
120 setEditable( true );
121 }
122
123 if ( oldBand >= 0 )
124 setBand( oldBand );
125 else
126 setCurrentIndex( 0 );
127
128 blockSignals( false );
129 const int newBand = currentBand();
130 //if ( newBand != oldBand )
131 // emit bandChanged( newBand );
132 mPrevBand = newBand;
133}
134
136{
137 if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
138 {
139 if ( band < 0 )
140 {
141 setCurrentIndex( -1 );
142 if ( mPrevBand != -1 )
143 emit bandChanged( -1 );
144 }
145 else
146 setCurrentText( QString::number( band ) );
147 }
148 else
149 {
150 setCurrentIndex( findData( band ) );
151 }
152 mPrevBand = band;
153}
154
156{
157 return mShowNotSet;
158}
159
160void QgsRasterBandComboBox::setShowNotSetOption( bool show, const QString &string )
161{
162 mShowNotSet = show;
163 mNotSetString = string.isEmpty() ? tr( "Not set" ) : string;
164 setLayer( mLayer );
165}
166
168{
169 if ( !provider )
170 return QString();
171
172 QString name { provider->displayBandName( band ) };
173 const QString description { provider->bandDescription( band ) };
174 // displayBandName() includes band description and this description can be the same
175 // as a band description from the metadata, so let's not append description to the band
176 // name if it is already there
177 if ( !description.isEmpty() )
178 {
179 return name.contains( description, Qt::CaseInsensitive ) ? name : QStringLiteral( "%1 - %2" ).arg( name, description );
180 }
181 return name;
182}
virtual bool isValid() const =0
Returns true if this is a valid layer.
Base class for all map layer types.
Definition qgsmaplayer.h:80
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.