QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
raster
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
18
#include "
qgsrasterdataprovider.h
"
19
#include "
qgsrasterlayer.h
"
20
21
#include <QString>
22
23
#include "moc_qgsrasterbandcombobox.cpp"
24
25
using namespace
Qt::StringLiterals;
26
27
QgsRasterBandComboBox::QgsRasterBandComboBox
( QWidget *parent )
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
67
QgsRasterLayer
*
QgsRasterBandComboBox::layer
()
const
68
{
69
return
mLayer;
70
}
71
72
int
QgsRasterBandComboBox::currentBand
()
const
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
91
void
QgsRasterBandComboBox::setLayer
(
QgsMapLayer
*
layer
)
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
139
void
QgsRasterBandComboBox::setBand
(
int
band
)
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
159
bool
QgsRasterBandComboBox::isShowingNotSetOption
()
const
160
{
161
return
mShowNotSet;
162
}
163
164
void
QgsRasterBandComboBox::setShowNotSetOption
(
bool
show,
const
QString &
string
)
165
{
166
mShowNotSet = show;
167
mNotSetString =
string
.isEmpty() ? tr(
"Not set"
) : string;
168
setLayer
( mLayer );
169
}
170
171
QString
QgsRasterBandComboBox::displayBandName
(
QgsRasterDataProvider
*provider,
int
band
)
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
}
QgsDataProvider::isValid
virtual bool isValid() const =0
Returns true if this is a valid layer.
QgsMapLayer
Base class for all map layer types.
Definition
qgsmaplayer.h:83
QgsRasterBandComboBox::layer
QgsRasterLayer * layer() const
Returns the layer currently associated with the combobox.
Definition
qgsrasterbandcombobox.cpp:67
QgsRasterBandComboBox::bandChanged
void bandChanged(int band)
Emitted when the currently selected band changes.
QgsRasterBandComboBox::setShowNotSetOption
void setShowNotSetOption(bool show, const QString &string=QString())
Sets whether the combo box should show the "not set" option.
Definition
qgsrasterbandcombobox.cpp:164
QgsRasterBandComboBox::band
int band
Definition
qgsrasterbandcombobox.h:37
QgsRasterBandComboBox::currentBand
int currentBand() const
Returns the current band number selected in the combobox, or -1 if no band is selected.
Definition
qgsrasterbandcombobox.cpp:72
QgsRasterBandComboBox::QgsRasterBandComboBox
QgsRasterBandComboBox(QWidget *parent=nullptr)
Constructor for QgsRasterBandComboBox.
Definition
qgsrasterbandcombobox.cpp:27
QgsRasterBandComboBox::setLayer
void setLayer(QgsMapLayer *layer)
Sets the raster layer for which the bands are listed in the combobox.
Definition
qgsrasterbandcombobox.cpp:91
QgsRasterBandComboBox::displayBandName
static QString displayBandName(QgsRasterDataProvider *provider, int band)
Returns a user-friendly band name for the specified band.
Definition
qgsrasterbandcombobox.cpp:171
QgsRasterBandComboBox::isShowingNotSetOption
bool isShowingNotSetOption() const
Returns true if the combo box is showing the "not set" option.
Definition
qgsrasterbandcombobox.cpp:159
QgsRasterBandComboBox::setBand
void setBand(int band)
Sets the current band number selected in the combobox.
Definition
qgsrasterbandcombobox.cpp:139
QgsRasterDataProvider
Base class for raster data providers.
Definition
qgsrasterdataprovider.h:94
QgsRasterDataProvider::bandDescription
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...
Definition
qgsrasterdataprovider.cpp:772
QgsRasterInterface::bandCount
virtual int bandCount() const =0
Gets number of bands.
QgsRasterInterface::displayBandName
QString displayBandName(int bandNumber) const
Generates a friendly, descriptive name for the specified bandNumber.
Definition
qgsrasterinterface.cpp:639
QgsRasterLayer
Represents a raster layer.
Definition
qgsrasterlayer.h:82
qgsrasterbandcombobox.h
qgsrasterdataprovider.h
qgsrasterlayer.h
Generated on
for QGIS API Documentation by
1.17.0