QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
18#include "qgsapplication.h"
19#include "qgsfillsymbol.h"
20#include "qgslayout.h"
21#include "qgslayoutitempage.h"
25#include "qgslayoutundostack.h"
26#include "qgspagesizeregistry.h"
27#include "qgsvectorlayer.h"
28
29#include "moc_qgslayoutpagepropertieswidget.cpp"
30
32 : QgsLayoutItemBaseWidget( parent, layoutItem )
33 , mPage( static_cast<QgsLayoutItemPage *>( layoutItem ) )
34{
35 setupUi( this );
36
37 mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
38 mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
39
40 const auto constEntries = QgsApplication::pageSizeRegistry()->entries();
41 for ( const QgsPageSize &size : constEntries )
42 {
43 mPageSizeComboBox->addItem( size.displayName, size.name );
44 }
45 mPageSizeComboBox->addItem( tr( "Custom" ) );
46
47 mWidthSpin->setValue( mPage->pageSize().width() );
48 mHeightSpin->setValue( mPage->pageSize().height() );
49 mSizeUnitsComboBox->setUnit( mPage->pageSize().units() );
50 mExcludePageCheckBox->setChecked( mPage->excludeFromExports() );
51
52 mPageOrientationComboBox->setCurrentIndex( mPageOrientationComboBox->findData( mPage->orientation() ) );
53
54 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
55 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
56 mSizeUnitsComboBox->setConverter( &mPage->layout()->renderContext().measurementConverter() );
57
58 mLockAspectRatio->setWidthSpinBox( mWidthSpin );
59 mLockAspectRatio->setHeightSpinBox( mHeightSpin );
60
61 mSymbolButton->setSymbolType( Qgis::SymbolType::Fill );
62 mSymbolButton->setSymbol( mPage->pageStyleSymbol()->clone() );
63
64 connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::pageSizeChanged );
65 connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::orientationChanged );
66
67 connect( mWidthSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
68 connect( mHeightSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
69 connect( mWidthSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
70 connect( mHeightSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
71 connect( mExcludePageCheckBox, &QCheckBox::toggled, this, &QgsLayoutPagePropertiesWidget::excludeExportsToggled );
72
73 connect( mSymbolButton, &QgsSymbolButton::changed, this, &QgsLayoutPagePropertiesWidget::symbolChanged );
79
80 connect( mPaperSizeDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
81 connect( mWidthDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
82 connect( mHeightDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
83 connect( mOrientationDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
84
85 mExcludePageDDBtn->registerEnabledWidget( mExcludePageCheckBox, false );
86
87 mSymbolButton->registerExpressionContextGenerator( mPage );
88 mSymbolButton->setLayer( coverageLayer() );
89
90 connect( mApplyToAllButton, &QPushButton::clicked, this, &QgsLayoutPagePropertiesWidget::applyToAll );
91
92 if ( mPage->layout() )
93 {
94 connect( &mPage->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, mSymbolButton, &QgsSymbolButton::setLayer );
95
97 const bool multiPage = pages->pageCount() > 1;
98 if ( multiPage )
99 {
100 const int pageNumber = mPage->layout()->pageCollection()->pageNumber( mPage );
101 mTitleLabel->setText( tr( "Page (%1/%2)" ).arg( pageNumber + 1 ).arg( pages->pageCount() ) );
102 }
103 else
104 {
105 mTitleLabel->setText( tr( "Page" ) );
106 }
107 mApplyToAllButton->setVisible( multiPage );
108 }
109
110 showCurrentPageSize();
111}
112
113void QgsLayoutPagePropertiesWidget::pageSizeChanged( int )
114{
115 mBlockPageUpdate = true;
116 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
117 {
118 //custom size
119 mLockAspectRatio->setEnabled( true );
120 mSizeUnitsComboBox->setEnabled( true );
121 mPageOrientationComboBox->setEnabled( false );
122 }
123 else
124 {
125 mLockAspectRatio->setEnabled( false );
126 mLockAspectRatio->setLocked( false );
127 mSizeUnitsComboBox->setEnabled( false );
128 mPageOrientationComboBox->setEnabled( true );
129 const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
130 const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
131 mSettingPresetSize = true;
132 switch ( mPageOrientationComboBox->currentData().toInt() )
133 {
135 mWidthSpin->setValue( convertedSize.height() );
136 mHeightSpin->setValue( convertedSize.width() );
137 break;
138
140 mWidthSpin->setValue( convertedSize.width() );
141 mHeightSpin->setValue( convertedSize.height() );
142 break;
143 }
144 mSettingPresetSize = false;
145 }
146 mBlockPageUpdate = false;
147 updatePageSize();
148}
149
150void QgsLayoutPagePropertiesWidget::orientationChanged( int )
151{
152 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
153 return;
154
155 const double width = mWidthSpin->value();
156 const double height = mHeightSpin->value();
157 switch ( mPageOrientationComboBox->currentData().toInt() )
158 {
160 if ( width < height )
161 {
162 whileBlocking( mWidthSpin )->setValue( height );
163 whileBlocking( mHeightSpin )->setValue( width );
164 }
165 break;
166
168 if ( width > height )
169 {
170 whileBlocking( mWidthSpin )->setValue( height );
171 whileBlocking( mHeightSpin )->setValue( width );
172 }
173 break;
174 }
175
176 updatePageSize();
177}
178
179void QgsLayoutPagePropertiesWidget::updatePageSize()
180{
181 if ( mBlockPageUpdate )
182 return;
183
184 mPage->layout()->undoStack()->beginMacro( tr( "Change Page Size" ) );
185 mPage->layout()->pageCollection()->beginPageSizeChange();
186 mPage->layout()->undoStack()->beginCommand( mPage, tr( "Change Page Size" ), 1 + mPage->layout()->pageCollection()->pageNumber( mPage ) );
187 mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
188 mPage->layout()->undoStack()->endCommand();
189 mPage->layout()->pageCollection()->reflow();
190 mPage->layout()->pageCollection()->endPageSizeChange();
191 mPage->layout()->undoStack()->endMacro();
192
193 refreshLayout();
195}
196
197void QgsLayoutPagePropertiesWidget::setToCustomSize()
198{
199 if ( mSettingPresetSize )
200 return;
201 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
202 mPageOrientationComboBox->setEnabled( false );
203 pageSizeChanged( mPageSizeComboBox->currentIndex() );
204}
205
206void QgsLayoutPagePropertiesWidget::symbolChanged()
207{
208 mPage->layout()->undoStack()->beginCommand( mPage->layout()->pageCollection(), tr( "Change Page Background" ), QgsLayoutItemPage::UndoPageSymbol );
209 mPage->setPageStyleSymbol( static_cast<QgsFillSymbol *>( mSymbolButton->symbol() )->clone() );
210 mPage->layout()->undoStack()->endCommand();
211}
212
213void QgsLayoutPagePropertiesWidget::excludeExportsToggled( bool checked )
214{
215 mPage->beginCommand( !checked ? tr( "Include Page in Exports" ) : tr( "Exclude Page from Exports" ) );
216 mPage->setExcludeFromExports( checked );
217 mPage->endCommand();
218}
219
220void QgsLayoutPagePropertiesWidget::refreshLayout()
221{
222 mPage->layout()->refresh();
223}
224
225void QgsLayoutPagePropertiesWidget::showCurrentPageSize()
226{
227 const QgsLayoutSize paperSize = mPage->pageSize();
228 const QString pageSize = QgsApplication::pageSizeRegistry()->find( paperSize );
229 if ( !pageSize.isEmpty() )
230 {
231 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->findData( pageSize ) );
232 mLockAspectRatio->setEnabled( false );
233 mLockAspectRatio->setLocked( false );
234 mSizeUnitsComboBox->setEnabled( false );
235 mPageOrientationComboBox->setEnabled( true );
236 }
237 else
238 {
239 // custom
240 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
241 mLockAspectRatio->setEnabled( true );
242 mSizeUnitsComboBox->setEnabled( true );
243 mPageOrientationComboBox->setEnabled( false );
244 }
245}
246
247void QgsLayoutPagePropertiesWidget::applyToAll()
248{
249 QgsLayoutPageCollection *pages = mPage->layout()->pageCollection();
250 pages->applyPropertiesToAllOtherPages( pages->pageNumber( mPage ) );
251}
@ Fill
Fill symbol.
Definition qgis.h:613
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
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.
QgsLayoutItemBaseWidget(QWidget *parent SIP_TRANSFERTHIS, QgsLayoutObject *layoutObject)
Constructor for QgsLayoutItemBaseWidget, linked with the specified layoutObject.
Item representing the paper in a layout.
@ Landscape
Landscape orientation.
@ Portrait
Portrait orientation.
@ UndoPageSymbol
Layout page symbol change.
Base class for graphical items within a QgsLayout.
@ ExcludeFromExports
Exclude item from exports.
@ PresetPaperSize
Preset paper size for composition.
A manager for a collection of pages in a layout.
QgsLayout * layout() override
Returns the layout the object belongs to.
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.
QgsLayoutPagePropertiesWidget(QWidget *parent, QgsLayoutItem *page)
Constructor for QgsLayoutPagePropertiesWidget.
void pageOrientationChanged()
Emitted when page orientation changes.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
double height() const
Returns the height of the size.
double width() const
Returns the width of the size.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
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:6511