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