Show known page size when opening page properties if current page size matches

This commit is contained in:
Nyall Dawson 2017-07-22 22:32:23 +10:00
parent a4113fe51d
commit fabfd77c2b
6 changed files with 80 additions and 2 deletions

View File

@ -95,6 +95,16 @@ class QgsPageSizeRegistry
:rtype: list of QgsPageSize
%End
QString find( const QgsLayoutSize &size ) const;
%Docstring
Finds a matching page ``size`` from the registry. Returns the page size name,
or an empty string if no matching size could be found.
Orientation is ignored when matching page sizes, so a landscape A4 page will
match to the portrait A4 size in the registry.
:rtype: str
%End
bool decodePageSize( const QString &string, QgsPageSize &size );
%Docstring
Decodes a ``string`` representing a preset page size.

View File

@ -33,8 +33,7 @@ QgsLayoutPagePropertiesWidget::QgsLayoutPagePropertiesWidget( QWidget *parent, Q
mPageSizeComboBox->addItem( size.displayName, size.name );
}
mPageSizeComboBox->addItem( tr( "Custom" ) );
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->count() - 1 );
//TODO - match to preset page sizes
showCurrentPageSize();
mWidthSpin->setValue( mPage->pageSize().width() );
mHeightSpin->setValue( mPage->pageSize().height() );
@ -128,3 +127,29 @@ void QgsLayoutPagePropertiesWidget::updatePageSize()
mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
mPage->layout()->pageCollection()->reflow();
}
void QgsLayoutPagePropertiesWidget::showCurrentPageSize()
{
QgsLayoutSize paperSize = mPage->pageSize();
QString pageSize = QgsApplication::pageSizeRegistry()->find( paperSize );
if ( !pageSize.isEmpty() )
{
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( pageSize ) );
mWidthSpin->setEnabled( false );
mHeightSpin->setEnabled( false );
mLockAspectRatio->setEnabled( false );
mLockAspectRatio->setLocked( false );
mSizeUnitsComboBox->setEnabled( false );
mPageOrientationComboBox->setEnabled( true );
}
else
{
// custom
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->count() - 1 );
mWidthSpin->setEnabled( true );
mHeightSpin->setEnabled( true );
mLockAspectRatio->setEnabled( true );
mSizeUnitsComboBox->setEnabled( true );
mPageOrientationComboBox->setEnabled( false );
}
}

View File

@ -53,6 +53,8 @@ class QgsLayoutPagePropertiesWidget : public QgsLayoutItemBaseWidget, private Ui
QgsLayoutMeasurementConverter mConverter;
void showCurrentPageSize();
};
#endif // QGSLAYOUTPAGEPROPERTIESWIDGET_H

View File

@ -15,6 +15,7 @@
***************************************************************************/
#include "qgspagesizeregistry.h"
#include "qgslayoutmeasurementconverter.h"
//
// QgsPageSizeRegistry
@ -83,6 +84,25 @@ QList<QgsPageSize> QgsPageSizeRegistry::find( const QString &name ) const
return result;
}
QString QgsPageSizeRegistry::find( const QgsLayoutSize &size ) const
{
//try to match to existing page size
QgsLayoutMeasurementConverter converter;
Q_FOREACH ( const QgsPageSize &pageSize, mPageSizes )
{
// convert passed size to same units
QgsLayoutSize xSize = converter.convert( size, pageSize.size.units() );
//consider width and height values may be exchanged
if ( ( qgsDoubleNear( xSize.width(), pageSize.size.width() ) && qgsDoubleNear( xSize.height(), pageSize.size.height() ) )
|| ( qgsDoubleNear( xSize.height(), pageSize.size.width() ) && qgsDoubleNear( xSize.width(), pageSize.size.height() ) ) )
{
return pageSize.name;
}
}
return QString();
}
bool QgsPageSizeRegistry::decodePageSize( const QString &pageSizeName, QgsPageSize &pageSize )
{
QList< QgsPageSize > matches = find( pageSizeName.trimmed() );

View File

@ -95,6 +95,15 @@ class CORE_EXPORT QgsPageSizeRegistry
*/
QList< QgsPageSize > find( const QString &name ) const;
/**
* Finds a matching page \a size from the registry. Returns the page size name,
* or an empty string if no matching size could be found.
*
* Orientation is ignored when matching page sizes, so a landscape A4 page will
* match to the portrait A4 size in the registry.
*/
QString find( const QgsLayoutSize &size ) const;
/**
* Decodes a \a string representing a preset page size.
* The decoded page size will be stored in the \a size argument.

View File

@ -36,6 +36,7 @@ class TestQgsPageSizeRegistry : public QObject
void instanceHasDefaultSizes(); // check that global instance is populated with default page sizes
void addSize(); // check adding a size to the registry
void findSize(); //find a size in the registry
void findBySize(); //find a matching size in the registry
void decodePageSize(); //test decoding a page size string
private:
@ -122,6 +123,17 @@ void TestQgsPageSizeRegistry::findSize()
QCOMPARE( results2.at( 0 ), newSize );
}
void TestQgsPageSizeRegistry::findBySize()
{
QgsPageSizeRegistry *registry = QgsApplication::pageSizeRegistry();
QVERIFY( registry->find( QgsLayoutSize( 1, 1 ) ).isEmpty() );
QCOMPARE( registry->find( QgsLayoutSize( 210, 297 ) ), QStringLiteral( "A4" ) );
QCOMPARE( registry->find( QgsLayoutSize( 297, 210 ) ), QStringLiteral( "A4" ) );
QCOMPARE( registry->find( QgsLayoutSize( 125, 176 ) ), QStringLiteral( "B6" ) );
QCOMPARE( registry->find( QgsLayoutSize( 21, 29.7, QgsUnitTypes::LayoutCentimeters ) ), QStringLiteral( "A4" ) );
}
void TestQgsPageSizeRegistry::decodePageSize()
{
QgsPageSizeRegistry *registry = QgsApplication::pageSizeRegistry();