QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgssinglebandpseudocolorrendererwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssinglebandpseudocolorrendererwidget.cpp
3 ------------------------------------------
4 begin : February 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_qgssinglebandpseudocolorrendererwidget.cpp"
21#include "qgsrasterlayer.h"
23#include "qgsrastershader.h"
25#include "qgsdoublevalidator.h"
26#include "qgstreewidgetitem.h"
27#include "qgssettings.h"
28#include "qgsmapcanvas.h"
29#include "qgsguiutils.h"
30
31// for color ramps - todo add rasterStyle and refactor raster vs. vector ramps
32#include "qgsstyle.h"
33#include "qgscolorramp.h"
34#include "qgscolorrampbutton.h"
35#include "qgscolordialog.h"
36
37#include <QCursor>
38#include <QPushButton>
39#include <QInputDialog>
40#include <QFileDialog>
41#include <QMenu>
42#include <QMessageBox>
43#include <QTextStream>
44#include <QTreeView>
45
47 : QgsRasterRendererWidget( layer, extent )
48 , mMinMaxOrigin( 0 )
49{
50 const QgsSettings settings;
51
52 setupUi( this );
53
54 mColorRampShaderWidget->initializeForUseWithRasterLayer();
55
56 connect( mMinLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textChanged );
57 connect( mMaxLineEdit, &QLineEdit::textChanged, this, &QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textChanged );
58 connect( mMinLineEdit, &QLineEdit::textEdited, this, &QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textEdited );
59 connect( mMaxLineEdit, &QLineEdit::textEdited, this, &QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textEdited );
60
61 if ( !mRasterLayer )
62 {
63 return;
64 }
65
67 if ( !provider )
68 {
69 return;
70 }
71
72 // Must be before adding items to mBandComboBox (signal)
73 mMinLineEdit->setValidator( new QgsDoubleValidator( mMinLineEdit ) );
74 mMaxLineEdit->setValidator( new QgsDoubleValidator( mMaxLineEdit ) );
75
76 mMinMaxWidget = new QgsRasterMinMaxWidget( layer, this );
77 mMinMaxWidget->setExtent( extent );
78 mMinMaxWidget->setMapCanvas( mCanvas );
79
80 QHBoxLayout *layout = new QHBoxLayout();
81 layout->setContentsMargins( 0, 0, 0, 0 );
82 mMinMaxContainerWidget->setLayout( layout );
83 layout->addWidget( mMinMaxWidget );
84
85 mColorRampShaderWidget->setRasterDataProvider( provider );
86 mBandComboBox->setLayer( mRasterLayer );
87
88 setFromRenderer( layer->renderer() );
89
92
93 // If there is currently no min/max, load default with user current default options
94 if ( mMinLineEdit->text().isEmpty() || mMaxLineEdit->text().isEmpty() )
95 {
96 QgsRasterMinMaxOrigin minMaxOrigin = mMinMaxWidget->minMaxOrigin();
97 if ( minMaxOrigin.limits() == QgsRasterMinMaxOrigin::None )
98 {
100 mMinMaxWidget->setFromMinMaxOrigin( minMaxOrigin );
101 }
102 mMinMaxWidget->doComputations();
103 }
104
105 whileBlocking( mColorRampShaderWidget )->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
106
107 connect( mBandComboBox, &QgsRasterBandComboBox::bandChanged, this, &QgsSingleBandPseudoColorRendererWidget::bandChanged );
110}
111
113{
114 QgsRasterShader *rasterShader = new QgsRasterShader();
115
116 mColorRampShaderWidget->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
117 mColorRampShaderWidget->setExtent( mMinMaxWidget->extent() );
118
119 QgsColorRampShader *fcn = new QgsColorRampShader( mColorRampShaderWidget->shader() );
120 rasterShader->setRasterShaderFunction( fcn );
121
122 const int bandNumber = mBandComboBox->currentBand();
124 renderer->setClassificationMin( lineEditValue( mMinLineEdit ) );
125 renderer->setClassificationMax( lineEditValue( mMaxLineEdit ) );
126 renderer->setMinMaxOrigin( mMinMaxWidget->minMaxOrigin() );
127 return renderer;
128}
129
134
136
138{
140 mMinMaxWidget->setMapCanvas( canvas );
141 mColorRampShaderWidget->setExtent( mMinMaxWidget->extent() );
142}
143
145{
146 const QgsSingleBandPseudoColorRenderer *pr = dynamic_cast<const QgsSingleBandPseudoColorRenderer *>( r );
147 if ( pr )
148 {
149 mBandComboBox->setBand( pr->inputBand() );
150 mMinMaxWidget->setBands( QList< int >() << pr->inputBand() );
151 mColorRampShaderWidget->setRasterBand( pr->inputBand() );
152
153 // need to set min/max properties here because if we use the raster shader below,
154 // we may set a new color ramp which needs to have min/max values defined.
155 setLineEditValue( mMinLineEdit, pr->classificationMin() );
156 setLineEditValue( mMaxLineEdit, pr->classificationMax() );
157 mMinMaxWidget->setFromMinMaxOrigin( pr->minMaxOrigin() );
158
159 const QgsRasterShader *rasterShader = pr->shader();
160 if ( rasterShader )
161 {
162 const QgsColorRampShader *colorRampShader = dynamic_cast<const QgsColorRampShader *>( rasterShader->rasterShaderFunction() );
163 if ( colorRampShader )
164 {
165 mColorRampShaderWidget->setFromShader( *colorRampShader );
166 }
167 }
168 }
169 else
170 {
171 mMinMaxWidget->setBands( QList< int >() << mBandComboBox->currentBand() );
172 mColorRampShaderWidget->setRasterBand( mBandComboBox->currentBand() );
173 }
174}
175
176void QgsSingleBandPseudoColorRendererWidget::bandChanged()
177{
178 QList<int> bands;
179 bands.append( mBandComboBox->currentBand() );
180 mMinMaxWidget->setBands( bands );
181 mColorRampShaderWidget->setRasterBand( mBandComboBox->currentBand() );
182 mColorRampShaderWidget->classify();
183}
184
185void QgsSingleBandPseudoColorRendererWidget::loadMinMax( int bandNo, double min, double max )
186{
187 QgsDebugMsgLevel( QStringLiteral( "theBandNo = %1 min = %2 max = %3" ).arg( bandNo ).arg( min ).arg( max ), 2 );
188
189 const QString oldMinTextvalue = mMinLineEdit->text();
190 const QString oldMaxTextvalue = mMaxLineEdit->text();
191
192 if ( std::isnan( min ) )
193 {
194 whileBlocking( mMinLineEdit )->clear();
195 }
196 else
197 {
198 whileBlocking( mMinLineEdit )->setText( displayValueWithMaxPrecision( min ) );
199 }
200
201 if ( std::isnan( max ) )
202 {
203 whileBlocking( mMaxLineEdit )->clear();
204 }
205 else
206 {
207 whileBlocking( mMaxLineEdit )->setText( displayValueWithMaxPrecision( max ) );
208 }
209
210 // We compare old min and new min as text because QString::number keeps a fixed number of significant
211 // digits (default 6) and so loaded min/max will always differ from current one, which triggers a
212 // classification, and wipe out every user modification (see https://github.com/qgis/QGIS/issues/36172)
213 if ( mMinLineEdit->text() != oldMinTextvalue || mMaxLineEdit->text() != oldMaxTextvalue )
214 {
215 whileBlocking( mColorRampShaderWidget )->setRasterBand( bandNo );
216 whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( min, max );
217 }
218}
219
220
222{
223 whileBlocking( mMinLineEdit )->setText( displayValueWithMaxPrecision( min ) );
224 whileBlocking( mMaxLineEdit )->setText( displayValueWithMaxPrecision( max ) );
225 minMaxModified();
226}
227
228
229void QgsSingleBandPseudoColorRendererWidget::setLineEditValue( QLineEdit *lineEdit, double value )
230{
231 QString s;
232 if ( !std::isnan( value ) )
233 {
234 s = displayValueWithMaxPrecision( value );
235 }
236 lineEdit->setText( s );
237}
238
239double QgsSingleBandPseudoColorRendererWidget::lineEditValue( const QLineEdit *lineEdit ) const
240{
241 if ( lineEdit->text().isEmpty() )
242 {
243 return std::numeric_limits<double>::quiet_NaN();
244 }
245
246 return QgsDoubleValidator::toDouble( lineEdit->text() );
247}
248
249void QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textEdited( const QString & )
250{
251 minMaxModified();
252 whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
253 emit widgetChanged();
254}
255
256void QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textEdited( const QString & )
257{
258 minMaxModified();
259 whileBlocking( mColorRampShaderWidget )->setMinimumMaximumAndClassify( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
260 emit widgetChanged();
261}
262
263void QgsSingleBandPseudoColorRendererWidget::mMinLineEdit_textChanged( const QString & )
264{
265 whileBlocking( mColorRampShaderWidget )->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
266 emit widgetChanged();
267}
268
269void QgsSingleBandPseudoColorRendererWidget::mMaxLineEdit_textChanged( const QString & )
270{
271 whileBlocking( mColorRampShaderWidget )->setMinimumMaximum( lineEditValue( mMinLineEdit ), lineEditValue( mMaxLineEdit ) );
272 emit widgetChanged();
273}
274
275
276void QgsSingleBandPseudoColorRendererWidget::minMaxModified()
277{
278 mMinMaxWidget->userHasSetManualMinMaxValues();
279}
280
281QString QgsSingleBandPseudoColorRendererWidget::displayValueWithMaxPrecision( const double value )
282{
283 if ( mRasterLayer->dataProvider() )
284 {
285 return QgsGuiUtils::displayValueWithMaximumDecimals( mRasterLayer->dataProvider()->dataType( mBandComboBox->currentBand() ), value );
286 }
287 else
288 {
289 // Use QLocale default
290 return QLocale().toString( value, 'g' );
291 }
292}
293
294void QgsSingleBandPseudoColorRendererWidget::setMin( const QString &value, int )
295{
296 mMinLineEdit->setText( value );
297 minMaxModified();
298 mColorRampShaderWidget->classify();
299}
300
301void QgsSingleBandPseudoColorRendererWidget::setMax( const QString &value, int )
302{
303 mMaxLineEdit->setText( value );
304 minMaxModified();
305 mColorRampShaderWidget->classify();
306}
void minimumMaximumChangedFromTree(double minimum, double maximum)
Color ramp tree has changed.
void widgetChanged()
Widget changed.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
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.
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.
This class describes the origin of min/max values.
QgsRasterMinMaxOrigin::Limits limits() const
Returns the raster limits.
void setLimits(QgsRasterMinMaxOrigin::Limits limits)
Sets the limits.
@ MinMax
Real min-max values.
void setExtent(const QgsRectangle &extent)
Sets the extent to use for minimum and maximum value calculation.
QgsRectangle extent()
Returns the extent selected by the user.
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.
Interface for all raster shaders.
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
QgsRasterShaderFunction * rasterShaderFunction()
A rectangle specified with double values.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
void setMax(const QString &value, int index=0) override
QgsSingleBandPseudoColorRendererWidget(QgsRasterLayer *layer, const QgsRectangle &extent=QgsRectangle())
Creates new raster renderer widget.
void doComputations() override
Load programmatically with current values.
QgsRasterMinMaxWidget * minMaxWidget() override
Returns min/max widget when it exists.
void setMin(const QString &value, int index=0) override
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
QgsRasterRenderer * renderer() override
Creates a new renderer, using the properties defined in the widget.
void setFromRenderer(const QgsRasterRenderer *r)
Sets the widget state from the specified renderer.
void loadMinMax(int bandNo, double min, double max)
called when new min/max values are loaded
void loadMinMaxFromTree(double min, double max)
called when the color ramp tree has changed
Raster renderer pipe for single band pseudocolor.
Q_DECL_DEPRECATED void setBand(int bandNo)
Sets the band used by the renderer.
QgsRasterShader * shader()
Returns the raster shader.
int inputBand() const override
Returns the input band for the renderer, or -1 if no input band is available.
QString displayValueWithMaximumDecimals(const Qgis::DataType dataType, const double value, bool displayTrailingZeroes)
Returns a localized string representation of the value with the appropriate number of decimals suppor...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5821
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39