QGIS API Documentation 3.39.0-Master (bca3cdb6021)
Loading...
Searching...
No Matches
qgslayoutpagepropertieswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutpagepropertieswidget.cpp
3 ---------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
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 ***************************************************************************/
15
17#include "qgsapplication.h"
18#include "qgspagesizeregistry.h"
19#include "qgslayoutitempage.h"
20#include "qgslayout.h"
22#include "qgslayoutundostack.h"
23#include "qgsvectorlayer.h"
24#include "qgsfillsymbol.h"
27
29 : QgsLayoutItemBaseWidget( parent, layoutItem )
30 , mPage( static_cast< QgsLayoutItemPage *>( layoutItem ) )
31{
32 setupUi( this );
33
34 mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
35 mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
36
37 const auto constEntries = QgsApplication::pageSizeRegistry()->entries();
38 for ( const QgsPageSize &size : constEntries )
39 {
40 mPageSizeComboBox->addItem( size.displayName, size.name );
41 }
42 mPageSizeComboBox->addItem( tr( "Custom" ) );
43
44 mWidthSpin->setValue( mPage->pageSize().width() );
45 mHeightSpin->setValue( mPage->pageSize().height() );
46 mSizeUnitsComboBox->setUnit( mPage->pageSize().units() );
47 mExcludePageCheckBox->setChecked( mPage->excludeFromExports() );
48
49 mPageOrientationComboBox->setCurrentIndex( mPageOrientationComboBox->findData( mPage->orientation() ) );
50
51 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
52 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
53 mSizeUnitsComboBox->setConverter( &mPage->layout()->renderContext().measurementConverter() );
54
55 mLockAspectRatio->setWidthSpinBox( mWidthSpin );
56 mLockAspectRatio->setHeightSpinBox( mHeightSpin );
57
58 mSymbolButton->setSymbolType( Qgis::SymbolType::Fill );
59 mSymbolButton->setSymbol( mPage->pageStyleSymbol()->clone() );
60
61 connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::pageSizeChanged );
62 connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::orientationChanged );
63
64 connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
65 connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
66 connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
67 connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
68 connect( mExcludePageCheckBox, &QCheckBox::toggled, this, &QgsLayoutPagePropertiesWidget::excludeExportsToggled );
69
70 connect( mSymbolButton, &QgsSymbolButton::changed, this, &QgsLayoutPagePropertiesWidget::symbolChanged );
76
77 connect( mPaperSizeDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
78 connect( mWidthDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
79 connect( mHeightDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
80 connect( mOrientationDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
81
82 mExcludePageDDBtn->registerEnabledWidget( mExcludePageCheckBox, false );
83
84 mSymbolButton->registerExpressionContextGenerator( mPage );
85 mSymbolButton->setLayer( coverageLayer() );
86 if ( mPage->layout() )
87 {
89
91 if ( pages->pageCount() > 1 )
92 {
93 const int pageNumber = mPage->layout()->pageCollection()->pageNumber( mPage );
94 mTitleLabel->setText( tr( "Page (%1/%2)" ).arg( pageNumber + 1 ).arg( pages->pageCount() ) );
95 }
96 else
97 {
98 mTitleLabel->setText( tr( "Page" ) );
99 }
100
101 }
102
103 showCurrentPageSize();
104}
105
106void QgsLayoutPagePropertiesWidget::pageSizeChanged( int )
107{
108 mBlockPageUpdate = true;
109 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
110 {
111 //custom size
112 mLockAspectRatio->setEnabled( true );
113 mSizeUnitsComboBox->setEnabled( true );
114 mPageOrientationComboBox->setEnabled( false );
115 }
116 else
117 {
118 mLockAspectRatio->setEnabled( false );
119 mLockAspectRatio->setLocked( false );
120 mSizeUnitsComboBox->setEnabled( false );
121 mPageOrientationComboBox->setEnabled( true );
122 const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
123 const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
124 mSettingPresetSize = true;
125 switch ( mPageOrientationComboBox->currentData().toInt() )
126 {
128 mWidthSpin->setValue( convertedSize.height() );
129 mHeightSpin->setValue( convertedSize.width() );
130 break;
131
133 mWidthSpin->setValue( convertedSize.width() );
134 mHeightSpin->setValue( convertedSize.height() );
135 break;
136 }
137 mSettingPresetSize = false;
138 }
139 mBlockPageUpdate = false;
140 updatePageSize();
141}
142
143void QgsLayoutPagePropertiesWidget::orientationChanged( int )
144{
145 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
146 return;
147
148 const double width = mWidthSpin->value();
149 const double height = mHeightSpin->value();
150 switch ( mPageOrientationComboBox->currentData().toInt() )
151 {
153 if ( width < height )
154 {
155 whileBlocking( mWidthSpin )->setValue( height );
156 whileBlocking( mHeightSpin )->setValue( width );
157 }
158 break;
159
161 if ( width > height )
162 {
163 whileBlocking( mWidthSpin )->setValue( height );
164 whileBlocking( mHeightSpin )->setValue( width );
165 }
166 break;
167 }
168
169 updatePageSize();
170}
171
172void QgsLayoutPagePropertiesWidget::updatePageSize()
173{
174 if ( mBlockPageUpdate )
175 return;
176
177 mPage->layout()->undoStack()->beginMacro( tr( "Change Page Size" ) );
179 mPage->layout()->undoStack()->beginCommand( mPage, tr( "Change Page Size" ), 1 + mPage->layout()->pageCollection()->pageNumber( mPage ) );
180 mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
181 mPage->layout()->undoStack()->endCommand();
182 mPage->layout()->pageCollection()->reflow();
184 mPage->layout()->undoStack()->endMacro();
185
186 refreshLayout();
188}
189
190void QgsLayoutPagePropertiesWidget::setToCustomSize()
191{
192 if ( mSettingPresetSize )
193 return;
194 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
195 mPageOrientationComboBox->setEnabled( false );
196 pageSizeChanged( mPageSizeComboBox->currentIndex() );
197}
198
199void QgsLayoutPagePropertiesWidget::symbolChanged()
200{
201 mPage->layout()->undoStack()->beginCommand( mPage->layout()->pageCollection(), tr( "Change Page Background" ), QgsLayoutItemPage::UndoPageSymbol );
202 mPage->setPageStyleSymbol( static_cast< QgsFillSymbol * >( mSymbolButton->symbol() )->clone() );
203 mPage->layout()->undoStack()->endCommand();
204}
205
206void QgsLayoutPagePropertiesWidget::excludeExportsToggled( bool checked )
207{
208 mPage->beginCommand( !checked ? tr( "Include Page in Exports" ) : tr( "Exclude Page from Exports" ) );
209 mPage->setExcludeFromExports( checked );
210 mPage->endCommand();
211}
212
213void QgsLayoutPagePropertiesWidget::refreshLayout()
214{
215 mPage->layout()->refresh();
216}
217
218void QgsLayoutPagePropertiesWidget::showCurrentPageSize()
219{
220 const QgsLayoutSize paperSize = mPage->pageSize();
221 const QString pageSize = QgsApplication::pageSizeRegistry()->find( paperSize );
222 if ( !pageSize.isEmpty() )
223 {
224 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->findData( pageSize ) );
225 mLockAspectRatio->setEnabled( false );
226 mLockAspectRatio->setLocked( false );
227 mSizeUnitsComboBox->setEnabled( false );
228 mPageOrientationComboBox->setEnabled( true );
229 }
230 else
231 {
232 // custom
233 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
234 mLockAspectRatio->setEnabled( true );
235 mSizeUnitsComboBox->setEnabled( true );
236 mPageOrientationComboBox->setEnabled( false );
237 }
238}
@ Fill
Fill symbol.
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
A base class for property widgets for layout items.
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property)
Registers a data defined button, setting up its initial value, connections and description.
Item representing the paper in a layout.
void setPageSize(const QgsLayoutSize &size)
Sets the size of the page.
QgsLayoutSize pageSize() const
Returns the size of the page.
const QgsFillSymbol * pageStyleSymbol() const
Returns the symbol to use for drawing the page background.
Orientation orientation() const
Returns the page orientation.
void setPageStyleSymbol(QgsFillSymbol *symbol)
Sets the symbol to use for drawing the page background.
@ Landscape
Landscape orientation.
@ Portrait
Portrait orientation.
@ UndoPageSymbol
Layout page symbol change.
Base class for graphical items within a QgsLayout.
bool excludeFromExports() const
Returns whether the item should be excluded from layout exports and prints.
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
void endCommand()
Completes the current item command and push it onto the layout's undo stack.
void setExcludeFromExports(bool exclude)
Sets whether the item should be excluded from layout exports and prints.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, Qgis::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
const QgsLayout * layout() const
Returns the layout the object is attached to.
@ ExcludeFromExports
Exclude item from exports.
@ PaperOrientation
Paper orientation.
@ PresetPaperSize
Preset paper size for composition.
A manager for a collection of pages in a layout.
void reflow()
Forces the page collection to reflow the arrangement of pages, e.g.
void endPageSizeChange()
Should be called after changing any page item sizes, and preceded by a call to beginPageSizeChange().
int pageCount() const
Returns the number of pages in the collection.
int pageNumber(QgsLayoutItemPage *page) const
Returns the page number for the specified page, or -1 if the page is not contained in the collection.
void beginPageSizeChange()
Should be called before changing any page item sizes, and followed by a call to endPageSizeChange().
QgsLayoutPagePropertiesWidget(QWidget *parent, QgsLayoutItem *page)
Constructor for QgsLayoutPagePropertiesWidget.
void pageOrientationChanged()
Emitted when page orientation changes.
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
double height() const
Returns the height of the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
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.
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
void refresh()
Forces the layout, and all items contained within it, to refresh.
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
QList< QgsPageSize > entries() const
Returns a list of page sizes in the registry.
QList< QgsPageSize > find(const QString &name) const
Finds matching page sizes from the registry, using a case insensitive match on the page size name.
A named page size for layouts.
QgsLayoutSize size
Page size.
void changed()
Emitted when property definition changes.
void setLayer(QgsVectorLayer *layer)
Sets a layer to associate with the widget.
void changed()
Emitted when the symbol's settings are changed.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5787