QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgslabelingenginerulewidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelingenginerulewidget.cpp
3 ------------------------
4 begin : September 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_qgslabelingenginerulewidget.cpp"
19#include "qgsgui.h"
20#include "qgshelp.h"
21
22#include <QDialogButtonBox>
23#include <QPushButton>
24
25//
26// QgsLabelingEngineRuleDialog
27//
28
30 : QDialog( parent, flags )
31 , mWidget( widget )
32{
33 Q_ASSERT( mWidget );
34 setWindowTitle( tr( "Configure Rule" ) );
35 setObjectName( QStringLiteral( "QgsLabelingEngineRuleDialog" ) );
36
37 QVBoxLayout *layout = new QVBoxLayout( this );
38 layout->addWidget( mWidget );
39
40 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help, Qt::Horizontal, this );
41 layout->addWidget( mButtonBox );
42
43 setLayout( layout );
45
46 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked, this, &QDialog::accept );
47 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked, this, &QDialog::reject );
48 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [=] {
49 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#labeling-rules" ) );
50 } );
51}
52
57
62
64//
65// QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget
66//
67
68QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget( QWidget *parent )
70{
71 setupUi( this );
72
73 setWindowTitle( tr( "Prevent Labels Overlapping Features" ) );
74 setPanelTitle( tr( "Configure Rule" ) );
75
76 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
77 mComboTargetLayer->setFilters( Qgis::LayerFilter::HasGeometry );
78
79 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged );
80 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged );
81 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged );
82}
83
84void QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
85{
87 if ( !castRule )
88 return;
89
90 mBlockSignals = true;
91 mEditName->setText( castRule->name() );
92 mComboLabeledLayer->setLayer( const_cast<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature *>( castRule )->labeledLayer() );
93 mComboTargetLayer->setLayer( const_cast<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature *>( castRule )->targetLayer() );
94
95 mBlockSignals = false;
96}
97
98QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::rule()
99{
100 std::unique_ptr<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature> res = std::make_unique<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature>();
101 res->setName( mEditName->text() );
102 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
103 res->setTargetLayer( qobject_cast<QgsVectorLayer *>( mComboTargetLayer->currentLayer() ) );
104 return res.release();
105}
106
107void QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged()
108{
109 if ( !mBlockSignals )
110 emit changed();
111}
112
113//
114// QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget
115//
116
117QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget( QWidget *parent )
119{
120 setupUi( this );
121
122 setWindowTitle( tr( "Push Labels Away from Features" ) );
123 setPanelTitle( tr( "Configure Rule" ) );
124
125 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
126 mComboTargetLayer->setFilters( Qgis::LayerFilter::HasGeometry );
127
129
130 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
131 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
132 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
133
134 connect( mSpinDistance, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
135 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
136 connect( mCostSlider, &QSlider::valueChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
137}
138
139void QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
140{
142 if ( !castRule )
143 return;
144
145 mBlockSignals = true;
146 mEditName->setText( castRule->name() );
147 mComboLabeledLayer->setLayer( const_cast<QgsLabelingEngineRuleMinimumDistanceLabelToFeature *>( castRule )->labeledLayer() );
148 mComboTargetLayer->setLayer( const_cast<QgsLabelingEngineRuleMinimumDistanceLabelToFeature *>( castRule )->targetLayer() );
149
150 mSpinDistance->setValue( castRule->distance() );
151 mDistanceUnitWidget->setUnit( castRule->distanceUnit() );
152 mDistanceUnitWidget->setMapUnitScale( castRule->distanceUnitScale() );
153
154 mCostSlider->setValue( static_cast<int>( castRule->cost() * 10 ) );
155
156 mBlockSignals = false;
157}
158
159QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::rule()
160{
161 std::unique_ptr<QgsLabelingEngineRuleMinimumDistanceLabelToFeature> res = std::make_unique<QgsLabelingEngineRuleMinimumDistanceLabelToFeature>();
162 res->setName( mEditName->text() );
163 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
164 res->setTargetLayer( qobject_cast<QgsVectorLayer *>( mComboTargetLayer->currentLayer() ) );
165
166 res->setDistance( mSpinDistance->value() );
167 res->setDistanceUnit( mDistanceUnitWidget->unit() );
168 res->setDistanceUnitScale( mDistanceUnitWidget->getMapUnitScale() );
169
170 res->setCost( mCostSlider->value() / 10.0 );
171
172 return res.release();
173}
174
175void QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged()
176{
177 if ( !mBlockSignals )
178 emit changed();
179}
180
181//
182// QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget
183//
184
185QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget( QWidget *parent )
187{
188 setupUi( this );
189
190 setWindowTitle( tr( "Pull Labels toward Features" ) );
191 setPanelTitle( tr( "Configure Rule" ) );
192
193 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
194 mComboTargetLayer->setFilters( Qgis::LayerFilter::HasGeometry );
195
197
198 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
199 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
200 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
201
202 connect( mSpinDistance, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
203 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
204 connect( mCostSlider, &QSlider::valueChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
205}
206
207void QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
208{
210 if ( !castRule )
211 return;
212
213 mBlockSignals = true;
214 mEditName->setText( castRule->name() );
215 mComboLabeledLayer->setLayer( const_cast<QgsLabelingEngineRuleMaximumDistanceLabelToFeature *>( castRule )->labeledLayer() );
216 mComboTargetLayer->setLayer( const_cast<QgsLabelingEngineRuleMaximumDistanceLabelToFeature *>( castRule )->targetLayer() );
217
218 mSpinDistance->setValue( castRule->distance() );
219 mDistanceUnitWidget->setUnit( castRule->distanceUnit() );
220 mDistanceUnitWidget->setMapUnitScale( castRule->distanceUnitScale() );
221
222 mCostSlider->setValue( static_cast<int>( castRule->cost() * 10 ) );
223
224 mBlockSignals = false;
225}
226
227QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::rule()
228{
229 std::unique_ptr<QgsLabelingEngineRuleMaximumDistanceLabelToFeature> res = std::make_unique<QgsLabelingEngineRuleMaximumDistanceLabelToFeature>();
230 res->setName( mEditName->text() );
231 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
232 res->setTargetLayer( qobject_cast<QgsVectorLayer *>( mComboTargetLayer->currentLayer() ) );
233
234 res->setDistance( mSpinDistance->value() );
235 res->setDistanceUnit( mDistanceUnitWidget->unit() );
236 res->setDistanceUnitScale( mDistanceUnitWidget->getMapUnitScale() );
237
238 res->setCost( mCostSlider->value() / 10.0 );
239
240 return res.release();
241}
242
243void QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged()
244{
245 if ( !mBlockSignals )
246 emit changed();
247}
248
249
250//
251// QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget
252//
253
254QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget( QWidget *parent )
256{
257 setupUi( this );
258
259 setWindowTitle( tr( "Pull Labels toward Features" ) );
260 setPanelTitle( tr( "Configure Rule" ) );
261
262 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
263 mComboTargetLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
264
266
267 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
268 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
269 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
270
271 connect( mSpinDistance, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
272 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
273}
274
275void QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
276{
278 if ( !castRule )
279 return;
280
281 mBlockSignals = true;
282 mEditName->setText( castRule->name() );
283 mComboLabeledLayer->setLayer( const_cast<QgsLabelingEngineRuleMinimumDistanceLabelToLabel *>( castRule )->labeledLayer() );
284 mComboTargetLayer->setLayer( const_cast<QgsLabelingEngineRuleMinimumDistanceLabelToLabel *>( castRule )->targetLayer() );
285
286 mSpinDistance->setValue( castRule->distance() );
287 mDistanceUnitWidget->setUnit( castRule->distanceUnit() );
288 mDistanceUnitWidget->setMapUnitScale( castRule->distanceUnitScale() );
289
290 mBlockSignals = false;
291}
292
293QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::rule()
294{
295 std::unique_ptr<QgsLabelingEngineRuleMinimumDistanceLabelToLabel> res = std::make_unique<QgsLabelingEngineRuleMinimumDistanceLabelToLabel>();
296 res->setName( mEditName->text() );
297 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
298 res->setTargetLayer( mComboTargetLayer->currentLayer() );
299
300 res->setDistance( mSpinDistance->value() );
301 res->setDistanceUnit( mDistanceUnitWidget->unit() );
302 res->setDistanceUnitScale( mDistanceUnitWidget->getMapUnitScale() );
303
304 return res.release();
305}
306
307void QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged()
308{
309 if ( !mBlockSignals )
310 emit changed();
311}
312
313
@ SpatialLayer
All spatial layers.
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels and the features from the targetLayer().
const QgsMapUnitScale & distanceUnitScale() const
Returns the scaling for the distance between labels and the features from the targetLayer().
double distance() const
Returns the acceptable distance threshold between labels and the features from the targetLayer().
QgsVectorLayer * targetLayer() const
Returns the layer providing the features which labels must be distant from (or close to).
double cost() const
Returns the penalty cost incurred when the rule is violated.
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
Abstract base class for labeling engine rules.
QString name() const
Returns the name for this instance of the rule.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
A labeling engine rule which prevents labels being placed overlapping features from a different layer...
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
QgsVectorLayer * targetLayer() const
Returns the layer providing the features which labels must not overlap.
void setRule(const QgsAbstractLabelingEngineRule *rule)
Sets the rule to show in the dialog.
QgsAbstractLabelingEngineRule * rule()
Returns the rule defined by the current settings in the dialog.
QgsLabelingEngineRuleDialog(QgsLabelingEngineRuleWidget *widget, QWidget *parent=nullptr, Qt::WindowFlags flags=QgsGuiUtils::ModalDialogFlags)
Constructor for QgsLabelingEngineRuleDialog.
A labeling engine rule which prevents labels being placed too far from features from a different laye...
A labeling engine rule which prevents labels being placed too close to features from a different laye...
A labeling engine rule which prevents labels being placed too close to labels from a different layer.
double distance() const
Returns the minimum permitted distance between labels from the labeledLayer() and the labels from the...
const QgsMapUnitScale & distanceUnitScale() const
Returns the scaling for the distance between labels from the labeledLayer() and the labels from the t...
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
QgsMapLayer * targetLayer() const
Returns the layer providing the labels which labels must be distant from.
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels from the labeledLayer() and the labels from the tar...
Base class for widgets which allow control over the properties of QgsAbstractLabelingEngineRule subcl...
virtual void setRule(const QgsAbstractLabelingEngineRule *rule)=0
Sets the rule to show in the widget.
virtual QgsAbstractLabelingEngineRule * rule()=0
Returns the rule defined by the current settings in the widget.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
void changed()
Emitted when the selected unit is changed, or the definition of the map unit scale is changed.
QList< Qgis::RenderUnit > RenderUnitList
List of render units.