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