QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
24  setupUi( this );
25 
26  mGradientComboBox->insertItem( 0, tr( "Black to white" ), QgsSingleBandGrayRenderer::BlackToWhite );
27  mGradientComboBox->insertItem( 1, tr( "White to black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
28 
29  mMinLineEdit->setValidator( new QDoubleValidator( mMinLineEdit ) );
30  mMaxLineEdit->setValidator( new QDoubleValidator( mMaxLineEdit ) );
31 
32  if ( mRasterLayer )
33  {
35  if ( !provider )
36  {
37  return;
38  }
39 
40  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
41  mMinMaxWidget->setExtent( extent );
42 
43  QHBoxLayout *layout = new QHBoxLayout();
44  layout->setContentsMargins( 0, 0, 0, 0 );
45  mMinMaxContainerWidget->setLayout( layout );
46  layout->addWidget( mMinMaxWidget );
47 
48  connect( mMinMaxWidget, SIGNAL( load( int, double, double, int ) ),
49  this, SLOT( loadMinMax( int, double, double, int ) ) );
50 
51  //fill available bands into combo box
52  int nBands = provider->bandCount();
53  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
54  {
55  mGrayBandComboBox->addItem( displayBandName( i ), i );
56  }
57 
58  //contrast enhancement algorithms
59  mContrastEnhancementComboBox->addItem( tr( "No enhancement" ), 0 );
60  mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), 1 );
61  mContrastEnhancementComboBox->addItem( tr( "Stretch and clip to MinMax" ), 2 );
62  mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), 3 );
63 
64  setFromRenderer( layer->renderer() );
65  }
66 }
67 
69 {
70 }
71 
73 {
74  if ( !mRasterLayer )
75  {
76  return 0;
77  }
79  if ( !provider )
80  {
81  return 0;
82  }
83  int band = mGrayBandComboBox->itemData( mGrayBandComboBox->currentIndex() ).toInt();
84 
86  provider->dataType( band ) ) );
87  e->setMinimumValue( mMinLineEdit->text().toDouble() );
88  e->setMaximumValue( mMaxLineEdit->text().toDouble() );
90  mContrastEnhancementComboBox->currentIndex() ).toInt() ) );
91 
92 
94  renderer->setContrastEnhancement( e );
95 
96  renderer->setGradient(( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->itemData( mGradientComboBox->currentIndex() ).toInt() );
97 
98  return renderer;
99 }
100 
101 void QgsSingleBandGrayRendererWidget::loadMinMax( int theBandNo, double theMin, double theMax, int theOrigin )
102 {
103  Q_UNUSED( theBandNo );
104  Q_UNUSED( theOrigin );
105  QgsDebugMsg( QString( "theBandNo = %1 theMin = %2 theMax = %3" ).arg( theBandNo ).arg( theMin ).arg( theMax ) );
106 
107  if ( qIsNaN( theMin ) )
108  {
109  mMinLineEdit->clear();
110  }
111  else
112  {
113  mMinLineEdit->setText( QString::number( theMin ) );
114  }
115 
116  if ( qIsNaN( theMax ) )
117  {
118  mMaxLineEdit->clear();
119  }
120  else
121  {
122  mMaxLineEdit->setText( QString::number( theMax ) );
123  }
124 }
125 
127 {
128  QList<int> myBands;
129  myBands.append( mGrayBandComboBox->itemData( index ).toInt() );
130  mMinMaxWidget->setBands( myBands );
131 }
132 
134 {
135  const QgsSingleBandGrayRenderer* gr = dynamic_cast<const QgsSingleBandGrayRenderer*>( r );
136  if ( gr )
137  {
138  //band
139  mGrayBandComboBox->setCurrentIndex( mGrayBandComboBox->findData( gr->grayBand() ) );
141 
142  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
143  //minmax
144  mMinLineEdit->setText( QString::number( ce->minimumValue() ) );
145  mMaxLineEdit->setText( QString::number( ce->maximumValue() ) );
146  //contrast enhancement algorithm
147  mContrastEnhancementComboBox->setCurrentIndex(
148  mContrastEnhancementComboBox->findData(( int )( ce->contrastEnhancementAlgorithm() ) ) );
149  }
150 }
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:36
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)
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.
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
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.
double maximumValue() const
Return the maximum value for the contrast enhancement range.
void setFromRenderer(const QgsRasterRenderer *r)
DataType
Raster data types.
Definition: qgis.h:204
Manipulates raster pixel values so that they enhanceContrast or clip into a specified numerical range...
QgsRasterDataProvider * dataProvider()
Returns the data provider.
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.
#define tr(sourceText)