QGIS API Documentation  3.0.2-Girona (307d082)
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  emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
27  } );
28 }
29 
31 {
32  return mLayer;
33 }
34 
36 {
37  return currentIndex() >= 0 ? currentData().toInt() : -1;
38 }
39 
41 {
42  QgsRasterLayer *rl = qobject_cast< QgsRasterLayer * >( layer );
43  mLayer = rl;
44  int oldBand = currentBand();
45 
46  blockSignals( true );
47  clear();
48 
49  if ( mShowNotSet )
50  addItem( mNotSetString, -1 );
51 
52  if ( mLayer )
53  {
54  QgsRasterDataProvider *provider = mLayer->dataProvider();
55  if ( provider )
56  {
57  //fill available bands into combo box
58  int nBands = provider->bandCount();
59  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
60  {
61  addItem( displayBandName( provider, i ), i );
62  }
63  }
64  }
65 
66  if ( count() > 0 )
67  setCurrentIndex( findData( oldBand ) >= 0 ? findData( oldBand ) : 0 );
68 
69  blockSignals( false );
70  emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
71 }
72 
74 {
75  setCurrentIndex( findData( band ) );
76 }
77 
79 {
80  return mShowNotSet;
81 }
82 
83 void QgsRasterBandComboBox::setShowNotSetOption( bool show, const QString &string )
84 {
85  mShowNotSet = show;
86  mNotSetString = string.isEmpty() ? tr( "Not set" ) : string;
87  setLayer( mLayer );
88 }
89 
90 QString QgsRasterBandComboBox::displayBandName( QgsRasterDataProvider *provider, int band ) const
91 {
92  if ( !provider )
93  return QString();
94 
95  QString name = provider->generateBandName( band );
96 
97  QString colorInterp = provider->colorInterpretationName( band );
98  if ( colorInterp != QLatin1String( "Undefined" ) )
99  {
100  name.append( QStringLiteral( " (%1)" ).arg( colorInterp ) );
101  }
102  return name;
103 }
virtual int bandCount() const =0
Get 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:56
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.
void bandChanged(int band)
This signal is 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.