QGIS API Documentation 3.29.0-Master (19d7edcfed)
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"
22#include "qgsrasterlayer.h"
32#include "qgsapplication.h"
33#include "qgsproject.h"
34#include "qgsprojectutils.h"
35
36static void _initRendererWidgetFunctions()
37{
38 static bool sInitialized = false;
39 if ( sInitialized )
40 return;
41
48
49 sInitialized = true;
50}
51
52
53
55 : QgsMapLayerConfigWidget( layer, canvas, parent )
56 , mRasterLayer( qobject_cast<QgsRasterLayer *>( layer ) )
57{
58 if ( !mRasterLayer )
59 return;
60
61 setupUi( this );
62 connect( mResetColorRenderingBtn, &QToolButton::clicked, this, &QgsRendererRasterPropertiesWidget::mResetColorRenderingBtn_clicked );
63
64 _initRendererWidgetFunctions();
65
66 mResamplingUtils.initWidgets( mRasterLayer, mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox, mMaximumOversamplingSpinBox, mCbEarlyResampling );
67
68 connect( cboRenderers, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRendererRasterPropertiesWidget::rendererChanged );
69
70 connect( mSliderBrightness, &QAbstractSlider::valueChanged, mBrightnessSpinBox, &QSpinBox::setValue );
71 connect( mBrightnessSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), mSliderBrightness, &QAbstractSlider::setValue );
72 mBrightnessSpinBox->setClearValue( 0 );
73
74 connect( mSliderContrast, &QAbstractSlider::valueChanged, mContrastSpinBox, &QSpinBox::setValue );
75 connect( mContrastSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), mSliderContrast, &QAbstractSlider::setValue );
76 mContrastSpinBox->setClearValue( 0 );
77
78 connect( mSliderGamma, &QAbstractSlider::valueChanged, this, &QgsRendererRasterPropertiesWidget::updateGammaSpinBox );
79 connect( mGammaSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsRendererRasterPropertiesWidget::updateGammaSlider );
80 mGammaSpinBox->setClearValue( 1.0 );
81
82 // Connect saturation slider and spin box
83 connect( sliderSaturation, &QAbstractSlider::valueChanged, spinBoxSaturation, &QSpinBox::setValue );
84 connect( spinBoxSaturation, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), sliderSaturation, &QAbstractSlider::setValue );
85 spinBoxSaturation->setClearValue( 0 );
86
87 // Connect colorize strength slider and spin box
88 connect( sliderColorizeStrength, &QAbstractSlider::valueChanged, spinColorizeStrength, &QSpinBox::setValue );
89 connect( spinColorizeStrength, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), sliderColorizeStrength, &QAbstractSlider::setValue );
90 spinColorizeStrength->setClearValue( 100 );
91
92 // enable or disable saturation slider and spin box depending on grayscale combo choice
93 connect( comboGrayscale, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRendererRasterPropertiesWidget::toggleSaturationControls );
94
95 // enable or disable colorize colorbutton with colorize checkbox
96 connect( mColorizeCheck, &QAbstractButton::toggled, this, &QgsRendererRasterPropertiesWidget::toggleColorizeControls );
97
98 // Just connect the spin boxes because the sliders update the spinners
99 connect( mBrightnessSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
100 connect( mContrastSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
101 connect( mGammaSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
102 connect( spinBoxSaturation, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
103 connect( spinColorizeStrength, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
104 connect( btnColorizeColor, &QgsColorButton::colorChanged, this, &QgsPanelWidget::widgetChanged );
105 connect( mInvertColorsCheck, &QAbstractButton::toggled, this, &QgsPanelWidget::widgetChanged );
106
107 connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
108 connect( mZoomedInResamplingComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
109 connect( mZoomedOutResamplingComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
110 connect( mMaximumOversamplingSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
111 connect( mCbEarlyResampling, &QAbstractButton::toggled, this, &QgsPanelWidget::widgetChanged );
112
113 // finally sync to the layer - even though some actions may emit widgetChanged signal,
114 // this is not a problem - nobody is listening to our signals yet
115 syncToLayer( mRasterLayer );
116
117 connect( mRasterLayer, &QgsMapLayer::styleChanged, this, &QgsRendererRasterPropertiesWidget::refreshAfterStyleChanged );
118}
119
121{
122 mMapCanvas = canvas;
123}
124
126{
127 const QString rendererName = cboRenderers->currentData().toString();
128 setRendererWidget( rendererName );
129 emit widgetChanged();
130}
131
133{
134 if ( QgsBrightnessContrastFilter *brightnessFilter = mRasterLayer->brightnessFilter() )
135 {
136 brightnessFilter->setBrightness( mSliderBrightness->value() );
137 brightnessFilter->setContrast( mSliderContrast->value() );
138 brightnessFilter->setGamma( mGammaSpinBox->value() );
139 }
140
141 if ( QgsRasterRendererWidget *rendererWidget = dynamic_cast<QgsRasterRendererWidget *>( stackedWidget->currentWidget() ) )
142 {
143 rendererWidget->doComputations();
144
145 if ( QgsRasterRenderer *newRenderer = rendererWidget->renderer() )
146 {
147 // there are transparency related data stored in renderer instances, but they
148 // are not configured in the widget, so we need to copy them over from existing renderer
149 if ( QgsRasterRenderer *oldRenderer = mRasterLayer->renderer() )
150 newRenderer->copyCommonProperties( oldRenderer, false );
151 mRasterLayer->setRenderer( newRenderer );
152 }
153 }
154
155 // Hue and saturation controls
156 if ( QgsHueSaturationFilter *hueSaturationFilter = mRasterLayer->hueSaturationFilter() )
157 {
158 hueSaturationFilter->setSaturation( sliderSaturation->value() );
159 hueSaturationFilter->setGrayscaleMode( ( QgsHueSaturationFilter::GrayscaleMode ) comboGrayscale->currentIndex() );
160 hueSaturationFilter->setColorizeOn( mColorizeCheck->checkState() );
161 hueSaturationFilter->setColorizeColor( btnColorizeColor->color() );
162 hueSaturationFilter->setColorizeStrength( sliderColorizeStrength->value() );
163 hueSaturationFilter->setInvertColors( mInvertColorsCheck->isChecked() );
164 }
165
166 mResamplingUtils.refreshLayerFromWidgets();
167
168 mRasterLayer->setBlendMode( mBlendModeComboBox->blendMode() );
169}
170
172{
173 mRasterLayer = layer;
174
175 cboRenderers->blockSignals( true );
176 cboRenderers->clear();
178 const auto constRenderersList = QgsApplication::rasterRendererRegistry()->renderersList();
179 for ( const QString &name : constRenderersList )
180 {
181 if ( QgsApplication::rasterRendererRegistry()->rendererData( name, entry ) )
182 {
183 if ( ( mRasterLayer->rasterType() != Qgis::RasterLayerType::SingleBandColorData && entry.name != QLatin1String( "singlebandcolordata" ) ) ||
184 ( mRasterLayer->rasterType() == Qgis::RasterLayerType::SingleBandColorData && entry.name == QLatin1String( "singlebandcolordata" ) ) )
185 {
186 cboRenderers->addItem( entry.icon(), entry.visibleName, entry.name );
187 }
188 }
189 }
190 cboRenderers->setCurrentIndex( -1 );
191 cboRenderers->blockSignals( false );
192
193 if ( QgsRasterRenderer *renderer = mRasterLayer->renderer() )
194 {
195 setRendererWidget( renderer->type() );
196 }
197
198 if ( QgsBrightnessContrastFilter *brightnessFilter = mRasterLayer->brightnessFilter() )
199 {
200 mSliderBrightness->setValue( brightnessFilter->brightness() );
201 mSliderContrast->setValue( brightnessFilter->contrast() );
202 mGammaSpinBox->setValue( brightnessFilter->gamma() );
203 }
204
205 btnColorizeColor->setColorDialogTitle( tr( "Select Color" ) );
206 btnColorizeColor->setContext( QStringLiteral( "symbology" ) );
207
208 // Hue and saturation color control
209 //set hue and saturation controls to current values
210 if ( const QgsHueSaturationFilter *hueSaturationFilter = mRasterLayer->hueSaturationFilter() )
211 {
212 sliderSaturation->setValue( hueSaturationFilter->saturation() );
213 comboGrayscale->setCurrentIndex( ( int ) hueSaturationFilter->grayscaleMode() );
214
215 // Set initial state of saturation controls based on grayscale mode choice
216 toggleSaturationControls( static_cast<int>( hueSaturationFilter->grayscaleMode() ) );
217
218 // Set initial state of colorize controls
219 mColorizeCheck->setChecked( hueSaturationFilter->colorizeOn() );
220 btnColorizeColor->setColor( hueSaturationFilter->colorizeColor() );
221 toggleColorizeControls( hueSaturationFilter->colorizeOn() );
222 sliderColorizeStrength->setValue( hueSaturationFilter->colorizeStrength() );
223
224 mInvertColorsCheck->setChecked( hueSaturationFilter->invertColors() );
225 }
226
227 //blend mode
228 mBlendModeComboBox->setShowClippingModes( QgsProjectUtils::layerIsContainedInGroupLayer( QgsProject::instance(), mRasterLayer ) );
229 mBlendModeComboBox->setBlendMode( mRasterLayer->blendMode() );
230
231 //set combo boxes to current resampling types
232 mResamplingUtils.refreshWidgetsFromLayer();
233}
234
235void QgsRendererRasterPropertiesWidget::mResetColorRenderingBtn_clicked()
236{
237 mBlendModeComboBox->setBlendMode( QPainter::CompositionMode_SourceOver );
238 mSliderBrightness->setValue( 0 );
239 mSliderContrast->setValue( 0 );
240 mGammaSpinBox->setValue( 1.0 );
241 sliderSaturation->setValue( 0 );
242 comboGrayscale->setCurrentIndex( ( int ) QgsHueSaturationFilter::GrayscaleOff );
243 mColorizeCheck->setChecked( false );
244 sliderColorizeStrength->setValue( 100 );
245 mInvertColorsCheck->setChecked( false );
246}
247
248void QgsRendererRasterPropertiesWidget::toggleSaturationControls( int grayscaleMode )
249{
250 // Enable or disable saturation controls based on choice of grayscale mode
251 if ( grayscaleMode == 0 )
252 {
253 sliderSaturation->setEnabled( true );
254 spinBoxSaturation->setEnabled( true );
255 }
256 else
257 {
258 sliderSaturation->setEnabled( false );
259 spinBoxSaturation->setEnabled( false );
260 }
261 emit widgetChanged();
262}
263
264void QgsRendererRasterPropertiesWidget::toggleColorizeControls( bool colorizeEnabled )
265{
266 // Enable or disable colorize controls based on checkbox
267 btnColorizeColor->setEnabled( colorizeEnabled );
268 sliderColorizeStrength->setEnabled( colorizeEnabled );
269 spinColorizeStrength->setEnabled( colorizeEnabled );
270 emit widgetChanged();
271}
272
273void QgsRendererRasterPropertiesWidget::setRendererWidget( const QString &rendererName )
274{
275 QgsDebugMsgLevel( "rendererName = " + rendererName, 3 );
276 QgsRasterRendererWidget *oldWidget = mRendererWidget;
277
278 int alphaBand = -1;
279 double opacity = 1;
280 QColor nodataColor;
281 if ( QgsRasterRenderer *oldRenderer = mRasterLayer->renderer() )
282 {
283 // Retain alpha band and opacity when switching renderer
284 alphaBand = oldRenderer->alphaBand();
285 opacity = oldRenderer->opacity();
286 nodataColor = oldRenderer->nodataColor();
287 }
288
289 QgsRasterRendererRegistryEntry rendererEntry;
290 if ( QgsApplication::rasterRendererRegistry()->rendererData( rendererName, rendererEntry ) )
291 {
292 if ( rendererEntry.widgetCreateFunction ) // Single band color data renderer e.g. has no widget
293 {
294 QgsDebugMsgLevel( QStringLiteral( "renderer has widgetCreateFunction" ), 3 );
295 // Current canvas extent (used to calc min/max) in layer CRS
298 : mRasterLayer->extent();
299 if ( oldWidget )
300 {
301 std::unique_ptr< QgsRasterRenderer > oldRenderer( oldWidget->renderer() );
302 if ( !oldRenderer || oldRenderer->type() != rendererName )
303 {
304 if ( rendererName == QLatin1String( "singlebandgray" ) )
305 {
306 whileBlocking( mRasterLayer )->setRenderer( QgsApplication::rasterRendererRegistry()->defaultRendererForDrawingStyle( Qgis::RasterDrawingStyle::SingleBandGray, mRasterLayer->dataProvider() ) );
307 whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
308 }
309 else if ( rendererName == QLatin1String( "multibandcolor" ) )
310 {
311 whileBlocking( mRasterLayer )->setRenderer( QgsApplication::rasterRendererRegistry()->defaultRendererForDrawingStyle( Qgis::RasterDrawingStyle::MultiBandColor, mRasterLayer->dataProvider() ) );
312 whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
313 }
314 }
315 }
316 mRasterLayer->renderer()->setAlphaBand( alphaBand );
317 mRasterLayer->renderer()->setOpacity( opacity );
318 mRasterLayer->renderer()->setNodataColor( nodataColor );
319 mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
320 mRendererWidget->setMapCanvas( mMapCanvas );
322 stackedWidget->addWidget( mRendererWidget );
323 stackedWidget->setCurrentWidget( mRendererWidget );
324 if ( oldWidget )
325 {
326 // Compare used bands in new and old renderer and reset transparency dialog if different
327 QgsRasterRenderer *oldRenderer = oldWidget->renderer();
328 QgsRasterRenderer *newRenderer = mRendererWidget->renderer();
329#if 0
330 QList<int> oldBands = oldRenderer->usesBands();
331 QList<int> newBands = newRenderer->usesBands();
332
333 if ( oldBands != newBands )
334 {
335 populateTransparencyTable( newRenderer );
336 }
337#endif
338
339 delete oldRenderer;
340 delete newRenderer;
341 }
342 }
343 }
344
345 if ( mRendererWidget != oldWidget )
346 delete oldWidget;
347
348 const int widgetIndex = cboRenderers->findData( rendererName );
349 if ( widgetIndex != -1 )
350 {
351 whileBlocking( cboRenderers )->setCurrentIndex( widgetIndex );
352 }
353
354}
355
356void QgsRendererRasterPropertiesWidget::refreshAfterStyleChanged()
357{
358 if ( mRendererWidget )
359 {
360 QgsRasterRenderer *renderer = mRasterLayer->renderer();
361 if ( QgsMultiBandColorRenderer *mbcr = dynamic_cast<QgsMultiBandColorRenderer *>( renderer ) )
362 {
363 const QgsContrastEnhancement *redCe = mbcr->redContrastEnhancement();
364 if ( redCe )
365 {
366 mRendererWidget->setMin( QLocale().toString( redCe->minimumValue() ), 0 );
367 mRendererWidget->setMax( QLocale().toString( redCe->maximumValue() ), 0 );
368 }
369 const QgsContrastEnhancement *greenCe = mbcr->greenContrastEnhancement();
370 if ( greenCe )
371 {
372 mRendererWidget->setMin( QLocale().toString( greenCe->minimumValue() ), 1 );
373 mRendererWidget->setMax( QLocale().toString( greenCe->maximumValue() ), 1 );
374 }
375 const QgsContrastEnhancement *blueCe = mbcr->blueContrastEnhancement();
376 if ( blueCe )
377 {
378 mRendererWidget->setMin( QLocale().toString( blueCe->minimumValue() ), 2 );
379 mRendererWidget->setMax( QLocale().toString( blueCe->maximumValue() ), 2 );
380 }
381 }
382 else if ( QgsSingleBandGrayRenderer *sbgr = dynamic_cast<QgsSingleBandGrayRenderer *>( renderer ) )
383 {
384 const QgsContrastEnhancement *ce = sbgr->contrastEnhancement();
385 if ( ce )
386 {
387 mRendererWidget->setMin( QLocale().toString( ce->minimumValue() ) );
388 mRendererWidget->setMax( QLocale().toString( ce->maximumValue() ) );
389 }
390 }
391 }
392}
393
394void QgsRendererRasterPropertiesWidget::updateGammaSpinBox( int value )
395{
396 mGammaSpinBox->setValue( value / 100.0 );
397}
398
399void QgsRendererRasterPropertiesWidget::updateGammaSlider( double value )
400{
401 mSliderGamma->setValue( value * 100 );
402}
@ SingleBandGray
A single band image drawn as a range of gray colors.
@ MultiBandColor
A layer containing 2 or more bands, mapped to RGB color space. In the case of a multiband with only t...
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 bool isTransformationPossible(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination)
Returns true if it is theoretically possible to transform between source and destination CRSes.
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
virtual QgsRectangle extent() const
Returns the extent of the layer.
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
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
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
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:477
static QgsRasterRendererWidget * create(QgsRasterLayer *layer, const QgsRectangle &extent)
Widget creation function (mainly for the use by the renderer registry)
Represents a raster layer.
Qgis::RasterLayerType rasterType() const
Returns the raster layer type (which is a read only property).
QgsBrightnessContrastFilter * brightnessFilter() const
Returns the raster's brightness/contrast filter.
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.
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
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....
void setNodataColor(const QColor &color)
Sets the color to use for shading nodata pixels.
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:3003
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
Registry for raster renderer entries.
QgsRasterRendererWidgetCreateFunc widgetCreateFunction