QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 if ( mLayer && mLayer->isValid() )
27 {
28 const int newBand = currentIndex() >= 0 ? currentData().toInt() : -1;
29 if ( newBand != mPrevBand )
30 {
31 emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
32 mPrevBand = newBand;
33 }
34 }
35 } );
36
37 connect( this, &QComboBox::currentTextChanged, this, [=]( const QString &value ) {
38 if ( !mLayer || !mLayer->isValid() )
39 {
40 bool ok = false;
41 const int band = value.toInt( &ok );
42 if ( ok && band != mPrevBand )
43 {
44 emit bandChanged( band );
45 mPrevBand = band;
46 }
47 else if ( mShowNotSet && mPrevBand != -1 )
48 {
49 emit bandChanged( -1 );
50 mPrevBand = -1;
51 }
52 }
53 } );
54
55 // default to editable, until a layer is set
56 setEditable( true );
57}
58
60{
61 return mLayer;
62}
63
65{
66 if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
67 {
68 bool ok = false;
69 const int band = currentText().toInt( &ok );
70 if ( ok )
71 return band;
72 return -1;
73 }
74 else
75 {
76 if ( currentIndex() < 0 )
77 return -1;
78
79 return currentData().toInt();
80 }
81}
82
84{
85 const int oldBand = currentBand();
86
87 QgsRasterLayer *rl = qobject_cast<QgsRasterLayer *>( layer );
88 mLayer = rl;
89
90 blockSignals( true );
91 clear();
92
93 if ( mShowNotSet )
94 addItem( mNotSetString, -1 );
95
96 if ( mLayer )
97 {
98 QgsRasterDataProvider *provider = mLayer->dataProvider();
99 if ( provider && mLayer->isValid() )
100 {
101 setEditable( false );
102 //fill available bands into combo box
103 const int nBands = provider->bandCount();
104 for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
105 {
106 addItem( displayBandName( provider, i ), i );
107 }
108 }
109 else
110 {
111 setEditable( true );
112 }
113 }
114 else
115 {
116 setEditable( true );
117 }
118
119 if ( oldBand >= 0 )
120 setBand( oldBand );
121 else
122 setCurrentIndex( 0 );
123
124 blockSignals( false );
125 const int newBand = currentBand();
126 //if ( newBand != oldBand )
127 // emit bandChanged( newBand );
128 mPrevBand = newBand;
129}
130
132{
133 if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
134 {
135 if ( band < 0 )
136 {
137 setCurrentIndex( -1 );
138 if ( mPrevBand != -1 )
139 emit bandChanged( -1 );
140 }
141 else
142 setCurrentText( QString::number( band ) );
143 }
144 else
145 {
146 setCurrentIndex( findData( band ) );
147 }
148 mPrevBand = band;
149}
150
152{
153 return mShowNotSet;
154}
155
156void QgsRasterBandComboBox::setShowNotSetOption( bool show, const QString &string )
157{
158 mShowNotSet = show;
159 mNotSetString = string.isEmpty() ? tr( "Not set" ) : string;
160 setLayer( mLayer );
161}
162
164{
165 if ( !provider )
166 return QString();
167
168 QString name { provider->displayBandName( band ) };
169 const QString description { provider->bandDescription( band ) };
170 // displayBandName() includes band description and this description can be the same
171 // as a band description from the metadata, so let's not append description to the band
172 // name if it is already there
173 if ( !description.isEmpty() )
174 {
175 return name.contains( description, Qt::CaseInsensitive ) ? name : QStringLiteral( "%1 - %2" ).arg( name, description );
176 }
177 return name;
178}
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.