QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
18#include "qgshelp.h"
19#include "qgslayout.h"
23#include "qgspagesizeregistry.h"
24#include "qgssettings.h"
25
26#include <QString>
27
28#include "moc_qgslayoutaddpagesdialog.cpp"
29
30using namespace Qt::StringLiterals;
31
32QgsLayoutAddPagesDialog::QgsLayoutAddPagesDialog( QWidget *parent, Qt::WindowFlags flags )
33 : QDialog( parent, flags )
34{
35 setupUi( this );
36
37 mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
38 mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
39 mPageOrientationComboBox->setCurrentIndex( 1 );
40
41 const auto constEntries = QgsApplication::pageSizeRegistry()->entries();
42 for ( const QgsPageSize &size : constEntries )
43 {
44 mPageSizeComboBox->addItem( size.displayName, size.name );
45 }
46 mPageSizeComboBox->addItem( tr( "Custom" ) );
47 mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( u"A4"_s ) );
48 pageSizeChanged( mPageSizeComboBox->currentIndex() );
49 orientationChanged( 1 );
50
51 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
52 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
53
54 mLockAspectRatio->setWidthSpinBox( mWidthSpin );
55 mLockAspectRatio->setHeightSpinBox( mHeightSpin );
56
57 connect( mPositionComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::positionChanged );
58 mExistingPageSpinBox->setEnabled( false );
59
60 connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::pageSizeChanged );
61 connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::orientationChanged );
62
63 connect( mWidthSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
64 connect( mHeightSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
65
66 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutAddPagesDialog::showHelp );
67}
68
70{
71 mConverter = layout->renderContext().measurementConverter();
72 mSizeUnitsComboBox->setConverter( &mConverter );
73 mExistingPageSpinBox->setMaximum( layout->pageCollection()->pageCount() );
74 mSizeUnitsComboBox->setUnit( layout->units() );
75}
76
78{
79 return mPagesSpinBox->value();
80}
81
83{
84 return static_cast<PagePosition>( mPositionComboBox->currentIndex() );
85}
86
88{
89 return mExistingPageSpinBox->value();
90}
91
93{
94 return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
95}
96
97void QgsLayoutAddPagesDialog::positionChanged( int index )
98{
99 mExistingPageSpinBox->setEnabled( index != 2 );
100}
101
102void QgsLayoutAddPagesDialog::pageSizeChanged( int )
103{
104 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
105 {
106 //custom size
107 mLockAspectRatio->setEnabled( true );
108 mSizeUnitsComboBox->setEnabled( true );
109 mPageOrientationComboBox->setEnabled( false );
110 }
111 else
112 {
113 mLockAspectRatio->setEnabled( false );
114 mLockAspectRatio->setLocked( false );
115 mSizeUnitsComboBox->setEnabled( false );
116 mPageOrientationComboBox->setEnabled( true );
117 const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
118 const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
119 mSettingPresetSize = true;
120 switch ( mPageOrientationComboBox->currentData().toInt() )
121 {
123 mWidthSpin->setValue( convertedSize.height() );
124 mHeightSpin->setValue( convertedSize.width() );
125 break;
126
128 mWidthSpin->setValue( convertedSize.width() );
129 mHeightSpin->setValue( convertedSize.height() );
130 break;
131 }
132 mSettingPresetSize = false;
133 }
134}
135
136void QgsLayoutAddPagesDialog::orientationChanged( int )
137{
138 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
139 return;
140
141 const double width = mWidthSpin->value();
142 const double height = mHeightSpin->value();
143 switch ( mPageOrientationComboBox->currentData().toInt() )
144 {
146 if ( width < height )
147 {
148 whileBlocking( mWidthSpin )->setValue( height );
149 whileBlocking( mHeightSpin )->setValue( width );
150 }
151 break;
152
154 if ( width > height )
155 {
156 whileBlocking( mWidthSpin )->setValue( height );
157 whileBlocking( mHeightSpin )->setValue( width );
158 }
159 break;
160 }
161}
162
163void QgsLayoutAddPagesDialog::setToCustomSize()
164{
165 if ( mSettingPresetSize )
166 return;
167 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
168 mPageOrientationComboBox->setEnabled( false );
169 mLockAspectRatio->setEnabled( true );
170 mSizeUnitsComboBox->setEnabled( true );
171}
172
173void QgsLayoutAddPagesDialog::showHelp()
174{
175 QgsHelp::openHelp( u"print_composer/overview_composer.html#working-with-the-page-properties"_s );
176}
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:41
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.
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.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
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:50
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:330
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:6828