QGIS API Documentation  2.99.0-Master (53aba61)
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 
30  mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
31  mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
32 
33  mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
34  mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );
35 
36  if ( mRasterLayer )
37  {
39  if ( !provider )
40  {
41  return;
42  }
43 
44  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
45  mMinMaxWidget->setExtent( extent );
46  mMinMaxWidget->setMapCanvas( mCanvas );
47 
48  QHBoxLayout *layout = new QHBoxLayout();
49  layout->setContentsMargins( 0, 0, 0, 0 );
50  mMinMaxContainerWidget->setLayout( layout );
51  layout->addWidget( mMinMaxWidget );
52 
53  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::widgetChanged,
55 
56  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::load,
58 
59  mGrayBandComboBox->setLayer( mRasterLayer );
60 
61  //contrast enhancement algorithms
62  mContrastEnhancementComboBox->addItem( tr( "No enhancement" ), QgsContrastEnhancement::NoEnhancement );
63  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
64  mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
65  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
66 
67  setFromRenderer( layer->renderer() );
68 
69  connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
70  connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
71  connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
72  }
73 }
74 
76 {
77  if ( !mRasterLayer )
78  {
79  return nullptr;
80  }
82  if ( !provider )
83  {
84  return nullptr;
85  }
86  int band = mGrayBandComboBox->currentBand();
87 
89  provider->dataType( band ) ) );
90  e->setMinimumValue( mMinLineEdit->text().toDouble() );
91  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
92  e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
93 
95  renderer->setContrastEnhancement( e );
96 
97  renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
98  renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
99 
100  return renderer;
101 }
102 
104 {
105  mMinMaxWidget->doComputations();
106 }
107 
109 {
111  mMinMaxWidget->setMapCanvas( canvas );
112 }
113 
114 void QgsSingleBandGrayRendererWidget::on_mMinLineEdit_textChanged( const QString & )
115 {
116  minMaxModified();
117 }
118 
119 void QgsSingleBandGrayRendererWidget::on_mMaxLineEdit_textChanged( const QString & )
120 {
121  minMaxModified();
122 }
123 
124 void QgsSingleBandGrayRendererWidget::minMaxModified()
125 {
126  if ( !mDisableMinMaxWidgetRefresh )
127  {
128  if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
129  {
130  mContrastEnhancementComboBox->setCurrentIndex(
131  mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum ) );
132  }
133  mMinMaxWidget->userHasSetManualMinMaxValues();
134  emit widgetChanged();
135  }
136 }
137 
138 
139 void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
140 {
141  Q_UNUSED( bandNo );
142 
143  QgsDebugMsg( QString( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
144 
145  mDisableMinMaxWidgetRefresh = true;
146  if ( std::isnan( min ) )
147  {
148  mMinLineEdit->clear();
149  }
150  else
151  {
152  mMinLineEdit->setText( QString::number( min ) );
153  }
154 
155  if ( std::isnan( max ) )
156  {
157  mMaxLineEdit->clear();
158  }
159  else
160  {
161  mMaxLineEdit->setText( QString::number( max ) );
162  }
163  mDisableMinMaxWidgetRefresh = false;
164 }
165 
166 void QgsSingleBandGrayRendererWidget::bandChanged()
167 {
168  QList<int> myBands;
169  myBands.append( mGrayBandComboBox->currentBand() );
170  mMinMaxWidget->setBands( myBands );
171  emit widgetChanged();
172 }
173 
175 {
176  const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
177  if ( gr )
178  {
179  //band
180  mGrayBandComboBox->setBand( gr->grayBand() );
182 
183  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
184  //minmax
185  mDisableMinMaxWidgetRefresh = true;
186  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
187  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
188  mDisableMinMaxWidgetRefresh = false;
189  //contrast enhancement algorithm
190  mContrastEnhancementComboBox->setCurrentIndex(
191  mContrastEnhancementComboBox->findData( ( int )( ce->contrastEnhancementAlgorithm() ) ) );
192 
193  mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
194  }
195 }
196 
197 void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
198 {
199  mDisableMinMaxWidgetRefresh = true;
200  mMinLineEdit->setText( value );
201  mDisableMinMaxWidgetRefresh = false;
202 }
203 
204 void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
205 {
206  mDisableMinMaxWidgetRefresh = true;
207  mMaxLineEdit->setText( value );
208  mDisableMinMaxWidgetRefresh = false;
209 }
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.
double maximumValue() const
Return the maximum value for the contrast enhancement range.
A rectangle specified with double values.
Definition: qgsrectangle.h:38
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
DataType
Raster data types.
Definition: qgis.h:74
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: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
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 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())
void setMinimumValue(double, bool generateTable=true)
Return the minimum value for the contrast enhancement range.
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 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.