QGIS API Documentation  2.12.0-Lyon
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( NULL )
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 }
69 
71 {
72 }
73 
75 {
76  if ( !mRasterLayer )
77  {
78  return 0;
79  }
81  if ( !provider )
82  {
83  return 0;
84  }
85  int band = mGrayBandComboBox->itemData( mGrayBandComboBox->currentIndex() ).toInt();
86 
88  provider->dataType( band ) ) );
89  e->setMinimumValue( mMinLineEdit->text().toDouble() );
90  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
92  mContrastEnhancementComboBox->currentIndex() ).toInt() ) );
93 
94 
96  renderer->setContrastEnhancement( e );
97 
98  renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->itemData( mGradientComboBox->currentIndex() ).toInt() );
99 
100  return renderer;
101 }
102 
103 void QgsSingleBandGrayRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
104 {
105  Q_UNUSED( theBandNo );
106  Q_UNUSED( theOrigin );
107  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
108 
109  if ( qIsNaN( theMin ) )
110  {
111  mMinLineEdit->clear();
112  }
113  else
114  {
115  mMinLineEdit->setText( QString::number( theMin ) );
116  }
117 
118  if ( qIsNaN( theMax ) )
119  {
120  mMaxLineEdit->clear();
121  }
122  else
123  {
124  mMaxLineEdit->setText( QString::number( theMax ) );
125  }
126 }
127 
128 void QgsSingleBandGrayRendererWidget::on_mGrayBandComboBox_currentIndexChanged( int index )
129 {
130  QList<int> myBands;
131  myBands.append( mGrayBandComboBox->itemData( index ).toInt() );
132  mMinMaxWidget->setBands( myBands );
133 }
134 
136 {
137  const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
138  if ( gr )
139  {
140  //band
141  mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
143 
144  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
145  //minmax
146  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
147  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
148  //contrast enhancement algorithm
149  mContrastEnhancementComboBox->setCurrentIndex(
150  mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
151  }
152 }
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)
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used...
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.
DataType
Raster data types.
Definition: qgis.h:122
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.
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)
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)
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.