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