QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgshillshaderendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshillshaderendererwidget.cpp
3  ---------------------------------
4  begin : May 2016
5  copyright : (C) 2016 by Nathan Woodrow
6  email : woodrow dot nathan at gmail dot com
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 
18 
20 #include "qgsrasterlayer.h"
22 #include "qgshillshaderenderer.h"
23 
24 
26  : QgsRasterRendererWidget( layer, extent )
27 {
28  setupUi( this );
29 
30  mLightAngle->setMaximum( 90 );
31  mLightAzimuth->setMaximum( 360.00 );
32 
33  mLightAngle->setValue( 45.00 );
34  mLightAngle->setClearValue( 45.0 );
35  mLightAzimuth->setValue( 315.00 );
36  mLightAzimuth->setClearValue( 315.00 );
37 
38  // Update the dial correctly
39  on_mLightAzimuth_updated( 315.00 );
40  mZFactor->setValue( 1 );
41  mZFactor->setClearValue( 1 );
42 
43  mMultiDirection->setChecked( false );
44 
45  if ( mRasterLayer )
46  {
48  if ( !provider )
49  {
50  return;
51  }
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  mBandsCombo->addItem( displayBandName( i ), i );
58  }
59 
60  }
61 
62  setFromRenderer( layer->renderer() );
63 
64  connect( mLightAngle, SIGNAL( valueChanged( double ) ), this, SIGNAL( widgetChanged() ) );
65  connect( mLightAzimuth, SIGNAL( valueChanged( double ) ), this, SLOT( on_mLightAzimuth_updated( double ) ) );
66  connect( mLightAzimuthDial, SIGNAL( valueChanged( int ) ), this, SLOT( on_mLightAzimuthDail_updated( int ) ) );
67  connect( mZFactor, SIGNAL( valueChanged( double ) ), this, SIGNAL( widgetChanged() ) );
68  connect( mMultiDirection, SIGNAL( toggled( bool ) ), this, SIGNAL( widgetChanged() ) );
69 }
70 
72 {
73 
74 }
75 
77 {
78  if ( !mRasterLayer )
79  {
80  return nullptr;
81  }
82 
84  if ( !provider )
85  {
86  return nullptr;
87  }
88 
89  int band = mBandsCombo->itemData( mBandsCombo->currentIndex() ).toInt();
90  QgsHillshadeRenderer* renderer = new QgsHillshadeRenderer( provider, band, mLightAzimuth->value(), mLightAngle->value() );
91  double value = mZFactor->value();
92  renderer->setZFactor( value );
93  renderer->setMultiDirectional( mMultiDirection->checkState() );
94  return renderer;
95 }
96 
98 {
99  const QgsHillshadeRenderer* r = dynamic_cast<const QgsHillshadeRenderer*>( renderer );
100  if ( r )
101  {
102  mBandsCombo->setCurrentIndex( mBandsCombo->findData( r->band() ) );
103  mLightAngle->setValue( r->altitude() );
104  mLightAzimuth->setValue( r->azimuth() );
105  mZFactor->setValue( r->zFactor() );
106  mMultiDirection->setChecked( r->multiDirectional() );
107  }
108 }
109 
111 {
112  mLightAngle->setValue( altitude );
113 }
114 
116 {
117  mLightAzimuth->setValue( azimuth );
118 }
119 
121 {
122  mZFactor->setValue( zfactor );
123 }
124 
125 void QgsHillshadeRendererWidget::setMultiDirectional( bool isMultiDirectional )
126 {
127  mMultiDirection->setChecked( isMultiDirectional );
128 }
129 
130 void QgsHillshadeRendererWidget::on_mLightAzimuth_updated( double value )
131 {
132  int newvalue = ( int )value - 180;
133  if ( newvalue < 0 )
134  newvalue += 360;
135  whileBlocking( mLightAzimuthDial )->setValue( newvalue );
136  emit widgetChanged();
137 }
138 
139 void QgsHillshadeRendererWidget::on_mLightAzimuthDail_updated( int value )
140 {
141  int newvalue = ( int )value + 180;
142  if ( newvalue > 360 )
143  newvalue -= 360 ;
144  whileBlocking( mLightAzimuth )->setValue( newvalue );
145  emit widgetChanged();
146 }
147 
149 {
150  return mLightAzimuth->value();
151 }
152 
154 {
155  return mLightAngle->value();
156 }
157 
159 {
160  return mZFactor->value();
161 }
162 
164 {
165  return mMultiDirection->isChecked();
166 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void setupUi(QWidget *widget)
void setMultiDirectional(bool isMultiDirectional)
Sets whether to render using a multi-directional hillshade algorithm.
double altitude() const
Returns the angle of the light source over the raster.
void setMultiDirectional(bool isMultiDirectional)
Sets whether to render using a multi-directional hillshade algorithm.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsHillshadeRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Renderer widget for the hill shade renderer.
void setFromRenderer(const QgsRasterRenderer *renderer)
Set the widget state from the given renderer.
QgsRasterRenderer * renderer() const
void setAltitude(double altitude)
Set the altitude of the light source.
void setZFactor(double zfactor)
Set the Z scaling factor of the result image.
int band() const
Returns the band used by the renderer.
bool multiDirectional() const
Returns true if the renderer should use the multi-directional hillshade algorithm.
double azimuth() const
Returns the direction of the light over the raster between 0-360.
bool multiDirectional() const
Returns true if the renderer is using multi-directional hillshading.
A renderer for generating live hillshade models.
QString displayBandName(int band) const
Returns a band name for display.
void setZFactor(double zfactor)
Set the Z scaling factor of the result image.
double zFactor() const
Returns the Z scaling factor.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:333
void setAzimuth(double azimuth)
Set the azimuth of the light source.
double altitude() const
Returns the angle of the light source over the raster.
QgsRasterDataProvider * dataProvider()
Returns the data provider.
double zFactor() const
Returns the Z scaling factor.
double azimuth() const
Returns the direction of the light over the raster between 0-360.
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.
QgsRasterRenderer * renderer() override
The renderer for the widget.
Raster renderer pipe that applies colors to a raster.
Base class for raster data providers.