QGIS API Documentation  3.0.2-Girona (307d082)
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 #include "qgsrasterdataprovider.h"
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  mLightAzimuth_updated( 315.00 );
40  mZFactor->setValue( 1 );
41  mZFactor->setClearValue( 1 );
42 
43  mMultiDirection->setChecked( false );
44  mBandsCombo->setLayer( mRasterLayer );
45 
46  setFromRenderer( layer->renderer() );
47 
48  connect( mLightAngle, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsRasterRendererWidget::widgetChanged );
49  connect( mLightAzimuth, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsHillshadeRendererWidget::mLightAzimuth_updated );
50  connect( mLightAzimuthDial, &QAbstractSlider::valueChanged, this, &QgsHillshadeRendererWidget::mLightAzimuthDial_updated );
51  connect( mZFactor, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsRasterRendererWidget::widgetChanged );
52  connect( mMultiDirection, &QAbstractButton::toggled, this, &QgsRasterRendererWidget::widgetChanged );
54 }
55 
57 {
58  if ( !mRasterLayer )
59  {
60  return nullptr;
61  }
62 
64  if ( !provider )
65  {
66  return nullptr;
67  }
68 
69  int band = mBandsCombo->currentBand();
70  QgsHillshadeRenderer *renderer = new QgsHillshadeRenderer( provider, band, mLightAzimuth->value(), mLightAngle->value() );
71  double value = mZFactor->value();
72  renderer->setZFactor( value );
73  renderer->setMultiDirectional( mMultiDirection->checkState() );
74  return renderer;
75 }
76 
78 {
79  const QgsHillshadeRenderer *r = dynamic_cast<const QgsHillshadeRenderer *>( renderer );
80  if ( r )
81  {
82  mBandsCombo->setBand( r->band() );
83  mLightAngle->setValue( r->altitude() );
84  mLightAzimuth->setValue( r->azimuth() );
85  mZFactor->setValue( r->zFactor() );
86  mMultiDirection->setChecked( r->multiDirectional() );
87  }
88 }
89 
91 {
92  mLightAngle->setValue( altitude );
93 }
94 
96 {
97  mLightAzimuth->setValue( azimuth );
98 }
99 
101 {
102  mZFactor->setValue( zfactor );
103 }
104 
105 void QgsHillshadeRendererWidget::setMultiDirectional( bool isMultiDirectional )
106 {
107  mMultiDirection->setChecked( isMultiDirectional );
108 }
109 
110 void QgsHillshadeRendererWidget::mLightAzimuth_updated( double value )
111 {
112  int newvalue = ( int )value - 180;
113  if ( newvalue < 0 )
114  newvalue += 360;
115  whileBlocking( mLightAzimuthDial )->setValue( newvalue );
116  emit widgetChanged();
117 }
118 
119 void QgsHillshadeRendererWidget::mLightAzimuthDial_updated( int value )
120 {
121  int newvalue = ( int )value + 180;
122  if ( newvalue > 360 )
123  newvalue -= 360;
124  whileBlocking( mLightAzimuth )->setValue( newvalue );
125  emit widgetChanged();
126 }
127 
129 {
130  return mLightAzimuth->value();
131 }
132 
134 {
135  return mLightAngle->value();
136 }
137 
139 {
140  return mZFactor->value();
141 }
142 
144 {
145  return mMultiDirection->isChecked();
146 }
A rectangle specified with double values.
Definition: qgsrectangle.h:39
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 setBand(int bandNo)
Sets the band used by the renderer.
void setZFactor(double zfactor)
Set the Z scaling factor of the result image.
int band() const
Returns the band used by the renderer.
QgsRasterDataProvider * dataProvider() override
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.
void bandChanged(int band)
This signal is emitted when the currently selected band changes.
A renderer for generating live hillshade models.
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:224
void setAzimuth(double azimuth)
Set the azimuth of the light source.
double altitude() const
Returns the angle of the light source over the raster.
double zFactor() const
Returns the Z scaling factor.
double azimuth() const
Returns the direction of the light over the raster between 0-360.
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.