QGIS API Documentation  3.6.0-Noosa (5873452)
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 }
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
DataType
Raster data types.
Definition: qgis.h:79
const QgsContrastEnhancement * contrastEnhancement() const
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
QgsRasterMinMaxOrigin minMaxOrigin()
Returns a QgsRasterMinMaxOrigin object with the widget values.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
QgsRasterRenderer * renderer() const
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void widgetChanged()
Emitted when something on the widget has changed.
QgsRasterDataProvider * dataProvider() override
Returns the layer&#39;s data provider, it may be null.
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
void setMax(const QString &value, int index=0) override
void doComputations() override
Load programmatically with current values.
void bandChanged(int band)
This signal is emitted when the currently selected band changes.
void setBands(const QList< int > &bands)
QgsSingleBandGrayRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Raster renderer pipe for single band gray.
void setGradient(Gradient gradient)
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
void userHasSetManualMinMaxValues()
Uncheck cumulative cut, min/max, std-dev radio buttons.
void setContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used...
void setMin(const QString &value, int index=0) override
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setFromRenderer(const QgsRasterRenderer *r)
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
QgsMapCanvas * mCanvas
Associated map canvas.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
void doComputations()
Load programmatically with current values.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
Base class for raster data providers.
void setFromMinMaxOrigin(const QgsRasterMinMaxOrigin &)
Sets the "source" of min/max values.