QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsrasterlabelsettingswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterlabelsettingswidget.cpp
3 ---------------------------
4 begin : December 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsrasterlabelsettingswidget.cpp"
19#include "qgsrasterlabeling.h"
22
23#include <QPushButton>
24
26 : QgsLabelingGui( mapCanvas, parent, layer )
27 , mNumberFormat( std::make_unique<QgsBasicNumericFormat>() )
28{
29 mGeomType = Qgis::GeometryType::Point;
30 mMode = Labels;
31
32 init();
33
34 QWidget *labelWithWidget = new QWidget();
35 QGridLayout *gLayout = new QGridLayout();
36 gLayout->setContentsMargins( 0, 0, 0, 0 );
37 gLayout->addWidget( new QLabel( tr( "Value" ) ), 0, 0 );
38 mBandCombo = new QgsRasterBandComboBox();
39 mBandCombo->setLayer( layer );
40 gLayout->addWidget( mBandCombo, 0, 1 );
41 gLayout->setColumnStretch( 0, 1 );
42 gLayout->setColumnStretch( 1, 2 );
43
44 gLayout->addWidget( new QLabel( tr( "Number format" ) ), 1, 0 );
45
46 QPushButton *numberFormatButton = new QPushButton( tr( "Customize" ) );
47 connect( numberFormatButton, &QPushButton::clicked, this, &QgsRasterLabelSettingsWidget::changeNumberFormat );
48
49 gLayout->addWidget( numberFormatButton, 1, 1 );
50
51 gLayout->addWidget( new QLabel( tr( "Resample over" ) ), 2, 0 );
52 mResampleOverSpin = new QgsSpinBox();
53 mResampleOverSpin->setMinimum( 1 );
54 mResampleOverSpin->setMaximum( 128 );
55 mResampleOverSpin->setClearValue( 1 );
56 mResampleOverSpin->setSuffix( tr( " pixels" ) );
57 connect( mResampleOverSpin, qOverload<int>( &QgsSpinBox::valueChanged ), this, &QgsRasterLabelSettingsWidget::widgetChanged );
58 gLayout->addWidget( mResampleOverSpin, 2, 1 );
59
60 gLayout->addWidget( new QLabel( tr( "Resample using" ) ), 3, 0 );
61 mResampleMethodComboBox = new QComboBox();
62 mResampleMethodComboBox->addItem( QObject::tr( "Nearest Neighbour" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Nearest ) );
63 mResampleMethodComboBox->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Bilinear ) );
64 mResampleMethodComboBox->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Cubic ) );
65 mResampleMethodComboBox->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::CubicSpline ) );
66 mResampleMethodComboBox->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Lanczos ) );
67 mResampleMethodComboBox->addItem( QObject::tr( "Average" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Average ) );
68 mResampleMethodComboBox->addItem( QObject::tr( "Mode" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Mode ) );
69 mResampleMethodComboBox->addItem( QObject::tr( "Gauss" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Gauss ) );
70 connect( mResampleMethodComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsRasterLabelSettingsWidget::widgetChanged );
71 gLayout->addWidget( mResampleMethodComboBox, 3, 1 );
72
73 labelWithWidget->setLayout( gLayout );
74
75 mStackedWidgetLabelWith->addWidget( labelWithWidget );
76 mStackedWidgetLabelWith->setCurrentWidget( labelWithWidget );
77
78 setLayer( layer );
79
80 const int prevIndex = mOptionsTab->currentIndex();
81
82 setPropertyOverrideButtonsVisible( true );
83 mTextFormatsListWidget->setEntityTypes( QList<QgsStyle::StyleEntity>() << QgsStyle::TextFormatEntity );
84
85 delete mCalloutItem;
86 mCalloutItem = nullptr;
87 delete mMaskItem;
88 mMaskItem = nullptr;
89
90 mOptionsTab->removeTab( mOptionsTab->indexOf( calloutsTab ) );
91 mOptionsTab->removeTab( mOptionsTab->indexOf( maskTab ) );
92
93 mLabelStackedWidget->removeWidget( mLabelPage_Callouts );
94 mLabelStackedWidget->removeWidget( mLabelPage_Mask );
95
96 switch ( prevIndex )
97 {
98 case 0:
99 case 1:
100 case 2:
101 break;
102
103 case 4: // background - account for removed mask tab
104 case 5: // shadow
105 mLabelStackedWidget->setCurrentIndex( prevIndex - 1 );
106 mOptionsTab->setCurrentIndex( prevIndex - 1 );
107 break;
108
109 case 7: // background - account for removed mask & callouts tab
110 case 8: // shadow- account for removed mask & callouts tab
111 mLabelStackedWidget->setCurrentIndex( prevIndex - 2 );
112 mOptionsTab->setCurrentIndex( prevIndex - 2 );
113 break;
114
115 case 3: // mask
116 case 6: // callouts
117 mLabelStackedWidget->setCurrentIndex( 0 );
118 mOptionsTab->setCurrentIndex( 0 );
119 break;
120
121 default:
122 break;
123 }
124
125 // hide settings which have no relevance to raster labeling
126 mDirectSymbolsFrame->hide();
127 mFormatNumFrame->hide();
128 mFormatNumChkBx->hide();
129 mFormatNumDDBtn->hide();
130 mCheckBoxSubstituteText->hide();
131 mToolButtonConfigureSubstitutes->hide();
132 mLabelWrapOnCharacter->hide();
133 wrapCharacterEdit->hide();
134 mWrapCharDDBtn->hide();
135 mLabelWrapLinesTo->hide();
136 mAutoWrapLengthSpinBox->hide();
137 mAutoWrapLengthDDBtn->hide();
138 mAutoWrapTypeComboBox->hide();
139 mFontMultiLineLabel->hide();
140 mFontMultiLineAlignComboBox->hide();
141 mFontMultiLineAlignDDBtn->hide();
142 mGeometryGeneratorGroupBox->hide();
143 mObstaclesGroupBox->hide();
144 mPlacementDDGroupBox->hide();
145 mPlacementGroupBox->hide();
146 mInferiorPlacementWidget->hide();
147 mLabelRenderingDDFrame->hide();
148 mUpsidedownFrame->hide();
149 mLabelEveryPartWidget->hide();
150 mFramePixelSizeVisibility->hide();
151 line->hide();
152
153 mMinSizeFrame->show();
154 mMinSizeLabel->setText( tr( "Suppress labeling of pixels smaller than" ) );
155
156 mLimitLabelChkBox->setText( tr( "Limit number of pixels to be labeled to" ) );
157
158 // fix precision for priority slider
159 mPrioritySlider->setRange( 0, 100 );
160 mPrioritySlider->setTickInterval( 10 );
161
162 connect( mBandCombo, &QgsRasterBandComboBox::bandChanged, this, &QgsRasterLabelSettingsWidget::widgetChanged );
163}
164
166
168{
169 if ( QgsRasterLayerSimpleLabeling *simpleLabeling = dynamic_cast<QgsRasterLayerSimpleLabeling *>( labeling ) )
170 {
171 setFormat( simpleLabeling->textFormat() );
172 mBandCombo->setBand( simpleLabeling->band() );
173 mPrioritySlider->setValue( static_cast<int>( 100 - simpleLabeling->priority() * 100 ) );
174
175 mComboOverlapHandling->setCurrentIndex( mComboOverlapHandling->findData( static_cast<int>( simpleLabeling->placementSettings().overlapHandling() ) ) );
176 mZIndexSpinBox->setValue( simpleLabeling->zIndex() );
177 if ( const QgsNumericFormat *format = simpleLabeling->numericFormat() )
178 mNumberFormat.reset( format->clone() );
179
180 mLimitLabelChkBox->setChecked( simpleLabeling->thinningSettings().limitNumberOfLabelsEnabled() );
181 mLimitLabelSpinBox->setValue( simpleLabeling->thinningSettings().maximumNumberLabels() );
182 mMinSizeSpinBox->setValue( simpleLabeling->thinningSettings().minimumFeatureSize() );
183
184 mScaleBasedVisibilityChkBx->setChecked( simpleLabeling->hasScaleBasedVisibility() );
185 mMinScaleWidget->setScale( simpleLabeling->minimumScale() );
186 mMaxScaleWidget->setScale( simpleLabeling->maximumScale() );
187
188 mResampleOverSpin->setValue( simpleLabeling->resampleOver() );
189 mResampleMethodComboBox->setCurrentIndex( mResampleMethodComboBox->findData( QVariant::fromValue( simpleLabeling->resampleMethod() ) ) );
190
191 updateUi();
192 }
193}
194
196{
197 if ( QgsRasterLayerSimpleLabeling *simpleLabeling = dynamic_cast<QgsRasterLayerSimpleLabeling *>( labeling ) )
198 {
199 simpleLabeling->setTextFormat( format() );
200 simpleLabeling->setBand( mBandCombo->currentBand() );
201 simpleLabeling->setPriority( 1.0 - mPrioritySlider->value() / 100.0 );
202 simpleLabeling->placementSettings().setOverlapHandling( static_cast<Qgis::LabelOverlapHandling>( mComboOverlapHandling->currentData().toInt() ) );
203 simpleLabeling->setZIndex( mZIndexSpinBox->value() );
204 simpleLabeling->setNumericFormat( mNumberFormat->clone() );
205
206 simpleLabeling->thinningSettings().setLimitNumberLabelsEnabled( mLimitLabelChkBox->isChecked() );
207 simpleLabeling->thinningSettings().setMaximumNumberLabels( mLimitLabelSpinBox->value() );
208 simpleLabeling->thinningSettings().setMinimumFeatureSize( mMinSizeSpinBox->value() );
209
210 simpleLabeling->setScaleBasedVisibility( mScaleBasedVisibilityChkBx->isChecked() );
211 simpleLabeling->setMinimumScale( mMinScaleWidget->scale() );
212 simpleLabeling->setMaximumScale( mMaxScaleWidget->scale() );
213
214 simpleLabeling->setResampleOver( mResampleOverSpin->value() );
215 simpleLabeling->setResampleMethod( mResampleMethodComboBox->currentData().value<Qgis::RasterResamplingMethod>() );
216 }
217}
218
220{
221 if ( mBandCombo )
222 mBandCombo->setLayer( layer );
223
224 QgsLabelingGui::setLayer( layer );
225 mMinSizeFrame->show();
226}
227
228void QgsRasterLabelSettingsWidget::changeNumberFormat()
229{
231 if ( panel && panel->dockMode() )
232 {
234 widget->setPanelTitle( tr( "Number Format" ) );
235 widget->setFormat( mNumberFormat.get() );
237 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [=] {
238 if ( !mBlockChangesSignal )
239 {
240 mNumberFormat.reset( widget->format() );
241 emit widgetChanged();
242 }
243 } );
244 panel->openPanel( widget );
245 }
246 else
247 {
248 QgsNumericFormatSelectorDialog dialog( this );
249 dialog.setFormat( mNumberFormat.get() );
250 dialog.registerExpressionContextGenerator( this );
251 if ( dialog.exec() )
252 {
253 mNumberFormat.reset( dialog.format() );
254 emit widgetChanged();
255 }
256 }
257}
RasterResamplingMethod
Resampling method for raster provider-level resampling.
Definition qgis.h:1394
@ Lanczos
Lanczos windowed sinc interpolation (6x6 kernel)
@ Nearest
Nearest-neighbour resampling.
@ Mode
Mode (selects the value which appears most often of all the sampled points)
@ Bilinear
Bilinear (2x2 kernel) resampling.
@ Average
Average resampling.
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel)
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
LabelOverlapHandling
Label overlap handling.
Definition qgis.h:1098
Abstract base class for labeling settings for raster layers.
A numeric formatter which returns a simple text representation of a value.
Map canvas is a class for displaying all GIS data types on a canvas.
Base class for all map layer types.
Definition qgsmaplayer.h:76
A simple dialog for customizing a numeric format.
A widget which allows choice of numeric formats and the properties of them.
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the widget.
void changed()
Emitted whenever the format configured55 in the widget is changed.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the widget.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
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 ...
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.
A combobox widget which displays the bands present in a raster layer.
void bandChanged(int band)
Emitted when the currently selected band changes.
int currentBand() const
Returns the current band number selected in the combobox, or -1 if no band is selected.
void setLayer(QgsMapLayer *layer)
Sets the raster layer for which the bands are listed in the combobox.
void setBand(int band)
Sets the current band number selected in the combobox.
void setLayer(QgsMapLayer *layer) final
~QgsRasterLabelSettingsWidget() override
QgsRasterLabelSettingsWidget(QgsRasterLayer *layer, QgsMapCanvas *mapCanvas, QWidget *parent=nullptr)
Constructor for QgsRasterLabelSettingsWidget, for configuring a raster layer labeling.
void updateLabeling(QgsAbstractRasterLayerLabeling *labeling)
Updates labeling by setting properties to match the current state of the widget.
void setLabeling(QgsAbstractRasterLayerLabeling *labeling)
Sets the labeling settings to show in the widget.
Basic implementation of the labeling interface for raster layers.
Represents a raster layer.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
Definition qgsspinbox.h:43
void setClearValue(int customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
@ TextFormatEntity
Text formats.
Definition qgsstyle.h:208