QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
25 
27  : QgsLayoutItemBaseWidget( parent, layoutItem )
28  , mPage( static_cast< QgsLayoutItemPage *>( layoutItem ) )
29 {
30  setupUi( this );
31 
32  mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
33  mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
34 
35  const auto constEntries = QgsApplication::pageSizeRegistry()->entries();
36  for ( const QgsPageSize &size : constEntries )
37  {
38  mPageSizeComboBox->addItem( size.displayName, size.name );
39  }
40  mPageSizeComboBox->addItem( tr( "Custom" ) );
41 
42  mWidthSpin->setValue( mPage->pageSize().width() );
43  mHeightSpin->setValue( mPage->pageSize().height() );
44  mSizeUnitsComboBox->setUnit( mPage->pageSize().units() );
45  mExcludePageCheckBox->setChecked( mPage->excludeFromExports() );
46 
47  mPageOrientationComboBox->setCurrentIndex( mPageOrientationComboBox->findData( mPage->orientation() ) );
48 
49  mSizeUnitsComboBox->linkToWidget( mWidthSpin );
50  mSizeUnitsComboBox->linkToWidget( mHeightSpin );
51  mSizeUnitsComboBox->setConverter( &mPage->layout()->renderContext().measurementConverter() );
52 
53  mLockAspectRatio->setWidthSpinBox( mWidthSpin );
54  mLockAspectRatio->setHeightSpinBox( mHeightSpin );
55 
56  mSymbolButton->setSymbolType( Qgis::SymbolType::Fill );
57  mSymbolButton->setSymbol( mPage->pageStyleSymbol()->clone() );
58 
59  connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::pageSizeChanged );
60  connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::orientationChanged );
61 
62  connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
63  connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
64  connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
65  connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::setToCustomSize );
66  connect( mExcludePageCheckBox, &QCheckBox::toggled, this, &QgsLayoutPagePropertiesWidget::excludeExportsToggled );
67 
68  connect( mSymbolButton, &QgsSymbolButton::changed, this, &QgsLayoutPagePropertiesWidget::symbolChanged );
74 
75  connect( mPaperSizeDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
76  connect( mWidthDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
77  connect( mHeightDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
78  connect( mOrientationDDBtn, &QgsPropertyOverrideButton::changed, this, &QgsLayoutPagePropertiesWidget::refreshLayout );
79 
80  mExcludePageDDBtn->registerEnabledWidget( mExcludePageCheckBox, false );
81 
82  mSymbolButton->registerExpressionContextGenerator( mPage );
83  mSymbolButton->setLayer( coverageLayer() );
84  if ( mPage->layout() )
85  {
87  }
88 
89  showCurrentPageSize();
90 }
91 
92 void QgsLayoutPagePropertiesWidget::pageSizeChanged( int )
93 {
94  mBlockPageUpdate = true;
95  if ( mPageSizeComboBox->currentData().toString().isEmpty() )
96  {
97  //custom size
98  mLockAspectRatio->setEnabled( true );
99  mSizeUnitsComboBox->setEnabled( true );
100  mPageOrientationComboBox->setEnabled( false );
101  }
102  else
103  {
104  mLockAspectRatio->setEnabled( false );
105  mLockAspectRatio->setLocked( false );
106  mSizeUnitsComboBox->setEnabled( false );
107  mPageOrientationComboBox->setEnabled( true );
108  const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
109  const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
110  mSettingPresetSize = true;
111  switch ( mPageOrientationComboBox->currentData().toInt() )
112  {
114  mWidthSpin->setValue( convertedSize.height() );
115  mHeightSpin->setValue( convertedSize.width() );
116  break;
117 
119  mWidthSpin->setValue( convertedSize.width() );
120  mHeightSpin->setValue( convertedSize.height() );
121  break;
122  }
123  mSettingPresetSize = false;
124  }
125  mBlockPageUpdate = false;
126  updatePageSize();
127 }
128 
129 void QgsLayoutPagePropertiesWidget::orientationChanged( int )
130 {
131  if ( mPageSizeComboBox->currentData().toString().isEmpty() )
132  return;
133 
134  const double width = mWidthSpin->value();
135  const double height = mHeightSpin->value();
136  switch ( mPageOrientationComboBox->currentData().toInt() )
137  {
139  if ( width < height )
140  {
141  whileBlocking( mWidthSpin )->setValue( height );
142  whileBlocking( mHeightSpin )->setValue( width );
143  }
144  break;
145 
147  if ( width > height )
148  {
149  whileBlocking( mWidthSpin )->setValue( height );
150  whileBlocking( mHeightSpin )->setValue( width );
151  }
152  break;
153  }
154 
155  updatePageSize();
156 }
157 
158 void QgsLayoutPagePropertiesWidget::updatePageSize()
159 {
160  if ( mBlockPageUpdate )
161  return;
162 
163  mPage->layout()->undoStack()->beginMacro( tr( "Change Page Size" ) );
164  mPage->layout()->pageCollection()->beginPageSizeChange();
165  mPage->layout()->undoStack()->beginCommand( mPage, tr( "Change Page Size" ), 1 + mPage->layout()->pageCollection()->pageNumber( mPage ) );
166  mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
167  mPage->layout()->undoStack()->endCommand();
168  mPage->layout()->pageCollection()->reflow();
169  mPage->layout()->pageCollection()->endPageSizeChange();
170  mPage->layout()->undoStack()->endMacro();
171 
172  refreshLayout();
173  emit pageOrientationChanged();
174 }
175 
176 void QgsLayoutPagePropertiesWidget::setToCustomSize()
177 {
178  if ( mSettingPresetSize )
179  return;
180  whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
181  mPageOrientationComboBox->setEnabled( false );
182  pageSizeChanged( mPageSizeComboBox->currentIndex() );
183 }
184 
185 void QgsLayoutPagePropertiesWidget::symbolChanged()
186 {
187  mPage->layout()->undoStack()->beginCommand( mPage->layout()->pageCollection(), tr( "Change Page Background" ), QgsLayoutItemPage::UndoPageSymbol );
188  mPage->setPageStyleSymbol( static_cast< QgsFillSymbol * >( mSymbolButton->symbol() )->clone() );
189  mPage->layout()->undoStack()->endCommand();
190 }
191 
192 void QgsLayoutPagePropertiesWidget::excludeExportsToggled( bool checked )
193 {
194  mPage->beginCommand( !checked ? tr( "Include Page in Exports" ) : tr( "Exclude Page from Exports" ) );
195  mPage->setExcludeFromExports( checked );
196  mPage->endCommand();
197 }
198 
199 void QgsLayoutPagePropertiesWidget::refreshLayout()
200 {
201  mPage->layout()->refresh();
202 }
203 
204 void QgsLayoutPagePropertiesWidget::showCurrentPageSize()
205 {
206  const QgsLayoutSize paperSize = mPage->pageSize();
207  const QString pageSize = QgsApplication::pageSizeRegistry()->find( paperSize );
208  if ( !pageSize.isEmpty() )
209  {
210  whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->findData( pageSize ) );
211  mLockAspectRatio->setEnabled( false );
212  mLockAspectRatio->setLocked( false );
213  mSizeUnitsComboBox->setEnabled( false );
214  mPageOrientationComboBox->setEnabled( true );
215  }
216  else
217  {
218  // custom
219  whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
220  mLockAspectRatio->setEnabled( true );
221  mSizeUnitsComboBox->setEnabled( true );
222  mPageOrientationComboBox->setEnabled( false );
223  }
224 }
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:216
QgsLayoutItemPage::pageSize
QgsLayoutSize pageSize() const
Returns the size of the page.
Definition: qgslayoutitempage.cpp:127
QgsLayoutRenderContext::measurementConverter
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
Definition: qgslayoutrendercontext.h:133
QgsSymbolButton::changed
void changed()
Emitted when the symbol's settings are changed.
qgslayoutundostack.h
QgsApplication::pageSizeRegistry
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
Definition: qgsapplication.cpp:2465
QgsLayoutItemPage
Item representing the paper in a layout.
Definition: qgslayoutitempage.h:58
QgsLayoutSize::width
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76
Qgis::SymbolType::Fill
@ Fill
Fill symbol.
QgsLayoutSize::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
Definition: qgslayoutsize.h:103
QgsLayoutPagePropertiesWidget::QgsLayoutPagePropertiesWidget
QgsLayoutPagePropertiesWidget(QWidget *parent, QgsLayoutItem *page)
Constructor for QgsLayoutPagePropertiesWidget.
Definition: qgslayoutpagepropertieswidget.cpp:26
QgsPageSizeRegistry::entries
QList< QgsPageSize > entries() const
Returns a list of page sizes in the registry.
Definition: qgspagesizeregistry.cpp:66
QgsPropertyOverrideButton::changed
void changed()
Emitted when property definition changes.
QgsLayout::renderContext
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:359
QgsLayoutItemPage::orientation
Orientation orientation() const
Returns the page orientation.
Definition: qgslayoutitempage.cpp:132
QgsLayoutObject::PaperOrientation
@ PaperOrientation
Paper orientation.
Definition: qgslayoutobject.h:142
QgsLayoutObject::ItemHeight
@ ItemHeight
Height of item.
Definition: qgslayoutobject.h:148
QgsLayoutItemBaseWidget::registerDataDefinedButton
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property)
Registers a data defined button, setting up its initial value, connections and description.
Definition: qgslayoutitemwidget.cpp:246
QgsLayoutItem::excludeFromExports
bool excludeFromExports() const
Returns whether the item should be excluded from layout exports and prints.
Definition: qgslayoutitem.cpp:915
QgsPageSizeRegistry::find
QList< QgsPageSize > find(const QString &name) const
Finds matching page sizes from the registry, using a case insensitive match on the page size name.
Definition: qgspagesizeregistry.cpp:77
QgsLayoutItemBaseWidget::coverageLayer
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
Definition: qgslayoutitemwidget.cpp:256
qgsapplication.h
QgsPageSize
A named page size for layouts.
Definition: qgspagesizeregistry.h:32
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2191
QgsLayoutItemPage::pageStyleSymbol
const QgsFillSymbol * pageStyleSymbol() const
Returns the symbol to use for drawing the page background.
Definition: qgslayoutitempage.h:147
qgslayoutpagepropertieswidget.h
QgsLayoutItemPage::UndoPageSymbol
@ UndoPageSymbol
Layout page symbol change.
Definition: qgslayoutitempage.h:75
QgsLayout::reportContext
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
Definition: qgslayout.cpp:369
QgsLayoutObject::ExcludeFromExports
@ ExcludeFromExports
Exclude item from exports.
Definition: qgslayoutobject.h:153
QgsLayoutItemPage::Landscape
@ Landscape
Landscape orientation.
Definition: qgslayoutitempage.h:69
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
qgslayout.h
qgspagesizeregistry.h
QgsLayoutItemBaseWidget
A base class for property widgets for layout items. All layout item widgets should inherit from this ...
Definition: qgslayoutitemwidget.h:122
QgsLayoutSize::height
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
qgsvectorlayer.h
qgslayoutitempage.h
QgsSymbolButton::setLayer
void setLayer(QgsVectorLayer *layer)
Sets a layer to associate with the widget.
Definition: qgssymbolbutton.cpp:208
QgsLayoutItemPage::Portrait
@ Portrait
Portrait orientation.
Definition: qgslayoutitempage.h:68
QgsFillSymbol::clone
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
Definition: qgsfillsymbol.cpp:144
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:29
qgslayoutpagecollection.h
QgsLayoutSize
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
QgsPageSize::size
QgsLayoutSize size
Page size.
Definition: qgspagesizeregistry.h:54
QgsLayoutReportContext::layerChanged
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
QgsLayoutObject::PresetPaperSize
@ PresetPaperSize
Preset paper size for composition.
Definition: qgslayoutobject.h:138
qgsfillsymbol.h
QgsLayoutObject::ItemWidth
@ ItemWidth
Width of item.
Definition: qgslayoutobject.h:147