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