Skip to content

Commit

Permalink
Support data defined paper sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 57c9668 commit ec56983
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/core/layout/qgslayoutitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "qgslayoutitem.h"
#include "qgslayout.h"
#include "qgslayoututils.h"
#include "qgspagesizeregistry.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QUuid>
Expand Down Expand Up @@ -278,8 +279,22 @@ QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )
}

QgsExpressionContext context = createExpressionContext();
double evaluatedWidth = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemWidth, context, size.width() );
double evaluatedHeight = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemHeight, context, size.height() );

// lowest priority is page size
QString pageSize = mDataDefinedProperties.valueAsString( QgsLayoutObject::PresetPaperSize, context );
QgsPageSize matchedSize;
double evaluatedWidth = size.width();
double evaluatedHeight = size.height();
if ( QgsApplication::pageSizeRegistry()->decodePageSize( pageSize, matchedSize ) )
{
QgsLayoutSize convertedSize = mLayout->context().measurementConverter().convert( matchedSize.size, size.units() );
evaluatedWidth = convertedSize.width();
evaluatedHeight = convertedSize.height();
}

// highest priority is dd width/height
evaluatedWidth = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemWidth, context, evaluatedWidth );
evaluatedHeight = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemHeight, context, evaluatedHeight );
return QgsLayoutSize( evaluatedWidth, evaluatedHeight, size.units() );
}

Expand Down
29 changes: 29 additions & 0 deletions tests/src/core/testqgslayoutitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,35 @@ void TestQgsLayoutItem::dataDefinedSize()
QCOMPARE( item->rect().width(), 70.0 ); //mm
QCOMPARE( item->rect().height(), 60.0 ); //mm

// data defined page size
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PresetPaperSize, QgsProperty::fromValue( QStringLiteral( "A5" ) ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 14.8 );
QCOMPARE( item->sizeWithUnits().height(), 21.0 );
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
QCOMPARE( item->rect().width(), 148.0 ); //mm
QCOMPARE( item->rect().height(), 210.0 ); //mm
// data defined height/width should override page size
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromValue( "13.0" ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 13.0 );
QCOMPARE( item->sizeWithUnits().height(), 21.0 );
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
QCOMPARE( item->rect().width(), 130.0 ); //mm
QCOMPARE( item->rect().height(), 210.0 ); //mm
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromValue( "3.0" ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 13.0 );
QCOMPARE( item->sizeWithUnits().height(), 3.0 );
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
QCOMPARE( item->rect().width(), 130.0 ); //mm
QCOMPARE( item->rect().height(), 30.0 ); //mm
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PresetPaperSize, QgsProperty() );

//check change of units should apply to data defined size
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
Expand Down

0 comments on commit ec56983

Please sign in to comment.