QGIS API Documentation  2.99.0-Master (c558d51)
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 {
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, SIGNAL( load( int, double, double, int ) ),
54  this, SLOT( loadMinMax( int, double, double, int ) ) );
55 
56  //fill available bands into combo box
57  int nBands = provider->bandCount();
58  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
59  {
60  mGrayBandComboBox->addItem( displayBandName( i ), i );
61  }
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( mGradientComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
72  connect( mContrastEnhancementComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
73  connect( mMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
74  connect( mMinLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
75  }
76 }
77 
79 {
80 }
81 
83 {
84  if ( !mRasterLayer )
85  {
86  return nullptr;
87  }
89  if ( !provider )
90  {
91  return nullptr;
92  }
93  int band = mGrayBandComboBox->currentData().toInt();
94 
96  provider->dataType( band ) ) );
97  e->setMinimumValue( mMinLineEdit->text().toDouble() );
98  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
99  e->setContrastEnhancementAlgorithm(( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
100 
101 
103  renderer->setContrastEnhancement( e );
104 
105  renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
106 
107  return renderer;
108 }
109 
111 {
113  mMinMaxWidget->setMapCanvas( canvas );
114 }
115 
116 void QgsSingleBandGrayRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
117 {
118  Q_UNUSED( theBandNo );
119  Q_UNUSED( theOrigin );
120  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
121 
122  if ( qIsNaN( theMin ) )
123  {
124  mMinLineEdit->clear();
125  }
126  else
127  {
128  mMinLineEdit->setText( QString::number( theMin ) );
129  }
130 
131  if ( qIsNaN( theMax ) )
132  {
133  mMaxLineEdit->clear();
134  }
135  else
136  {
137  mMaxLineEdit->setText( QString::number( theMax ) );
138  }
139 
140  //automaticlly activate contrast enhancement algorithm if set to none
141  if ( mContrastEnhancementComboBox->currentData().toInt() == QgsContrastEnhancement::NoEnhancement )
142  {
143  mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( QgsContrastEnhancement::StretchToMinimumMaximum ) );
144  }
145 }
146 
147 void QgsSingleBandGrayRendererWidget::on_mGrayBandComboBox_currentIndexChanged( int index )
148 {
149  QList<int> myBands;
150  myBands.append( mGrayBandComboBox->itemData( index ).toInt() );
151  mMinMaxWidget->setBands( myBands );
152  emit widgetChanged();
153 }
154 
156 {
157  const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
158  if ( gr )
159  {
160  //band
161  mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
163 
164  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
165  //minmax
166  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
167  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
168  //contrast enhancement algorithm
169  mContrastEnhancementComboBox->setCurrentIndex(
170  mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
171  }
172 }
virtual int bandCount() const =0
Get number of bands.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm, bool generateTable=true)
Set the contrast enhancement algorithm.
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void setBands(const QList< int > &theBands)
const QgsContrastEnhancement * contrastEnhancement() const
DataType
Raster data types.
Definition: qgis.h:60
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
void loadMinMax(int theBandNo, double theMin, double theMax, int theOrigin)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:106
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
virtual Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
QgsRasterRenderer * renderer() const
void setExtent(const QgsRectangle &theExtent)
Sets the extent to use for minimum and maximum value calculation.
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.
double minimumValue() const
Return the minimum value for the contrast enhancement range.
void setGradient(Gradient theGradient)
void setContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used...
QString displayBandName(int band) const
Returns a band name for display. First choice is color name, otherwise band number.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
double maximumValue() const
Return the maximum value for the contrast enhancement range.
void setFromRenderer(const QgsRasterRenderer *r)
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
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 setMaximumValue(double, bool generateTable=true)
Set the maximum value for the contrast enhancement range.
Base class for raster data providers.