QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "moc_qgssinglebandgrayrendererwidget.cpp"
21#include "qgsrasterlayer.h"
24#include "qgsdoublevalidator.h"
26
28 : QgsRasterRendererWidget( layer, extent )
29 , mDisableMinMaxWidgetRefresh( false )
30{
31 setupUi( this );
32 connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged );
33 connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged );
34
35 connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsSingleBandGrayRendererWidget::showLegendSettings );
36
37 mGradientComboBox->insertItem( 0, tr( "Black to White" ), QgsSingleBandGrayRenderer::BlackToWhite );
38 mGradientComboBox->insertItem( 1, tr( "White to Black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
39
40 mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
41 mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
42
43 if ( mRasterLayer )
44 {
46 if ( !provider )
47 {
48 return;
49 }
50
51 mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
52 mMinMaxWidget->setExtent( extent );
53 mMinMaxWidget->setMapCanvas( mCanvas );
54
55 QHBoxLayout *layout = new QHBoxLayout();
56 layout->setContentsMargins( 0, 0, 0, 0 );
57 mMinMaxContainerWidget->setLayout( layout );
58 layout->addWidget( mMinMaxWidget );
59
61
63
64 mGrayBandComboBox->setLayer( mRasterLayer );
65
66 //contrast enhancement algorithms
67 mContrastEnhancementComboBox->addItem( tr( "No Enhancement" ), QgsContrastEnhancement::NoEnhancement );
68 mContrastEnhancementComboBox->addItem( tr( "Stretch to MinMax" ), QgsContrastEnhancement::StretchToMinimumMaximum );
69 mContrastEnhancementComboBox->addItem( tr( "Stretch and Clip to MinMax" ), QgsContrastEnhancement::StretchAndClipToMinimumMaximum );
70 mContrastEnhancementComboBox->addItem( tr( "Clip to MinMax" ), QgsContrastEnhancement::ClipToMinimumMaximum );
71
72 setFromRenderer( layer->renderer() );
73
74 connect( mGrayBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandGrayRendererWidget::bandChanged );
75 connect( mGradientComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
76 connect( mContrastEnhancementComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterRendererWidget::widgetChanged );
77 }
78}
79
81{
82 if ( !mRasterLayer )
83 {
84 return nullptr;
85 }
87 if ( !provider )
88 {
89 return nullptr;
90 }
91 const int band = mGrayBandComboBox->currentBand();
92
94 provider->dataType( band )
95 ) );
96 e->setMinimumValue( QgsDoubleValidator::toDouble( mMinLineEdit->text() ) );
97 e->setMaximumValue( QgsDoubleValidator::toDouble( mMaxLineEdit->text() ) );
98 e->setContrastEnhancementAlgorithm( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) );
99
101 renderer->setContrastEnhancement( e );
102
103 renderer->setGradient( ( QgsSingleBandGrayRenderer::Gradient ) mGradientComboBox->currentData().toInt() );
104 renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
105
106 renderer->setLegendSettings( new QgsColorRampLegendNodeSettings( mLegendSettings ) );
107
108 return renderer;
109}
110
115
117{
119 mMinMaxWidget->setMapCanvas( canvas );
120}
121
122void QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged( const QString & )
123{
124 minMaxModified();
125}
126
127void QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged( const QString & )
128{
129 minMaxModified();
130}
131
132void QgsSingleBandGrayRendererWidget::minMaxModified()
133{
134 if ( !mDisableMinMaxWidgetRefresh )
135 {
136 if ( ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )( mContrastEnhancementComboBox->currentData().toInt() ) == QgsContrastEnhancement::NoEnhancement )
137 {
138 mContrastEnhancementComboBox->setCurrentIndex(
139 mContrastEnhancementComboBox->findData( ( int ) QgsContrastEnhancement::StretchToMinimumMaximum )
140 );
141 }
142 mMinMaxWidget->userHasSetManualMinMaxValues();
143 emit widgetChanged();
144 }
145}
146
147
148void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
149{
150 Q_UNUSED( bandNo )
151
152 QgsDebugMsgLevel( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ), 2 );
153
154 mDisableMinMaxWidgetRefresh = true;
155 if ( std::isnan( min ) )
156 {
157 mMinLineEdit->clear();
158 }
159 else
160 {
161 mMinLineEdit->setText( QLocale().toString( min ) );
162 }
163
164 if ( std::isnan( max ) )
165 {
166 mMaxLineEdit->clear();
167 }
168 else
169 {
170 mMaxLineEdit->setText( QLocale().toString( max ) );
171 }
172 mDisableMinMaxWidgetRefresh = false;
173}
174
175void QgsSingleBandGrayRendererWidget::bandChanged()
176{
177 QList<int> myBands;
178 myBands.append( mGrayBandComboBox->currentBand() );
179 mMinMaxWidget->setBands( myBands );
180 emit widgetChanged();
181}
182
184{
185 const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
186 if ( gr )
187 {
188 //band
189 mGrayBandComboBox->setBand( gr->inputBand() );
190 mMinMaxWidget->setBands( QList<int>() << gr->inputBand() );
191 mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
192
194 if ( ce )
195 {
196 //minmax
197 mDisableMinMaxWidgetRefresh = true;
198 mMinLineEdit->setText( QLocale().toString( ce->minimumValue() ) );
199 mMaxLineEdit->setText( QLocale().toString( ce->maximumValue() ) );
200 mDisableMinMaxWidgetRefresh = false;
201 //contrast enhancement algorithm
202 mContrastEnhancementComboBox->setCurrentIndex(
203 mContrastEnhancementComboBox->findData( ( int ) ( ce->contrastEnhancementAlgorithm() ) )
204 );
205 }
206
207 mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
208
209 if ( gr->legendSettings() )
210 mLegendSettings = *gr->legendSettings();
211 }
212}
213
214void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
215{
216 mDisableMinMaxWidgetRefresh = true;
217 mMinLineEdit->setText( value );
218 mDisableMinMaxWidgetRefresh = false;
219}
220
221void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
222{
223 mDisableMinMaxWidgetRefresh = true;
224 mMaxLineEdit->setText( value );
225 mDisableMinMaxWidgetRefresh = false;
226}
227
228void QgsSingleBandGrayRendererWidget::showLegendSettings()
229{
230 QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast<QWidget *>( parent() ) );
231 if ( panel && panel->dockMode() )
232 {
234 legendPanel->setUseContinuousRampCheckBoxVisibility( false );
235 legendPanel->setPanelTitle( tr( "Legend Settings" ) );
236 legendPanel->setSettings( mLegendSettings );
237 connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [=] {
238 mLegendSettings = legendPanel->settings();
239 emit widgetChanged();
240 } );
241 panel->openPanel( legendPanel );
242 }
243 else
244 {
245 QgsColorRampLegendNodeDialog dialog( mLegendSettings, this );
246 dialog.setUseContinuousRampCheckBoxVisibility( false );
247 dialog.setWindowTitle( tr( "Legend Settings" ) );
248 if ( dialog.exec() )
249 {
250 mLegendSettings = dialog.settings();
251 emit widgetChanged();
252 }
253 }
254}
255
260
262{
263 mDisableMinMaxWidgetRefresh = true;
264 mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( static_cast<int>( algorithm ) ) );
265 mDisableMinMaxWidgetRefresh = false;
266}
DataType
Raster data types.
Definition qgis.h:351
A dialog for configuring a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
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.
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
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.
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
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 a 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 ...
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.
bool dockMode()
Returns the dock mode state.
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.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
void doComputations()
Load programmatically with current values.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
void setFromMinMaxOrigin(const QgsRasterMinMaxOrigin &)
Sets the "source" of min/max values.
void setBands(const QList< int > &bands)
QgsRasterMinMaxOrigin minMaxOrigin()
Returns a QgsRasterMinMaxOrigin object with the widget values.
void widgetChanged()
Emitted when something on the widget has changed.
void userHasSetManualMinMaxValues()
Uncheck cumulative cut, min/max, std-dev radio buttons.
Abstract base class for widgets which configure a QgsRasterRenderer.
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
QgsMapCanvas * mCanvas
Associated map canvas.
void widgetChanged()
Emitted when something on the widget has changed.
Raster renderer pipe that applies colors to a raster.
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
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())
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:39