QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgssinglebandgrayrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssinglebandgrayrendererwidget.h
3 ---------------------------------
4 begin : March 2012
5 copyright : (C) 2012 by Marco Hugentobler
6 email : marco at sourcepole dot ch
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
19
21#include "qgsdoublevalidator.h"
23#include "qgsrasterlayer.h"
26
27#include "moc_qgssinglebandgrayrendererwidget.cpp"
28
30 : QgsRasterRendererWidget( layer, extent )
31{
32 setupUi( this );
33 connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged );
34 connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged );
35
36 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsSingleBandGrayRendererWidget::showLegendSettings );
37
38 mGradientComboBox->insertItem( 0, tr( "Black to White" ), QgsSingleBandGrayRenderer::BlackToWhite );
39 mGradientComboBox->insertItem( 1, tr( "White to Black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
40
41 mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
42 mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
43
44 if ( mRasterLayer )
45 {
46 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
47 if ( !provider )
48 {
49 return;
50 }
51
52 mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
53 mMinMaxWidget->setExtent( extent );
54 mMinMaxWidget->setMapCanvas( mCanvas );
55
56 QHBoxLayout *layout = new QHBoxLayout();
57 layout->setContentsMargins( 0, 0, 0, 0 );
58 mMinMaxContainerWidget->setLayout( layout );
59 layout->addWidget( mMinMaxWidget );
60
62
64
65 mGrayBandComboBox->setLayer( mRasterLayer );
66
67 //contrast enhancement algorithms
68 mContrastEnhancementComboBox->addItem( tr( "No Enhancement" ), QgsContrastEnhancement::NoEnhancement );
69 mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
70 mContrastEnhancementComboBox->addItem( tr( "Stretch and Clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
71 mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
72
73 setFromRenderer( layer->renderer() );
74
75 connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
76 connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
77 connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
78 }
79}
80
82{
83 if ( !mRasterLayer )
84 {
85 return nullptr;
86 }
87 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
88 if ( !provider )
89 {
90 return nullptr;
91 }
92 const int band = mGrayBandComboBox->currentBand();
93
95 provider->dataType( band )
96 ) );
97 e->setMinimumValue( QgsDoubleValidator::toDouble( mMinLineEdit->text() ) );
98 e->setMaximumValue( QgsDoubleValidator::toDouble( mMaxLineEdit->text() ) );
99 e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
100
102 renderer->setContrastEnhancement( e );
103
104 renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
105 renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
106
107 renderer->setLegendSettings( new QgsColorRampLegendNodeSettings( mLegendSettings ) );
108
109 return renderer;
110}
111
113{
114 mMinMaxWidget->doComputations();
115}
116
118{
120 mMinMaxWidget->setMapCanvas( canvas );
121}
122
123void QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged( const QString & )
124{
125 minMaxModified();
126}
127
128void QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged( const QString & )
129{
130 minMaxModified();
131}
132
133void QgsSingleBandGrayRendererWidget::minMaxModified()
134{
135 if ( !mDisableMinMaxWidgetRefresh )
136 {
137 if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
138 {
139 mContrastEnhancementComboBox->setCurrentIndex(
140 mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum )
141 );
142 }
143 mMinMaxWidget->userHasSetManualMinMaxValues();
144 emit widgetChanged();
145 }
146}
147
148
149void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
150{
151 Q_UNUSED( bandNo )
152
153 QgsDebugMsgLevel( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ), 2 );
154
155 mDisableMinMaxWidgetRefresh = true;
156 if ( std::isnan( min ) )
157 {
158 mMinLineEdit->clear();
159 }
160 else
161 {
162 mMinLineEdit->setText( QLocale().toString( min ) );
163 }
164
165 if ( std::isnan( max ) )
166 {
167 mMaxLineEdit->clear();
168 }
169 else
170 {
171 mMaxLineEdit->setText( QLocale().toString( max ) );
172 }
173 mDisableMinMaxWidgetRefresh = false;
174}
175
176void QgsSingleBandGrayRendererWidget::bandChanged()
177{
178 QList<int> myBands;
179 myBands.append( mGrayBandComboBox->currentBand() );
180 mMinMaxWidget->setBands( myBands );
181 emit widgetChanged();
182}
183
185{
186 const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
187 if ( gr )
188 {
189 //band
190 mGrayBandComboBox->setBand( gr->inputBand() );
191 mMinMaxWidget->setBands( QList<int>() << gr->inputBand() );
192 mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
193
195 if ( ce )
196 {
197 //minmax
198 mDisableMinMaxWidgetRefresh = true;
199 mMinLineEdit->setText( QLocale().toString( ce->minimumValue() ) );
200 mMaxLineEdit->setText( QLocale().toString( ce->maximumValue() ) );
201 mDisableMinMaxWidgetRefresh = false;
202 //contrast enhancement algorithm
203 mContrastEnhancementComboBox->setCurrentIndex(
204 mContrastEnhancementComboBox->findData( ( int ) ( ce->contrastEnhancementAlgorithm() ) )
205 );
206 }
207
208 mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
209
210 if ( gr->legendSettings() )
211 mLegendSettings = *gr->legendSettings();
212 }
213}
214
215void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
216{
217 mDisableMinMaxWidgetRefresh = true;
218 mMinLineEdit->setText( value );
219 mDisableMinMaxWidgetRefresh = false;
220}
221
222void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
223{
224 mDisableMinMaxWidgetRefresh = true;
225 mMaxLineEdit->setText( value );
226 mDisableMinMaxWidgetRefresh = false;
227}
228
229void QgsSingleBandGrayRendererWidget::showLegendSettings()
230{
231 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast<QWidget *>( parent() ) );
232 if ( panel && panel->dockMode() )
233 {
235 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
236 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
237 legendPanel->setSettings( mLegendSettings );
238 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [this, legendPanel] {
239 mLegendSettings = legendPanel->settings();
240 emit widgetChanged();
241 } );
242 panel->openPanel( legendPanel );
243 }
244 else
245 {
246 QgsColorRampLegendNodeDialog dialog( mLegendSettings, this );
247 dialog.setUseContinuousRampCheckBoxVisibility( false );
248 dialog.setWindowTitle( tr( "Legend Settings" ) );
249 if ( dialog.exec() )
250 {
251 mLegendSettings = dialog.settings();
252 emit widgetChanged();
253 }
254 }
255}
256
261
263{
264 mDisableMinMaxWidgetRefresh = true;
265 mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( static_cast<int>( algorithm ) ) );
266 mDisableMinMaxWidgetRefresh = false;
267}
DataType
Raster data types.
Definition qgis.h:372
Settings for a color ramp legend node.
A widget for properties relating to a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the widget.
void setSettings(const QgsColorRampLegendNodeSettings &settings)
Sets the settings to show in the widget.
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox to visible.
Handles contrast enhancement and clipping.
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
@ StretchToMinimumMaximum
Linear histogram.
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
A custom validator which allows entry of doubles in a locale-tolerant way.
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
Map canvas is a class for displaying all GIS data types on a canvas.
Base class for any widget that can be shown as an inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
bool dockMode() const
Returns the dock mode state.
void widgetChanged()
Emitted when the widget state changes.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
void bandChanged(int band)
Emitted when the currently selected band changes.
Base class for raster data providers.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
Represents a raster layer.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
A widget for configuring how the minimum and maximum value of a raster layer is determined.
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
void setBands(const QList< int > &bands)
void widgetChanged()
Emitted when something on the widget has changed.
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
QgsMapCanvas * mCanvas
Associated map canvas.
QgsRasterRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent)
Constructor for QgsRasterRendererWidget.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
A rectangle specified with double values.
void setFromRenderer(const QgsRasterRenderer *r)
Sets the widget state from the specified renderer.
QgsRasterRenderer * renderer() override
Creates a new renderer, using the properties defined in the widget.
void doComputations() override
Load programmatically with current values.
QgsSingleBandGrayRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Constructor for QgsSingleBandGrayRendererWidget.
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
QgsContrastEnhancement::ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const override
Returns the contrast enhancement algorithm to be used by the raster renderer.
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
void setMax(const QString &value, int index=0) override
void setMin(const QString &value, int index=0) override
void setContrastEnhancementAlgorithm(QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm) override
Sets the contrast enhancement algorithm to be used by the raster renderer.
Raster renderer pipe for single band gray.
const QgsContrastEnhancement * contrastEnhancement() const
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61