QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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 <QString>
23
24#include "moc_qgslabellineanchorwidget.cpp"
25
26using namespace Qt::StringLiterals;
27
29 : QgsLabelSettingsWidgetBase( parent, vl )
30{
31 setupUi( this );
32
33 setPanelTitle( tr( "Line Anchor Settings" ) );
34
35 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( u"/mActionLabelAnchorCenter.svg"_s ), tr( "Center of Line" ), 0.5 );
36 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( u"/mActionLabelAnchorStart.svg"_s ), tr( "Start of Line" ), 0.0 );
37 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( u"/mActionLabelAnchorEnd.svg"_s ), tr( "End of Line" ), 1.0 );
38 mPercentPlacementComboBox->addItem( QgsApplication::getThemeIcon( u"/mActionLabelAnchorCustom.svg"_s ), tr( "Custom…" ), -1.0 );
39
40 mClippingComboBox->addItem( tr( "Use Visible Part of Line" ), static_cast<int>( QgsLabelLineSettings::AnchorClipping::UseVisiblePartsOfLine ) );
41 mClippingComboBox->addItem( tr( "Use Entire Line" ), static_cast<int>( QgsLabelLineSettings::AnchorClipping::UseEntireLine ) );
42
43 mAnchorTypeComboBox->addItem( tr( "Preferred Placement Hint" ), static_cast<int>( QgsLabelLineSettings::AnchorType::HintOnly ) );
44 mAnchorTypeComboBox->addItem( tr( "Strict" ), static_cast<int>( QgsLabelLineSettings::AnchorType::Strict ) );
45
46 mAnchorTextPointComboBox->addItem( tr( "Automatic" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::FollowPlacement ) );
47 mAnchorTextPointComboBox->addItem( tr( "Start of Text" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::StartOfText ) );
48 mAnchorTextPointComboBox->addItem( tr( "Center of Text" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::CenterOfText ) );
49 mAnchorTextPointComboBox->addItem( tr( "End of Text" ), static_cast<int>( QgsLabelLineSettings::AnchorTextPoint::EndOfText ) );
50
51 connect( mPercentPlacementComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
52 if ( !mBlockSignals )
53 emit changed();
54
55 if ( mPercentPlacementComboBox->currentData().toDouble() < 0 )
56 mCustomPlacementSpinBox->setEnabled( true );
57 else
58 {
59 mCustomPlacementSpinBox->setEnabled( false );
60 mBlockSignals = true;
61 mCustomPlacementSpinBox->setValue( mPercentPlacementComboBox->currentData().toDouble() * 100 );
62 mBlockSignals = false;
63 }
64 } );
65 connect( mCustomPlacementSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [this]( double ) {
66 if ( !mBlockSignals )
67 emit changed();
68 } );
69
70 connect( mAnchorTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
71 if ( !mBlockSignals )
72 emit changed();
73
74 updateAnchorTypeHint();
75 } );
76
77 connect( mClippingComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
78 if ( !mBlockSignals )
79 emit changed();
80 } );
81
82 connect( mAnchorTextPointComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
83 if ( !mBlockSignals )
84 emit changed();
85
86 updateAnchorTextPointHint();
87 } );
88
93 updateAnchorTypeHint();
94 updateAnchorTextPointHint();
95}
96
98{
99 mBlockSignals = true;
100 const int comboIndex = mPercentPlacementComboBox->findData( settings.lineAnchorPercent() );
101 if ( comboIndex >= 0 )
102 {
103 mPercentPlacementComboBox->setCurrentIndex( comboIndex );
104 }
105 else
106 {
107 // set custom control
108 mPercentPlacementComboBox->setCurrentIndex( mPercentPlacementComboBox->findData( -1.0 ) );
109 mCustomPlacementSpinBox->setValue( settings.lineAnchorPercent() * 100.0 );
110 }
111 mCustomPlacementSpinBox->setEnabled( mPercentPlacementComboBox->currentData().toDouble() < 0 );
112
113 mAnchorTypeComboBox->setCurrentIndex( mAnchorTypeComboBox->findData( static_cast<int>( settings.anchorType() ) ) );
114 mClippingComboBox->setCurrentIndex( mClippingComboBox->findData( static_cast<int>( settings.anchorClipping() ) ) );
115 mAnchorTextPointComboBox->setCurrentIndex( mAnchorTextPointComboBox->findData( static_cast<int>( settings.anchorTextPoint() ) ) );
116 mBlockSignals = false;
117}
118
120{
122
123 if ( mPercentPlacementComboBox->currentData().toDouble() >= 0 )
124 {
125 settings.setLineAnchorPercent( mPercentPlacementComboBox->currentData().toDouble() );
126 }
127 else
128 {
129 settings.setLineAnchorPercent( mCustomPlacementSpinBox->value() / 100.0 );
130 }
131
132 settings.setAnchorType( static_cast<QgsLabelLineSettings::AnchorType>( mAnchorTypeComboBox->currentData().toInt() ) );
133 settings.setAnchorClipping( static_cast<QgsLabelLineSettings::AnchorClipping>( mClippingComboBox->currentData().toInt() ) );
134 settings.setAnchorTextPoint( static_cast<QgsLabelLineSettings::AnchorTextPoint>( mAnchorTextPointComboBox->currentData().toInt() ) );
135 return settings;
136}
137
145
146void QgsLabelLineAnchorWidget::updateAnchorTypeHint()
147{
148 QString hint;
149 switch ( static_cast<QgsLabelLineSettings::AnchorType>( mAnchorTypeComboBox->currentData().toInt() ) )
150 {
152 hint = tr( "Labels are placed exactly on the label anchor only, and no other fallback placements are permitted." );
153 break;
154
156 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." );
157 break;
158 }
159 mAnchorTypeHintLabel->setText( hint );
160}
161
162void QgsLabelLineAnchorWidget::updateAnchorTextPointHint()
163{
164 QString hint;
165 switch ( static_cast<QgsLabelLineSettings::AnchorTextPoint>( mAnchorTextPointComboBox->currentData().toInt() ) )
166 {
168 hint = tr( "Labels are placed so that the start of their text is placed at the anchor point." );
169 break;
171 hint = tr( "Labels are placed so that the center of their text is placed at the anchor point." );
172 break;
174 hint = tr( "Labels are placed so that the end of their text is placed at the anchor point." );
175 break;
177 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." );
178 break;
179 }
180 mAnchorTextPointHintLabel->setText( hint );
181}
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.