QGIS API Documentation  2.9.0-Master
 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  : 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 
45  QHBoxLayout *layout = new QHBoxLayout();
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 }