QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsrendererrasterpropertieswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrendererrasterpropertieswidget.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  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
16 
17 #include "qgis.h"
18 #include "qgsmapcanvas.h"
20 #include "qgshuesaturationfilter.h"
22 #include "qgsrasterlayer.h"
32 #include "qgsapplication.h"
33 #include "qgscolorrampimpl.h"
34 #include "qgsproject.h"
35 #include "qgsprojectutils.h"
36 
37 #include "qgsmessagelog.h"
38 
39 static void _initRendererWidgetFunctions()
40 {
41  static bool sInitialized = false;
42  if ( sInitialized )
43  return;
44 
51 
52  sInitialized = true;
53 }
54 
55 
56 
58  : QgsMapLayerConfigWidget( layer, canvas, parent )
59  , mRasterLayer( qobject_cast<QgsRasterLayer *>( layer ) )
60 {
61  if ( !mRasterLayer )
62  return;
63 
64  setupUi( this );
65  connect( mResetColorRenderingBtn, &QToolButton::clicked, this, &QgsRendererRasterPropertiesWidget::mResetColorRenderingBtn_clicked );
66 
67  _initRendererWidgetFunctions();
68 
69  mResamplingUtils.initWidgets( mRasterLayer, mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox, mMaximumOversamplingSpinBox, mCbEarlyResampling );
70 
71  connect( cboRenderers, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRendererRasterPropertiesWidget::rendererChanged );
72 
73  connect( mSliderBrightness, &QAbstractSlider::valueChanged, mBrightnessSpinBox, &QSpinBox::setValue );
74  connect( mBrightnessSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), mSliderBrightness, &QAbstractSlider::setValue );
75  mBrightnessSpinBox->setClearValue( 0 );
76 
77  connect( mSliderContrast, &QAbstractSlider::valueChanged, mContrastSpinBox, &QSpinBox::setValue );
78  connect( mContrastSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), mSliderContrast, &QAbstractSlider::setValue );
79  mContrastSpinBox->setClearValue( 0 );
80 
81  connect( mSliderGamma, &QAbstractSlider::valueChanged, this, &QgsRendererRasterPropertiesWidget::updateGammaSpinBox );
82  connect( mGammaSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsRendererRasterPropertiesWidget::updateGammaSlider );
83  mGammaSpinBox->setClearValue( 1.0 );
84 
85  // Connect saturation slider and spin box
86  connect( sliderSaturation, &QAbstractSlider::valueChanged, spinBoxSaturation, &QSpinBox::setValue );
87  connect( spinBoxSaturation, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), sliderSaturation, &QAbstractSlider::setValue );
88  spinBoxSaturation->setClearValue( 0 );
89 
90  // Connect colorize strength slider and spin box
91  connect( sliderColorizeStrength, &QAbstractSlider::valueChanged, spinColorizeStrength, &QSpinBox::setValue );
92  connect( spinColorizeStrength, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), sliderColorizeStrength, &QAbstractSlider::setValue );
93  spinColorizeStrength->setClearValue( 100 );
94 
95  // enable or disable saturation slider and spin box depending on grayscale combo choice
96  connect( comboGrayscale, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRendererRasterPropertiesWidget::toggleSaturationControls );
97 
98  // enable or disable colorize colorbutton with colorize checkbox
99  connect( mColorizeCheck, &QAbstractButton::toggled, this, &QgsRendererRasterPropertiesWidget::toggleColorizeControls );
100 
101  // Just connect the spin boxes because the sliders update the spinners
102  connect( mBrightnessSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
103  connect( mContrastSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
104  connect( mGammaSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
105  connect( spinBoxSaturation, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
106  connect( spinColorizeStrength, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
107  connect( btnColorizeColor, &QgsColorButton::colorChanged, this, &QgsPanelWidget::widgetChanged );
108  connect( mInvertColorsCheck, &QAbstractButton::toggled, this, &QgsPanelWidget::widgetChanged );
109 
110  connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
111  connect( mZoomedInResamplingComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
112  connect( mZoomedOutResamplingComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
113  connect( mMaximumOversamplingSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
114  connect( mCbEarlyResampling, &QAbstractButton::toggled, this, &QgsPanelWidget::widgetChanged );
115 
116  // finally sync to the layer - even though some actions may emit widgetChanged signal,
117  // this is not a problem - nobody is listening to our signals yet
118  syncToLayer( mRasterLayer );
119 
120  connect( mRasterLayer, &QgsMapLayer::styleChanged, this, &QgsRendererRasterPropertiesWidget::refreshAfterStyleChanged );
121 }
122 
124 {
125  mMapCanvas = canvas;
126 }
127 
129 {
130  const QString rendererName = cboRenderers->currentData().toString();
131  setRendererWidget( rendererName );
132  emit widgetChanged();
133 }
134 
136 {
137  if ( QgsBrightnessContrastFilter *brightnessFilter = mRasterLayer->brightnessFilter() )
138  {
139  brightnessFilter->setBrightness( mSliderBrightness->value() );
140  brightnessFilter->setContrast( mSliderContrast->value() );
141  brightnessFilter->setGamma( mGammaSpinBox->value() );
142  }
143 
144  if ( QgsRasterRendererWidget *rendererWidget = dynamic_cast<QgsRasterRendererWidget *>( stackedWidget->currentWidget() ) )
145  {
146  rendererWidget->doComputations();
147 
148  if ( QgsRasterRenderer *newRenderer = rendererWidget->renderer() )
149  {
150  // there are transparency related data stored in renderer instances, but they
151  // are not configured in the widget, so we need to copy them over from existing renderer
152  if ( QgsRasterRenderer *oldRenderer = mRasterLayer->renderer() )
153  newRenderer->copyCommonProperties( oldRenderer, false );
154  mRasterLayer->setRenderer( newRenderer );
155  }
156  }
157 
158  // Hue and saturation controls
159  if ( QgsHueSaturationFilter *hueSaturationFilter = mRasterLayer->hueSaturationFilter() )
160  {
161  hueSaturationFilter->setSaturation( sliderSaturation->value() );
162  hueSaturationFilter->setGrayscaleMode( ( QgsHueSaturationFilter::GrayscaleMode ) comboGrayscale->currentIndex() );
163  hueSaturationFilter->setColorizeOn( mColorizeCheck->checkState() );
164  hueSaturationFilter->setColorizeColor( btnColorizeColor->color() );
165  hueSaturationFilter->setColorizeStrength( sliderColorizeStrength->value() );
166  hueSaturationFilter->setInvertColors( mInvertColorsCheck->isChecked() );
167  }
168 
169  mResamplingUtils.refreshLayerFromWidgets();
170 
171  mRasterLayer->setBlendMode( mBlendModeComboBox->blendMode() );
172 }
173 
175 {
176  mRasterLayer = layer;
177 
178  cboRenderers->blockSignals( true );
179  cboRenderers->clear();
181  const auto constRenderersList = QgsApplication::rasterRendererRegistry()->renderersList();
182  for ( const QString &name : constRenderersList )
183  {
184  if ( QgsApplication::rasterRendererRegistry()->rendererData( name, entry ) )
185  {
186  if ( ( mRasterLayer->rasterType() != QgsRasterLayer::ColorLayer && entry.name != QLatin1String( "singlebandcolordata" ) ) ||
187  ( mRasterLayer->rasterType() == QgsRasterLayer::ColorLayer && entry.name == QLatin1String( "singlebandcolordata" ) ) )
188  {
189  cboRenderers->addItem( entry.icon(), entry.visibleName, entry.name );
190  }
191  }
192  }
193  cboRenderers->setCurrentIndex( -1 );
194  cboRenderers->blockSignals( false );
195 
196  if ( QgsRasterRenderer *renderer = mRasterLayer->renderer() )
197  {
198  setRendererWidget( renderer->type() );
199  }
200 
201  if ( QgsBrightnessContrastFilter *brightnessFilter = mRasterLayer->brightnessFilter() )
202  {
203  mSliderBrightness->setValue( brightnessFilter->brightness() );
204  mSliderContrast->setValue( brightnessFilter->contrast() );
205  mGammaSpinBox->setValue( brightnessFilter->gamma() );
206  }
207 
208  btnColorizeColor->setColorDialogTitle( tr( "Select Color" ) );
209  btnColorizeColor->setContext( QStringLiteral( "symbology" ) );
210 
211  // Hue and saturation color control
212  //set hue and saturation controls to current values
213  if ( const QgsHueSaturationFilter *hueSaturationFilter = mRasterLayer->hueSaturationFilter() )
214  {
215  sliderSaturation->setValue( hueSaturationFilter->saturation() );
216  comboGrayscale->setCurrentIndex( ( int ) hueSaturationFilter->grayscaleMode() );
217 
218  // Set initial state of saturation controls based on grayscale mode choice
219  toggleSaturationControls( static_cast<int>( hueSaturationFilter->grayscaleMode() ) );
220 
221  // Set initial state of colorize controls
222  mColorizeCheck->setChecked( hueSaturationFilter->colorizeOn() );
223  btnColorizeColor->setColor( hueSaturationFilter->colorizeColor() );
224  toggleColorizeControls( hueSaturationFilter->colorizeOn() );
225  sliderColorizeStrength->setValue( hueSaturationFilter->colorizeStrength() );
226 
227  mInvertColorsCheck->setChecked( hueSaturationFilter->invertColors() );
228  }
229 
230  //blend mode
231  mBlendModeComboBox->setShowClippingModes( QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject::instance(), mRasterLayer ) );
232  mBlendModeComboBox->setBlendMode( mRasterLayer->blendMode() );
233 
234  //set combo boxes to current resampling types
235  mResamplingUtils.refreshWidgetsFromLayer();
236 }
237 
238 void QgsRendererRasterPropertiesWidget::mResetColorRenderingBtn_clicked()
239 {
240  mBlendModeComboBox->setBlendMode( QPainter::CompositionMode_SourceOver );
241  mSliderBrightness->setValue( 0 );
242  mSliderContrast->setValue( 0 );
243  mGammaSpinBox->setValue( 1.0 );
244  sliderSaturation->setValue( 0 );
245  comboGrayscale->setCurrentIndex( ( int ) QgsHueSaturationFilter::GrayscaleOff );
246  mColorizeCheck->setChecked( false );
247  sliderColorizeStrength->setValue( 100 );
248  mInvertColorsCheck->setChecked( false );
249 }
250 
251 void QgsRendererRasterPropertiesWidget::toggleSaturationControls( int grayscaleMode )
252 {
253  // Enable or disable saturation controls based on choice of grayscale mode
254  if ( grayscaleMode == 0 )
255  {
256  sliderSaturation->setEnabled( true );
257  spinBoxSaturation->setEnabled( true );
258  }
259  else
260  {
261  sliderSaturation->setEnabled( false );
262  spinBoxSaturation->setEnabled( false );
263  }
264  emit widgetChanged();
265 }
266 
267 void QgsRendererRasterPropertiesWidget::toggleColorizeControls( bool colorizeEnabled )
268 {
269  // Enable or disable colorize controls based on checkbox
270  btnColorizeColor->setEnabled( colorizeEnabled );
271  sliderColorizeStrength->setEnabled( colorizeEnabled );
272  spinColorizeStrength->setEnabled( colorizeEnabled );
273  emit widgetChanged();
274 }
275 
276 void QgsRendererRasterPropertiesWidget::setRendererWidget( const QString &rendererName )
277 {
278  QgsDebugMsgLevel( "rendererName = " + rendererName, 3 );
279  QgsRasterRendererWidget *oldWidget = mRendererWidget;
280 
281  int alphaBand = -1;
282  double opacity = 1;
283  QColor nodataColor;
284  if ( QgsRasterRenderer *oldRenderer = mRasterLayer->renderer() )
285  {
286  // Retain alpha band and opacity when switching renderer
287  alphaBand = oldRenderer->alphaBand();
288  opacity = oldRenderer->opacity();
289  nodataColor = oldRenderer->nodataColor();
290  }
291 
292  QgsRasterRendererRegistryEntry rendererEntry;
293  if ( QgsApplication::rasterRendererRegistry()->rendererData( rendererName, rendererEntry ) )
294  {
295  if ( rendererEntry.widgetCreateFunction ) // Single band color data renderer e.g. has no widget
296  {
297  QgsDebugMsgLevel( QStringLiteral( "renderer has widgetCreateFunction" ), 3 );
298  // Current canvas extent (used to calc min/max) in layer CRS
299  const QgsRectangle myExtent = mMapCanvas->mapSettings().outputExtentToLayerExtent( mRasterLayer, mMapCanvas->extent() );
300  if ( oldWidget )
301  {
302  std::unique_ptr< QgsRasterRenderer > oldRenderer( oldWidget->renderer() );
303  if ( !oldRenderer || oldRenderer->type() != rendererName )
304  {
305  if ( rendererName == QLatin1String( "singlebandgray" ) )
306  {
307  whileBlocking( mRasterLayer )->setRenderer( QgsApplication::rasterRendererRegistry()->defaultRendererForDrawingStyle( QgsRaster::SingleBandGray, mRasterLayer->dataProvider() ) );
308  whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
309  }
310  else if ( rendererName == QLatin1String( "multibandcolor" ) )
311  {
312  whileBlocking( mRasterLayer )->setRenderer( QgsApplication::rasterRendererRegistry()->defaultRendererForDrawingStyle( QgsRaster::MultiBandColor, mRasterLayer->dataProvider() ) );
313  whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
314  }
315  }
316  }
317  mRasterLayer->renderer()->setAlphaBand( alphaBand );
318  mRasterLayer->renderer()->setOpacity( opacity );
319  mRasterLayer->renderer()->setNodataColor( nodataColor );
320  mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
321  mRendererWidget->setMapCanvas( mMapCanvas );
322  connect( mRendererWidget, &QgsRasterRendererWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
323  stackedWidget->addWidget( mRendererWidget );
324  stackedWidget->setCurrentWidget( mRendererWidget );
325  if ( oldWidget )
326  {
327  // Compare used bands in new and old renderer and reset transparency dialog if different
328  QgsRasterRenderer *oldRenderer = oldWidget->renderer();
329  QgsRasterRenderer *newRenderer = mRendererWidget->renderer();
330 #if 0
331  QList<int> oldBands = oldRenderer->usesBands();
332  QList<int> newBands = newRenderer->usesBands();
333 
334  if ( oldBands != newBands )
335  {
336  populateTransparencyTable( newRenderer );
337  }
338 #endif
339 
340  delete oldRenderer;
341  delete newRenderer;
342  }
343  }
344  }
345 
346  if ( mRendererWidget != oldWidget )
347  delete oldWidget;
348 
349  const int widgetIndex = cboRenderers->findData( rendererName );
350  if ( widgetIndex != -1 )
351  {
352  whileBlocking( cboRenderers )->setCurrentIndex( widgetIndex );
353  }
354 
355 }
356 
357 void QgsRendererRasterPropertiesWidget::refreshAfterStyleChanged()
358 {
359  if ( mRendererWidget )
360  {
361  QgsRasterRenderer *renderer = mRasterLayer->renderer();
362  if ( QgsMultiBandColorRenderer *mbcr = dynamic_cast<QgsMultiBandColorRenderer *>( renderer ) )
363  {
364  const QgsContrastEnhancement *redCe = mbcr->redContrastEnhancement();
365  if ( redCe )
366  {
367  mRendererWidget->setMin( QLocale().toString( redCe->minimumValue() ), 0 );
368  mRendererWidget->setMax( QLocale().toString( redCe->maximumValue() ), 0 );
369  }
370  const QgsContrastEnhancement *greenCe = mbcr->greenContrastEnhancement();
371  if ( greenCe )
372  {
373  mRendererWidget->setMin( QLocale().toString( greenCe->minimumValue() ), 1 );
374  mRendererWidget->setMax( QLocale().toString( greenCe->maximumValue() ), 1 );
375  }
376  const QgsContrastEnhancement *blueCe = mbcr->blueContrastEnhancement();
377  if ( blueCe )
378  {
379  mRendererWidget->setMin( QLocale().toString( blueCe->minimumValue() ), 2 );
380  mRendererWidget->setMax( QLocale().toString( blueCe->maximumValue() ), 2 );
381  }
382  }
383  else if ( QgsSingleBandGrayRenderer *sbgr = dynamic_cast<QgsSingleBandGrayRenderer *>( renderer ) )
384  {
385  const QgsContrastEnhancement *ce = sbgr->contrastEnhancement();
386  if ( ce )
387  {
388  mRendererWidget->setMin( QLocale().toString( ce->minimumValue() ) );
389  mRendererWidget->setMax( QLocale().toString( ce->maximumValue() ) );
390  }
391  }
392  }
393 }
394 
395 void QgsRendererRasterPropertiesWidget::updateGammaSpinBox( int value )
396 {
397  mGammaSpinBox->setValue( value / 100.0 );
398 }
399 
400 void QgsRendererRasterPropertiesWidget::updateGammaSlider( double value )
401 {
402  mSliderGamma->setValue( value * 100 );
403 }
static QgsRasterRendererRegistry * rasterRendererRegistry()
Returns the application's raster renderer registry, used for managing raster layer renderers.
Brightness/contrast and gamma correction filter pipe for rasters.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Factory method to create the renderer for this type.
Color and saturation filter pipe for rasters.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
A panel widget that can be shown in the map style dock.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
void styleChanged()
Signal emitted whenever a change affects the layer's style.
QgsRectangle outputExtentToLayerExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from output CRS to layer's CRS
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Renderer for multiband images with the color components.
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
void widgetChanged()
Emitted when the widget state changes.
static bool layerIsContainedInGroupLayer(QgsProject *project, QgsMapLayer *layer)
Returns true if the specified layer is a child layer from any QgsGroupLayer in the given project.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:470
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Widget creation function (mainly for the use by the renderer registry)
Represents a raster layer.
QgsBrightnessContrastFilter * brightnessFilter() const
Returns the raster's brightness/contrast filter.
LayerType rasterType()
Returns the raster layer type (which is a read only property).
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
void setRenderer(QgsRasterRenderer *renderer)
Sets the raster's renderer.
QgsHueSaturationFilter * hueSaturationFilter() const
Returns the raster's hue/saturation filter.
void insertWidgetFunction(const QString &rendererName, QgsRasterRendererWidgetCreateFunc func)
Abstract base class for widgets which configure a QgsRasterRenderer.
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
virtual void setMax(const QString &value, int index=0)
virtual void setMin(const QString &value, int index=0)
virtual QgsRasterRenderer * renderer()=0
Creates a new renderer, using the properties defined in the widget.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
void setAlphaBand(int band)
void setOpacity(double opacity)
Sets the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
void setNodataColor(const QColor &color)
Sets the color to use for shading nodata pixels.
@ MultiBandColor
Definition: qgsraster.h:100
@ SingleBandGray
Definition: qgsraster.h:92
A rectangle specified with double values.
Definition: qgsrectangle.h:42
void rendererChanged()
called when user changes renderer type
QgsRendererRasterPropertiesWidget(QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr)
A widget to hold the renderer properties for a raster layer.
void syncToLayer(QgsRasterLayer *layer)
Sync the widget to the given layer.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
void apply() override
Apply the changes from the dialog to the layer.
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Raster renderer pipe for single band gray.
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Creates new raster renderer widget.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:1517
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
Registry for raster renderer entries.
QgsRasterRendererWidgetCreateFunc widgetCreateFunction