QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgslayoutaddpagesdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutaddpagesdialog.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 "qgspagesizeregistry.h"
18 #include "qgssettings.h"
19 #include "qgslayout.h"
22 #include "qgshelp.h"
23 
24 QgsLayoutAddPagesDialog::QgsLayoutAddPagesDialog( QWidget *parent, Qt::WindowFlags flags )
25  : QDialog( parent, flags )
26 {
27  setupUi( this );
28 
29  mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
30  mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
31  mPageOrientationComboBox->setCurrentIndex( 1 );
32 
33  const auto constEntries = QgsApplication::pageSizeRegistry()->entries();
34  for ( const QgsPageSize &size : constEntries )
35  {
36  mPageSizeComboBox->addItem( size.displayName, size.name );
37  }
38  mPageSizeComboBox->addItem( tr( "Custom" ) );
39  mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( QStringLiteral( "A4" ) ) );
40  pageSizeChanged( mPageSizeComboBox->currentIndex() );
41  orientationChanged( 1 );
42 
43  mSizeUnitsComboBox->linkToWidget( mWidthSpin );
44  mSizeUnitsComboBox->linkToWidget( mHeightSpin );
45 
46  mLockAspectRatio->setWidthSpinBox( mWidthSpin );
47  mLockAspectRatio->setHeightSpinBox( mHeightSpin );
48 
49  connect( mPositionComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::positionChanged );
50  mExistingPageSpinBox->setEnabled( false );
51 
52  connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::pageSizeChanged );
53  connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::orientationChanged );
54 
55  connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
56  connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
57 
58  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutAddPagesDialog::showHelp );
59 }
60 
62 {
63  mConverter = layout->renderContext().measurementConverter();
64  mSizeUnitsComboBox->setConverter( &mConverter );
65  mExistingPageSpinBox->setMaximum( layout->pageCollection()->pageCount() );
66  mSizeUnitsComboBox->setUnit( layout->units() );
67 }
68 
70 {
71  return mPagesSpinBox->value();
72 }
73 
75 {
76  return static_cast< PagePosition >( mPositionComboBox->currentIndex() );
77 }
78 
80 {
81  return mExistingPageSpinBox->value();
82 }
83 
85 {
86  return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
87 }
88 
89 void QgsLayoutAddPagesDialog::positionChanged( int index )
90 {
91  mExistingPageSpinBox->setEnabled( index != 2 );
92 }
93 
94 void QgsLayoutAddPagesDialog::pageSizeChanged( int )
95 {
96  if ( mPageSizeComboBox->currentData().toString().isEmpty() )
97  {
98  //custom size
99  mLockAspectRatio->setEnabled( true );
100  mSizeUnitsComboBox->setEnabled( true );
101  mPageOrientationComboBox->setEnabled( false );
102  }
103  else
104  {
105  mLockAspectRatio->setEnabled( false );
106  mLockAspectRatio->setLocked( false );
107  mSizeUnitsComboBox->setEnabled( false );
108  mPageOrientationComboBox->setEnabled( true );
109  const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
110  const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
111  mSettingPresetSize = true;
112  switch ( mPageOrientationComboBox->currentData().toInt() )
113  {
115  mWidthSpin->setValue( convertedSize.height() );
116  mHeightSpin->setValue( convertedSize.width() );
117  break;
118 
120  mWidthSpin->setValue( convertedSize.width() );
121  mHeightSpin->setValue( convertedSize.height() );
122  break;
123  }
124  mSettingPresetSize = false;
125  }
126 }
127 
128 void QgsLayoutAddPagesDialog::orientationChanged( int )
129 {
130  if ( mPageSizeComboBox->currentData().toString().isEmpty() )
131  return;
132 
133  const double width = mWidthSpin->value();
134  const 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 
155 void QgsLayoutAddPagesDialog::setToCustomSize()
156 {
157  if ( mSettingPresetSize )
158  return;
159  whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
160  mPageOrientationComboBox->setEnabled( false );
161  mLockAspectRatio->setEnabled( true );
162  mSizeUnitsComboBox->setEnabled( true );
163 }
164 
165 void QgsLayoutAddPagesDialog::showHelp()
166 {
167  QgsHelp::openHelp( QStringLiteral( "print_composer/overview_composer.html#working-with-the-page-properties" ) );
168 }
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
int numberPages() const
Returns the number of pages to insert.
QgsLayoutAddPagesDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutAddPagesDialog.
void setLayout(QgsLayout *layout)
Sets the layout associated with the dialog.
PagePosition
Page insertion positions.
PagePosition pagePosition() const
Returns the position at which to insert the new pages.
QgsLayoutSize pageSize() const
Returns the desired page size.
int beforePage() const
Returns the page number for which new pages should be inserted before/after.
@ Landscape
Landscape orientation.
@ Portrait
Portrait orientation.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
int pageCount() const
Returns the number of pages in the collection.
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:41
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:359
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
Definition: qgslayout.cpp:459
QgsUnitTypes::LayoutUnit units() const
Returns the native units for the layout.
Definition: qgslayout.h:329
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.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:1517