QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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#include "moc_qgslabelengineconfigdialog.cpp"
17
19#include "qgsproject.h"
20#include "pal/pal.h"
21#include "qgshelp.h"
22#include "qgsmessagebar.h"
23#include "qgsmapcanvas.h"
24#include "qgsgui.h"
25#include "qgsapplication.h"
26#include "qgsrendercontext.h"
27#include <QAction>
28#include <QDialogButtonBox>
29#include <QPushButton>
30#include <QMessageBox>
31#include <QMenu>
32
34 : QgsPanelWidget( parent ), mCanvas( canvas )
35{
36 setupUi( this );
37
38 setPanelTitle( tr( "Placement Engine Settings" ) );
39
40 mMessageBar = new QgsMessageBar();
41 mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
42 verticalLayout->insertWidget( 0, mMessageBar );
43
45
46 mTextRenderFormatComboBox->addItem( tr( "Always Render Labels as Paths (Recommended)" ), QVariant::fromValue( Qgis::TextRenderFormat::AlwaysOutlines ) );
47 mTextRenderFormatComboBox->addItem( tr( "Always Render Labels as Text" ), QVariant::fromValue( Qgis::TextRenderFormat::AlwaysText ) );
48 mTextRenderFormatComboBox->addItem( tr( "Prefer Rendering Labels as Text" ), QVariant::fromValue( Qgis::TextRenderFormat::PreferText ) );
49
50 mPlacementVersionComboBox->addItem( tr( "Version 1" ), static_cast<int>( Qgis::LabelPlacementEngineVersion::Version1 ) );
51 mPlacementVersionComboBox->addItem( tr( "Version 2 (Recommended)" ), static_cast<int>( Qgis::LabelPlacementEngineVersion::Version2 ) );
52
53 mPreviousEngineVersion = engineSettings.placementVersion();
54 mPlacementVersionComboBox->setCurrentIndex( mPlacementVersionComboBox->findData( static_cast<int>( mPreviousEngineVersion ) ) );
55 connect( mPlacementVersionComboBox, &QComboBox::currentTextChanged, this, [=]() {
56 if ( static_cast<Qgis::LabelPlacementEngineVersion>( mPlacementVersionComboBox->currentData().toInt() ) != mPreviousEngineVersion )
57 {
58 mMessageBar->pushMessage( QString(), tr( "Version changes will alter label placement in the project." ), Qgis::MessageLevel::Warning );
59 }
60 } );
61
62 spinCandLine->setClearValue( 5 );
63 spinCandPolygon->setClearValue( 2.5 );
64
65 // candidate numbers
66 spinCandLine->setValue( engineSettings.maximumLineCandidatesPerCm() );
67 spinCandPolygon->setValue( engineSettings.maximumPolygonCandidatesPerCmSquared() );
68
69 chkShowCandidates->setChecked( engineSettings.testFlag( Qgis::LabelingFlag::DrawCandidates ) );
70 chkShowMetrics->setChecked( engineSettings.testFlag( Qgis::LabelingFlag::DrawLabelMetrics ) );
71 chkShowAllLabels->setChecked( engineSettings.testFlag( Qgis::LabelingFlag::UseAllLabels ) );
72 chkShowUnplaced->setChecked( engineSettings.testFlag( Qgis::LabelingFlag::DrawUnplacedLabels ) );
73 chkShowPartialsLabels->setChecked( engineSettings.testFlag( Qgis::LabelingFlag::UsePartialCandidates ) );
74
75 mUnplacedColorButton->setColor( engineSettings.unplacedLabelColor() );
76 mUnplacedColorButton->setAllowOpacity( false );
77 mUnplacedColorButton->setDefaultColor( QColor( 255, 0, 0 ) );
78 mUnplacedColorButton->setWindowTitle( tr( "Unplaced Label Color" ) );
79
80 mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( QVariant::fromValue( engineSettings.defaultTextRenderFormat() ) ) );
81
82 connect( spinCandLine, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
83 connect( spinCandPolygon, qOverload<double>( &QgsDoubleSpinBox::valueChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
84 connect( chkShowCandidates, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
85 connect( chkShowAllLabels, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
86 connect( chkShowUnplaced, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
87 connect( chkShowPartialsLabels, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
88 connect( chkShowMetrics, &QCheckBox::toggled, this, &QgsLabelEngineConfigWidget::widgetChanged );
89 connect( mTextRenderFormatComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
90 connect( mUnplacedColorButton, &QgsColorButton::colorChanged, this, &QgsLabelEngineConfigWidget::widgetChanged );
91 connect( mPlacementVersionComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsLabelEngineConfigWidget::widgetChanged );
92
93 mWidgetMenu = new QMenu( this );
94 QAction *resetAction = new QAction( tr( "Restore Defaults" ), this );
95 mWidgetMenu->addAction( resetAction );
96 connect( resetAction, &QAction::triggered, this, &QgsLabelEngineConfigWidget::setDefaults );
97 QAction *helpAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionHelpContents.svg" ) ), tr( "Help…" ), this );
98 mWidgetMenu->addAction( helpAction );
99 connect( helpAction, &QAction::triggered, this, &QgsLabelEngineConfigWidget::showHelp );
100}
101
103{
104 return mWidgetMenu;
105}
106
108{
109 return tr( "Additional Options" );
110}
111
113{
115
116 // save
117 engineSettings.setMaximumLineCandidatesPerCm( spinCandLine->value() );
118 engineSettings.setMaximumPolygonCandidatesPerCmSquared( spinCandPolygon->value() );
119
120 engineSettings.setFlag( Qgis::LabelingFlag::DrawCandidates, chkShowCandidates->isChecked() );
121 engineSettings.setFlag( Qgis::LabelingFlag::UseAllLabels, chkShowAllLabels->isChecked() );
122 engineSettings.setFlag( Qgis::LabelingFlag::DrawUnplacedLabels, chkShowUnplaced->isChecked() );
123 engineSettings.setFlag( Qgis::LabelingFlag::UsePartialCandidates, chkShowPartialsLabels->isChecked() );
124 engineSettings.setFlag( Qgis::LabelingFlag::DrawLabelMetrics, chkShowMetrics->isChecked() );
125
126 engineSettings.setDefaultTextRenderFormat( mTextRenderFormatComboBox->currentData().value<Qgis::TextRenderFormat>() );
127
128 engineSettings.setUnplacedLabelColor( mUnplacedColorButton->color() );
129
130 engineSettings.setPlacementVersion( static_cast<Qgis::LabelPlacementEngineVersion>( mPlacementVersionComboBox->currentData().toInt() ) );
131
133 mCanvas->refreshAllLayers();
134}
135
137{
138 const pal::Pal p;
139 spinCandLine->setValue( 5 );
140 spinCandPolygon->setValue( 10 );
141 chkShowCandidates->setChecked( false );
142 chkShowMetrics->setChecked( false );
143 chkShowAllLabels->setChecked( false );
144 chkShowPartialsLabels->setChecked( p.showPartialLabels() );
145 mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( QVariant::fromValue( Qgis::TextRenderFormat::AlwaysOutlines ) ) );
146 mPlacementVersionComboBox->setCurrentIndex( mPlacementVersionComboBox->findData( static_cast<int>( Qgis::LabelPlacementEngineVersion::Version2 ) ) );
147}
148
150{
151 QgsHelp::openHelp( QStringLiteral( "working_with_vector/vector_properties.html#setting-the-automated-placement-engine" ) );
152}
153
154//
155// QgsLabelEngineConfigDialog
156//
157
159 : QDialog( parent )
160{
161 mWidget = new QgsLabelEngineConfigWidget( canvas );
162 setWindowTitle( mWidget->windowTitle() );
163 QVBoxLayout *vLayout = new QVBoxLayout();
164 vLayout->addWidget( mWidget );
165 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::RestoreDefaults, Qt::Horizontal );
166 connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
167 connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
168 connect( bbox, &QDialogButtonBox::helpRequested, mWidget, &QgsLabelEngineConfigWidget::showHelp );
169 connect( bbox->button( QDialogButtonBox::RestoreDefaults ), &QAbstractButton::clicked, mWidget, &QgsLabelEngineConfigWidget::setDefaults );
170 vLayout->addWidget( bbox );
171 setLayout( vLayout );
172
173 setObjectName( QStringLiteral( "QgsLabelSettingsWidgetDialog" ) );
175}
176
178{
179 mWidget->apply();
180 QDialog::accept();
181}
@ DrawCandidates
Whether to draw rectangles of generated candidates (good for debugging)
@ DrawLabelMetrics
Whether to render label metric guides (for debugging)
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
@ 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.
TextRenderFormat
Options for rendering text.
Definition qgis.h:2687
@ PreferText
Render text as text objects, unless doing so results in rendering artifacts or poor quality rendering...
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
@ AlwaysText
Always render text as text objects. While this mode preserves text objects as text for post-processin...
LabelPlacementEngineVersion
Labeling placement engine version.
Definition qgis.h:2733
@ Version2
Version 2 (default for new projects since QGIS 3.12)
@ Version1
Version 1, matches placement from QGIS <= 3.10.1.
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:210
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
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.
void setFlag(Qgis::LabelingFlag f, bool enabled=true)
Sets whether a particual flag is enabled.
void setPlacementVersion(Qgis::LabelPlacementEngineVersion version)
Sets the placement engine version, which dictates how the label placement problem is solved.
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.
Qgis::LabelPlacementEngineVersion placementVersion() const
Returns the placement engine version, which dictates how the label placement problem is solved.
bool testFlag(Qgis::LabelingFlag f) const
Test whether a particular flag is enabled.
void setUnplacedLabelColor(const QColor &color)
Sets the color to use when rendering unplaced labels.
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.
void refreshAllLayers()
Reload all layers (including refreshing layer properties from their data sources),...
A bar for displaying non-blocking messages to the user.
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.
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:83
bool showPartialLabels() const
Returns whether partial labels should be allowed.
Definition pal.cpp:797