QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgslabelengineconfigdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslabelengineconfigdialog.cpp
3  ---------------------
4  begin : May 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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  ***************************************************************************/
16 
18 #include "qgsproject.h"
19 #include "pal/pal.h"
20 #include "qgshelp.h"
21 #include "qgsmessagebar.h"
22 #include "qgsmapcanvas.h"
23 #include "qgsgui.h"
24 #include "qgsapplication.h"
25 #include "qgsrendercontext.h"
26 #include <QAction>
27 #include <QDialogButtonBox>
28 #include <QPushButton>
29 #include <QMessageBox>
30 #include <QMenu>
31 
33  : QgsPanelWidget( parent ), mCanvas( canvas )
34 {
35  setupUi( this );
36 
37  setPanelTitle( tr( "Placement Engine Settings" ) );
38 
39  mMessageBar = new QgsMessageBar();
40  mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
41  verticalLayout->insertWidget( 0, mMessageBar );
42 
44 
45  mTextRenderFormatComboBox->addItem( tr( "Always Render Labels as Paths (Recommended)" ), static_cast< int >( Qgis::TextRenderFormat::AlwaysOutlines ) );
46  mTextRenderFormatComboBox->addItem( tr( "Always Render Labels as Text" ), static_cast< int >( Qgis::TextRenderFormat::AlwaysText ) );
47 
48  mPlacementVersionComboBox->addItem( tr( "Version 1" ), QgsLabelingEngineSettings::PlacementEngineVersion1 );
49  mPlacementVersionComboBox->addItem( tr( "Version 2 (Recommended)" ), QgsLabelingEngineSettings::PlacementEngineVersion2 );
50 
51  mPreviousEngineVersion = engineSettings.placementVersion();
52  mPlacementVersionComboBox->setCurrentIndex( mPlacementVersionComboBox->findData( mPreviousEngineVersion ) );
53  connect( mPlacementVersionComboBox, &QComboBox::currentTextChanged, this, [ = ]()
54  {
55  if ( static_cast< QgsLabelingEngineSettings::PlacementEngineVersion >( mPlacementVersionComboBox->currentData().toInt() ) != mPreviousEngineVersion )
56  {
57  mMessageBar->pushMessage( QString(), tr( "Version changes will alter label placement in the project." ), Qgis::MessageLevel::Warning );
58  }
59  } );
60 
61  spinCandLine->setClearValue( 5 );
62  spinCandPolygon->setClearValue( 2.5 );
63 
64  // candidate numbers
65  spinCandLine->setValue( engineSettings.maximumLineCandidatesPerCm() );
66  spinCandPolygon->setValue( engineSettings.maximumPolygonCandidatesPerCmSquared() );
67 
68  chkShowCandidates->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::DrawCandidates ) );
69  chkShowAllLabels->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::UseAllLabels ) );
70  chkShowUnplaced->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::DrawUnplacedLabels ) );
71  chkShowPartialsLabels->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::UsePartialCandidates ) );
72 
73  mUnplacedColorButton->setColor( engineSettings.unplacedLabelColor() );
74  mUnplacedColorButton->setAllowOpacity( false );
75  mUnplacedColorButton->setDefaultColor( QColor( 255, 0, 0 ) );
76  mUnplacedColorButton->setWindowTitle( tr( "Unplaced Label Color" ) );
77 
78  mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( static_cast< int >( engineSettings.defaultTextRenderFormat() ) ) );
79 
80  connect( spinCandLine, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
81  connect( spinCandPolygon, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
82  connect( chkShowCandidates, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
83  connect( chkShowAllLabels, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
84  connect( chkShowUnplaced, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
85  connect( chkShowPartialsLabels, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
86  connect( mTextRenderFormatComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
87  connect( mUnplacedColorButton, &QgsColorButton::colorChanged, this, &QgsLabelEngineConfigWidget::widgetChanged );
88  connect( mPlacementVersionComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
89 
90  mWidgetMenu = new QMenu( this );
91  QAction *resetAction = new QAction( tr( "Restore Defaults" ), this );
92  mWidgetMenu->addAction( resetAction );
93  connect( resetAction, &QAction::triggered, this, &QgsLabelEngineConfigWidget::setDefaults );
94  QAction *helpAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionHelpContents.svg" ) ), tr( "Help…" ), this );
95  mWidgetMenu->addAction( helpAction );
96  connect( helpAction, &QAction::triggered, this, &QgsLabelEngineConfigWidget::showHelp );
97 }
98 
100 {
101  return mWidgetMenu;
102 }
103 
105 {
106  return tr( "Additional Options" );
107 }
108 
110 {
111  QgsLabelingEngineSettings engineSettings;
112 
113  // save
114  engineSettings.setMaximumLineCandidatesPerCm( spinCandLine->value() );
115  engineSettings.setMaximumPolygonCandidatesPerCmSquared( spinCandPolygon->value() );
116 
117  engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, chkShowCandidates->isChecked() );
118  engineSettings.setFlag( QgsLabelingEngineSettings::UseAllLabels, chkShowAllLabels->isChecked() );
119  engineSettings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, chkShowUnplaced->isChecked() );
120  engineSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, chkShowPartialsLabels->isChecked() );
121 
122  engineSettings.setDefaultTextRenderFormat( static_cast< Qgis::TextRenderFormat >( mTextRenderFormatComboBox->currentData().toInt() ) );
123 
124  engineSettings.setUnplacedLabelColor( mUnplacedColorButton->color() );
125 
126  engineSettings.setPlacementVersion( static_cast< QgsLabelingEngineSettings::PlacementEngineVersion >( mPlacementVersionComboBox->currentData().toInt() ) );
127 
128  QgsProject::instance()->setLabelingEngineSettings( engineSettings );
129  mCanvas->refreshAllLayers();
130 }
131 
133 {
134  const pal::Pal p;
135  spinCandLine->setValue( 5 );
136  spinCandPolygon->setValue( 10 );
137  chkShowCandidates->setChecked( false );
138  chkShowAllLabels->setChecked( false );
139  chkShowPartialsLabels->setChecked( p.showPartialLabels() );
140  mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( static_cast< int >( Qgis::TextRenderFormat::AlwaysOutlines ) ) );
141  mPlacementVersionComboBox->setCurrentIndex( mPlacementVersionComboBox->findData( QgsLabelingEngineSettings::PlacementEngineVersion2 ) );
142 }
143 
145 {
146  QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#setting-the-automated-placement-engine" ) );
147 }
148 
149 //
150 // QgsLabelEngineConfigDialog
151 //
152 
154  : QDialog( parent )
155 {
156  mWidget = new QgsLabelEngineConfigWidget( canvas );
157  setWindowTitle( mWidget->windowTitle() );
158  QVBoxLayout *vLayout = new QVBoxLayout();
159  vLayout->addWidget( mWidget );
160  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::RestoreDefaults, Qt::Horizontal );
161  connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
162  connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
163  connect( bbox, &QDialogButtonBox::helpRequested, mWidget, &QgsLabelEngineConfigWidget::showHelp );
164  connect( bbox->button( QDialogButtonBox::RestoreDefaults ), &QAbstractButton::clicked,
166  vLayout->addWidget( bbox );
167  setLayout( vLayout );
168 
169  setObjectName( QStringLiteral( "QgsLabelSettingsWidgetDialog" ) );
171 }
172 
174 {
175  mWidget->apply();
176  QDialog::accept();
177 }
TextRenderFormat
Options for rendering text.
Definition: qgis.h:1003
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:174
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsLabelEngineConfigDialog(QgsMapCanvas *canvas, QWidget *parent=nullptr)
constructor
Widget for configuring the labeling engine.
QMenu * menuButtonMenu() override
Returns the menu to use for the menu button for this panel, or nullptr if no menu button is required.
QString menuButtonTooltip() const override
Returns the (translated) tooltip text to use for the menu button for this panel.
QgsLabelEngineConfigWidget(QgsMapCanvas *canvas, QWidget *parent=nullptr)
constructor
void setDefaults()
Resets the settings to the defaults.
Stores global configuration for labeling engine.
Qgis::TextRenderFormat defaultTextRenderFormat() const
Returns the default text rendering format for the labels.
void setDefaultTextRenderFormat(Qgis::TextRenderFormat format)
Sets the default text rendering format for the labels.
bool testFlag(Flag f) const
Test whether a particular flag is enabled.
@ UseAllLabels
Whether to draw all labels even if there would be collisions.
@ UsePartialCandidates
Whether to use also label candidates that are partially outside of the map view.
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
@ DrawCandidates
Whether to draw rectangles of generated candidates (good for debugging)
void setFlag(Flag f, bool enabled=true)
Sets whether a particual flag is enabled.
void setMaximumPolygonCandidatesPerCmSquared(double candidates)
Sets the maximum number of polygon label candidates per centimeter squared.
QColor unplacedLabelColor() const
Returns the color to use when rendering unplaced labels.
PlacementEngineVersion
Placement engine version.
@ PlacementEngineVersion1
Version 1, matches placement from QGIS <= 3.10.1.
@ PlacementEngineVersion2
Version 2 (default for new projects since QGIS 3.12)
void setUnplacedLabelColor(const QColor &color)
Sets the color to use when rendering unplaced labels.
PlacementEngineVersion placementVersion() const
Returns the placement engine version, which dictates how the label placement problem is solved.
void setPlacementVersion(PlacementEngineVersion version)
Sets the placement engine version, which dictates how the label placement problem is solved.
double maximumPolygonCandidatesPerCmSquared() const
Returns the maximum number of polygon label candidate positions per centimeter squared.
void setMaximumLineCandidatesPerCm(double candidates)
Sets the maximum number of line label candidates per centimeter.
double maximumLineCandidatesPerCm() const
Returns the maximum number of line label candidate positions per centimeter.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
void refreshAllLayers()
Reload all layers (including refreshing layer properties from their data sources),...
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
Base class for any widget that can be shown as a inline panel.
void widgetChanged()
Emitted when the widget state changes.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:470
void setLabelingEngineSettings(const QgsLabelingEngineSettings &settings)
Sets project's global labeling engine settings.
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns project's global labeling engine settings.
Main Pal labeling class.
Definition: pal.h:80
bool showPartialLabels() const
Returns whether partial labels should be allowed.
Definition: pal.cpp:650