-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsrendererrasterpropertieswidget.cpp
431 lines (372 loc) · 18 KB
/
qgsrendererrasterpropertieswidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/***************************************************************************
qgsrendererrasterpropertieswidget.cpp
---------------------
begin : May 2016
copyright : (C) 2016 by Nathan Woodrow
email : woodrow dot nathan at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsrendererrasterpropertieswidget.h"
#include "qgis.h"
#include "qgsmapcanvas.h"
#include "qgsbrightnesscontrastfilter.h"
#include "qgshuesaturationfilter.h"
#include "qgsrasterlayer.h"
#include "qgsrasterrendererwidget.h"
#include "qgsrasterrendererregistry.h"
#include "qgssinglebandgrayrendererwidget.h"
#include "qgssinglebandpseudocolorrendererwidget.h"
#include "qgsmultibandcolorrendererwidget.h"
#include "qgspalettedrendererwidget.h"
#include "qgshillshaderendererwidget.h"
#include "qgsrasterresamplefilter.h"
#include "qgsbilinearrasterresampler.h"
#include "qgscubicrasterresampler.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgssinglebandgrayrenderer.h"
#include "qgsmessagelog.h"
static void _initRendererWidgetFunctions()
{
static bool sInitialized = false;
if ( sInitialized )
return;
QgsApplication::rasterRendererRegistry()->insertWidgetFunction( QStringLiteral( "paletted" ), QgsPalettedRendererWidget::create );
QgsApplication::rasterRendererRegistry()->insertWidgetFunction( QStringLiteral( "multibandcolor" ), QgsMultiBandColorRendererWidget::create );
QgsApplication::rasterRendererRegistry()->insertWidgetFunction( QStringLiteral( "singlebandpseudocolor" ), QgsSingleBandPseudoColorRendererWidget::create );
QgsApplication::rasterRendererRegistry()->insertWidgetFunction( QStringLiteral( "singlebandgray" ), QgsSingleBandGrayRendererWidget::create );
QgsApplication::rasterRendererRegistry()->insertWidgetFunction( QStringLiteral( "hillshade" ), QgsHillshadeRendererWidget::create );
sInitialized = true;
}
QgsRendererRasterPropertiesWidget::QgsRendererRasterPropertiesWidget( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
: QgsMapLayerConfigWidget( layer, canvas, parent )
, mRendererWidget( nullptr )
{
mRasterLayer = qobject_cast<QgsRasterLayer *>( layer );
if ( !mRasterLayer )
return;
setupUi( this );
_initRendererWidgetFunctions();
mZoomedInResamplingComboBox->insertItem( 0, tr( "Nearest neighbour" ) );
mZoomedInResamplingComboBox->insertItem( 1, tr( "Bilinear" ) );
mZoomedInResamplingComboBox->insertItem( 2, tr( "Cubic" ) );
mZoomedOutResamplingComboBox->insertItem( 0, tr( "Nearest neighbour" ) );
mZoomedOutResamplingComboBox->insertItem( 1, tr( "Average" ) );
connect( cboRenderers, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRendererRasterPropertiesWidget::rendererChanged );
connect( mSliderBrightness, &QAbstractSlider::valueChanged, mBrightnessSpinBox, &QSpinBox::setValue );
connect( mBrightnessSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), mSliderBrightness, &QAbstractSlider::setValue );
connect( mSliderContrast, &QAbstractSlider::valueChanged, mContrastSpinBox, &QSpinBox::setValue );
connect( mContrastSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), mSliderContrast, &QAbstractSlider::setValue );
// Connect saturation slider and spin box
connect( sliderSaturation, &QAbstractSlider::valueChanged, spinBoxSaturation, &QSpinBox::setValue );
connect( spinBoxSaturation, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), sliderSaturation, &QAbstractSlider::setValue );
// Connect colorize strength slider and spin box
connect( sliderColorizeStrength, &QAbstractSlider::valueChanged, spinColorizeStrength, &QSpinBox::setValue );
connect( spinColorizeStrength, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), sliderColorizeStrength, &QAbstractSlider::setValue );
// enable or disable saturation slider and spin box depending on grayscale combo choice
connect( comboGrayscale, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRendererRasterPropertiesWidget::toggleSaturationControls );
// enable or disable colorize colorbutton with colorize checkbox
connect( mColorizeCheck, &QAbstractButton::toggled, this, &QgsRendererRasterPropertiesWidget::toggleColorizeControls );
// Just connect the spin boxes because the sliders update the spinners
connect( mBrightnessSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mContrastSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
connect( spinBoxSaturation, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
connect( spinColorizeStrength, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
connect( btnColorizeColor, &QgsColorButton::colorChanged, this, &QgsPanelWidget::widgetChanged );
connect( mBlendModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mZoomedInResamplingComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mZoomedOutResamplingComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mMaximumOversamplingSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
// finally sync to the layer - even though some actions may emit widgetChanged signal,
// this is not a problem - nobody is listening to our signals yet
syncToLayer( mRasterLayer );
connect( mRasterLayer, &QgsMapLayer::styleChanged, this, &QgsRendererRasterPropertiesWidget::refreshAfterStyleChanged );
}
void QgsRendererRasterPropertiesWidget::setMapCanvas( QgsMapCanvas *canvas )
{
mMapCanvas = canvas;
}
void QgsRendererRasterPropertiesWidget::rendererChanged()
{
QString rendererName = cboRenderers->currentData().toString();
setRendererWidget( rendererName );
emit widgetChanged();
}
void QgsRendererRasterPropertiesWidget::apply()
{
mRasterLayer->brightnessFilter()->setBrightness( mSliderBrightness->value() );
mRasterLayer->brightnessFilter()->setContrast( mSliderContrast->value() );
QgsRasterRendererWidget *rendererWidget = dynamic_cast<QgsRasterRendererWidget *>( stackedWidget->currentWidget() );
if ( rendererWidget )
{
rendererWidget->doComputations();
QgsRasterRenderer *newRenderer = rendererWidget->renderer();
// there are transparency related data stored in renderer instances, but they
// are not configured in the widget, so we need to copy them over from existing renderer
QgsRasterRenderer *oldRenderer = mRasterLayer->renderer();
if ( oldRenderer )
newRenderer->copyCommonProperties( oldRenderer, false );
mRasterLayer->setRenderer( newRenderer );
}
// Hue and saturation controls
QgsHueSaturationFilter *hueSaturationFilter = mRasterLayer->hueSaturationFilter();
if ( hueSaturationFilter )
{
hueSaturationFilter->setSaturation( sliderSaturation->value() );
hueSaturationFilter->setGrayscaleMode( ( QgsHueSaturationFilter::GrayscaleMode ) comboGrayscale->currentIndex() );
hueSaturationFilter->setColorizeOn( mColorizeCheck->checkState() );
hueSaturationFilter->setColorizeColor( btnColorizeColor->color() );
hueSaturationFilter->setColorizeStrength( sliderColorizeStrength->value() );
}
QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
if ( resampleFilter )
{
QgsRasterResampler *zoomedInResampler = nullptr;
QString zoomedInResamplingMethod = mZoomedInResamplingComboBox->currentText();
if ( zoomedInResamplingMethod == tr( "Bilinear" ) )
{
zoomedInResampler = new QgsBilinearRasterResampler();
}
else if ( zoomedInResamplingMethod == tr( "Cubic" ) )
{
zoomedInResampler = new QgsCubicRasterResampler();
}
resampleFilter->setZoomedInResampler( zoomedInResampler );
//raster resampling
QgsRasterResampler *zoomedOutResampler = nullptr;
QString zoomedOutResamplingMethod = mZoomedOutResamplingComboBox->currentText();
if ( zoomedOutResamplingMethod == tr( "Average" ) )
{
zoomedOutResampler = new QgsBilinearRasterResampler();
}
resampleFilter->setZoomedOutResampler( zoomedOutResampler );
resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
}
mRasterLayer->setBlendMode( mBlendModeComboBox->blendMode() );
}
void QgsRendererRasterPropertiesWidget::syncToLayer( QgsRasterLayer *layer )
{
mRasterLayer = layer;
cboRenderers->blockSignals( true );
cboRenderers->clear();
QgsRasterRendererRegistryEntry entry;
Q_FOREACH ( const QString &name, QgsApplication::rasterRendererRegistry()->renderersList() )
{
if ( QgsApplication::rasterRendererRegistry()->rendererData( name, entry ) )
{
if ( ( mRasterLayer->rasterType() != QgsRasterLayer::ColorLayer && entry.name != QLatin1String( "singlebandcolordata" ) ) ||
( mRasterLayer->rasterType() == QgsRasterLayer::ColorLayer && entry.name == QLatin1String( "singlebandcolordata" ) ) )
{
cboRenderers->addItem( entry.icon(), entry.visibleName, entry.name );
}
}
}
cboRenderers->setCurrentIndex( -1 );
cboRenderers->blockSignals( false );
QgsRasterRenderer *renderer = mRasterLayer->renderer();
if ( renderer )
{
setRendererWidget( renderer->type() );
}
QgsBrightnessContrastFilter *brightnessFilter = mRasterLayer->brightnessFilter();
if ( brightnessFilter )
{
mSliderBrightness->setValue( brightnessFilter->brightness() );
mSliderContrast->setValue( brightnessFilter->contrast() );
}
btnColorizeColor->setColorDialogTitle( tr( "Select Color" ) );
btnColorizeColor->setContext( QStringLiteral( "symbology" ) );
// Hue and saturation color control
const QgsHueSaturationFilter *hueSaturationFilter = mRasterLayer->hueSaturationFilter();
//set hue and saturation controls to current values
if ( hueSaturationFilter )
{
sliderSaturation->setValue( hueSaturationFilter->saturation() );
comboGrayscale->setCurrentIndex( ( int ) hueSaturationFilter->grayscaleMode() );
// Set initial state of saturation controls based on grayscale mode choice
toggleSaturationControls( ( int )hueSaturationFilter->grayscaleMode() );
// Set initial state of colorize controls
mColorizeCheck->setChecked( hueSaturationFilter->colorizeOn() );
btnColorizeColor->setColor( hueSaturationFilter->colorizeColor() );
toggleColorizeControls( hueSaturationFilter->colorizeOn() );
sliderColorizeStrength->setValue( hueSaturationFilter->colorizeStrength() );
}
//blend mode
mBlendModeComboBox->setBlendMode( mRasterLayer->blendMode() );
const QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
//set combo boxes to current resampling types
if ( resampleFilter )
{
const QgsRasterResampler *zoomedInResampler = resampleFilter->zoomedInResampler();
if ( zoomedInResampler )
{
if ( zoomedInResampler->type() == QLatin1String( "bilinear" ) )
{
mZoomedInResamplingComboBox->setCurrentIndex( 1 );
}
else if ( zoomedInResampler->type() == QLatin1String( "cubic" ) )
{
mZoomedInResamplingComboBox->setCurrentIndex( 2 );
}
}
else
{
mZoomedInResamplingComboBox->setCurrentIndex( 0 );
}
const QgsRasterResampler *zoomedOutResampler = resampleFilter->zoomedOutResampler();
if ( zoomedOutResampler )
{
if ( zoomedOutResampler->type() == QLatin1String( "bilinear" ) ) //bilinear resampler does averaging when zooming out
{
mZoomedOutResamplingComboBox->setCurrentIndex( 1 );
}
}
else
{
mZoomedOutResamplingComboBox->setCurrentIndex( 0 );
}
mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
}
}
void QgsRendererRasterPropertiesWidget::on_mResetColorRenderingBtn_clicked()
{
mBlendModeComboBox->setBlendMode( QPainter::CompositionMode_SourceOver );
mSliderBrightness->setValue( 0 );
mSliderContrast->setValue( 0 );
sliderSaturation->setValue( 0 );
comboGrayscale->setCurrentIndex( ( int ) QgsHueSaturationFilter::GrayscaleOff );
mColorizeCheck->setChecked( false );
sliderColorizeStrength->setValue( 100 );
}
void QgsRendererRasterPropertiesWidget::toggleSaturationControls( int grayscaleMode )
{
// Enable or disable saturation controls based on choice of grayscale mode
if ( grayscaleMode == 0 )
{
sliderSaturation->setEnabled( true );
spinBoxSaturation->setEnabled( true );
}
else
{
sliderSaturation->setEnabled( false );
spinBoxSaturation->setEnabled( false );
}
emit widgetChanged();
}
void QgsRendererRasterPropertiesWidget::toggleColorizeControls( bool colorizeEnabled )
{
// Enable or disable colorize controls based on checkbox
btnColorizeColor->setEnabled( colorizeEnabled );
sliderColorizeStrength->setEnabled( colorizeEnabled );
spinColorizeStrength->setEnabled( colorizeEnabled );
emit widgetChanged();
}
void QgsRendererRasterPropertiesWidget::setRendererWidget( const QString &rendererName )
{
QgsDebugMsg( "rendererName = " + rendererName );
QgsRasterRendererWidget *oldWidget = mRendererWidget;
QgsRasterRenderer *oldRenderer = mRasterLayer->renderer();
int alphaBand = -1;
double opacity = 1;
if ( oldRenderer )
{
// Retain alpha band and opacity when switching renderer
alphaBand = oldRenderer->alphaBand();
opacity = oldRenderer->opacity();
}
QgsRasterRendererRegistryEntry rendererEntry;
if ( QgsApplication::rasterRendererRegistry()->rendererData( rendererName, rendererEntry ) )
{
if ( rendererEntry.widgetCreateFunction ) // Single band color data renderer e.g. has no widget
{
QgsDebugMsg( "renderer has widgetCreateFunction" );
// Current canvas extent (used to calc min/max) in layer CRS
QgsRectangle myExtent = mMapCanvas->mapSettings().outputExtentToLayerExtent( mRasterLayer, mMapCanvas->extent() );
if ( oldWidget )
{
if ( rendererName == "singlebandgray" )
{
whileBlocking( mRasterLayer )->setRenderer( QgsApplication::rasterRendererRegistry()->defaultRendererForDrawingStyle( QgsRaster::SingleBandGray, mRasterLayer->dataProvider() ) );
whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
}
else if ( rendererName == "multibandcolor" )
{
whileBlocking( mRasterLayer )->setRenderer( QgsApplication::rasterRendererRegistry()->defaultRendererForDrawingStyle( QgsRaster::MultiBandColor, mRasterLayer->dataProvider() ) );
whileBlocking( mRasterLayer )->setDefaultContrastEnhancement();
}
}
mRasterLayer->renderer()->setAlphaBand( alphaBand );
mRasterLayer->renderer()->setOpacity( opacity );
mRendererWidget = rendererEntry.widgetCreateFunction( mRasterLayer, myExtent );
mRendererWidget->setMapCanvas( mMapCanvas );
connect( mRendererWidget, &QgsRasterRendererWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
stackedWidget->addWidget( mRendererWidget );
stackedWidget->setCurrentWidget( mRendererWidget );
if ( oldWidget )
{
// Compare used bands in new and old renderer and reset transparency dialog if different
QgsRasterRenderer *oldRenderer = oldWidget->renderer();
QgsRasterRenderer *newRenderer = mRendererWidget->renderer();
#if 0
QList<int> oldBands = oldRenderer->usesBands();
QList<int> newBands = newRenderer->usesBands();
if ( oldBands != newBands )
{
populateTransparencyTable( newRenderer );
}
#endif
delete oldRenderer;
delete newRenderer;
}
}
}
if ( mRendererWidget != oldWidget )
delete oldWidget;
int widgetIndex = cboRenderers->findData( rendererName );
if ( widgetIndex != -1 )
{
whileBlocking( cboRenderers )->setCurrentIndex( widgetIndex );
}
}
void QgsRendererRasterPropertiesWidget::refreshAfterStyleChanged()
{
if ( mRendererWidget )
{
QgsRasterRenderer *renderer = mRasterLayer->renderer();
if ( QgsMultiBandColorRenderer *mbcr = dynamic_cast<QgsMultiBandColorRenderer *>( renderer ) )
{
const QgsContrastEnhancement *redCe = mbcr->redContrastEnhancement();
if ( redCe )
{
mRendererWidget->setMin( QString::number( redCe->minimumValue() ), 0 );
mRendererWidget->setMax( QString::number( redCe->maximumValue() ), 0 );
}
const QgsContrastEnhancement *greenCe = mbcr->greenContrastEnhancement();
if ( greenCe )
{
mRendererWidget->setMin( QString::number( greenCe->minimumValue() ), 1 );
mRendererWidget->setMax( QString::number( greenCe->maximumValue() ), 1 );
}
const QgsContrastEnhancement *blueCe = mbcr->blueContrastEnhancement();
if ( blueCe )
{
mRendererWidget->setMin( QString::number( blueCe->minimumValue() ), 2 );
mRendererWidget->setMax( QString::number( blueCe->maximumValue() ), 2 );
}
}
else if ( QgsSingleBandGrayRenderer *sbgr = dynamic_cast<QgsSingleBandGrayRenderer *>( renderer ) )
{
const QgsContrastEnhancement *ce = sbgr->contrastEnhancement();
if ( ce )
{
mRendererWidget->setMin( QString::number( ce->minimumValue() ) );
mRendererWidget->setMax( QString::number( ce->maximumValue() ) );
}
}
}
}