Skip to content
Permalink
Browse files

Support data defined paper sizes

  • Loading branch information
nyalldawson committed Jul 23, 2017
1 parent 57c9668 commit ec5698393bb44e5f23092307a4ccbdb7a57feb92
Showing with 46 additions and 2 deletions.
  1. +17 −2 src/core/layout/qgslayoutitem.cpp
  2. +29 −0 tests/src/core/testqgslayoutitem.cpp
@@ -17,6 +17,7 @@
#include "qgslayoutitem.h"
#include "qgslayout.h"
#include "qgslayoututils.h"
#include "qgspagesizeregistry.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QUuid>
@@ -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() );
}

@@ -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" ) ) );

0 comments on commit ec56983

Please sign in to comment.
You can’t perform that action at this time.