QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
23  : QgsRasterRendererWidget( layer, extent )
24  , mMinMaxWidget( nullptr )
25 {
26  setupUi( this );
27 
28  mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
29  mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
30 
31  mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
32  mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );
33 
34  if ( mRasterLayer )
35  {
37  if ( !provider )
38  {
39  return;
40  }
41 
42  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
43  mMinMaxWidget->setExtent( extent );
44  mMinMaxWidget->setMapCanvas( mCanvas );
45 
47  layout->setContentsMargins( 0, 0, 0, 0 );
48  mMinMaxContainerWidget->setLayout( layout );
49  layout->addWidget( mMinMaxWidget );
50 
51  connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
52  this, SLOT( loadMinMax( int, double, double, int ) ) );
53 
54  //fill available bands into combo box
55  int nBands = provider->bandCount();
56  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
57  {
58  mGrayBandComboBox->addItem( displayBandName( i ), i );
59  }
60 
61  //contrast enhancement algorithms
62  mContrastEnhancementComboBox->addItem( tr( "No enhancement" ), 0 );
63  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), 1 );
64  mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), 2 );
65  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), 3 );
66 
67  setFromRenderer( layer->renderer() );
68 
69  connect( mGradientComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
70  connect( mContrastEnhancementComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
71  connect( mMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
72  connect( mMinLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
73  }
74 }
75 
77 {
78 }
79 
81 {
82  if ( !mRasterLayer )
83  {
84  return nullptr;
85  }
87  if ( !provider )
88  {
89  return nullptr;
90  }
91  int band = mGrayBandComboBox->itemData( mGrayBandComboBox->currentIndex() ).toInt();
92 
94  provider->dataType( band ) ) );
95  e->setMinimumValue( mMinLineEdit->text().toDouble() );
96  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
98  mContrastEnhancementComboBox->currentIndex() ).toInt() ) );
99 
100 
102  renderer->setContrastEnhancement( e );
103 
104  renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->itemData( mGradientComboBox->currentIndex() ).toInt() );
105 
106  return renderer;
107 }
108 
110 {
112  mMinMaxWidget->setMapCanvas( canvas );
113 }
114 
115 void QgsSingleBandGrayRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
116 {
117  Q_UNUSED( theBandNo );
118  Q_UNUSED( theOrigin );
119  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
120 
121  if ( qIsNaN( theMin ) )
122  {
123  mMinLineEdit->clear();
124  }
125  else
126  {
127  mMinLineEdit->setText( QString::number( theMin ) );
128  }
129 
130  if ( qIsNaN( theMax ) )
131  {
132  mMaxLineEdit->clear();
133  }
134  else
135  {
136  mMaxLineEdit->setText( QString::number( theMax ) );
137  }
138 }
139 
140 void QgsSingleBandGrayRendererWidget::on_mGrayBandComboBox_currentIndexChanged( int index )
141 {
142  QList<int> myBands;
143  myBands.append( mGrayBandComboBox->itemData( index ).toInt() );
144  mMinMaxWidget->setBands( myBands );
145  emit widgetChanged();
146 }
147 
149 {
150  const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
151  if ( gr )
152  {
153  //band
154  mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
155  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
156 
158  if ( ce )
159  {
160  //minmax
161  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
162  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
163  //contrast enhancement algorithm
164  mContrastEnhancementComboBox->setCurrentIndex(
165  mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
166  }
167  }
168 }
QLayout * layout() const
virtual int bandCount() const =0
Get number of bands.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm, bool generateTable=true)
Set the contrast enhancement algorithm.
static unsigned index
double maximumValue() const
Return the maximum value for the contrast enhancement range.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void setContentsMargins(int left, int top, int right, int bottom)
void setupUi(QWidget *widget)
#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
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.
QgsRasterRenderer * renderer() const
void loadMinMax(int theBandNo, double theMin, double theMax, int theOrigin)
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString number(int n, int base)
void append(const T &value)
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.
QString displayBandName(int band) const
Returns a band name for display.
Raster renderer pipe for single band gray.
void setGradient(Gradient theGradient)
virtual QGis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
void setContrastEnhancement(QgsContrastEnhancement *ce)
Takes ownership.
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used...
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setFromRenderer(const QgsRasterRenderer *r)
DataType
Raster data types.
Definition: qgis.h:133
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
QgsRasterDataProvider * dataProvider()
Returns the data provider.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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.