QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 "moc_qgslayoutpagepropertieswidget.cpp"
18#include "qgsapplication.h"
19#include "qgspagesizeregistry.h"
20#include "qgslayoutitempage.h"
21#include "qgslayout.h"
23#include "qgslayoutundostack.h"
24#include "qgsvectorlayer.h"
25#include "qgsfillsymbol.h"
28
30 : QgsLayoutItemBaseWidget( parent, layoutItem )
31 , mPage( static_cast<QgsLayoutItemPage *>( layoutItem ) )
32{
33 setupUi( this );
34
35 mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
36 mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
37
38 const auto constEntries = QgsApplication::pageSizeRegistry()->entries();
39 for ( const QgsPageSize &size : constEntries )
40 {
41 mPageSizeComboBox->addItem( size.displayName, size.name );
42 }
43 mPageSizeComboBox->addItem( tr( "Custom" ) );
44
45 mWidthSpin->setValue( mPage->pageSize().width() );
46 mHeightSpin->setValue( mPage->pageSize().height() );
47 mSizeUnitsComboBox->setUnit( mPage->pageSize().units() );
48 mExcludePageCheckBox->setChecked( mPage->excludeFromExports() );
49
50 mPageOrientationComboBox->setCurrentIndex( mPageOrientationComboBox->findData( mPage->orientation() ) );
51
52 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
53 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
54 mSizeUnitsComboBox->setConverter( &mPage->layout()->renderContext().measurementConverter() );
55
56 mLockAspectRatio->setWidthSpinBox( mWidthSpin );
57 mLockAspectRatio->setHeightSpinBox( mHeightSpin );
58
59 mSymbolButton->setSymbolType( Qgis::SymbolType::Fill );
60 mSymbolButton->setSymbol( mPage->pageStyleSymbol()->clone() );
61
62 connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::pageSizeChanged );
63 connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::orientationChanged );
64
65 connect( mWidthSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
66 connect( mHeightSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
67 connect( mWidthSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
68 connect( mHeightSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
69 connect( mExcludePageCheckBox, &QCheckBox::toggled, this, &QgsLayoutPagePropertiesWidget::excludeExportsToggled );
70
71 connect( mSymbolButton, &QgsSymbolButton::changed, this, &QgsLayoutPagePropertiesWidget::symbolChanged );
77
78 connect( mPaperSizeDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
79 connect( mWidthDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
80 connect( mHeightDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
81 connect( mOrientationDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
82
83 mExcludePageDDBtn->registerEnabledWidget( mExcludePageCheckBox, false );
84
85 mSymbolButton->registerExpressionContextGenerator( mPage );
86 mSymbolButton->setLayer( coverageLayer() );
87
88 connect( mApplyToAllButton, &QPushButton::clicked, this, &QgsLayoutPagePropertiesWidget::applyToAll );
89
90 if ( mPage->layout() )
91 {
93
95 const bool multiPage = pages->pageCount() > 1;
96 if ( multiPage )
97 {
98 const int pageNumber = mPage->layout()->pageCollection()->pageNumber( mPage );
99 mTitleLabel->setText( tr( "Page (%1/%2)" ).arg( pageNumber + 1 ).arg( pages->pageCount() ) );
100 }
101 else
102 {
103 mTitleLabel->setText( tr( "Page" ) );
104 }
105 mApplyToAllButton->setVisible( multiPage );
106 }
107
108 showCurrentPageSize();
109}
110
111void QgsLayoutPagePropertiesWidget::pageSizeChanged( int )
112{
113 mBlockPageUpdate = true;
114 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
115 {
116 //custom size
117 mLockAspectRatio->setEnabled( true );
118 mSizeUnitsComboBox->setEnabled( true );
119 mPageOrientationComboBox->setEnabled( false );
120 }
121 else
122 {
123 mLockAspectRatio->setEnabled( false );
124 mLockAspectRatio->setLocked( false );
125 mSizeUnitsComboBox->setEnabled( false );
126 mPageOrientationComboBox->setEnabled( true );
127 const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
128 const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
129 mSettingPresetSize = true;
130 switch ( mPageOrientationComboBox->currentData().toInt() )
131 {
133 mWidthSpin->setValue( convertedSize.height() );
134 mHeightSpin->setValue( convertedSize.width() );
135 break;
136
138 mWidthSpin->setValue( convertedSize.width() );
139 mHeightSpin->setValue( convertedSize.height() );
140 break;
141 }
142 mSettingPresetSize = false;
143 }
144 mBlockPageUpdate = false;
145 updatePageSize();
146}
147
148void QgsLayoutPagePropertiesWidget::orientationChanged( int )
149{
150 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
151 return;
152
153 const double width = mWidthSpin->value();
154 const double height = mHeightSpin->value();
155 switch ( mPageOrientationComboBox->currentData().toInt() )
156 {
158 if ( width < height )
159 {
160 whileBlocking( mWidthSpin )->setValue( height );
161 whileBlocking( mHeightSpin )->setValue( width );
162 }
163 break;
164
166 if ( width > height )
167 {
168 whileBlocking( mWidthSpin )->setValue( height );
169 whileBlocking( mHeightSpin )->setValue( width );
170 }
171 break;
172 }
173
174 updatePageSize();
175}
176
177void QgsLayoutPagePropertiesWidget::updatePageSize()
178{
179 if ( mBlockPageUpdate )
180 return;
181
182 mPage->layout()->undoStack()->beginMacro( tr( "Change Page Size" ) );
184 mPage->layout()->undoStack()->beginCommand( mPage, tr( "Change Page Size" ), 1 + mPage->layout()->pageCollection()->pageNumber( mPage ) );
185 mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
186 mPage->layout()->undoStack()->endCommand();
187 mPage->layout()->pageCollection()->reflow();
189 mPage->layout()->undoStack()->endMacro();
190
191 refreshLayout();
193}
194
195void QgsLayoutPagePropertiesWidget::setToCustomSize()
196{
197 if ( mSettingPresetSize )
198 return;
199 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
200 mPageOrientationComboBox->setEnabled( false );
201 pageSizeChanged( mPageSizeComboBox->currentIndex() );
202}
203
204void QgsLayoutPagePropertiesWidget::symbolChanged()
205{
206 mPage->layout()->undoStack()->beginCommand( mPage->layout()->pageCollection(), tr( "Change Page Background" ), QgsLayoutItemPage::UndoPageSymbol );
207 mPage->setPageStyleSymbol( static_cast<QgsFillSymbol *>( mSymbolButton->symbol() )->clone() );
208 mPage->layout()->undoStack()->endCommand();
209}
210
211void QgsLayoutPagePropertiesWidget::excludeExportsToggled( bool checked )
212{
213 mPage->beginCommand( !checked ? tr( "Include Page in Exports" ) : tr( "Exclude Page from Exports" ) );
214 mPage->setExcludeFromExports( checked );
215 mPage->endCommand();
216}
217
218void QgsLayoutPagePropertiesWidget::refreshLayout()
219{
220 mPage->layout()->refresh();
221}
222
223void QgsLayoutPagePropertiesWidget::showCurrentPageSize()
224{
225 const QgsLayoutSize paperSize = mPage->pageSize();
226 const QString pageSize = QgsApplication::pageSizeRegistry()->find( paperSize );
227 if ( !pageSize.isEmpty() )
228 {
229 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->findData( pageSize ) );
230 mLockAspectRatio->setEnabled( false );
231 mLockAspectRatio->setLocked( false );
232 mSizeUnitsComboBox->setEnabled( false );
233 mPageOrientationComboBox->setEnabled( true );
234 }
235 else
236 {
237 // custom
238 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
239 mLockAspectRatio->setEnabled( true );
240 mSizeUnitsComboBox->setEnabled( true );
241 mPageOrientationComboBox->setEnabled( false );
242 }
243}
244
245void QgsLayoutPagePropertiesWidget::applyToAll()
246{
247 QgsLayoutPageCollection *pages = mPage->layout()->pageCollection();
248 pages->applyPropertiesToAllOtherPages( pages->pageNumber( mPage ) );
249}
@ 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.
void applyPropertiesToAllOtherPages(int sourcePage)
Apply the source page properties (size & background color) to all other pages.
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:5928