QGIS API Documentation  2.99.0-Master (90ae728)
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  , mMinMaxWidget( nullptr )
27  , mDisableMinMaxWidgetRefresh( false )
28 {
29  setupUi( this );
30 
31  mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
32  mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
33 
34  mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
35  mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );
36 
37  if ( mRasterLayer )
38  {
40  if ( !provider )
41  {
42  return;
43  }
44 
45  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
46  mMinMaxWidget->setExtent( extent );
47  mMinMaxWidget->setMapCanvas( mCanvas );
48 
49  QHBoxLayout *layout = new QHBoxLayout();
50  layout->setContentsMargins( 0, 0, 0, 0 );
51  mMinMaxContainerWidget->setLayout( layout );
52  layout->addWidget( mMinMaxWidget );
53 
54  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::widgetChanged,
56 
57  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::load,
59 
60  //fill available bands into combo box
61  int nBands = provider->bandCount();
62  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
63  {
64  mGrayBandComboBox->addItem( displayBandName( i ), i );
65  }
66 
67  //contrast enhancement algorithms
68  mContrastEnhancementComboBox->addItem( tr( "No enhancement" ), QgsContrastEnhancement::NoEnhancement );
69  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
70  mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
71  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
72 
73  setFromRenderer( layer->renderer() );
74 
75  connect( mGradientComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
76  connect( mContrastEnhancementComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
77  }
78 }
79 
81 {
82  if ( !mRasterLayer )
83  {
84  return nullptr;
85  }
87  if ( !provider )
88  {
89  return nullptr;
90  }
91  int band = mGrayBandComboBox->currentData().toInt();
92 
94  provider->dataType( band ) ) );
95  e->setMinimumValue( mMinLineEdit->text().toDouble() );
96  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
97  e->setContrastEnhancementAlgorithm(( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
98 
100  renderer->setContrastEnhancement( e );
101 
102  renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
103  renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
104 
105  return renderer;
106 }
107 
109 {
110  mMinMaxWidget->doComputations();
111 }
112 
114 {
116  mMinMaxWidget->setMapCanvas( canvas );
117 }
118 
119 void QgsSingleBandGrayRendererWidget::on_mMinLineEdit_textChanged( const QString & )
120 {
121  minMaxModified();
122 }
123 
124 void QgsSingleBandGrayRendererWidget::on_mMaxLineEdit_textChanged( const QString & )
125 {
126  minMaxModified();
127 }
128 
129 void QgsSingleBandGrayRendererWidget::minMaxModified()
130 {
131  if ( !mDisableMinMaxWidgetRefresh )
132  {
133  if (( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
134  {
135  mContrastEnhancementComboBox->setCurrentIndex(
136  mContrastEnhancementComboBox->findData(( int ) QgsContrastEnhancement::StretchToMinimumMaximum ) );
137  }
138  mMinMaxWidget->userHasSetManualMinMaxValues();
139  emit widgetChanged();
140  }
141 }
142 
143 
144 void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
145 {
146  Q_UNUSED( bandNo );
147 
148  QgsDebugMsg( QString( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
149 
150  mDisableMinMaxWidgetRefresh = true;
151  if ( qIsNaN( min ) )
152  {
153  mMinLineEdit->clear();
154  }
155  else
156  {
157  mMinLineEdit->setText( QString::number( min ) );
158  }
159 
160  if ( qIsNaN( max ) )
161  {
162  mMaxLineEdit->clear();
163  }
164  else
165  {
166  mMaxLineEdit->setText( QString::number( max ) );
167  }
168  mDisableMinMaxWidgetRefresh = false;
169 }
170 
171 void QgsSingleBandGrayRendererWidget::on_mGrayBandComboBox_currentIndexChanged( int index )
172 {
173  QList<int> myBands;
174  myBands.append( mGrayBandComboBox->itemData( index ).toInt() );
175  mMinMaxWidget->setBands( myBands );
176  emit widgetChanged();
177 }
178 
180 {
181  const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
182  if ( gr )
183  {
184  //band
185  mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
187 
188  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
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  mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
199  }
200 }
201 
202 void QgsSingleBandGrayRendererWidget::setMin( const QString& value, int )
203 {
204  mDisableMinMaxWidgetRefresh = true;
205  mMinLineEdit->setText( value );
206  mDisableMinMaxWidgetRefresh = false;
207 }
208 
209 void QgsSingleBandGrayRendererWidget::setMax( const QString& value, int )
210 {
211  mDisableMinMaxWidgetRefresh = true;
212  mMaxLineEdit->setText( value );
213  mDisableMinMaxWidgetRefresh = false;
214 }
virtual int bandCount() const =0
Get number of bands.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm, bool generateTable=true)
Set the contrast enhancement algorithm.
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
static unsigned index
double maximumValue() const
Return the maximum value for the contrast enhancement range.
A rectangle specified with double values.
Definition: qgsrectangle.h:36
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
DataType
Raster data types.
Definition: qgis.h:61
const QgsContrastEnhancement * contrastEnhancement() const
double minimumValue() const
Return the minimum value for the contrast enhancement range.
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
QgsRasterMinMaxOrigin minMaxOrigin()
Return 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:72
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void widgetChanged()
Emitted when something on the widget has changed.
virtual Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
void setMax(const QString &value, int index=0) override
void doComputations() override
Load programmatically with current values.
void setBands(const QList< int > &bands)
QgsSingleBandGrayRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
void setMinimumValue(double, bool generateTable=true)
Return the minimum value for the contrast enhancement range.
QString displayBandName(int band) const
Returns a band name for display. First choice is color name, otherwise band number.
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
QgsRasterDataProvider * dataProvider()
Returns the data provider.
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 doComputations()
Load programmatically with current values.
void setMaximumValue(double, bool generateTable=true)
Set the maximum value for the contrast enhancement range.
Base class for raster data providers.
void setFromMinMaxOrigin(const QgsRasterMinMaxOrigin &)
Set the "source" of min/max values.