Skip to content

Commit 5d6a509

Browse files
committed
Restore data defined page orientation
1 parent 65f4c4a commit 5d6a509

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/core/layout/qgslayoutitem.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgslayouteffect.h"
2727
#include "qgslayoutundostack.h"
2828
#include "qgslayoutpagecollection.h"
29+
#include "qgslayoutitempage.h"
2930
#include <QPainter>
3031
#include <QStyleOptionGraphicsItem>
3132
#include <QUuid>
@@ -932,6 +933,37 @@ QgsLayoutPoint QgsLayoutItem::applyDataDefinedPosition( const QgsLayoutPoint &po
932933
return QgsLayoutPoint( evaluatedX, evaluatedY, position.units() );
933934
}
934935

936+
void QgsLayoutItem::applyDataDefinedOrientation( double &width, double &height, const QgsExpressionContext &context )
937+
{
938+
bool ok = false;
939+
QString orientationString = mDataDefinedProperties.valueAsString( QgsLayoutObject::PaperOrientation, context, QString(), &ok );
940+
if ( ok && !orientationString.isEmpty() )
941+
{
942+
QgsLayoutItemPage::Orientation orientation = QgsLayoutUtils::decodePaperOrientation( orientationString, ok );
943+
if ( ok )
944+
{
945+
double heightD, widthD;
946+
switch ( orientation )
947+
{
948+
case QgsLayoutItemPage::Portrait:
949+
{
950+
heightD = std::max( height, width );
951+
widthD = std::min( height, width );
952+
break;
953+
}
954+
case QgsLayoutItemPage::Landscape:
955+
{
956+
heightD = std::min( height, width );
957+
widthD = std::max( height, width );
958+
break;
959+
}
960+
}
961+
width = widthD;
962+
height = heightD;
963+
}
964+
}
965+
}
966+
935967
QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )
936968
{
937969
if ( !mLayout )
@@ -941,7 +973,8 @@ QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )
941973

942974
if ( !mDataDefinedProperties.isActive( QgsLayoutObject::PresetPaperSize ) &&
943975
!mDataDefinedProperties.isActive( QgsLayoutObject::ItemWidth ) &&
944-
!mDataDefinedProperties.isActive( QgsLayoutObject::ItemHeight ) )
976+
!mDataDefinedProperties.isActive( QgsLayoutObject::ItemHeight ) &&
977+
!mDataDefinedProperties.isActive( QgsLayoutObject::PaperOrientation ) )
945978
return size;
946979

947980

@@ -962,6 +995,10 @@ QgsLayoutSize QgsLayoutItem::applyDataDefinedSize( const QgsLayoutSize &size )
962995
// highest priority is dd width/height
963996
evaluatedWidth = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemWidth, context, evaluatedWidth );
964997
evaluatedHeight = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ItemHeight, context, evaluatedHeight );
998+
999+
//which is finally overwritten by data defined orientation
1000+
applyDataDefinedOrientation( evaluatedWidth, evaluatedHeight, context );
1001+
9651002
return QgsLayoutSize( evaluatedWidth, evaluatedHeight, size.units() );
9661003
}
9671004

src/core/layout/qgslayoutitem.h

+2
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,8 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
11381138
void setScenePos( const QPointF &destinationPos );
11391139
bool shouldBlockUndoCommands() const;
11401140

1141+
void applyDataDefinedOrientation( double &width, double &height, const QgsExpressionContext &context );
1142+
11411143
friend class TestQgsLayoutItem;
11421144
friend class TestQgsLayoutView;
11431145
friend class QgsLayoutItemGroup;

src/core/layout/qgslayoutobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CORE_EXPORT QgsLayoutObject: public QObject, public QgsExpressionContextGe
5353
PresetPaperSize, //!< Preset paper size for composition
5454
PaperWidth, //!< Paper width (deprecated)
5555
PaperHeight, //!< Paper height (deprecated)
56-
NumPages, //!< Number of pages in composition
56+
NumPages, //!< Number of pages in composition (deprecated)
5757
PaperOrientation, //!< Paper orientation
5858
//general composer item properties
5959
PageNumber, //!< Page number for item placement

tests/src/core/testqgslayoutitem.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,26 @@ void TestQgsLayoutItem::dataDefinedSize()
607607
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
608608
QCOMPARE( item->rect().width(), 130.0 ); //mm
609609
QCOMPARE( item->rect().height(), 30.0 ); //mm
610+
// data defined orientation
611+
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty::fromValue( "portrait" ) );
612+
item->attemptResize( QgsLayoutSize( 7.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
613+
QCOMPARE( item->sizeWithUnits().width(), 3.0 );
614+
QCOMPARE( item->sizeWithUnits().height(), 13.0 );
615+
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
616+
QCOMPARE( item->rect().width(), 30.0 ); //mm
617+
QCOMPARE( item->rect().height(), 130.0 ); //mm
618+
610619
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
611620
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
612621
item->dataDefinedProperties().setProperty( QgsLayoutObject::PresetPaperSize, QgsProperty() );
622+
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty::fromValue( "landscape" ) );
623+
item->attemptResize( QgsLayoutSize( 1.0, 1.50, QgsUnitTypes::LayoutCentimeters ) );
624+
QCOMPARE( item->sizeWithUnits().width(), 1.5 );
625+
QCOMPARE( item->sizeWithUnits().height(), 1.0 );
626+
QCOMPARE( item->sizeWithUnits().units(), QgsUnitTypes::LayoutCentimeters );
627+
QCOMPARE( item->rect().width(), 15.0 ); //mm
628+
QCOMPARE( item->rect().height(), 10.0 ); //mm
629+
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty() );
613630

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

0 commit comments

Comments
 (0)