QGIS API Documentation 4.3.0-Master (bf28115e945)
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 <QString>
28
29#include "moc_qgssinglebandgrayrendererwidget.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QgsRasterRendererWidget( layer, extent )
35{
36 setupUi( this );
37 connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged );
38 connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged );
39
40 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsSingleBandGrayRendererWidget::showLegendSettings );
41
42 mGradientComboBox->insertItem( 0, tr( "Black to White" ), QgsSingleBandGrayRenderer::BlackToWhite );
43 mGradientComboBox->insertItem( 1, tr( "White to Black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
44
45 mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
46 mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
47
48 if ( mRasterLayer )
49 {
50 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
51 if ( !provider )
52 {
53 return;
54 }
55
56 mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
57 mMinMaxWidget->setExtent( extent );
58 mMinMaxWidget->setMapCanvas( mCanvas );
59
60 QHBoxLayout *layout = new QHBoxLayout();
61 layout->setContentsMargins( 0, 0, 0, 0 );
62 mMinMaxContainerWidget->setLayout( layout );
63 layout->addWidget( mMinMaxWidget );
64
66
68
69 mGrayBandComboBox->setLayer( mRasterLayer );
70
71 //contrast enhancement algorithms
72 mContrastEnhancementComboBox->addItem( tr( "No Enhancement" ), QgsContrastEnhancement::NoEnhancement );
73 mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
74 mContrastEnhancementComboBox->addItem( tr( "Stretch and Clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
75 mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
76
77 setFromRenderer( layer->renderer() );
78
79 connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
80 connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
81 connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
82 }
83}
84
86{
87 if ( !mRasterLayer )
88 {
89 return nullptr;
90 }
91 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
92 if ( !provider )
93 {
94 return nullptr;
95 }
96 const int band = mGrayBandComboBox->currentBand();
97
98 QgsContrastEnhancement *e = new QgsContrastEnhancement( ( Qgis::DataType ) ( provider->dataType( band ) ) );
99 e->setMinimumValue( QgsDoubleValidator::toDouble( mMinLineEdit->text() ) );
100 e->setMaximumValue( QgsDoubleValidator::toDouble( mMaxLineEdit->text() ) );
101 e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm ) ( mContrastEnhancementComboBox->currentData().toInt() ) );
102
104 renderer->setContrastEnhancement( e );
105
106 renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
107 renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
108
109 renderer->setLegendSettings( new QgsColorRampLegendNodeSettings( mLegendSettings ) );
110
111 return renderer;
112}
113
115{
116 mMinMaxWidget->doComputations();
117}
118
120{
122 mMinMaxWidget->setMapCanvas( canvas );
123}
124
125void QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged( const QString & )
126{
127 QString text = mMinLineEdit->text();
128 int pos = 0;
129 if ( mMinLineEdit->validator()->validate( text, pos ) == QValidator::Acceptable )
130 {
131 minMaxModified();
132 }
133}
134
135void QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged( const QString & )
136{
137 QString text = mMaxLineEdit->text();
138 int pos = 0;
139 if ( mMaxLineEdit->validator()->validate( text, pos ) == QValidator::Acceptable )
140 {
141 minMaxModified();
142 }
143}
144
145void QgsSingleBandGrayRendererWidget::minMaxModified()
146{
147 if ( !mDisableMinMaxWidgetRefresh )
148 {
149 if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm ) ( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
150 {
151 mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum ) );
152 }
153 mMinMaxWidget->userHasSetManualMinMaxValues();
154 emit widgetChanged();
155 }
156}
157
158
159void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
160{
161 Q_UNUSED( bandNo )
162
163 QgsDebugMsgLevel( u"theBandNo = %1 min = %2 max = %3"_s.arg( bandNo ).arg( min ).arg( max ), 2 );
164
165 mDisableMinMaxWidgetRefresh = true;
166 if ( std::isnan( min ) )
167 {
168 mMinLineEdit->clear();
169 }
170 else
171 {
172 mMinLineEdit->setText( QLocale().toString( min ) );
173 }
174
175 if ( std::isnan( max ) )
176 {
177 mMaxLineEdit->clear();
178 }
179 else
180 {
181 mMaxLineEdit->setText( QLocale().toString( max ) );
182 }
183 mDisableMinMaxWidgetRefresh = false;
184}
185
186void QgsSingleBandGrayRendererWidget::bandChanged()
187{
188 QList<int> myBands;
189 myBands.append( mGrayBandComboBox->currentBand() );
190 mMinMaxWidget->setBands( myBands );
191 emit widgetChanged();
192}
193
195{
196 const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
197 if ( gr )
198 {
199 //band
200 mGrayBandComboBox->setBand( gr->inputBand() );
201 mMinMaxWidget->setBands( QList<int>() << gr->inputBand() );
202 mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
203
205 if ( ce )
206 {
207 //minmax
208 mDisableMinMaxWidgetRefresh = true;
209 mMinLineEdit->setText( QLocale().toString( ce->minimumValue() ) );
210 mMaxLineEdit->setText( QLocale().toString( ce->maximumValue() ) );
211 mDisableMinMaxWidgetRefresh = false;
212 //contrast enhancement algorithm
213 mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( ( int ) ( ce->contrastEnhancementAlgorithm() ) ) );
214 }
215
216 mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
217
218 if ( gr->legendSettings() )
219 mLegendSettings = *gr->legendSettings();
220 }
221}
222
223void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
224{
225 mDisableMinMaxWidgetRefresh = true;
226 mMinLineEdit->setText( value );
227 mDisableMinMaxWidgetRefresh = false;
228}
229
230void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
231{
232 mDisableMinMaxWidgetRefresh = true;
233 mMaxLineEdit->setText( value );
234 mDisableMinMaxWidgetRefresh = false;
235}
236
237void QgsSingleBandGrayRendererWidget::showLegendSettings()
238{
239 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast<QWidget *>( parent() ) );
240 if ( panel && panel->dockMode() )
241 {
243 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
244 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
245 legendPanel->setSettings( mLegendSettings );
246 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [this, legendPanel] {
247 mLegendSettings = legendPanel->settings();
248 emit widgetChanged();
249 } );
250 panel->openPanel( legendPanel );
251 }
252 else
253 {
254 QgsColorRampLegendNodeDialog dialog( mLegendSettings, this );
255 dialog.setUseContinuousRampCheckBoxVisibility( false );
256 dialog.setWindowTitle( tr( "Legend Settings" ) );
257 if ( dialog.exec() )
258 {
259 mLegendSettings = dialog.settings();
260 emit widgetChanged();
261 }
262 }
263}
264
269
271{
272 mDisableMinMaxWidgetRefresh = true;
273 mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( static_cast<int>( algorithm ) ) );
274 mDisableMinMaxWidgetRefresh = false;
275}
DataType
Raster data types.
Definition qgis.h:393
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:80