QGIS API Documentation 4.3.0-Master (52aa6503bed)
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
21#include "qgsrasterlabeling.h"
22
23#include <QPushButton>
24
25#include "moc_qgsrasterlabelsettingswidget.cpp"
26
28 : QgsLabelingGui( mapCanvas, parent, layer )
29 , mNumberFormat( std::make_unique<QgsBasicNumericFormat>() )
30{
31 mGeomType = Qgis::GeometryType::Point;
32 mMode = Labels;
33
34 init();
35
36 QWidget *labelWithWidget = new QWidget();
37 QGridLayout *gLayout = new QGridLayout();
38 gLayout->setContentsMargins( 0, 0, 0, 0 );
39 gLayout->addWidget( new QLabel( tr( "Value" ) ), 0, 0 );
40 mBandCombo = new QgsRasterBandComboBox();
41 mBandCombo->setLayer( layer );
42 gLayout->addWidget( mBandCombo, 0, 1 );
43 gLayout->setColumnStretch( 0, 1 );
44 gLayout->setColumnStretch( 1, 2 );
45
46 gLayout->addWidget( new QLabel( tr( "Number format" ) ), 1, 0 );
47
48 QPushButton *numberFormatButton = new QPushButton( tr( "Customize" ) );
49 connect( numberFormatButton, &QPushButton::clicked, this, &QgsRasterLabelSettingsWidget::changeNumberFormat );
50
51 gLayout->addWidget( numberFormatButton, 1, 1 );
52
53 gLayout->addWidget( new QLabel( tr( "Resample over" ) ), 2, 0 );
54 mResampleOverSpin = new QgsSpinBox();
55 mResampleOverSpin->setMinimum( 1 );
56 mResampleOverSpin->setMaximum( 128 );
57 mResampleOverSpin->setClearValue( 1 );
58 mResampleOverSpin->setSuffix( tr( " pixels" ) );
59 connect( mResampleOverSpin, qOverload<int>( &QgsSpinBox::valueChanged ), this, &QgsRasterLabelSettingsWidget::widgetChanged );
60 gLayout->addWidget( mResampleOverSpin, 2, 1 );
61
62 gLayout->addWidget( new QLabel( tr( "Resample using" ) ), 3, 0 );
63 mResampleMethodComboBox = new QComboBox();
64 mResampleMethodComboBox->addItem( QObject::tr( "Nearest Neighbour" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Nearest ) );
65 mResampleMethodComboBox->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Bilinear ) );
66 mResampleMethodComboBox->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Cubic ) );
67 mResampleMethodComboBox->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::CubicSpline ) );
68 mResampleMethodComboBox->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Lanczos ) );
69 mResampleMethodComboBox->addItem( QObject::tr( "Average" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Average ) );
70 mResampleMethodComboBox->addItem( QObject::tr( "Mode" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Mode ) );
71 mResampleMethodComboBox->addItem( QObject::tr( "Gauss" ), QVariant::fromValue( Qgis::RasterResamplingMethod::Gauss ) );
72 connect( mResampleMethodComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsRasterLabelSettingsWidget::widgetChanged );
73 gLayout->addWidget( mResampleMethodComboBox, 3, 1 );
74
75 labelWithWidget->setLayout( gLayout );
76
77 mStackedWidgetLabelWith->addWidget( labelWithWidget );
78 mStackedWidgetLabelWith->setCurrentWidget( labelWithWidget );
79
80 setLayer( layer );
81
82 const int prevIndex = mOptionsTab->currentIndex();
83
84 setPropertyOverrideButtonsVisible( true );
85 mTextFormatsListWidget->setEntityTypes( QList<QgsStyle::StyleEntity>() << QgsStyle::TextFormatEntity );
86
87 delete mCalloutItem;
88 mCalloutItem = nullptr;
89 delete mMaskItem;
90 mMaskItem = nullptr;
91
92 mOptionsTab->removeTab( mOptionsTab->indexOf( calloutsTab ) );
93 mOptionsTab->removeTab( mOptionsTab->indexOf( maskTab ) );
94
95 mLabelStackedWidget->removeWidget( mLabelPage_Callouts );
96 mLabelStackedWidget->removeWidget( mLabelPage_Mask );
97
98 switch ( prevIndex )
99 {
100 case 0:
101 case 1:
102 case 2:
103 break;
104
105 case 4: // background - account for removed mask tab
106 case 5: // shadow
107 mLabelStackedWidget->setCurrentIndex( prevIndex - 1 );
108 mOptionsTab->setCurrentIndex( prevIndex - 1 );
109 break;
110
111 case 7: // background - account for removed mask & callouts tab
112 case 8: // shadow- account for removed mask & callouts tab
113 mLabelStackedWidget->setCurrentIndex( prevIndex - 2 );
114 mOptionsTab->setCurrentIndex( prevIndex - 2 );
115 break;
116
117 case 3: // mask
118 case 6: // callouts
119 mLabelStackedWidget->setCurrentIndex( 0 );
120 mOptionsTab->setCurrentIndex( 0 );
121 break;
122
123 default:
124 break;
125 }
126
127 // hide settings which have no relevance to raster labeling
128 mDirectSymbolsFrame->hide();
129 mFormatNumFrame->hide();
130 mFormatNumChkBx->hide();
131 mFormatNumDDBtn->hide();
132 mCheckBoxSubstituteText->hide();
133 mToolButtonConfigureSubstitutes->hide();
134 mLabelWrapOnCharacter->hide();
135 wrapCharacterEdit->hide();
136 mWrapCharDDBtn->hide();
137 mLabelWrapLinesTo->hide();
138 mAutoWrapLengthSpinBox->hide();
139 mAutoWrapLengthDDBtn->hide();
140 mAutoWrapTypeComboBox->hide();
141 mFontMultiLineLabel->hide();
142 mFontMultiLineAlignComboBox->hide();
143 mFontMultiLineAlignDDBtn->hide();
144 mGeometryGeneratorGroupBox->hide();
145 mObstaclesGroupBox->hide();
146 mPlacementDDGroupBox->hide();
147 mPlacementGroupBox->hide();
148 mInferiorPlacementWidget->hide();
149 mLabelRenderingDDFrame->hide();
150 mUpsidedownFrame->hide();
151 mMultipartBehaviorWidget->hide();
152 mFramePixelSizeVisibility->hide();
153 line->hide();
154
155 mMinSizeFrame->show();
156 mMinSizeLabel->setText( tr( "Suppress labeling of pixels smaller than" ) );
157
158 mLimitLabelChkBox->setText( tr( "Limit number of pixels to be labeled to" ) );
159
160 // fix precision for priority slider
161 mPrioritySlider->setRange( 0, 100 );
162 mPrioritySlider->setTickInterval( 10 );
163
164 connect( mBandCombo, &QgsRasterBandComboBox::bandChanged, this, &QgsRasterLabelSettingsWidget::widgetChanged );
165}
166
168
170{
171 if ( QgsRasterLayerSimpleLabeling *simpleLabeling = dynamic_cast<QgsRasterLayerSimpleLabeling *>( labeling ) )
172 {
173 setFormat( simpleLabeling->textFormat() );
174 mBandCombo->setBand( simpleLabeling->band() );
175 mPrioritySlider->setValue( static_cast<int>( 100 - simpleLabeling->priority() * 100 ) );
176
177 mComboOverlapHandling->setCurrentIndex( mComboOverlapHandling->findData( static_cast<int>( simpleLabeling->placementSettings().overlapHandling() ) ) );
178 mZIndexSpinBox->setValue( simpleLabeling->zIndex() );
179 if ( const QgsNumericFormat *format = simpleLabeling->numericFormat() )
180 mNumberFormat.reset( format->clone() );
181
182 mLimitLabelChkBox->setChecked( simpleLabeling->thinningSettings().limitNumberOfLabelsEnabled() );
183 mLimitLabelSpinBox->setValue( simpleLabeling->thinningSettings().maximumNumberLabels() );
184 mMinSizeSpinBox->setValue( simpleLabeling->thinningSettings().minimumFeatureSize() );
185
186 mScaleBasedVisibilityChkBx->setChecked( simpleLabeling->hasScaleBasedVisibility() );
187 mMinScaleWidget->setScale( simpleLabeling->minimumScale() );
188 mMaxScaleWidget->setScale( simpleLabeling->maximumScale() );
189
190 mResampleOverSpin->setValue( simpleLabeling->resampleOver() );
191 mResampleMethodComboBox->setCurrentIndex( mResampleMethodComboBox->findData( QVariant::fromValue( simpleLabeling->resampleMethod() ) ) );
192
193 updateUi();
194 }
195}
196
198{
199 if ( QgsRasterLayerSimpleLabeling *simpleLabeling = dynamic_cast<QgsRasterLayerSimpleLabeling *>( labeling ) )
200 {
201 simpleLabeling->setTextFormat( format() );
202 simpleLabeling->setBand( mBandCombo->currentBand() );
203 simpleLabeling->setPriority( 1.0 - mPrioritySlider->value() / 100.0 );
204 simpleLabeling->placementSettings().setOverlapHandling( static_cast<Qgis::LabelOverlapHandling>( mComboOverlapHandling->currentData().toInt() ) );
205 simpleLabeling->setZIndex( mZIndexSpinBox->value() );
206 simpleLabeling->setNumericFormat( mNumberFormat->clone() );
207
208 simpleLabeling->thinningSettings().setLimitNumberLabelsEnabled( mLimitLabelChkBox->isChecked() );
209 simpleLabeling->thinningSettings().setMaximumNumberLabels( mLimitLabelSpinBox->value() );
210 simpleLabeling->thinningSettings().setMinimumFeatureSize( mMinSizeSpinBox->value() );
211
212 simpleLabeling->setScaleBasedVisibility( mScaleBasedVisibilityChkBx->isChecked() );
213 simpleLabeling->setMinimumScale( mMinScaleWidget->scale() );
214 simpleLabeling->setMaximumScale( mMaxScaleWidget->scale() );
215
216 simpleLabeling->setResampleOver( mResampleOverSpin->value() );
217 simpleLabeling->setResampleMethod( mResampleMethodComboBox->currentData().value<Qgis::RasterResamplingMethod>() );
218 }
219}
220
222{
223 if ( mBandCombo )
224 mBandCombo->setLayer( layer );
225
226 QgsLabelingGui::setLayer( layer );
227 mMinSizeFrame->show();
228}
229
230void QgsRasterLabelSettingsWidget::changeNumberFormat()
231{
233 if ( panel && panel->dockMode() )
234 {
236 widget->setPanelTitle( tr( "Number Format" ) );
237 widget->setFormat( mNumberFormat.get() );
239 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [this, widget] {
240 if ( !mBlockChangesSignal )
241 {
242 mNumberFormat.reset( widget->format() );
243 emit widgetChanged();
244 }
245 } );
246 panel->openPanel( widget );
247 }
248 else
249 {
250 QgsNumericFormatSelectorDialog dialog( this );
251 dialog.setFormat( mNumberFormat.get() );
252 dialog.registerExpressionContextGenerator( this );
253 if ( dialog.exec() )
254 {
255 mNumberFormat.reset( dialog.format() );
256 emit widgetChanged();
257 }
258 }
259}
RasterResamplingMethod
Resampling method for raster provider-level resampling.
Definition qgis.h:1653
@ Lanczos
Lanczos windowed sinc interpolation (6x6 kernel).
Definition qgis.h:1658
@ Nearest
Nearest-neighbour resampling.
Definition qgis.h:1654
@ Mode
Mode (selects the value which appears most often of all the sampled points).
Definition qgis.h:1660
@ Bilinear
Bilinear (2x2 kernel) resampling.
Definition qgis.h:1655
@ Average
Average resampling.
Definition qgis.h:1659
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel).
Definition qgis.h:1657
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
Definition qgis.h:1656
@ Gauss
Gauss blurring.
Definition qgis.h:1661
@ Point
Points.
Definition qgis.h:400
LabelOverlapHandling
Label overlap handling.
Definition qgis.h:1268
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:83
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 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.
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
Base class for any widget that can be shown as an 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 ...
bool dockMode() const
Returns the dock mode state.
void changed()
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.
A combobox widget which displays the bands present in a raster layer.
void bandChanged(int band)
Emitted when the currently selected band changes.
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.
A spin box with a clear button that will set the value to the defined clear value.
Definition qgsspinbox.h:45
@ TextFormatEntity
Text formats.
Definition qgsstyle.h:211