QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgslabellineanchorwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslabellineanchorwidget.cpp
3  ----------------------
4  begin : August 2020
5  copyright : (C) 2020 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 
16 
19 #include "qgsapplication.h"
20 
22  : QgsLabelSettingsWidgetBase( parent, vl )
23 {
24  setupUi( this );
25 
26  setPanelTitle( tr( "Line Anchor Settings" ) );
27 
28  mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorCenter.svg" ) ), tr( "Center of Line" ), 0.5 );
29  mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorStart.svg" ) ), tr( "Start of Line" ), 0.0 );
30  mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorEnd.svg" ) ), tr( "End of Line" ), 1.0 );
31  mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorCustom.svg" ) ), tr( "Custom…" ), -1.0 );
32 
33  mClippingComboBox->addItem( tr( "Use Visible Part of Line" ), static_cast< int >( QgsLabelLineSettings::AnchorClipping::UseVisiblePartsOfLine ) );
34  mClippingComboBox->addItem( tr( "Use Entire Line" ), static_cast< int >( QgsLabelLineSettings::AnchorClipping::UseEntireLine ) );
35 
36  mAnchorTypeComboBox->addItem( tr( "Preferred Placement Hint" ), static_cast< int >( QgsLabelLineSettings::AnchorType::HintOnly ) );
37  mAnchorTypeComboBox->addItem( tr( "Strict" ), static_cast< int >( QgsLabelLineSettings::AnchorType::Strict ) );
38 
39  mAnchorTextPointComboBox->addItem( tr( "Automatic" ), static_cast< int >( QgsLabelLineSettings::AnchorTextPoint::FollowPlacement ) );
40  mAnchorTextPointComboBox->addItem( tr( "Start of Text" ), static_cast< int >( QgsLabelLineSettings::AnchorTextPoint::StartOfText ) );
41  mAnchorTextPointComboBox->addItem( tr( "Center of Text" ), static_cast< int >( QgsLabelLineSettings::AnchorTextPoint::CenterOfText ) );
42  mAnchorTextPointComboBox->addItem( tr( "End of Text" ), static_cast< int >( QgsLabelLineSettings::AnchorTextPoint::EndOfText ) );
43 
44  connect( mPercentPlacementComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
45  {
46  if ( !mBlockSignals )
47  emit changed();
48 
49  if ( mPercentPlacementComboBox->currentData().toDouble() < 0 )
50  mCustomPlacementSpinBox->setEnabled( true );
51  else
52  {
53  mCustomPlacementSpinBox->setEnabled( false );
54  mBlockSignals = true;
55  mCustomPlacementSpinBox->setValue( mPercentPlacementComboBox->currentData().toDouble() * 100 );
56  mBlockSignals = false;
57  }
58  } );
59  connect( mCustomPlacementSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double )
60  {
61  if ( !mBlockSignals )
62  emit changed();
63  } );
64 
65  connect( mAnchorTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
66  {
67  if ( !mBlockSignals )
68  emit changed();
69 
70  updateAnchorTypeHint();
71  } );
72 
73  connect( mClippingComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
74  {
75  if ( !mBlockSignals )
76  emit changed();
77  } );
78 
79  connect( mAnchorTextPointComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
80  {
81  if ( !mBlockSignals )
82  emit changed();
83 
84  updateAnchorTextPointHint();
85  } );
86 
91  updateAnchorTypeHint();
92  updateAnchorTextPointHint();
93 }
94 
96 {
97  mBlockSignals = true;
98  const int comboIndex = mPercentPlacementComboBox->findData( settings.lineAnchorPercent() );
99  if ( comboIndex >= 0 )
100  {
101  mPercentPlacementComboBox->setCurrentIndex( comboIndex );
102  }
103  else
104  {
105  // set custom control
106  mPercentPlacementComboBox->setCurrentIndex( mPercentPlacementComboBox->findData( -1.0 ) );
107  mCustomPlacementSpinBox->setValue( settings.lineAnchorPercent() * 100.0 );
108  }
109  mCustomPlacementSpinBox->setEnabled( mPercentPlacementComboBox->currentData().toDouble() < 0 );
110 
111  mAnchorTypeComboBox->setCurrentIndex( mAnchorTypeComboBox->findData( static_cast< int >( settings.anchorType() ) ) );
112  mClippingComboBox->setCurrentIndex( mClippingComboBox->findData( static_cast< int >( settings.anchorClipping() ) ) );
113  mAnchorTextPointComboBox->setCurrentIndex( mAnchorTextPointComboBox->findData( static_cast< int >( settings.anchorTextPoint() ) ) );
114  mBlockSignals = false;
115 }
116 
118 {
120 
121  if ( mPercentPlacementComboBox->currentData().toDouble() >= 0 )
122  {
123  settings.setLineAnchorPercent( mPercentPlacementComboBox->currentData().toDouble() );
124  }
125  else
126  {
127  settings.setLineAnchorPercent( mCustomPlacementSpinBox->value() / 100.0 );
128  }
129 
130  settings.setAnchorType( static_cast< QgsLabelLineSettings::AnchorType >( mAnchorTypeComboBox->currentData().toInt() ) );
131  settings.setAnchorClipping( static_cast< QgsLabelLineSettings::AnchorClipping >( mClippingComboBox->currentData().toInt() ) );
132  settings.setAnchorTextPoint( static_cast< QgsLabelLineSettings::AnchorTextPoint >( mAnchorTextPointComboBox->currentData().toInt() ) );
133  return settings;
134 }
135 
137 {
142 }
143 
144 void QgsLabelLineAnchorWidget::updateAnchorTypeHint()
145 {
146  QString hint;
147  switch ( static_cast< QgsLabelLineSettings::AnchorType >( mAnchorTypeComboBox->currentData().toInt() ) )
148  {
150  hint = tr( "Labels are placed exactly on the label anchor only, and no other fallback placements are permitted." );
151  break;
152 
154  hint = tr( "The label anchor is treated as a hint for the preferred label placement, but other placements close to the anchor point are permitted." );
155  break;
156  }
157  mAnchorTypeHintLabel->setText( hint );
158 }
159 
160 void QgsLabelLineAnchorWidget::updateAnchorTextPointHint()
161 {
162  QString hint;
163  switch ( static_cast< QgsLabelLineSettings::AnchorTextPoint >( mAnchorTextPointComboBox->currentData().toInt() ) )
164  {
166  hint = tr( "Labels are placed so that the start of their text is placed at the anchor point." );
167  break;
169  hint = tr( "Labels are placed so that the center of their text is placed at the anchor point." );
170  break;
172  hint = tr( "Labels are placed so that the end of their text is placed at the anchor point." );
173  break;
175  hint = tr( "The text justification is determined based on the anchor point. Anchors close to the start of the line will use the start of the text, anchors close to the end will use the end of the text, and central values will use the center of the text." );
176  break;
177  }
178  mAnchorTextPointHintLabel->setText( hint );
179 }
qgslabellineanchorwidget.h
QgsLabelLineSettings::AnchorClipping::UseEntireLine
@ UseEntireLine
Entire original feature line geometry is used when calculating the line anchor for labels.
qgsexpressioncontextutils.h
QgsLabelLineAnchorWidget::updateDataDefinedProperties
void updateDataDefinedProperties(QgsPropertyCollection &properties) override
Updates a data defined properties collection, correctly setting the values for any properties related...
Definition: qgslabellineanchorwidget.cpp:136
QgsLabelLineSettings::AnchorTextPoint::StartOfText
@ StartOfText
Anchor using start of text.
QgsLabelLineSettings::anchorTextPoint
AnchorTextPoint anchorTextPoint() const
Returns the line anchor text point, which dictates which part of the label text should be placed at t...
Definition: qgslabellinesettings.h:356
QgsLabelLineAnchorWidget::setSettings
void setSettings(const QgsLabelLineSettings &settings)
Sets the line settings to show in the widget.
Definition: qgslabellineanchorwidget.cpp:95
QgsLabelLineSettings::setAnchorTextPoint
void setAnchorTextPoint(AnchorTextPoint point)
Sets the line anchor text point, which dictates which part of the label text should be placed at the ...
Definition: qgslabellinesettings.h:366
QgsLabelLineSettings::setAnchorClipping
void setAnchorClipping(AnchorClipping clipping)
Sets the line anchor clipping mode, which dictates how line strings are clipped before calculating th...
Definition: qgslabellinesettings.h:346
QgsPropertyCollection::property
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
Definition: qgspropertycollection.cpp:214
qgsapplication.h
QgsLabelLineAnchorWidget::QgsLabelLineAnchorWidget
QgsLabelLineAnchorWidget(QWidget *parent=nullptr, QgsVectorLayer *vl=nullptr)
Constructor for QgsLabelLineAnchorWidget.
Definition: qgslabellineanchorwidget.cpp:21
QgsLabelLineSettings::lineAnchorPercent
double lineAnchorPercent() const
Returns the percent along the line at which labels should be placed.
Definition: qgslabellinesettings.h:287
QgsLabelLineSettings::anchorClipping
AnchorClipping anchorClipping() const
Returns the line anchor clipping mode, which dictates how line strings are clipped before calculating...
Definition: qgslabellinesettings.h:334
QgsLabelLineSettings::AnchorTextPoint::EndOfText
@ EndOfText
Anchor using end of text.
QgsLabelLineSettings::AnchorType::Strict
@ Strict
Line anchor is a strict placement, and other placements are not permitted.
QgsLabelLineSettings::AnchorClipping::UseVisiblePartsOfLine
@ UseVisiblePartsOfLine
Only visible parts of lines are considered when calculating the line anchor for labels.
QgsLabelLineSettings::AnchorTextPoint::CenterOfText
@ CenterOfText
Anchor using center of text.
QgsLabelLineSettings::anchorType
AnchorType anchorType() const
Returns the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
Definition: qgslabellinesettings.h:312
QgsLabelLineSettings::AnchorTextPoint
AnchorTextPoint
Anchor point of label text.
Definition: qgslabellinesettings.h:83
QgsPalLayerSettings::LineAnchorPercent
@ LineAnchorPercent
Portion along line at which labels should be anchored (since QGIS 3.16)
Definition: qgspallabeling.h:258
QgsPalLayerSettings::LineAnchorClipping
@ LineAnchorClipping
Clipping mode for line anchor calculation (since QGIS 3.20)
Definition: qgspallabeling.h:259
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
QgsLabelLineSettings::AnchorType::HintOnly
@ HintOnly
Line anchor is a hint for preferred placement only, but other placements close to the hint are permit...
QgsPalLayerSettings::LineAnchorTextPoint
@ LineAnchorTextPoint
Line anchor text point (since QGIS 3.26)
Definition: qgspallabeling.h:261
QgsLabelLineSettings::AnchorType
AnchorType
Line anchor types.
Definition: qgslabellinesettings.h:59
QgsLabelLineSettings::setLineAnchorPercent
void setLineAnchorPercent(double percent)
Sets the percent along the line at which labels should be placed.
Definition: qgslabellinesettings.h:302
QgsLabelSettingsWidgetBase::registerDataDefinedButton
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key)
Registers a data defined override button.
Definition: qgslabelsettingswidgetbase.cpp:145
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Definition: qgspropertycollection.h:318
QgsLabelSettingsWidgetBase
Base class for widgets which allow customization of label engine properties, such as label placement ...
Definition: qgslabelsettingswidgetbase.h:38
QgsLabelLineSettings
Contains settings related to how the label engine places and formats labels for line features (or pol...
Definition: qgslabellinesettings.h:39
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsLabelLineSettings::setAnchorType
void setAnchorType(AnchorType type)
Sets the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
Definition: qgslabellinesettings.h:322
QgsPalLayerSettings::LineAnchorType
@ LineAnchorType
Line anchor type (since QGIS 3.26)
Definition: qgspallabeling.h:260
QgsPropertyCollection::setProperty
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
Definition: qgspropertycollection.cpp:187
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
QgsLabelSettingsWidgetBase::mDataDefinedProperties
QgsPropertyCollection mDataDefinedProperties
Contains the data defined properties defined by the widget.
Definition: qgslabelsettingswidgetbase.h:118
QgsLabelSettingsWidgetBase::changed
void changed()
Emitted when any of the settings described by the widget are changed.
QgsLabelLineAnchorWidget::settings
QgsLabelLineSettings settings() const
Returns the line settings defined by the widget.
Definition: qgslabellineanchorwidget.cpp:117
QgsLabelLineSettings::AnchorClipping
AnchorClipping
Clipping behavior for line anchor calculation.
Definition: qgslabellinesettings.h:71
QgsLabelLineSettings::AnchorTextPoint::FollowPlacement
@ FollowPlacement
Automatically set the anchor point based on the lineAnchorPercent() value. Values <25% will use the s...