QGIS API Documentation  2.99.0-Master (0cba29c)
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  mGrayBandComboBox->setLayer( mRasterLayer );
61 
62  //contrast enhancement algorithms
63  mContrastEnhancementComboBox->addItem( tr( "No enhancement" ), QgsContrastEnhancement::NoEnhancement );
64  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
65  mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
66  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
67 
68  setFromRenderer( layer->renderer() );
69 
70  connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
71  connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
72  connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
73  }
74 }
75 
77 {
78  if ( !mRasterLayer )
79  {
80  return nullptr;
81  }
83  if ( !provider )
84  {
85  return nullptr;
86  }
87  int band = mGrayBandComboBox->currentBand();
88 
90  provider->dataType( band ) ) );
91  e->setMinimumValue( mMinLineEdit->text().toDouble() );
92  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
93  e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
94 
96  renderer->setContrastEnhancement( e );
97 
98  renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
99  renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
100 
101  return renderer;
102 }
103 
105 {
106  mMinMaxWidget->doComputations();
107 }
108 
110 {
112  mMinMaxWidget->setMapCanvas( canvas );
113 }
114 
115 void QgsSingleBandGrayRendererWidget::on_mMinLineEdit_textChanged( const QString & )
116 {
117  minMaxModified();
118 }
119 
120 void QgsSingleBandGrayRendererWidget::on_mMaxLineEdit_textChanged( const QString & )
121 {
122  minMaxModified();
123 }
124 
125 void QgsSingleBandGrayRendererWidget::minMaxModified()
126 {
127  if ( !mDisableMinMaxWidgetRefresh )
128  {
129  if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
130  {
131  mContrastEnhancementComboBox->setCurrentIndex(
132  mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum ) );
133  }
134  mMinMaxWidget->userHasSetManualMinMaxValues();
135  emit widgetChanged();
136  }
137 }
138 
139 
140 void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
141 {
142  Q_UNUSED( bandNo );
143 
144  QgsDebugMsg( QString( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
145 
146  mDisableMinMaxWidgetRefresh = true;
147  if ( qIsNaN( min ) )
148  {
149  mMinLineEdit->clear();
150  }
151  else
152  {
153  mMinLineEdit->setText( QString::number( min ) );
154  }
155 
156  if ( qIsNaN( max ) )
157  {
158  mMaxLineEdit->clear();
159  }
160  else
161  {
162  mMaxLineEdit->setText( QString::number( max ) );
163  }
164  mDisableMinMaxWidgetRefresh = false;
165 }
166 
167 void QgsSingleBandGrayRendererWidget::bandChanged()
168 {
169  QList<int> myBands;
170  myBands.append( mGrayBandComboBox->currentBand() );
171  mMinMaxWidget->setBands( myBands );
172  emit widgetChanged();
173 }
174 
176 {
177  const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
178  if ( gr )
179  {
180  //band
181  mGrayBandComboBox->setBand( gr->grayBand() );
183 
184  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
185  //minmax
186  mDisableMinMaxWidgetRefresh = true;
187  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
188  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
189  mDisableMinMaxWidgetRefresh = false;
190  //contrast enhancement algorithm
191  mContrastEnhancementComboBox->setCurrentIndex(
192  mContrastEnhancementComboBox->findData( ( int )( ce->contrastEnhancementAlgorithm() ) ) );
193 
194  mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
195  }
196 }
197 
198 void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
199 {
200  mDisableMinMaxWidgetRefresh = true;
201  mMinLineEdit->setText( value );
202  mDisableMinMaxWidgetRefresh = false;
203 }
204 
205 void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
206 {
207  mDisableMinMaxWidgetRefresh = true;
208  mMaxLineEdit->setText( value );
209  mDisableMinMaxWidgetRefresh = false;
210 }
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.