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