Skip to content
Permalink
Browse files

Restore data defined page orientation

  • Loading branch information
nyalldawson committed Dec 15, 2017
1 parent 65f4c4a commit 5d6a5096367fa0920891e52abe19b1a77ae705df
@@ -26,6 +26,7 @@
#include "qgslayouteffect.h"
#include "qgslayoutundostack.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutitempage.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QUuid>
@@ -932,6 +933,37 @@ QgsLayoutPoint QgsLayoutItem::applyDataDefinedPosition( const QgsLayoutPoint &po
return QgsLayoutPoint( evaluatedX, evaluatedY, position.units() );
}

void QgsLayoutItem::applyDataDefinedOrientation( double &width, double &height, const QgsExpressionContext &context )
{
bool ok = false;
QString orientationString = mDataDefinedProperties.valueAsString( QgsLayoutObject::PaperOrientation, context, QString(), &ok );
if ( ok && !orientationString.isEmpty() )
{
QgsLayoutItemPage::Orientation orientation = QgsLayoutUtils::decodePaperOrientation( orientationString, ok );
if ( ok )
{
double heightD, widthD;
switch ( orientation )
{
case QgsLayoutItemPage::Portrait:
{
heightD = std::max( height, width );
widthD = std::min( height, width );
break;
}
case QgsLayoutItemPage::Landscape:
{
heightD = std::min( height, width );
widthD = std::max( height, width );
break;
}
}
width = widthD;
height = heightD;
}
}
}

QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )
{
if ( !mLayout )
@@ -941,7 +973,8 @@ QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )

if ( !mDataDefinedProperties.isActive( QgsLayoutObject::PresetPaperSize ) &&
!mDataDefinedProperties.isActive( QgsLayoutObject::ItemWidth ) &&
!mDataDefinedProperties.isActive( QgsLayoutObject::ItemHeight ) )
!mDataDefinedProperties.isActive( QgsLayoutObject::ItemHeight ) &&
!mDataDefinedProperties.isActive( QgsLayoutObject::PaperOrientation ) )
return size;


@@ -962,6 +995,10 @@ QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )
// highest priority is dd width/height
evaluatedWidth = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemWidth, context, evaluatedWidth );
evaluatedHeight = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemHeight, context, evaluatedHeight );

//which is finally overwritten by data defined orientation
applyDataDefinedOrientation( evaluatedWidth, evaluatedHeight, context );

return QgsLayoutSize( evaluatedWidth, evaluatedHeight, size.units() );
}

@@ -1138,6 +1138,8 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
void setScenePos( const QPointF &destinationPos );
bool shouldBlockUndoCommands() const;

void applyDataDefinedOrientation( double &width, double &height, const QgsExpressionContext &context );

friend class TestQgsLayoutItem;
friend class TestQgsLayoutView;
friend class QgsLayoutItemGroup;
@@ -53,7 +53,7 @@ class CORE_EXPORT QgsLayoutObject: public QObject, public QgsExpressionContextGe
PresetPaperSize, //!< Preset paper size for composition
PaperWidth, //!< Paper width (deprecated)
PaperHeight, //!< Paper height (deprecated)
NumPages, //!< Number of pages in composition
NumPages, //!< Number of pages in composition (deprecated)
PaperOrientation, //!< Paper orientation
//general composer item properties
PageNumber, //!< Page number for item placement
@@ -607,9 +607,26 @@ void TestQgsLayoutItem::dataDefinedSize()
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
QCOMPARE( item->rect().width(), 130.0 ); //mm
QCOMPARE( item->rect().height(), 30.0 ); //mm
// data defined orientation
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty::fromValue( "portrait" ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 3.0 );
QCOMPARE( item->sizeWithUnits().height(), 13.0 );
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
QCOMPARE( item->rect().width(), 30.0 ); //mm
QCOMPARE( item->rect().height(), 130.0 ); //mm

item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PresetPaperSize, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty::fromValue( "landscape" ) );
item->attemptResize( QgsLayoutSize( 1.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 1.5 );
QCOMPARE( item->sizeWithUnits().height(), 1.0 );
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
QCOMPARE( item->rect().width(), 15.0 ); //mm
QCOMPARE( item->rect().height(), 10.0 ); //mm
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty() );

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

0 comments on commit 5d6a509

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