QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
25  : QgsRasterRendererWidget( layer, extent )
26  , mDisableMinMaxWidgetRefresh( false )
27 {
28  setupUi( this );
29  connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged );
30  connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged );
31 
32  mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
33  mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
34 
35  mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
36  mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );
37 
38  if ( mRasterLayer )
39  {
41  if ( !provider )
42  {
43  return;
44  }
45 
46  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
47  mMinMaxWidget->setExtent( extent );
48  mMinMaxWidget->setMapCanvas( mCanvas );
49 
50  QHBoxLayout *layout = new QHBoxLayout();
51  layout->setContentsMargins( 0, 0, 0, 0 );
52  mMinMaxContainerWidget->setLayout( layout );
53  layout->addWidget( mMinMaxWidget );
54 
55  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::widgetChanged,
57 
58  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::load,
60 
61  mGrayBandComboBox->setLayer( mRasterLayer );
62 
63  //contrast enhancement algorithms
64  mContrastEnhancementComboBox->addItem( tr( "No Enhancement" ), QgsContrastEnhancement::NoEnhancement );
65  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
66  mContrastEnhancementComboBox->addItem( tr( "Stretch and Clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
67  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
68 
69  setFromRenderer( layer->renderer() );
70 
71  connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
72  connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
73  connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
74  }
75 }
76 
78 {
79  if ( !mRasterLayer )
80  {
81  return nullptr;
82  }
84  if ( !provider )
85  {
86  return nullptr;
87  }
88  int band = mGrayBandComboBox->currentBand();
89 
91  provider->dataType( band ) ) );
92  e->setMinimumValue( mMinLineEdit->text().toDouble() );
93  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
94  e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
95 
97  renderer->setContrastEnhancement( e );
98 
99  renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
100  renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
101 
102  return renderer;
103 }
104 
106 {
107  mMinMaxWidget->doComputations();
108 }
109 
111 {
113  mMinMaxWidget->setMapCanvas( canvas );
114 }
115 
116 void QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged( const QString & )
117 {
118  minMaxModified();
119 }
120 
121 void QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged( const QString & )
122 {
123  minMaxModified();
124 }
125 
126 void QgsSingleBandGrayRendererWidget::minMaxModified()
127 {
128  if ( !mDisableMinMaxWidgetRefresh )
129  {
130  if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
131  {
132  mContrastEnhancementComboBox->setCurrentIndex(
133  mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum ) );
134  }
135  mMinMaxWidget->userHasSetManualMinMaxValues();
136  emit widgetChanged();
137  }
138 }
139 
140 
141 void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
142 {
143  Q_UNUSED( bandNo )
144 
145  QgsDebugMsg( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
146 
147  mDisableMinMaxWidgetRefresh = true;
148  if ( std::isnan( min ) )
149  {
150  mMinLineEdit->clear();
151  }
152  else
153  {
154  mMinLineEdit->setText( QString::number( min ) );
155  }
156 
157  if ( std::isnan( max ) )
158  {
159  mMaxLineEdit->clear();
160  }
161  else
162  {
163  mMaxLineEdit->setText( QString::number( max ) );
164  }
165  mDisableMinMaxWidgetRefresh = false;
166 }
167 
168 void QgsSingleBandGrayRendererWidget::bandChanged()
169 {
170  QList<int> myBands;
171  myBands.append( mGrayBandComboBox->currentBand() );
172  mMinMaxWidget->setBands( myBands );
173  emit widgetChanged();
174 }
175 
177 {
178  const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
179  if ( gr )
180  {
181  //band
182  mGrayBandComboBox->setBand( gr->grayBand() );
183  mMinMaxWidget->setBands( QList< int >() << gr->grayBand() );
184  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
185 
187  if ( ce )
188  {
189  //minmax
190  mDisableMinMaxWidgetRefresh = true;
191  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
192  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
193  mDisableMinMaxWidgetRefresh = false;
194  //contrast enhancement algorithm
195  mContrastEnhancementComboBox->setCurrentIndex(
196  mContrastEnhancementComboBox->findData( ( int )( ce->contrastEnhancementAlgorithm() ) ) );
197  }
198 
199  mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
200  }
201 }
202 
203 void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
204 {
205  mDisableMinMaxWidgetRefresh = true;
206  mMinLineEdit->setText( value );
207  mDisableMinMaxWidgetRefresh = false;
208 }
209 
210 void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
211 {
212  mDisableMinMaxWidgetRefresh = true;
213  mMaxLineEdit->setText( value );
214  mDisableMinMaxWidgetRefresh = false;
215 }
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:114
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:110
QgsRasterRenderer::minMaxOrigin
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
Definition: qgsrasterrenderer.h:135
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
Definition: qgsmapcanvas.h:83
QgsContrastEnhancement::StretchAndClipToMinimumMaximum
@ StretchAndClipToMinimumMaximum
Definition: qgscontrastenhancement.h:52
QgsSingleBandGrayRendererWidget::max
QString max(int index=0) override
Definition: qgssinglebandgrayrendererwidget.h:46
QgsSingleBandGrayRenderer
Definition: qgssinglebandgrayrenderer.h:33
QgsSingleBandGrayRenderer::gradient
Gradient gradient() const
Definition: qgssinglebandgrayrenderer.h:62
QgsSingleBandGrayRendererWidget::QgsSingleBandGrayRendererWidget
QgsSingleBandGrayRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Definition: qgssinglebandgrayrendererwidget.cpp:24
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsSingleBandGrayRendererWidget::renderer
QgsRasterRenderer * renderer() override
Definition: qgssinglebandgrayrendererwidget.cpp:77
QgsRectangle
Definition: qgsrectangle.h:41
QgsSingleBandGrayRendererWidget::loadMinMax
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
Definition: qgssinglebandgrayrendererwidget.cpp:141
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:37
QgsContrastEnhancement::ContrastEnhancementAlgorithm
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
Definition: qgscontrastenhancement.h:48
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
Definition: qgsrasterrenderer.h:38
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
QgsSingleBandGrayRendererWidget::setFromRenderer
void setFromRenderer(const QgsRasterRenderer *r)
Definition: qgssinglebandgrayrendererwidget.cpp:176
QgsContrastEnhancement::ClipToMinimumMaximum
@ ClipToMinimumMaximum
Definition: qgscontrastenhancement.h:53
QgsSingleBandGrayRendererWidget::doComputations
void doComputations() override
Load programmatically with current values.
Definition: qgssinglebandgrayrendererwidget.cpp:105
QgsSingleBandGrayRendererWidget::setMax
void setMax(const QString &value, int index=0) override
Definition: qgssinglebandgrayrendererwidget.cpp:210
QgsContrastEnhancement::contrastEnhancementAlgorithm
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
Definition: qgscontrastenhancement.h:157
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsSingleBandGrayRendererWidget::setMin
void setMin(const QString &value, int index=0) override
Definition: qgssinglebandgrayrendererwidget.cpp:203
QgsContrastEnhancement
Definition: qgscontrastenhancement.h:42
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:138
qgssinglebandgrayrendererwidget.h
qgsrasterminmaxwidget.h
QgsSingleBandGrayRenderer::Gradient
Gradient
Definition: qgssinglebandgrayrenderer.h:36
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:247
QgsRasterRendererWidget
Definition: qgsrasterrendererwidget.h:36
QgsRasterRendererWidget::mCanvas
QgsMapCanvas * mCanvas
Associated map canvas.
Definition: qgsrasterrendererwidget.h:98
QgsRasterRendererWidget::widgetChanged
void widgetChanged()
Emitted when something on the widget has changed.
QgsContrastEnhancement::setMaximumValue
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
Definition: qgscontrastenhancement.cpp:174
QgsRasterDataProvider
Definition: qgsrasterdataprovider.h:88
QgsRasterLayer::dataProvider
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Definition: qgsrasterlayer.cpp:233
qgsrasterdataprovider.h