QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgssinglebandgrayrendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssinglebandgrayrendererwidget.h
3  ---------------------------------
4  begin : March 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
20 #include "qgsrasterlayer.h"
21 #include "qgsrasterdataprovider.h"
22 #include "qgsrasterminmaxwidget.h"
23 #include "qgsdoublevalidator.h"
24 
26  : QgsRasterRendererWidget( layer, extent )
27  , mDisableMinMaxWidgetRefresh( false )
28 {
29  setupUi( this );
30  connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged );
31  connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged );
32 
33  mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
34  mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
35 
36  mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
37  mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
38 
39  if ( mRasterLayer )
40  {
42  if ( !provider )
43  {
44  return;
45  }
46 
47  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
48  mMinMaxWidget->setExtent( extent );
49  mMinMaxWidget->setMapCanvas( mCanvas );
50 
51  QHBoxLayout *layout = new QHBoxLayout();
52  layout->setContentsMargins( 0, 0, 0, 0 );
53  mMinMaxContainerWidget->setLayout( layout );
54  layout->addWidget( mMinMaxWidget );
55 
56  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::widgetChanged,
58 
59  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::load,
61 
62  mGrayBandComboBox->setLayer( mRasterLayer );
63 
64  //contrast enhancement algorithms
65  mContrastEnhancementComboBox->addItem( tr( "No Enhancement" ), QgsContrastEnhancement::NoEnhancement );
66  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
67  mContrastEnhancementComboBox->addItem( tr( "Stretch and Clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
68  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
69 
70  setFromRenderer( layer->renderer() );
71 
72  connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
73  connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
74  connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
75  }
76 }
77 
79 {
80  if ( !mRasterLayer )
81  {
82  return nullptr;
83  }
85  if ( !provider )
86  {
87  return nullptr;
88  }
89  int band = mGrayBandComboBox->currentBand();
90 
92  provider->dataType( band ) ) );
93  e->setMinimumValue( QgsDoubleValidator::toDouble( mMinLineEdit->text() ) );
94  e->setMaximumValue( QgsDoubleValidator::toDouble( mMaxLineEdit->text() ) );
95  e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
96 
98  renderer->setContrastEnhancement( e );
99 
100  renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
101  renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
102 
103  return renderer;
104 }
105 
107 {
108  mMinMaxWidget->doComputations();
109 }
110 
112 {
114  mMinMaxWidget->setMapCanvas( canvas );
115 }
116 
117 void QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged( const QString & )
118 {
119  minMaxModified();
120 }
121 
122 void QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged( const QString & )
123 {
124  minMaxModified();
125 }
126 
127 void QgsSingleBandGrayRendererWidget::minMaxModified()
128 {
129  if ( !mDisableMinMaxWidgetRefresh )
130  {
131  if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
132  {
133  mContrastEnhancementComboBox->setCurrentIndex(
134  mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum ) );
135  }
136  mMinMaxWidget->userHasSetManualMinMaxValues();
137  emit widgetChanged();
138  }
139 }
140 
141 
142 void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
143 {
144  Q_UNUSED( bandNo )
145 
146  QgsDebugMsg( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
147 
148  mDisableMinMaxWidgetRefresh = true;
149  if ( std::isnan( min ) )
150  {
151  mMinLineEdit->clear();
152  }
153  else
154  {
155  mMinLineEdit->setText( QLocale().toString( min ) );
156  }
157 
158  if ( std::isnan( max ) )
159  {
160  mMaxLineEdit->clear();
161  }
162  else
163  {
164  mMaxLineEdit->setText( QLocale().toString( max ) );
165  }
166  mDisableMinMaxWidgetRefresh = false;
167 }
168 
169 void QgsSingleBandGrayRendererWidget::bandChanged()
170 {
171  QList<int> myBands;
172  myBands.append( mGrayBandComboBox->currentBand() );
173  mMinMaxWidget->setBands( myBands );
174  emit widgetChanged();
175 }
176 
178 {
179  const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
180  if ( gr )
181  {
182  //band
183  mGrayBandComboBox->setBand( gr->grayBand() );
184  mMinMaxWidget->setBands( QList< int >() << gr->grayBand() );
185  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
186 
188  if ( ce )
189  {
190  //minmax
191  mDisableMinMaxWidgetRefresh = true;
192  mMinLineEdit->setText( QLocale().toString( ce->minimumValue() ) );
193  mMaxLineEdit->setText( QLocale().toString( ce->maximumValue() ) );
194  mDisableMinMaxWidgetRefresh = false;
195  //contrast enhancement algorithm
196  mContrastEnhancementComboBox->setCurrentIndex(
197  mContrastEnhancementComboBox->findData( ( int )( ce->contrastEnhancementAlgorithm() ) ) );
198  }
199 
200  mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
201  }
202 }
203 
204 void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
205 {
206  mDisableMinMaxWidgetRefresh = true;
207  mMinLineEdit->setText( value );
208  mDisableMinMaxWidgetRefresh = false;
209 }
210 
211 void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
212 {
213  mDisableMinMaxWidgetRefresh = true;
214  mMaxLineEdit->setText( value );
215  mDisableMinMaxWidgetRefresh = false;
216 }
QgsContrastEnhancement::maximumValue
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
Definition: qgscontrastenhancement.h:152
QgsSingleBandGrayRenderer::WhiteToBlack
@ WhiteToBlack
Definition: qgssinglebandgrayrenderer.h:39
QgsRasterMinMaxWidget::userHasSetManualMinMaxValues
void userHasSetManualMinMaxValues()
Uncheck cumulative cut, min/max, std-dev radio buttons.
Definition: qgsrasterminmaxwidget.cpp:84
QgsRasterBandComboBox::bandChanged
void bandChanged(int band)
Emitted when the currently selected band changes.
Qgis::DataType
DataType
Raster data types.
Definition: qgis.h:102
QgsSingleBandGrayRenderer::BlackToWhite
@ BlackToWhite
Definition: qgssinglebandgrayrenderer.h:38
qgsrasterlayer.h
QgsContrastEnhancement::setContrastEnhancementAlgorithm
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
Definition: qgscontrastenhancement.cpp:132
QgsRasterDataProvider::dataType
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
QgsRasterMinMaxWidget::widgetChanged
void widgetChanged()
Emitted when something on the widget has changed.
QgsContrastEnhancement::minimumValue
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
Definition: qgscontrastenhancement.h:155
QgsSingleBandGrayRendererWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
Definition: qgssinglebandgrayrendererwidget.cpp:111
QgsRasterRenderer::minMaxOrigin
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
Definition: qgsrasterrenderer.h:136
QgsRasterRendererWidget::mRasterLayer
QgsRasterLayer * mRasterLayer
Definition: qgsrasterrendererwidget.h:92
QgsSingleBandGrayRenderer::grayBand
int grayBand() const
Definition: qgssinglebandgrayrenderer.h:55
QgsContrastEnhancement::setMinimumValue
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
Definition: qgscontrastenhancement.cpp:200
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsContrastEnhancement::StretchAndClipToMinimumMaximum
@ StretchAndClipToMinimumMaximum
Definition: qgscontrastenhancement.h:52
QgsSingleBandGrayRendererWidget::max
QString max(int index=0) override
Definition: qgssinglebandgrayrendererwidget.h:46
QgsSingleBandGrayRenderer
Raster renderer pipe for single band gray.
Definition: qgssinglebandgrayrenderer.h:34
QgsSingleBandGrayRenderer::gradient
Gradient gradient() const
Definition: qgssinglebandgrayrenderer.h:62
QgsSingleBandGrayRendererWidget::QgsSingleBandGrayRendererWidget
QgsSingleBandGrayRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Definition: qgssinglebandgrayrendererwidget.cpp:25
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsSingleBandGrayRendererWidget::renderer
QgsRasterRenderer * renderer() override
Definition: qgssinglebandgrayrendererwidget.cpp:78
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsSingleBandGrayRendererWidget::loadMinMax
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
Definition: qgssinglebandgrayrendererwidget.cpp:142
QgsRasterMinMaxWidget::setFromMinMaxOrigin
void setFromMinMaxOrigin(const QgsRasterMinMaxOrigin &)
Sets the "source" of min/max values.
Definition: qgsrasterminmaxwidget.cpp:97
QgsSingleBandGrayRenderer::contrastEnhancement
const QgsContrastEnhancement * contrastEnhancement() const
Definition: qgssinglebandgrayrenderer.h:57
QgsRasterMinMaxWidget::minMaxOrigin
QgsRasterMinMaxOrigin minMaxOrigin()
Returns a QgsRasterMinMaxOrigin object with the widget values.
Definition: qgsrasterminmaxwidget.cpp:140
QgsRasterRendererWidget::setMapCanvas
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
Definition: qgsrasterrendererwidget.cpp:23
QgsRasterMinMaxWidget
Definition: qgsrasterminmaxwidget.h:38
QgsContrastEnhancement::ContrastEnhancementAlgorithm
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
Definition: qgscontrastenhancement.h:49
QgsRasterMinMaxWidget::setExtent
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
Definition: qgsrasterminmaxwidget.h:51
QgsRasterMinMaxWidget::load
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
QgsRasterRenderer
Raster renderer pipe that applies colors to a raster.
Definition: qgsrasterrenderer.h:39
QgsContrastEnhancement::StretchToMinimumMaximum
@ StretchToMinimumMaximum
Definition: qgscontrastenhancement.h:51
QgsContrastEnhancement::NoEnhancement
@ NoEnhancement
Definition: qgscontrastenhancement.h:50
QgsRasterMinMaxWidget::doComputations
void doComputations()
Load programmatically with current values.
Definition: qgsrasterminmaxwidget.cpp:181
QgsRasterMinMaxWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
Definition: qgsrasterminmaxwidget.cpp:54
QgsDoubleValidator
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
Definition: qgsdoublevalidator.h:39
QgsSingleBandGrayRendererWidget::setFromRenderer
void setFromRenderer(const QgsRasterRenderer *r)
Definition: qgssinglebandgrayrendererwidget.cpp:177
QgsContrastEnhancement::ClipToMinimumMaximum
@ ClipToMinimumMaximum
Definition: qgscontrastenhancement.h:53
QgsRasterLayer::dataProvider
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Definition: qgsrasterlayer.cpp:234
QgsSingleBandGrayRendererWidget::doComputations
void doComputations() override
Load programmatically with current values.
Definition: qgssinglebandgrayrendererwidget.cpp:106
QgsSingleBandGrayRendererWidget::setMax
void setMax(const QString &value, int index=0) override
Definition: qgssinglebandgrayrendererwidget.cpp:211
QgsContrastEnhancement::contrastEnhancementAlgorithm
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
Definition: qgscontrastenhancement.h:157
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
QgsSingleBandGrayRendererWidget::setMin
void setMin(const QString &value, int index=0) override
Definition: qgssinglebandgrayrendererwidget.cpp:204
QgsContrastEnhancement
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
Definition: qgscontrastenhancement.h:43
qgssinglebandgrayrenderer.h
QgsSingleBandGrayRendererWidget::min
QString min(int index=0) override
Definition: qgssinglebandgrayrendererwidget.h:45
QgsRasterRenderer::setMinMaxOrigin
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
Definition: qgsrasterrenderer.h:139
qgssinglebandgrayrendererwidget.h
qgsrasterminmaxwidget.h
QgsSingleBandGrayRenderer::Gradient
Gradient
Definition: qgssinglebandgrayrenderer.h:37
QgsDoubleValidator::toDouble
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
Definition: qgsdoublevalidator.cpp:122
QgsRasterMinMaxWidget::setBands
void setBands(const QList< int > &bands)
Definition: qgsrasterminmaxwidget.cpp:64
QgsRasterLayer::renderer
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
Definition: qgsrasterlayer.h:246
QgsRasterRendererWidget
Definition: qgsrasterrendererwidget.h:37
QgsRasterRendererWidget::mCanvas
QgsMapCanvas * mCanvas
Associated map canvas.
Definition: qgsrasterrendererwidget.h:98
QgsRasterRendererWidget::widgetChanged
void widgetChanged()
Emitted when something on the widget has changed.
qgsdoublevalidator.h
QgsContrastEnhancement::setMaximumValue
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
Definition: qgscontrastenhancement.cpp:174
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:89
qgsrasterdataprovider.h