QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18
19#include "qgsapplication.h"
21
22#include "moc_qgslabellineanchorwidget.cpp"
23
25 : QgsLabelSettingsWidgetBase( parent, vl )
26{
27 setupUi( this );
28
29 setPanelTitle( tr( "Line Anchor Settings" ) );
30
31 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorCenter.svg" ) ), tr( "Center of Line" ), 0.5 );
32 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorStart.svg" ) ), tr( "Start of Line" ), 0.0 );
33 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorEnd.svg" ) ), tr( "End of Line" ), 1.0 );
34 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabelAnchorCustom.svg" ) ), tr( "Custom…" ), -1.0 );
35
36 mClippingComboBox->addItem( tr( "Use Visible Part of Line" ), static_cast<int>( QgsLabelLineSettings::AnchorClipping::UseVisiblePartsOfLine ) );
37 mClippingComboBox->addItem( tr( "Use Entire Line" ), static_cast<int>( QgsLabelLineSettings::AnchorClipping::UseEntireLine ) );
38
39 mAnchorTypeComboBox->addItem( tr( "Preferred Placement Hint" ), static_cast<int>( QgsLabelLineSettings::AnchorType::HintOnly ) );
40 mAnchorTypeComboBox->addItem( tr( "Strict" ), static_cast<int>( QgsLabelLineSettings::AnchorType::Strict ) );
41
42 mAnchorTextPointComboBox->addItem( tr( "Automatic" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::FollowPlacement ) );
43 mAnchorTextPointComboBox->addItem( tr( "Start of Text" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::StartOfText ) );
44 mAnchorTextPointComboBox->addItem( tr( "Center of Text" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::CenterOfText ) );
45 mAnchorTextPointComboBox->addItem( tr( "End of Text" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::EndOfText ) );
46
47 connect( mPercentPlacementComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
48 if ( !mBlockSignals )
49 emit changed();
50
51 if ( mPercentPlacementComboBox->currentData().toDouble() < 0 )
52 mCustomPlacementSpinBox->setEnabled( true );
53 else
54 {
55 mCustomPlacementSpinBox->setEnabled( false );
56 mBlockSignals = true;
57 mCustomPlacementSpinBox->setValue( mPercentPlacementComboBox->currentData().toDouble() * 100 );
58 mBlockSignals = false;
59 }
60 } );
61 connect( mCustomPlacementSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [this]( double ) {
62 if ( !mBlockSignals )
63 emit changed();
64 } );
65
66 connect( mAnchorTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
67 if ( !mBlockSignals )
68 emit changed();
69
70 updateAnchorTypeHint();
71 } );
72
73 connect( mClippingComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
74 if ( !mBlockSignals )
75 emit changed();
76 } );
77
78 connect( mAnchorTextPointComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
79 if ( !mBlockSignals )
80 emit changed();
81
82 updateAnchorTextPointHint();
83 } );
84
89 updateAnchorTypeHint();
90 updateAnchorTextPointHint();
91}
92
94{
95 mBlockSignals = true;
96 const int comboIndex = mPercentPlacementComboBox->findData( settings.lineAnchorPercent() );
97 if ( comboIndex >= 0 )
98 {
99 mPercentPlacementComboBox->setCurrentIndex( comboIndex );
100 }
101 else
102 {
103 // set custom control
104 mPercentPlacementComboBox->setCurrentIndex( mPercentPlacementComboBox->findData( -1.0 ) );
105 mCustomPlacementSpinBox->setValue( settings.lineAnchorPercent() * 100.0 );
106 }
107 mCustomPlacementSpinBox->setEnabled( mPercentPlacementComboBox->currentData().toDouble() < 0 );
108
109 mAnchorTypeComboBox->setCurrentIndex( mAnchorTypeComboBox->findData( static_cast<int>( settings.anchorType() ) ) );
110 mClippingComboBox->setCurrentIndex( mClippingComboBox->findData( static_cast<int>( settings.anchorClipping() ) ) );
111 mAnchorTextPointComboBox->setCurrentIndex( mAnchorTextPointComboBox->findData( static_cast<int>( settings.anchorTextPoint() ) ) );
112 mBlockSignals = false;
113}
114
116{
118
119 if ( mPercentPlacementComboBox->currentData().toDouble() >= 0 )
120 {
121 settings.setLineAnchorPercent( mPercentPlacementComboBox->currentData().toDouble() );
122 }
123 else
124 {
125 settings.setLineAnchorPercent( mCustomPlacementSpinBox->value() / 100.0 );
126 }
127
128 settings.setAnchorType( static_cast<QgsLabelLineSettings::AnchorType>( mAnchorTypeComboBox->currentData().toInt() ) );
129 settings.setAnchorClipping( static_cast<QgsLabelLineSettings::AnchorClipping>( mClippingComboBox->currentData().toInt() ) );
130 settings.setAnchorTextPoint( static_cast<QgsLabelLineSettings::AnchorTextPoint>( mAnchorTextPointComboBox->currentData().toInt() ) );
131 return settings;
132}
133
141
142void QgsLabelLineAnchorWidget::updateAnchorTypeHint()
143{
144 QString hint;
145 switch ( static_cast<QgsLabelLineSettings::AnchorType>( mAnchorTypeComboBox->currentData().toInt() ) )
146 {
148 hint = tr( "Labels are placed exactly on the label anchor only, and no other fallback placements are permitted." );
149 break;
150
152 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." );
153 break;
154 }
155 mAnchorTypeHintLabel->setText( hint );
156}
157
158void QgsLabelLineAnchorWidget::updateAnchorTextPointHint()
159{
160 QString hint;
161 switch ( static_cast<QgsLabelLineSettings::AnchorTextPoint>( mAnchorTextPointComboBox->currentData().toInt() ) )
162 {
164 hint = tr( "Labels are placed so that the start of their text is placed at the anchor point." );
165 break;
167 hint = tr( "Labels are placed so that the center of their text is placed at the anchor point." );
168 break;
170 hint = tr( "Labels are placed so that the end of their text is placed at the anchor point." );
171 break;
173 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." );
174 break;
175 }
176 mAnchorTextPointHintLabel->setText( hint );
177}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsLabelLineAnchorWidget(QWidget *parent=nullptr, QgsVectorLayer *vl=nullptr)
Constructor for QgsLabelLineAnchorWidget.
void updateDataDefinedProperties(QgsPropertyCollection &properties) override
Updates a data defined properties collection, correctly setting the values for any properties related...
QgsLabelLineSettings settings() const
Returns the line settings defined by the widget.
void setSettings(const QgsLabelLineSettings &settings)
Sets the line settings to show in the widget.
Contains settings related to how the label engine places and formats labels for line features (or pol...
AnchorType
Line anchor types.
@ Strict
Line anchor is a strict placement, and other placements are not permitted.
@ HintOnly
Line anchor is a hint for preferred placement only, but other placements close to the hint are permit...
AnchorClipping
Clipping behavior for line anchor calculation.
@ UseEntireLine
Entire original feature line geometry is used when calculating the line anchor for labels.
@ UseVisiblePartsOfLine
Only visible parts of lines are considered when calculating the line anchor for labels.
AnchorTextPoint
Anchor point of label text.
@ StartOfText
Anchor using start of text.
@ CenterOfText
Anchor using center of text.
@ FollowPlacement
Automatically set the anchor point based on the lineAnchorPercent() value. Values <25% will use the s...
QgsLabelSettingsWidgetBase(QWidget *parent=nullptr, QgsMapLayer *vl=nullptr)
Constructor for QgsLabelSettingsWidgetBase.
void changed()
Emitted when any of the settings described by the widget are changed.
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key)
Registers a data defined override button.
QgsPropertyCollection mDataDefinedProperties
Contains the data defined properties defined by the widget.
@ LineAnchorType
Line anchor type.
@ LineAnchorClipping
Clipping mode for line anchor calculation.
@ LineAnchorPercent
Portion along line at which labels should be anchored.
@ LineAnchorTextPoint
Line anchor text point.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
Represents a vector layer which manages a vector based dataset.