QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  mAnchorTypeComboBox->addItem( tr( "Preferred Placement Hint" ), static_cast< int >( QgsLabelLineSettings::AnchorType::HintOnly ) );
34  mAnchorTypeComboBox->addItem( tr( "Strict" ), static_cast< int >( QgsLabelLineSettings::AnchorType::Strict ) );
35 
36  connect( mPercentPlacementComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int )
37  {
38  if ( !mBlockSignals )
39  emit changed();
40 
41  if ( mPercentPlacementComboBox->currentData().toDouble() < 0 )
42  mCustomPlacementSpinBox->setEnabled( true );
43  else
44  {
45  mCustomPlacementSpinBox->setEnabled( false );
46  mBlockSignals = true;
47  mCustomPlacementSpinBox->setValue( mPercentPlacementComboBox->currentData().toDouble() * 100 );
48  mBlockSignals = false;
49  }
50  } );
51  connect( mCustomPlacementSpinBox, qgis::overload<double>::of( &QDoubleSpinBox::valueChanged ), this, [ = ]( double )
52  {
53  if ( !mBlockSignals )
54  emit changed();
55  } );
56 
57  connect( mAnchorTypeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int )
58  {
59  if ( !mBlockSignals )
60  emit changed();
61 
62  updateAnchorTypeHint();
63  } );
64 
66  updateAnchorTypeHint();
67 }
68 
70 {
71  mBlockSignals = true;
72  const int comboIndex = mPercentPlacementComboBox->findData( settings.lineAnchorPercent() );
73  if ( comboIndex >= 0 )
74  {
75  mPercentPlacementComboBox->setCurrentIndex( comboIndex );
76  }
77  else
78  {
79  // set custom control
80  mPercentPlacementComboBox->setCurrentIndex( mPercentPlacementComboBox->findData( -1.0 ) );
81  mCustomPlacementSpinBox->setValue( settings.lineAnchorPercent() * 100.0 );
82  }
83  mCustomPlacementSpinBox->setEnabled( mPercentPlacementComboBox->currentData().toDouble() < 0 );
84 
85  mAnchorTypeComboBox->setCurrentIndex( mAnchorTypeComboBox->findData( static_cast< int >( settings.anchorType() ) ) );
86  mBlockSignals = false;
87 }
88 
90 {
92 
93  if ( mPercentPlacementComboBox->currentData().toDouble() >= 0 )
94  {
95  settings.setLineAnchorPercent( mPercentPlacementComboBox->currentData().toDouble() );
96  }
97  else
98  {
99  settings.setLineAnchorPercent( mCustomPlacementSpinBox->value() / 100.0 );
100  }
101 
102  settings.setAnchorType( static_cast< QgsLabelLineSettings::AnchorType >( mAnchorTypeComboBox->currentData().toInt() ) );
103  return settings;
104 }
105 
107 {
109 }
110 
111 void QgsLabelLineAnchorWidget::updateAnchorTypeHint()
112 {
113  QString hint;
114  switch ( static_cast< QgsLabelLineSettings::AnchorType >( mAnchorTypeComboBox->currentData().toInt() ) )
115  {
117  hint = tr( "Labels are placed exactly on the label anchor only, and no other fallback placements are permitted." );
118  break;
119 
121  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." );
122  break;
123  }
124  mAnchorTypeHintLabel->setText( hint );
125 }
qgslabellineanchorwidget.h
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:106
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsLabelLineAnchorWidget::setSettings
void setSettings(const QgsLabelLineSettings &settings)
Sets the line settings to show in the widget.
Definition: qgslabellineanchorwidget.cpp:69
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:255
QgsLabelLineSettings::AnchorType::Strict
@ Strict
Line anchor is a strict placement, and other placements are not permitted.
QgsLabelLineSettings::anchorType
AnchorType anchorType() const
Returns the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
Definition: qgslabellinesettings.h:277
QgsPalLayerSettings::LineAnchorPercent
@ LineAnchorPercent
Portion along line at which labels should be anchored (since QGIS 3.16)
Definition: qgspallabeling.h:456
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...
QgsLabelLineSettings::AnchorType
AnchorType
Line anchor types.
Definition: qgslabellinesettings.h:57
QgsLabelLineSettings::setLineAnchorPercent
void setLineAnchorPercent(double percent)
Sets the percent along the line at which labels should be placed.
Definition: qgslabellinesettings.h:268
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:319
QgsLabelSettingsWidgetBase
Base class for widgets which allow customization of label engine properties, such as label placement ...
Definition: qgslabelsettingswidgetbase.h:39
QgsLabelLineSettings
Contains settings related to how the label engine places and formats labels for line features (or pol...
Definition: qgslabellinesettings.h:40
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsLabelLineSettings::setAnchorType
void setAnchorType(AnchorType type)
Sets the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
Definition: qgslabellinesettings.h:286
QgsPropertyCollection::setProperty
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
Definition: qgspropertycollection.cpp:187
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:89