Skip to content

Commit fabfd77

Browse files
committed
Show known page size when opening page properties if current page size matches
1 parent a4113fe commit fabfd77

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

python/core/layout/qgspagesizeregistry.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ class QgsPageSizeRegistry
9595
:rtype: list of QgsPageSize
9696
%End
9797

98+
QString find( const QgsLayoutSize &size ) const;
99+
%Docstring
100+
Finds a matching page ``size`` from the registry. Returns the page size name,
101+
or an empty string if no matching size could be found.
102+
103+
Orientation is ignored when matching page sizes, so a landscape A4 page will
104+
match to the portrait A4 size in the registry.
105+
:rtype: str
106+
%End
107+
98108
bool decodePageSize( const QString &string, QgsPageSize &size );
99109
%Docstring
100110
Decodes a ``string`` representing a preset page size.

src/app/layout/qgslayoutpagepropertieswidget.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ QgsLayoutPagePropertiesWidget::QgsLayoutPagePropertiesWidget( QWidget *parent, Q
3333
mPageSizeComboBox->addItem( size.displayName, size.name );
3434
}
3535
mPageSizeComboBox->addItem( tr( "Custom" ) );
36-
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->count() - 1 );
37-
//TODO - match to preset page sizes
36+
showCurrentPageSize();
3837

3938
mWidthSpin->setValue( mPage->pageSize().width() );
4039
mHeightSpin->setValue( mPage->pageSize().height() );
@@ -128,3 +127,29 @@ void QgsLayoutPagePropertiesWidget::updatePageSize()
128127
mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
129128
mPage->layout()->pageCollection()->reflow();
130129
}
130+
131+
void QgsLayoutPagePropertiesWidget::showCurrentPageSize()
132+
{
133+
QgsLayoutSize paperSize = mPage->pageSize();
134+
QString pageSize = QgsApplication::pageSizeRegistry()->find( paperSize );
135+
if ( !pageSize.isEmpty() )
136+
{
137+
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( pageSize ) );
138+
mWidthSpin->setEnabled( false );
139+
mHeightSpin->setEnabled( false );
140+
mLockAspectRatio->setEnabled( false );
141+
mLockAspectRatio->setLocked( false );
142+
mSizeUnitsComboBox->setEnabled( false );
143+
mPageOrientationComboBox->setEnabled( true );
144+
}
145+
else
146+
{
147+
// custom
148+
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->count() - 1 );
149+
mWidthSpin->setEnabled( true );
150+
mHeightSpin->setEnabled( true );
151+
mLockAspectRatio->setEnabled( true );
152+
mSizeUnitsComboBox->setEnabled( true );
153+
mPageOrientationComboBox->setEnabled( false );
154+
}
155+
}

src/app/layout/qgslayoutpagepropertieswidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class QgsLayoutPagePropertiesWidget : public QgsLayoutItemBaseWidget, private Ui
5353

5454
QgsLayoutMeasurementConverter mConverter;
5555

56+
void showCurrentPageSize();
57+
5658
};
5759

5860
#endif // QGSLAYOUTPAGEPROPERTIESWIDGET_H

src/core/layout/qgspagesizeregistry.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***************************************************************************/
1616

1717
#include "qgspagesizeregistry.h"
18+
#include "qgslayoutmeasurementconverter.h"
1819

1920
//
2021
// QgsPageSizeRegistry
@@ -83,6 +84,25 @@ QList<QgsPageSize> QgsPageSizeRegistry::find( const QString &name ) const
8384
return result;
8485
}
8586

87+
QString QgsPageSizeRegistry::find( const QgsLayoutSize &size ) const
88+
{
89+
//try to match to existing page size
90+
QgsLayoutMeasurementConverter converter;
91+
Q_FOREACH ( const QgsPageSize &pageSize, mPageSizes )
92+
{
93+
// convert passed size to same units
94+
QgsLayoutSize xSize = converter.convert( size, pageSize.size.units() );
95+
96+
//consider width and height values may be exchanged
97+
if ( ( qgsDoubleNear( xSize.width(), pageSize.size.width() ) && qgsDoubleNear( xSize.height(), pageSize.size.height() ) )
98+
|| ( qgsDoubleNear( xSize.height(), pageSize.size.width() ) && qgsDoubleNear( xSize.width(), pageSize.size.height() ) ) )
99+
{
100+
return pageSize.name;
101+
}
102+
}
103+
return QString();
104+
}
105+
86106
bool QgsPageSizeRegistry::decodePageSize( const QString &pageSizeName, QgsPageSize &pageSize )
87107
{
88108
QList< QgsPageSize > matches = find( pageSizeName.trimmed() );

src/core/layout/qgspagesizeregistry.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ class CORE_EXPORT QgsPageSizeRegistry
9595
*/
9696
QList< QgsPageSize > find( const QString &name ) const;
9797

98+
/**
99+
* Finds a matching page \a size from the registry. Returns the page size name,
100+
* or an empty string if no matching size could be found.
101+
*
102+
* Orientation is ignored when matching page sizes, so a landscape A4 page will
103+
* match to the portrait A4 size in the registry.
104+
*/
105+
QString find( const QgsLayoutSize &size ) const;
106+
98107
/**
99108
* Decodes a \a string representing a preset page size.
100109
* The decoded page size will be stored in the \a size argument.

tests/src/core/testqgspagesizeregistry.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQgsPageSizeRegistry : public QObject
3636
void instanceHasDefaultSizes(); // check that global instance is populated with default page sizes
3737
void addSize(); // check adding a size to the registry
3838
void findSize(); //find a size in the registry
39+
void findBySize(); //find a matching size in the registry
3940
void decodePageSize(); //test decoding a page size string
4041

4142
private:
@@ -122,6 +123,17 @@ void TestQgsPageSizeRegistry::findSize()
122123
QCOMPARE( results2.at( 0 ), newSize );
123124
}
124125

126+
void TestQgsPageSizeRegistry::findBySize()
127+
{
128+
QgsPageSizeRegistry *registry = QgsApplication::pageSizeRegistry();
129+
QVERIFY( registry->find( QgsLayoutSize( 1, 1 ) ).isEmpty() );
130+
QCOMPARE( registry->find( QgsLayoutSize( 210, 297 ) ), QStringLiteral( "A4" ) );
131+
QCOMPARE( registry->find( QgsLayoutSize( 297, 210 ) ), QStringLiteral( "A4" ) );
132+
QCOMPARE( registry->find( QgsLayoutSize( 125, 176 ) ), QStringLiteral( "B6" ) );
133+
QCOMPARE( registry->find( QgsLayoutSize( 21, 29.7, QgsUnitTypes::LayoutCentimeters ) ), QStringLiteral( "A4" ) );
134+
135+
}
136+
125137
void TestQgsPageSizeRegistry::decodePageSize()
126138
{
127139
QgsPageSizeRegistry *registry = QgsApplication::pageSizeRegistry();

0 commit comments

Comments
 (0)