QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 "qgspagesizeregistry.h"
18#include "qgssettings.h"
19#include "qgslayout.h"
22#include "qgshelp.h"
24
25QgsLayoutAddPagesDialog::QgsLayoutAddPagesDialog( QWidget *parent, Qt::WindowFlags flags )
26 : QDialog( parent, flags )
27{
28 setupUi( this );
29
30 mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
31 mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
32 mPageOrientationComboBox->setCurrentIndex( 1 );
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 mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( QStringLiteral( "A4" ) ) );
41 pageSizeChanged( mPageSizeComboBox->currentIndex() );
42 orientationChanged( 1 );
43
44 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
45 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
46
47 mLockAspectRatio->setWidthSpinBox( mWidthSpin );
48 mLockAspectRatio->setHeightSpinBox( mHeightSpin );
49
50 connect( mPositionComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::positionChanged );
51 mExistingPageSpinBox->setEnabled( false );
52
53 connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::pageSizeChanged );
54 connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::orientationChanged );
55
56 connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
57 connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutAddPagesDialog::setToCustomSize );
58
59 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutAddPagesDialog::showHelp );
60}
61
63{
64 mConverter = layout->renderContext().measurementConverter();
65 mSizeUnitsComboBox->setConverter( &mConverter );
66 mExistingPageSpinBox->setMaximum( layout->pageCollection()->pageCount() );
67 mSizeUnitsComboBox->setUnit( layout->units() );
68}
69
71{
72 return mPagesSpinBox->value();
73}
74
76{
77 return static_cast< PagePosition >( mPositionComboBox->currentIndex() );
78}
79
81{
82 return mExistingPageSpinBox->value();
83}
84
86{
87 return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
88}
89
90void QgsLayoutAddPagesDialog::positionChanged( int index )
91{
92 mExistingPageSpinBox->setEnabled( index != 2 );
93}
94
95void QgsLayoutAddPagesDialog::pageSizeChanged( int )
96{
97 if ( mPageSizeComboBox->currentData().toString().isEmpty() )
98 {
99 //custom size
100 mLockAspectRatio->setEnabled( true );
101 mSizeUnitsComboBox->setEnabled( true );
102 mPageOrientationComboBox->setEnabled( false );
103 }
104 else
105 {
106 mLockAspectRatio->setEnabled( false );
107 mLockAspectRatio->setLocked( false );
108 mSizeUnitsComboBox->setEnabled( false );
109 mPageOrientationComboBox->setEnabled( true );
110 const QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
111 const QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
112 mSettingPresetSize = true;
113 switch ( mPageOrientationComboBox->currentData().toInt() )
114 {
116 mWidthSpin->setValue( convertedSize.height() );
117 mHeightSpin->setValue( convertedSize.width() );
118 break;
119
121 mWidthSpin->setValue( convertedSize.width() );
122 mHeightSpin->setValue( convertedSize.height() );
123 break;
124 }
125 mSettingPresetSize = false;
126 }
127}
128
129void QgsLayoutAddPagesDialog::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
156void QgsLayoutAddPagesDialog::setToCustomSize()
157{
158 if ( mSettingPresetSize )
159 return;
160 whileBlocking( mPageSizeComboBox )->setCurrentIndex( mPageSizeComboBox->count() - 1 );
161 mPageOrientationComboBox->setEnabled( false );
162 mLockAspectRatio->setEnabled( true );
163 mSizeUnitsComboBox->setEnabled( true );
164}
165
166void QgsLayoutAddPagesDialog::showHelp()
167{
168 QgsHelp::openHelp( QStringLiteral( "print_composer/overview_composer.html#working-with-the-page-properties" ) );
169}
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:38
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:5048