Skip to content

Commit

Permalink
Restore data defined page orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent 65f4c4a commit 5d6a509
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
39 changes: 38 additions & 1 deletion src/core/layout/qgslayoutitem.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgslayouteffect.h"
#include "qgslayoutundostack.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutitempage.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QUuid>
Expand Down Expand Up @@ -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 )
Expand All @@ -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;


Expand All @@ -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() );
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutobject.h
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -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" ) ) );
Expand Down

0 comments on commit 5d6a509

Please sign in to comment.