QGIS API Documentation  2.15.0-Master (1f0fce7)
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 
46  layout->setContentsMargins( 0, 0, 0, 0 );
47  mMinMaxContainerWidget->setLayout( layout );
48  layout->addWidget( mMinMaxWidget );
49 
50  connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
51  this, SLOT( loadMinMax( int, double, double, int ) ) );
52 
53  //fill available bands into combo box
54  int nBands = provider->bandCount();
55  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
56  {
57  mGrayBandComboBox->addItem( displayBandName( i ), i );
58  }
59 
60  //contrast enhancement algorithms
61  mContrastEnhancementComboBox->addItem( tr( "No enhancement" ), 0 );
62  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), 1 );
63  mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), 2 );
64  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), 3 );
65 
66  setFromRenderer( layer->renderer() );
67 
68  connect( mGradientComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
69  connect( mContrastEnhancementComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
70  connect( mMaxLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
71  connect( mMinLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( widgetChanged() ) );
72  }
73 }
74 
76 {
77 }
78 
80 {
81  if ( !mRasterLayer )
82  {
83  return nullptr;
84  }
86  if ( !provider )
87  {
88  return nullptr;
89  }
90  int band = mGrayBandComboBox->itemData( mGrayBandComboBox->currentIndex() ).toInt();
91 
93  provider->dataType( band ) ) );
94  e->setMinimumValue( mMinLineEdit->text().toDouble() );
95  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
97  mContrastEnhancementComboBox->currentIndex() ).toInt() ) );
98 
99 
101  renderer->setContrastEnhancement( e );
102 
103  renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->itemData( mGradientComboBox->currentIndex() ).toInt() );
104 
105  return renderer;
106 }
107 
108 void QgsSingleBandGrayRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
109 {
110  Q_UNUSED( theBandNo );
111  Q_UNUSED( theOrigin );
112  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
113 
114  if ( qIsNaN( theMin ) )
115  {
116  mMinLineEdit->clear();
117  }
118  else
119  {
120  mMinLineEdit->setText( QString::number( theMin ) );
121  }
122 
123  if ( qIsNaN( theMax ) )
124  {
125  mMaxLineEdit->clear();
126  }
127  else
128  {
129  mMaxLineEdit->setText( QString::number( theMax ) );
130  }
131 }
132 
133 void QgsSingleBandGrayRendererWidget::on_mGrayBandComboBox_currentIndexChanged( int index )
134 {
135  QList<int> myBands;
136  myBands.append( mGrayBandComboBox->itemData( index ).toInt() );
137  mMinMaxWidget->setBands( myBands );
138  emit widgetChanged();
139 }
140 
142 {
143  const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
144  if ( gr )
145  {
146  //band
147  mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
149 
150  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
151  //minmax
152  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
153  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
154  //contrast enhancement algorithm
155  mContrastEnhancementComboBox->setCurrentIndex(
156  mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
157  }
158 }
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
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void setContentsMargins(int left, int top, int right, int bottom)
void setupUi(QWidget *widget)
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
void loadMinMax(int theBandNo, double theMin, double theMax, int theOrigin)
QString tr(const char *sourceText, const char *disambiguation, int n)
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString number(int n, int base)
void append(const T &value)
QgsRasterRenderer * renderer() const
void setExtent(const QgsRectangle &theExtent)
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)
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...
QString displayBandName(int band) const
Returns a band name for display.
double maximumValue() const
Return the maximum value for the contrast enhancement range.
void setFromRenderer(const QgsRasterRenderer *r)
DataType
Raster data types.
Definition: qgis.h:133
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)
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.