QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
254 
256 {
257  return static_cast<QgsContrastEnhancement::ContrastEnhancementAlgorithm>( mContrastEnhancementComboBox->currentData().toInt() );
258 }
259 
261 {
262  mDisableMinMaxWidgetRefresh = true;
263  mContrastEnhancementComboBox->setCurrentIndex( mContrastEnhancementComboBox->findData( static_cast<int>( algorithm ) ) );
264  mDisableMinMaxWidgetRefresh = false;
265 }
QgsContrastEnhancement::maximumValue
double maximumValue() const
Returns the maximum value for the contrast enhancement range.
Definition: qgscontrastenhancement.h:155
QgsSingleBandGrayRenderer::WhiteToBlack
@ WhiteToBlack
Definition: qgssinglebandgrayrenderer.h:41
QgsRasterMinMaxWidget::userHasSetManualMinMaxValues
void userHasSetManualMinMaxValues()
Uncheck cumulative cut, min/max, std-dev radio buttons.
Definition: qgsrasterminmaxwidget.cpp:89
QgsRasterBandComboBox::bandChanged
void bandChanged(int band)
Emitted when the currently selected band changes.
QgsSingleBandGrayRenderer::BlackToWhite
@ BlackToWhite
Definition: qgssinglebandgrayrenderer.h:40
qgsrasterlayer.h
QgsContrastEnhancement::setContrastEnhancementAlgorithm
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
Definition: qgscontrastenhancement.cpp:132
QgsRasterDataProvider::dataType
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
QgsRasterMinMaxWidget::widgetChanged
void widgetChanged()
Emitted when something on the widget has changed.
QgsContrastEnhancement::minimumValue
double minimumValue() const
Returns the minimum value for the contrast enhancement range.
Definition: qgscontrastenhancement.h:158
QgsSingleBandGrayRendererWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
Definition: qgssinglebandgrayrendererwidget.cpp:116
QgsSingleBandGrayRenderer::legendSettings
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
Definition: qgssinglebandgrayrenderer.cpp:412
QgsColorRampLegendNodeWidget::settings
QgsColorRampLegendNodeSettings settings() const
Returns the legend node settings as defined by the widget.
Definition: qgscolorramplegendnodewidget.cpp:59
QgsRasterRenderer::minMaxOrigin
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
Definition: qgsrasterrenderer.h:154
QgsRasterRendererWidget::mRasterLayer
QgsRasterLayer * mRasterLayer
Definition: qgsrasterrendererwidget.h:123
QgsSingleBandGrayRenderer::grayBand
int grayBand() const
Definition: qgssinglebandgrayrenderer.h:57
QgsPanelWidget::findParentPanel
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
Definition: qgspanelwidget.cpp:54
algorithm
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
QgsContrastEnhancement::setMinimumValue
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
Definition: qgscontrastenhancement.cpp:200
QgsColorRampLegendNodeWidget
A widget for properties relating to a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
Definition: qgscolorramplegendnodewidget.h:41
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
Qgis::DataType
DataType
Raster data types.
Definition: qgis.h:128
QgsPanelWidget::openPanel
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Definition: qgspanelwidget.cpp:84
QgsContrastEnhancement::StretchAndClipToMinimumMaximum
@ StretchAndClipToMinimumMaximum
Definition: qgscontrastenhancement.h:52
QgsSingleBandGrayRendererWidget::max
QString max(int index=0) override
Definition: qgssinglebandgrayrendererwidget.h:50
QgsSingleBandGrayRenderer
Raster renderer pipe for single band gray.
Definition: qgssinglebandgrayrenderer.h:35
QgsSingleBandGrayRenderer::gradient
Gradient gradient() const
Definition: qgssinglebandgrayrenderer.h:64
QgsSingleBandGrayRendererWidget::QgsSingleBandGrayRendererWidget
QgsSingleBandGrayRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Definition: qgssinglebandgrayrendererwidget.cpp:26
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsSingleBandGrayRendererWidget::renderer
QgsRasterRenderer * renderer() override
Creates a new renderer, using the properties defined in the widget.
Definition: qgssinglebandgrayrendererwidget.cpp:81
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:93
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsSingleBandGrayRendererWidget::loadMinMax
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
Definition: qgssinglebandgrayrendererwidget.cpp:147
QgsRasterMinMaxWidget::setFromMinMaxOrigin
void setFromMinMaxOrigin(const QgsRasterMinMaxOrigin &)
Sets the "source" of min/max values.
Definition: qgsrasterminmaxwidget.cpp:102
QgsSingleBandGrayRenderer::contrastEnhancement
const QgsContrastEnhancement * contrastEnhancement() const
Definition: qgssinglebandgrayrenderer.h:59
QgsRasterMinMaxWidget::minMaxOrigin
QgsRasterMinMaxOrigin minMaxOrigin()
Returns a QgsRasterMinMaxOrigin object with the widget values.
Definition: qgsrasterminmaxwidget.cpp:145
QgsRasterLayer::renderer
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
Definition: qgsrasterlayer.cpp:1758
QgsColorRampLegendNodeWidget::setUseContinuousRampCheckBoxVisibility
void setUseContinuousRampCheckBoxVisibility(bool visible)
Sets visibility for the "Use Continuous Legend" checkbox to visible.
Definition: qgscolorramplegendnodewidget.cpp:91
QgsSingleBandGrayRendererWidget::contrastEnhancementAlgorithm
QgsContrastEnhancement::ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const override
Returns the constrast enhancement algorithm to be used by the raster renderer.
Definition: qgssinglebandgrayrendererwidget.cpp:255
QgsRasterRendererWidget::setMapCanvas
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
Definition: qgsrasterrendererwidget.cpp:23
QgsColorRampLegendNodeDialog
A dialog for configuring a QgsColorRampLegendNode (QgsColorRampLegendNodeSettings).
Definition: qgscolorramplegendnodewidget.h:93
QgsRasterMinMaxWidget
Definition: qgsrasterminmaxwidget.h:37
QgsContrastEnhancement::ContrastEnhancementAlgorithm
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
Definition: qgscontrastenhancement.h:48
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsRasterMinMaxWidget::setExtent
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
Definition: qgsrasterminmaxwidget.h:51
QgsRasterMinMaxWidget::load
void load(int bandNo, double min, double max)
signal emitted when new min/max values are computed from statistics.
QgsColorRampLegendNodeWidget::setSettings
void setSettings(const QgsColorRampLegendNodeSettings &settings)
Sets the settings to show in the widget.
Definition: qgscolorramplegendnodewidget.cpp:74
QgsRasterRenderer
Raster renderer pipe that applies colors to a raster.
Definition: qgsrasterrenderer.h:40
QgsContrastEnhancement::StretchToMinimumMaximum
@ StretchToMinimumMaximum
Linear histogram.
Definition: qgscontrastenhancement.h:51
QgsContrastEnhancement::NoEnhancement
@ NoEnhancement
Default color scaling algorithm, no scaling is applied.
Definition: qgscontrastenhancement.h:50
QgsRasterMinMaxWidget::doComputations
void doComputations()
Load programmatically with current values.
Definition: qgsrasterminmaxwidget.cpp:186
QgsRasterMinMaxWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
Definition: qgsrasterminmaxwidget.cpp:59
QgsDoubleValidator
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
Definition: qgsdoublevalidator.h:40
QgsSingleBandGrayRendererWidget::setFromRenderer
void setFromRenderer(const QgsRasterRenderer *r)
Sets the widget state from the specified renderer.
Definition: qgssinglebandgrayrendererwidget.cpp:182
QgsContrastEnhancement::ClipToMinimumMaximum
@ ClipToMinimumMaximum
Definition: qgscontrastenhancement.h:53
QgsPanelWidget::widgetChanged
void widgetChanged()
Emitted when the widget state changes.
QgsSingleBandGrayRendererWidget::doComputations
void doComputations() override
Load programmatically with current values.
Definition: qgssinglebandgrayrendererwidget.cpp:111
QgsSingleBandGrayRendererWidget::setMax
void setMax(const QString &value, int index=0) override
Definition: qgssinglebandgrayrendererwidget.cpp:219
QgsContrastEnhancement::contrastEnhancementAlgorithm
ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() const
Definition: qgscontrastenhancement.h:160
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsSingleBandGrayRendererWidget::setMin
void setMin(const QString &value, int index=0) override
Definition: qgssinglebandgrayrendererwidget.cpp:212
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
QgsContrastEnhancement
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
Definition: qgscontrastenhancement.h:42
qgssinglebandgrayrenderer.h
QgsSingleBandGrayRendererWidget::min
QString min(int index=0) override
Definition: qgssinglebandgrayrendererwidget.h:49
QgsRasterRenderer::setMinMaxOrigin
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
Definition: qgsrasterrenderer.h:157
qgssinglebandgrayrendererwidget.h
qgscolorramplegendnodewidget.h
qgsrasterminmaxwidget.h
QgsSingleBandGrayRenderer::Gradient
Gradient
Definition: qgssinglebandgrayrenderer.h:38
QgsDoubleValidator::toDouble
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
Definition: qgsdoublevalidator.cpp:134
QgsSingleBandGrayRendererWidget::setContrastEnhancementAlgorithm
void setContrastEnhancementAlgorithm(QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm) override
Sets the constrast enhancement algorithm to be used by the raster renderer.
Definition: qgssinglebandgrayrendererwidget.cpp:260
QgsRasterMinMaxWidget::setBands
void setBands(const QList< int > &bands)
Definition: qgsrasterminmaxwidget.cpp:69
QgsRasterRendererWidget
Abstract base class for widgets which configure a QgsRasterRenderer.
Definition: qgsrasterrendererwidget.h:39
QgsRasterRendererWidget::mCanvas
QgsMapCanvas * mCanvas
Associated map canvas.
Definition: qgsrasterrendererwidget.h:129
QgsRasterRendererWidget::widgetChanged
void widgetChanged()
Emitted when something on the widget has changed.
qgsdoublevalidator.h
QgsContrastEnhancement::setMaximumValue
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
Definition: qgscontrastenhancement.cpp:174
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:88
QgsColorRampLegendNodeSettings
Settings for a color ramp legend node.
Definition: qgscolorramplegendnodesettings.h:37
QgsRasterLayer::dataProvider
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Definition: qgsrasterlayer.cpp:257
qgsrasterdataprovider.h