QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
20 #include "qgsrasterlayer.h"
21 #include "qgsrasterdataprovider.h"
22 #include "qgsrasterminmaxwidget.h"
23 #include "qgsdoublevalidator.h"
25 
27  : QgsRasterRendererWidget( layer, extent )
28  , mDisableMinMaxWidgetRefresh( false )
29 {
30  setupUi( this );
31  connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged );
32  connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged );
33 
34  connect( mLegendSettingsButton, &QPushButton::clicked, this, &QgsSingleBandGrayRendererWidget::showLegendSettings );
35 
36  mGradientComboBox->insertItem( 0, tr( "Black to White" ), QgsSingleBandGrayRenderer::BlackToWhite );
37  mGradientComboBox->insertItem( 1, tr( "White to Black" ), QgsSingleBandGrayRenderer::WhiteToBlack );
38 
39  mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
40  mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
41 
42  if ( mRasterLayer )
43  {
45  if ( !provider )
46  {
47  return;
48  }
49 
50  mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
51  mMinMaxWidget->setExtent( extent );
52  mMinMaxWidget->setMapCanvas( mCanvas );
53 
54  QHBoxLayout *layout = new QHBoxLayout();
55  layout->setContentsMargins( 0, 0, 0, 0 );
56  mMinMaxContainerWidget->setLayout( layout );
57  layout->addWidget( mMinMaxWidget );
58 
59  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::widgetChanged,
61 
62  connect( mMinMaxWidget, &QgsRasterMinMaxWidget::load,
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  }
88  if ( !provider )
89  {
90  return nullptr;
91  }
92  const int band = mGrayBandComboBox->currentBand();
93 
95  provider->dataType( band ) ) );
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 
112 {
113  mMinMaxWidget->doComputations();
114 }
115 
117 {
119  mMinMaxWidget->setMapCanvas( canvas );
120 }
121 
122 void QgsSingleBandGrayRendererWidget::mMinLineEdit_textChanged( const QString & )
123 {
124  minMaxModified();
125 }
126 
127 void QgsSingleBandGrayRendererWidget::mMaxLineEdit_textChanged( const QString & )
128 {
129  minMaxModified();
130 }
131 
132 void 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  mMinMaxWidget->userHasSetManualMinMaxValues();
142  emit widgetChanged();
143  }
144 }
145 
146 
147 void QgsSingleBandGrayRendererWidget::loadMinMax( int bandNo, double min, double max )
148 {
149  Q_UNUSED( bandNo )
150 
151  QgsDebugMsg( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ) );
152 
153  mDisableMinMaxWidgetRefresh = true;
154  if ( std::isnan( min ) )
155  {
156  mMinLineEdit->clear();
157  }
158  else
159  {
160  mMinLineEdit->setText( QLocale().toString( min ) );
161  }
162 
163  if ( std::isnan( max ) )
164  {
165  mMaxLineEdit->clear();
166  }
167  else
168  {
169  mMaxLineEdit->setText( QLocale().toString( max ) );
170  }
171  mDisableMinMaxWidgetRefresh = false;
172 }
173 
174 void QgsSingleBandGrayRendererWidget::bandChanged()
175 {
176  QList<int> myBands;
177  myBands.append( mGrayBandComboBox->currentBand() );
178  mMinMaxWidget->setBands( myBands );
179  emit widgetChanged();
180 }
181 
183 {
184  const QgsSingleBandGrayRenderer *gr = dynamic_cast<const QgsSingleBandGrayRenderer *>( r );
185  if ( gr )
186  {
187  //band
188  mGrayBandComboBox->setBand( gr->grayBand() );
189  mMinMaxWidget->setBands( QList< int >() << gr->grayBand() );
190  mGradientComboBox->setCurrentIndex( mGradientComboBox->findData( gr->gradient() ) );
191 
193  if ( ce )
194  {
195  //minmax
196  mDisableMinMaxWidgetRefresh = true;
197  mMinLineEdit->setText( QLocale().toString( ce->minimumValue() ) );
198  mMaxLineEdit->setText( QLocale().toString( ce->maximumValue() ) );
199  mDisableMinMaxWidgetRefresh = false;
200  //contrast enhancement algorithm
201  mContrastEnhancementComboBox->setCurrentIndex(
202  mContrastEnhancementComboBox->findData( ( int )( ce->contrastEnhancementAlgorithm() ) ) );
203  }
204 
205  mMinMaxWidget->setFromMinMaxOrigin( gr->minMaxOrigin() );
206 
207  if ( gr->legendSettings() )
208  mLegendSettings = *gr->legendSettings();
209  }
210 }
211 
212 void QgsSingleBandGrayRendererWidget::setMin( const QString &value, int )
213 {
214  mDisableMinMaxWidgetRefresh = true;
215  mMinLineEdit->setText( value );
216  mDisableMinMaxWidgetRefresh = false;
217 }
218 
219 void QgsSingleBandGrayRendererWidget::setMax( const QString &value, int )
220 {
221  mDisableMinMaxWidgetRefresh = true;
222  mMaxLineEdit->setText( value );
223  mDisableMinMaxWidgetRefresh = false;
224 }
225 
226 void QgsSingleBandGrayRendererWidget::showLegendSettings()
227 {
228  QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( qobject_cast< QWidget * >( parent() ) );
229  if ( panel && panel->dockMode() )
230  {
232  legendPanel->setUseContinuousRampCheckBoxVisibility( false );
233  legendPanel->setPanelTitle( tr( "Legend Settings" ) );
234  legendPanel->setSettings( mLegendSettings );
235  connect( legendPanel, &QgsColorRampLegendNodeWidget::widgetChanged, this, [ = ]
236  {
237  mLegendSettings = legendPanel->settings();
238  emit widgetChanged();
239  } );
240  panel->openPanel( legendPanel );
241  }
242  else
243  {
244  QgsColorRampLegendNodeDialog dialog( mLegendSettings, this );
245  dialog.setUseContinuousRampCheckBoxVisibility( false );
246  dialog.setWindowTitle( tr( "Legend Settings" ) );
247  if ( dialog.exec() )
248  {
249  mLegendSettings = dialog.settings();
250  emit widgetChanged();
251  }
252  }
253 }
DataType
Raster data types.
Definition: qgis.h:121
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.
Definition: qgsmapcanvas.h:90
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.
Definition: qgsrectangle.h:42
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.
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
Raster renderer pipe for single band gray.
const QgsContrastEnhancement * contrastEnhancement() const
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38