QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
16 #include "qgsrasterbandcombobox.h"
17 #include "qgsrasterlayer.h"
18 #include "qgsrasterdataprovider.h"
19 
21  : QComboBox( parent )
22  , mNotSetString( tr( "Not set" ) )
23 {
24  connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
25  {
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  {
39  if ( !mLayer || !mLayer->isValid() )
40  {
41  bool ok = false;
42  const int band = value.toInt( &ok );
43  if ( ok && band != mPrevBand )
44  {
45  emit bandChanged( band );
46  mPrevBand = band;
47  }
48  else if ( mShowNotSet && mPrevBand != -1 )
49  {
50  emit bandChanged( -1 );
51  mPrevBand = -1;
52  }
53  }
54  } );
55 
56  // default to editable, until a layer is set
57  setEditable( true );
58 }
59 
61 {
62  return mLayer;
63 }
64 
66 {
67  if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
68  {
69  bool ok = false;
70  const int band = currentText().toInt( &ok );
71  if ( ok )
72  return band;
73  return -1;
74  }
75  else
76  {
77  if ( currentIndex() < 0 )
78  return -1;
79 
80  return currentData().toInt();
81  }
82 }
83 
85 {
86  const int oldBand = currentBand();
87 
88  QgsRasterLayer *rl = qobject_cast< QgsRasterLayer * >( layer );
89  mLayer = rl;
90 
91  blockSignals( true );
92  clear();
93 
94  if ( mShowNotSet )
95  addItem( mNotSetString, -1 );
96 
97  if ( mLayer )
98  {
99  QgsRasterDataProvider *provider = mLayer->dataProvider();
100  if ( provider && mLayer->isValid() )
101  {
102  setEditable( false );
103  //fill available bands into combo box
104  int nBands = provider->bandCount();
105  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
106  {
107  addItem( displayBandName( provider, i ), i );
108  }
109  }
110  else
111  {
112  setEditable( true );
113  }
114  }
115  else
116  {
117  setEditable( true );
118  }
119 
120  if ( oldBand >= 0 )
121  setBand( oldBand );
122  else
123  setCurrentIndex( 0 );
124 
125  blockSignals( false );
126  const int newBand = currentBand();
127  //if ( newBand != oldBand )
128  // emit bandChanged( newBand );
129  mPrevBand = newBand;
130 }
131 
133 {
134  if ( !mLayer || !mLayer->dataProvider() || !mLayer->isValid() )
135  {
136  if ( band < 0 )
137  {
138  setCurrentIndex( -1 );
139  if ( mPrevBand != -1 )
140  emit bandChanged( -1 );
141  }
142  else
143  setCurrentText( QString::number( band ) );
144  }
145  else
146  {
147  setCurrentIndex( findData( band ) );
148  }
149  mPrevBand = band;
150 }
151 
153 {
154  return mShowNotSet;
155 }
156 
157 void QgsRasterBandComboBox::setShowNotSetOption( bool show, const QString &string )
158 {
159  mShowNotSet = show;
160  mNotSetString = string.isEmpty() ? tr( "Not set" ) : string;
161  setLayer( mLayer );
162 }
163 
164 QString QgsRasterBandComboBox::displayBandName( QgsRasterDataProvider *provider, int band ) const
165 {
166  if ( !provider )
167  return QString();
168 
169  QString name = provider->generateBandName( band );
170 
171  QString colorInterp = provider->colorInterpretationName( band );
172  if ( colorInterp != QLatin1String( "Undefined" ) )
173  {
174  name.append( QStringLiteral( " (%1)" ).arg( colorInterp ) );
175  }
176  return name;
177 }
virtual int bandCount() const =0
Gets number of bands.
int currentBand() const
Returns the current band number selected in the combobox, or -1 if no band is selected.
Base class for all map layer types.
Definition: qgsmaplayer.h:79
void setLayer(QgsMapLayer *layer)
Sets the raster layer for which the bands are listed in the combobox.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void setShowNotSetOption(bool show, const QString &string=QString())
Sets whether the combo box should show the "not set" option.
virtual bool isValid() const =0
Returns true if this is a valid layer.
void bandChanged(int band)
Emitted when the currently selected band changes.
QgsRasterBandComboBox(QWidget *parent=nullptr)
Constructor for QgsRasterBandComboBox.
virtual QString colorInterpretationName(int bandNo) const
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.
virtual QString generateBandName(int bandNumber) const
helper function to create zero padded band names
Base class for raster data providers.
QgsRasterLayer * layer() const
Returns the layer currently associated with the combobox.