Skip to content

Commit

Permalink
Show known page size when opening page properties if current page siz…
Browse files Browse the repository at this point in the history
…e matches
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent a4113fe commit fabfd77
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
10 changes: 10 additions & 0 deletions python/core/layout/qgspagesizeregistry.sip
Expand Up @@ -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.
Expand Down
29 changes: 27 additions & 2 deletions src/app/layout/qgslayoutpagepropertieswidget.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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 );
}
}
2 changes: 2 additions & 0 deletions src/app/layout/qgslayoutpagepropertieswidget.h
Expand Up @@ -53,6 +53,8 @@ class QgsLayoutPagePropertiesWidget : public QgsLayoutItemBaseWidget, private Ui

QgsLayoutMeasurementConverter mConverter;

void showCurrentPageSize();

};

#endif // QGSLAYOUTPAGEPROPERTIESWIDGET_H
20 changes: 20 additions & 0 deletions src/core/layout/qgspagesizeregistry.cpp
Expand Up @@ -15,6 +15,7 @@
***************************************************************************/

#include "qgspagesizeregistry.h"
#include "qgslayoutmeasurementconverter.h"

//
// QgsPageSizeRegistry
Expand Down Expand Up @@ -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() );
Expand Down
9 changes: 9 additions & 0 deletions src/core/layout/qgspagesizeregistry.h
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions tests/src/core/testqgspagesizeregistry.cpp
Expand Up @@ -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:
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fabfd77

Please sign in to comment.