QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  connect( mPercentPlacementComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
40  {
41  if ( !mBlockSignals )
42  emit changed();
43 
44  if ( mPercentPlacementComboBox->currentData().toDouble() < 0 )
45  mCustomPlacementSpinBox->setEnabled( true );
46  else
47  {
48  mCustomPlacementSpinBox->setEnabled( false );
49  mBlockSignals = true;
50  mCustomPlacementSpinBox->setValue( mPercentPlacementComboBox->currentData().toDouble() * 100 );
51  mBlockSignals = false;
52  }
53  } );
54  connect( mCustomPlacementSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double )
55  {
56  if ( !mBlockSignals )
57  emit changed();
58  } );
59 
60  connect( mAnchorTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
61  {
62  if ( !mBlockSignals )
63  emit changed();
64 
65  updateAnchorTypeHint();
66  } );
67 
68  connect( mClippingComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
69  {
70  if ( !mBlockSignals )
71  emit changed();
72  } );
73 
76  updateAnchorTypeHint();
77 }
78 
80 {
81  mBlockSignals = true;
82  const int comboIndex = mPercentPlacementComboBox->findData( settings.lineAnchorPercent() );
83  if ( comboIndex >= 0 )
84  {
85  mPercentPlacementComboBox->setCurrentIndex( comboIndex );
86  }
87  else
88  {
89  // set custom control
90  mPercentPlacementComboBox->setCurrentIndex( mPercentPlacementComboBox->findData( -1.0 ) );
91  mCustomPlacementSpinBox->setValue( settings.lineAnchorPercent() * 100.0 );
92  }
93  mCustomPlacementSpinBox->setEnabled( mPercentPlacementComboBox->currentData().toDouble() < 0 );
94 
95  mAnchorTypeComboBox->setCurrentIndex( mAnchorTypeComboBox->findData( static_cast< int >( settings.anchorType() ) ) );
96  mClippingComboBox->setCurrentIndex( mClippingComboBox->findData( static_cast< int >( settings.anchorClipping() ) ) );
97  mBlockSignals = false;
98 }
99 
101 {
103 
104  if ( mPercentPlacementComboBox->currentData().toDouble() >= 0 )
105  {
106  settings.setLineAnchorPercent( mPercentPlacementComboBox->currentData().toDouble() );
107  }
108  else
109  {
110  settings.setLineAnchorPercent( mCustomPlacementSpinBox->value() / 100.0 );
111  }
112 
113  settings.setAnchorType( static_cast< QgsLabelLineSettings::AnchorType >( mAnchorTypeComboBox->currentData().toInt() ) );
114  settings.setAnchorClipping( static_cast< QgsLabelLineSettings::AnchorClipping >( mClippingComboBox->currentData().toInt() ) );
115  return settings;
116 }
117 
119 {
122 }
123 
124 void QgsLabelLineAnchorWidget::updateAnchorTypeHint()
125 {
126  QString hint;
127  switch ( static_cast< QgsLabelLineSettings::AnchorType >( mAnchorTypeComboBox->currentData().toInt() ) )
128  {
130  hint = tr( "Labels are placed exactly on the label anchor only, and no other fallback placements are permitted." );
131  break;
132 
134  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." );
135  break;
136  }
137  mAnchorTypeHintLabel->setText( hint );
138 }
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...
void setLineAnchorPercent(double percent)
Sets the percent along the line at which labels should be placed.
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.
AnchorType anchorType() const
Returns the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
void setAnchorClipping(AnchorClipping clipping)
Sets the line anchor clipping mode, which dictates how line strings are clipped before calculating th...
double lineAnchorPercent() const
Returns the percent along the line at which labels should be placed.
void setAnchorType(AnchorType type)
Sets the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
AnchorClipping anchorClipping() const
Returns the line anchor clipping mode, which dictates how line strings are clipped before calculating...
Base class for widgets which allow customization of label engine properties, such as label placement ...
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.
@ LineAnchorPercent
Portion along line at which labels should be anchored (since QGIS 3.16)
@ LineAnchorClipping
Clipping mode for line anchor calculation (since QGIS 3.20)
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 a integer key value.
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
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 data sets.