QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgslayoutpropertieswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutpropertieswidget.cpp
3 -----------------------------
4 begin : July 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18#include "qgslayout.h"
19#include "qgslayoutsnapper.h"
21#include "qgslayoutundostack.h"
22#include "qgslayoutitemmap.h"
24#include "qgsprintlayout.h"
25#include "qgslayoutatlas.h"
27#include "qgsmargins.h"
28
30 : QgsPanelWidget( parent )
31 , mLayout( layout )
32{
33 Q_ASSERT( mLayout );
34
35 setupUi( this );
36 setPanelTitle( tr( "Layout Properties" ) );
37 blockSignals( true );
38
39 mVariableEditor->setMinimumHeight( mVariableEditor->fontMetrics().height() * 15 );
40
41 updateSnappingElements();
42
43 mGridSpacingUnitsCombo->linkToWidget( mGridResolutionSpinBox );
44 mGridOffsetUnitsComboBox->linkToWidget( mOffsetXSpinBox );
45 mGridOffsetUnitsComboBox->linkToWidget( mOffsetYSpinBox );
46
47 blockSignals( false );
48
49 connect( mSnapToleranceSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::snapToleranceChanged );
50
51 connect( mGridOffsetUnitsComboBox, &QgsLayoutUnitsComboBox::unitChanged, this, &QgsLayoutPropertiesWidget::gridOffsetUnitsChanged );
52 connect( mGridSpacingUnitsCombo, &QgsLayoutUnitsComboBox::unitChanged, this, &QgsLayoutPropertiesWidget::gridResolutionUnitsChanged );
53 connect( mGridResolutionSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::gridResolutionChanged );
54 connect( mOffsetXSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::gridOffsetXChanged );
55 connect( mOffsetYSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::gridOffsetYChanged );
56
57 const double leftMargin = mLayout->customProperty( QStringLiteral( "resizeToContentsLeftMargin" ) ).toDouble();
58 const double topMargin = mLayout->customProperty( QStringLiteral( "resizeToContentsTopMargin" ) ).toDouble();
59 const double bottomMargin = mLayout->customProperty( QStringLiteral( "resizeToContentsBottomMargin" ) ).toDouble();
60 const double rightMargin = mLayout->customProperty( QStringLiteral( "resizeToContentsRightMargin" ) ).toDouble();
61 const Qgis::LayoutUnit marginUnit = static_cast< Qgis::LayoutUnit >(
62 mLayout->customProperty( QStringLiteral( "imageCropMarginUnit" ), static_cast< int >( Qgis::LayoutUnit::Millimeters ) ).toInt() );
63
64 const bool exportWorldFile = mLayout->customProperty( QStringLiteral( "exportWorldFile" ), false ).toBool();
65 mGenerateWorldFileCheckBox->setChecked( exportWorldFile );
66 connect( mGenerateWorldFileCheckBox, &QCheckBox::toggled, this, &QgsLayoutPropertiesWidget::worldFileToggled );
67
68 connect( mRasterizeCheckBox, &QCheckBox::toggled, this, &QgsLayoutPropertiesWidget::rasterizeToggled );
69 connect( mForceVectorCheckBox, &QCheckBox::toggled, this, &QgsLayoutPropertiesWidget::forceVectorToggled );
70
71 mTopMarginSpinBox->setValue( topMargin );
72 mMarginUnitsComboBox->linkToWidget( mTopMarginSpinBox );
73 mRightMarginSpinBox->setValue( rightMargin );
74 mMarginUnitsComboBox->linkToWidget( mRightMarginSpinBox );
75 mBottomMarginSpinBox->setValue( bottomMargin );
76 mMarginUnitsComboBox->linkToWidget( mBottomMarginSpinBox );
77 mLeftMarginSpinBox->setValue( leftMargin );
78 mMarginUnitsComboBox->linkToWidget( mLeftMarginSpinBox );
79 mMarginUnitsComboBox->setUnit( marginUnit );
80 mMarginUnitsComboBox->setConverter( &mLayout->renderContext().measurementConverter() );
81
82 connect( mTopMarginSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::resizeMarginsChanged );
83 connect( mRightMarginSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::resizeMarginsChanged );
84 connect( mBottomMarginSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::resizeMarginsChanged );
85 connect( mLeftMarginSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::resizeMarginsChanged );
86 connect( mResizePageButton, &QPushButton::clicked, this, &QgsLayoutPropertiesWidget::resizeToContents );
87
88 connect( mResolutionSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLayoutPropertiesWidget::dpiChanged );
89
90 mReferenceMapComboBox->setCurrentLayout( mLayout );
91 mReferenceMapComboBox->setItemType( QgsLayoutItemRegistry::LayoutMap );
92 connect( mReferenceMapComboBox, &QgsLayoutItemComboBox::itemChanged, this, &QgsLayoutPropertiesWidget::referenceMapChanged );
93
95
96 updateVariables();
97 connect( mVariableEditor, &QgsVariableEditorWidget::scopeChanged, this, &QgsLayoutPropertiesWidget::variablesChanged );
98 // listen out for variable edits
99 connect( QgsApplication::instance(), &QgsApplication::customVariablesChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
100 connect( QgsProject::instance(), &QgsProject::customVariablesChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
101 connect( QgsProject::instance(), &QgsProject::metadataChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
102 connect( &mLayout->renderContext(), &QgsLayoutRenderContext::dpiChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
103 connect( mLayout->pageCollection(), &QgsLayoutPageCollection::changed, this, &QgsLayoutPropertiesWidget::updateVariables );
104 connect( mLayout, &QgsLayout::variablesChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
105
106 updateGui();
107}
108
110{
111 if ( QgsPrintLayout *printLayout = dynamic_cast< QgsPrintLayout * >( masterLayout ) )
112 {
113 connect( printLayout, &QgsPrintLayout::nameChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
114 connect( printLayout->atlas(), &QgsLayoutAtlas::coverageLayerChanged, this, &QgsLayoutPropertiesWidget::updateVariables );
115 }
116}
117
119{
120 whileBlocking( mReferenceMapComboBox )->setItem( mLayout->referenceMap() );
121 whileBlocking( mResolutionSpinBox )->setValue( mLayout->renderContext().dpi() );
122
123 const bool rasterize = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
124 whileBlocking( mRasterizeCheckBox )->setChecked( rasterize );
125
126 const bool forceVectors = mLayout->customProperty( QStringLiteral( "forceVector" ), false ).toBool();
127 whileBlocking( mForceVectorCheckBox )->setChecked( forceVectors );
128
129 if ( rasterize )
130 {
131 mForceVectorCheckBox->setChecked( false );
132 mForceVectorCheckBox->setEnabled( false );
133 }
134 else
135 {
136 mForceVectorCheckBox->setEnabled( true );
137 }
138}
139
140void QgsLayoutPropertiesWidget::updateSnappingElements()
141{
142 mSnapToleranceSpinBox->setValue( mLayout->snapper().snapTolerance() );
143
144 mGridSpacingUnitsCombo->setUnit( mLayout->gridSettings().resolution().units() );
145 mGridResolutionSpinBox->setValue( mLayout->gridSettings().resolution().length() );
146
147 mGridOffsetUnitsComboBox->setUnit( mLayout->gridSettings().offset().units() );
148 mOffsetXSpinBox->setValue( mLayout->gridSettings().offset().x() );
149 mOffsetYSpinBox->setValue( mLayout->gridSettings().offset().y() );
150}
151
152void QgsLayoutPropertiesWidget::gridResolutionChanged( double d )
153{
155 m.setLength( d );
156 mLayout->gridSettings().setResolution( m );
157 mLayout->pageCollection()->redraw();
158}
159
160void QgsLayoutPropertiesWidget::gridResolutionUnitsChanged( Qgis::LayoutUnit unit )
161{
163 m.setUnits( unit );
164 mLayout->gridSettings().setResolution( m );
165 mLayout->pageCollection()->redraw();
166}
167
168void QgsLayoutPropertiesWidget::gridOffsetXChanged( double d )
169{
170 QgsLayoutPoint o = mLayout->gridSettings().offset();
171 o.setX( d );
172 mLayout->gridSettings().setOffset( o );
173 mLayout->pageCollection()->redraw();
174}
175
176void QgsLayoutPropertiesWidget::gridOffsetYChanged( double d )
177{
178 QgsLayoutPoint o = mLayout->gridSettings().offset();
179 o.setY( d );
180 mLayout->gridSettings().setOffset( o );
181 mLayout->pageCollection()->redraw();
182}
183
184void QgsLayoutPropertiesWidget::gridOffsetUnitsChanged( Qgis::LayoutUnit unit )
185{
186 QgsLayoutPoint o = mLayout->gridSettings().offset();
187 o.setUnits( unit );
188 mLayout->gridSettings().setOffset( o );
189 mLayout->pageCollection()->redraw();
190}
191
192void QgsLayoutPropertiesWidget::snapToleranceChanged( int tolerance )
193{
194 mLayout->snapper().setSnapTolerance( tolerance );
195}
196
197void QgsLayoutPropertiesWidget::resizeMarginsChanged()
198{
199 mLayout->setCustomProperty( QStringLiteral( "resizeToContentsLeftMargin" ), mLeftMarginSpinBox->value() );
200 mLayout->setCustomProperty( QStringLiteral( "resizeToContentsTopMargin" ), mTopMarginSpinBox->value() );
201 mLayout->setCustomProperty( QStringLiteral( "resizeToContentsBottomMargin" ), mBottomMarginSpinBox->value() );
202 mLayout->setCustomProperty( QStringLiteral( "resizeToContentsRightMargin" ), mRightMarginSpinBox->value() );
203 mLayout->setCustomProperty( QStringLiteral( "imageCropMarginUnit" ), static_cast< int >( mMarginUnitsComboBox->unit() ) );
204}
205
206void QgsLayoutPropertiesWidget::resizeToContents()
207{
208 mLayout->undoStack()->beginMacro( tr( "Resize to Contents" ) );
209
210 mLayout->pageCollection()->resizeToContents( QgsMargins( mLeftMarginSpinBox->value(),
211 mTopMarginSpinBox->value(),
212 mRightMarginSpinBox->value(),
213 mBottomMarginSpinBox->value() ),
214 mMarginUnitsComboBox->unit() );
215
216 mLayout->undoStack()->endMacro();
217}
218
219void QgsLayoutPropertiesWidget::referenceMapChanged( QgsLayoutItem *item )
220{
221 mLayout->undoStack()->beginCommand( mLayout, tr( "Set Reference Map" ) );
222 QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap * >( item );
223 mLayout->setReferenceMap( map );
224 mLayout->undoStack()->endCommand();
225}
226
227void QgsLayoutPropertiesWidget::dpiChanged( int value )
228{
229 mLayout->undoStack()->beginCommand( mLayout, tr( "Set Default DPI" ), QgsLayout::UndoLayoutDpi );
230 mLayout->renderContext().setDpi( value );
231 mLayout->undoStack()->endCommand();
232
233 mLayout->refresh();
234}
235
236void QgsLayoutPropertiesWidget::worldFileToggled()
237{
238 mLayout->setCustomProperty( QStringLiteral( "exportWorldFile" ), mGenerateWorldFileCheckBox->isChecked() );
239}
240
241void QgsLayoutPropertiesWidget::rasterizeToggled()
242{
243 mLayout->setCustomProperty( QStringLiteral( "rasterize" ), mRasterizeCheckBox->isChecked() );
244
245 if ( mRasterizeCheckBox->isChecked() )
246 {
247 mForceVectorCheckBox->setChecked( false );
248 mForceVectorCheckBox->setEnabled( false );
249 }
250 else
251 {
252 mForceVectorCheckBox->setEnabled( true );
253 }
254}
255
256void QgsLayoutPropertiesWidget::forceVectorToggled()
257{
258 mLayout->setCustomProperty( QStringLiteral( "forceVector" ), mForceVectorCheckBox->isChecked() );
259}
260
261void QgsLayoutPropertiesWidget::variablesChanged()
262{
263 mBlockVariableUpdates = true;
264 QgsExpressionContextUtils::setLayoutVariables( mLayout, mVariableEditor->variablesInActiveScope() );
265 mBlockVariableUpdates = false;
266}
267
268void QgsLayoutPropertiesWidget::updateVariables()
269{
270 if ( mBlockVariableUpdates )
271 return;
272
273 QgsExpressionContext context;
277 mVariableEditor->setContext( &context );
278 mVariableEditor->setEditableScopeIndex( 2 );
279}
280
281void QgsLayoutPropertiesWidget::blockSignals( bool block )
282{
283 mGridResolutionSpinBox->blockSignals( block );
284 mOffsetXSpinBox->blockSignals( block );
285 mOffsetYSpinBox->blockSignals( block );
286 mSnapToleranceSpinBox->blockSignals( block );
287}
288
LayoutUnit
Layout measurement units.
Definition: qgis.h:3196
void customVariablesChanged()
Emitted whenever a custom global variable changes.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
static QgsExpressionContextScope * layoutScope(const QgsLayout *layout)
Creates a new scope which contains variables and functions relating to a QgsLayout layout.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static void setLayoutVariables(QgsLayout *layout, const QVariantMap &variables)
Sets all layout context variables.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void coverageLayerChanged(QgsVectorLayer *layer)
Emitted when the coverage layer for the atlas changes.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
void setOffset(const QgsLayoutPoint &offset)
Sets the offset of the page/snap grid.
void setResolution(QgsLayoutMeasurement resolution)
Sets the page/snap grid resolution.
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
Layout graphical items for displaying a map.
Base class for graphical items within a QgsLayout.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
void setLength(const double length)
Sets the length of the measurement.
Qgis::LayoutUnit units() const
Returns the units for the measurement.
void setUnits(const Qgis::LayoutUnit units)
Sets the units for the measurement.
double length() const
Returns the length of the measurement.
void changed()
Emitted when pages are added or removed from the collection.
void resizeToContents(const QgsMargins &margins, Qgis::LayoutUnit marginUnits)
Resizes the layout to a single page which fits the current contents of the layout.
void redraw()
Triggers a redraw for all pages.
This class provides a method of storing points, consisting of an x and y coordinate,...
double x() const
Returns x coordinate of point.
void setX(const double x)
Sets the x coordinate of point.
void setUnits(const Qgis::LayoutUnit units)
Sets the units for the point.
double y() const
Returns y coordinate of point.
Qgis::LayoutUnit units() const
Returns the units for the point.
void setY(const double y)
Sets y coordinate of point.
void setMasterLayout(QgsMasterLayoutInterface *masterLayout)
Sets the master layout.
void updateGui()
Refreshes the gui to reflect the current layout settings.
QgsLayoutPropertiesWidget(QWidget *parent, QgsLayout *layout)
constructor
void setDpi(double dpi)
Sets the dpi for outputting the layout.
void dpiChanged()
Emitted when the context's DPI is changed.
double dpi() const
Returns the dpi for outputting the layout.
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
int snapTolerance() const
Returns the snap tolerance (in pixels) to use when snapping.
void setSnapTolerance(int snapTolerance)
Sets the snap tolerance (in pixels) to use when snapping.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
void endMacro()
Ends a macro command.
void unitChanged(Qgis::LayoutUnit unit)
Emitted when the unit is changed.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the layout.
Definition: qgslayout.cpp:413
QgsLayoutSnapper & snapper()
Returns a reference to the layout's snapper, which stores handles layout snap grids and lines and sna...
Definition: qgslayout.h:406
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:365
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
Definition: qgslayout.cpp:465
void variablesChanged()
Emitted whenever the expression variables stored in the layout have been changed.
QgsLayoutItemMap * referenceMap() const
Returns the map item which will be used to generate corresponding world files when the layout is expo...
Definition: qgslayout.cpp:436
void changed()
Emitted when properties of the layout change.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from the layout.
Definition: qgslayout.cpp:421
QgsLayoutGridSettings & gridSettings()
Returns a reference to the layout's grid settings, which stores settings relating to grid appearance,...
Definition: qgslayout.h:418
void refresh()
Forces the layout, and all items contained within it, to refresh.
Definition: qgslayout.cpp:810
void setReferenceMap(QgsLayoutItemMap *map)
Sets the map item which will be used to generate corresponding world files when the layout is exporte...
Definition: qgslayout.cpp:459
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
Definition: qgslayout.cpp:692
@ UndoLayoutDpi
Change layout default DPI.
Definition: qgslayout.h:71
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:38
Interface for master layout type objects, such as print layouts and reports.
Base class for any widget that can be shown as a inline panel.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
void nameChanged(const QString &name)
Emitted when the layout's name is changed.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
void metadataChanged()
Emitted when the project's metadata is changed.
void customVariablesChanged()
Emitted whenever the expression variables stored in the project have been changed.
void scopeChanged()
Emitted when the user has modified a scope using the widget.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:3435