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