QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
21#include <QDialogButtonBox>
22#include <QPushButton>
23
24//
25// QgsLabelingEngineRuleDialog
26//
27
29 : QDialog( parent, flags )
30 , mWidget( widget )
31{
32 Q_ASSERT( mWidget );
33 setWindowTitle( tr( "Configure Rule" ) );
34 setObjectName( QStringLiteral( "QgsLabelingEngineRuleDialog" ) );
35
36 QVBoxLayout *layout = new QVBoxLayout( this );
37 layout->addWidget( mWidget );
38
39 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
40 layout->addWidget( mButtonBox );
41
42 setLayout( layout );
44
45 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QAbstractButton::clicked, this, &QDialog::accept );
46 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QAbstractButton::clicked, this, &QDialog::reject );
47}
48
53
58
60//
61// QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget
62//
63
64QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget( QWidget *parent )
66{
67 setupUi( this );
68
69 setWindowTitle( tr( "Prevent Labels Overlapping Features" ) );
70 setPanelTitle( tr( "Configure Rule" ) );
71
72 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
73 mComboTargetLayer->setFilters( Qgis::LayerFilter::HasGeometry );
74
75 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged );
76 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged );
77 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged );
78}
79
80void QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
81{
83 if ( !castRule )
84 return;
85
86 mBlockSignals = true;
87 mEditName->setText( castRule->name() );
88 mComboLabeledLayer->setLayer( const_cast< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature * >( castRule )->labeledLayer() );
89 mComboTargetLayer->setLayer( const_cast< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature * >( castRule )->targetLayer() );
90
91 mBlockSignals = false;
92}
93
94QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::rule()
95{
96 std::unique_ptr< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature > res = std::make_unique< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature >();
97 res->setName( mEditName->text() );
98 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
99 res->setTargetLayer( qobject_cast< QgsVectorLayer * >( mComboTargetLayer->currentLayer() ) );
100 return res.release();
101}
102
103void QgsLabelingEngineRuleAvoidLabelOverlapWithFeatureWidget::onChanged()
104{
105 if ( !mBlockSignals )
106 emit changed();
107}
108
109//
110// QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget
111//
112
113QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget( QWidget *parent )
115{
116 setupUi( this );
117
118 setWindowTitle( tr( "Push Labels Away from Features" ) );
119 setPanelTitle( tr( "Configure Rule" ) );
120
121 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
122 mComboTargetLayer->setFilters( Qgis::LayerFilter::HasGeometry );
123
126
127 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
128 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
129 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
130
131 connect( mSpinDistance, qOverload< double >( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
132 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
133 connect( mCostSlider, &QSlider::valueChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged );
134}
135
136void QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
137{
139 if ( !castRule )
140 return;
141
142 mBlockSignals = true;
143 mEditName->setText( castRule->name() );
144 mComboLabeledLayer->setLayer( const_cast< QgsLabelingEngineRuleMinimumDistanceLabelToFeature * >( castRule )->labeledLayer() );
145 mComboTargetLayer->setLayer( const_cast< QgsLabelingEngineRuleMinimumDistanceLabelToFeature * >( castRule )->targetLayer() );
146
147 mSpinDistance->setValue( castRule->distance() );
148 mDistanceUnitWidget->setUnit( castRule->distanceUnit() );
149 mDistanceUnitWidget->setMapUnitScale( castRule->distanceUnitScale() );
150
151 mCostSlider->setValue( static_cast< int >( castRule->cost() * 10 ) );
152
153 mBlockSignals = false;
154}
155
156QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::rule()
157{
158 std::unique_ptr< QgsLabelingEngineRuleMinimumDistanceLabelToFeature > res = std::make_unique< QgsLabelingEngineRuleMinimumDistanceLabelToFeature >();
159 res->setName( mEditName->text() );
160 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
161 res->setTargetLayer( qobject_cast< QgsVectorLayer * >( mComboTargetLayer->currentLayer() ) );
162
163 res->setDistance( mSpinDistance->value() );
164 res->setDistanceUnit( mDistanceUnitWidget->unit() );
165 res->setDistanceUnitScale( mDistanceUnitWidget->getMapUnitScale() );
166
167 res->setCost( mCostSlider->value() / 10.0 );
168
169 return res.release();
170}
171
172void QgsLabelingEngineRuleMinimumDistanceLabelToFeatureWidget::onChanged()
173{
174 if ( !mBlockSignals )
175 emit changed();
176}
177
178//
179// QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget
180//
181
182QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget( QWidget *parent )
184{
185 setupUi( this );
186
187 setWindowTitle( tr( "Pull Labels toward Features" ) );
188 setPanelTitle( tr( "Configure Rule" ) );
189
190 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
191 mComboTargetLayer->setFilters( Qgis::LayerFilter::HasGeometry );
192
195
196 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
197 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
198 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
199
200 connect( mSpinDistance, qOverload< double >( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
201 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
202 connect( mCostSlider, &QSlider::valueChanged, this, &QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged );
203}
204
205void QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
206{
208 if ( !castRule )
209 return;
210
211 mBlockSignals = true;
212 mEditName->setText( castRule->name() );
213 mComboLabeledLayer->setLayer( const_cast< QgsLabelingEngineRuleMaximumDistanceLabelToFeature * >( castRule )->labeledLayer() );
214 mComboTargetLayer->setLayer( const_cast< QgsLabelingEngineRuleMaximumDistanceLabelToFeature * >( castRule )->targetLayer() );
215
216 mSpinDistance->setValue( castRule->distance() );
217 mDistanceUnitWidget->setUnit( castRule->distanceUnit() );
218 mDistanceUnitWidget->setMapUnitScale( castRule->distanceUnitScale() );
219
220 mCostSlider->setValue( static_cast< int >( castRule->cost() * 10 ) );
221
222 mBlockSignals = false;
223}
224
225QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::rule()
226{
227 std::unique_ptr< QgsLabelingEngineRuleMaximumDistanceLabelToFeature > res = std::make_unique< QgsLabelingEngineRuleMaximumDistanceLabelToFeature >();
228 res->setName( mEditName->text() );
229 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
230 res->setTargetLayer( qobject_cast< QgsVectorLayer * >( mComboTargetLayer->currentLayer() ) );
231
232 res->setDistance( mSpinDistance->value() );
233 res->setDistanceUnit( mDistanceUnitWidget->unit() );
234 res->setDistanceUnitScale( mDistanceUnitWidget->getMapUnitScale() );
235
236 res->setCost( mCostSlider->value() / 10.0 );
237
238 return res.release();
239}
240
241void QgsLabelingEngineRuleMaximumDistanceLabelToFeatureWidget::onChanged()
242{
243 if ( !mBlockSignals )
244 emit changed();
245}
246
247
248//
249// QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget
250//
251
252QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget( QWidget *parent )
254{
255 setupUi( this );
256
257 setWindowTitle( tr( "Pull Labels toward Features" ) );
258 setPanelTitle( tr( "Configure Rule" ) );
259
260 mComboLabeledLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
261 mComboTargetLayer->setFilters( Qgis::LayerFilter::SpatialLayer );
262
265
266 connect( mEditName, &QLineEdit::textChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
267 connect( mComboLabeledLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
268 connect( mComboTargetLayer, &QgsMapLayerComboBox::layerChanged, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
269
270 connect( mSpinDistance, qOverload< double >( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
271 connect( mDistanceUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged );
272}
273
274void QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::setRule( const QgsAbstractLabelingEngineRule *rule )
275{
277 if ( !castRule )
278 return;
279
280 mBlockSignals = true;
281 mEditName->setText( castRule->name() );
282 mComboLabeledLayer->setLayer( const_cast< QgsLabelingEngineRuleMinimumDistanceLabelToLabel * >( castRule )->labeledLayer() );
283 mComboTargetLayer->setLayer( const_cast< QgsLabelingEngineRuleMinimumDistanceLabelToLabel * >( castRule )->targetLayer() );
284
285 mSpinDistance->setValue( castRule->distance() );
286 mDistanceUnitWidget->setUnit( castRule->distanceUnit() );
287 mDistanceUnitWidget->setMapUnitScale( castRule->distanceUnitScale() );
288
289 mBlockSignals = false;
290}
291
292QgsAbstractLabelingEngineRule *QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::rule()
293{
294 std::unique_ptr< QgsLabelingEngineRuleMinimumDistanceLabelToLabel > res = std::make_unique< QgsLabelingEngineRuleMinimumDistanceLabelToLabel >();
295 res->setName( mEditName->text() );
296 res->setLabeledLayer( mComboLabeledLayer->currentLayer() );
297 res->setTargetLayer( mComboTargetLayer->currentLayer() );
298
299 res->setDistance( mSpinDistance->value() );
300 res->setDistanceUnit( mDistanceUnitWidget->unit() );
301 res->setDistanceUnitScale( mDistanceUnitWidget->getMapUnitScale() );
302
303 return res.release();
304}
305
306void QgsLabelingEngineRuleMinimumDistanceLabelToLabelWidget::onChanged()
307{
308 if ( !mBlockSignals )
309 emit changed();
310}
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:209
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.