QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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 "moc_qgslayoutaddpagesdialog.cpp"
18#include "qgspagesizeregistry.h"
19#include "qgssettings.h"
20#include "qgslayout.h"
23#include "qgshelp.h"
25
26QgsLayoutAddPagesDialog::QgsLayoutAddPagesDialog( QWidget *parent, Qt::WindowFlags flags )
27 : QDialog( parent, flags )
28{
29 setupUi( this );
30
31 mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
32 mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
33 mPageOrientationComboBox->setCurrentIndex( 1 );
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 mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( QStringLiteral( "A4" ) ) );
42 pageSizeChanged( mPageSizeComboBox->currentIndex() );
43 orientationChanged( 1 );
44
45 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
46 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
47
48 mLockAspectRatio->setWidthSpinBox( mWidthSpin );
49 mLockAspectRatio->setHeightSpinBox( mHeightSpin );
50
51 connect( mPositionComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::positionChanged );
52 mExistingPageSpinBox->setEnabled( false );
53
54 connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::pageSizeChanged );
55 connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::orientationChanged );
56
57 connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
58 connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
59
60 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutAddPagesDialog::showHelp );
61}
62
64{
65 mConverter = layout->renderContext().measurementConverter();
66 mSizeUnitsComboBox->setConverter( &mConverter );
67 mExistingPageSpinBox->setMaximum( layout->pageCollection()->pageCount() );
68 mSizeUnitsComboBox->setUnit( layout->units() );
69}
70
72{
73 return mPagesSpinBox->value();
74}
75
77{
78 return static_cast< PagePosition >( mPositionComboBox->currentIndex() );
79}
80
82{
83 return mExistingPageSpinBox->value();
84}
85
87{
88 return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
89}
90
91void QgsLayoutAddPagesDialog::positionChanged( int index )
92{
93 mExistingPageSpinBox->setEnabled( index != 2 );
94}
95
96void QgsLayoutAddPagesDialog::pageSizeChanged( int )
97{
98 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
99 {
100 //custom size
101 mLockAspectRatio->setEnabled( true );
102 mSizeUnitsComboBox->setEnabled( true );
103 mPageOrientationComboBox->setEnabled( false );
104 }
105 else
106 {
107 mLockAspectRatio->setEnabled( false );
108 mLockAspectRatio->setLocked( false );
109 mSizeUnitsComboBox->setEnabled( false );
110 mPageOrientationComboBox->setEnabled( true );
111 const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
112 const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
113 mSettingPresetSize = true;
114 switch ( mPageOrientationComboBox->currentData().toInt() )
115 {
117 mWidthSpin->setValue( convertedSize.height() );
118 mHeightSpin->setValue( convertedSize.width() );
119 break;
120
122 mWidthSpin->setValue( convertedSize.width() );
123 mHeightSpin->setValue( convertedSize.height() );
124 break;
125 }
126 mSettingPresetSize = false;
127 }
128}
129
130void QgsLayoutAddPagesDialog::orientationChanged( int )
131{
132 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
133 return;
134
135 const double width = mWidthSpin->value();
136 const double height = mHeightSpin->value();
137 switch ( mPageOrientationComboBox->currentData().toInt() )
138 {
140 if ( width < height )
141 {
142 whileBlocking( mWidthSpin )->setValue( height );
143 whileBlocking( mHeightSpin )->setValue( width );
144 }
145 break;
146
148 if ( width > height )
149 {
150 whileBlocking( mWidthSpin )->setValue( height );
151 whileBlocking( mHeightSpin )->setValue( width );
152 }
153 break;
154 }
155}
156
157void QgsLayoutAddPagesDialog::setToCustomSize()
158{
159 if ( mSettingPresetSize )
160 return;
161 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
162 mPageOrientationComboBox->setEnabled( false );
163 mLockAspectRatio->setEnabled( true );
164 mSizeUnitsComboBox->setEnabled( true );
165}
166
167void QgsLayoutAddPagesDialog::showHelp()
168{
169 QgsHelp::openHelp( QStringLiteral( "print_composer/overview_composer.html#working-with-the-page-properties" ) );
170}
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:39
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, Qgis::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...
double height() const
Returns the height of the size.
double width() const
Returns the width of the size.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
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.
Qgis::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:5862