1818#include " qgslayout.h"
1919#include " qgslayoutsnapper.h"
2020#include " qgslayoutpagecollection.h"
21+ #include " qgslayoutundostack.h"
2122
2223QgsLayoutPropertiesWidget::QgsLayoutPropertiesWidget ( QWidget *parent, QgsLayout *layout )
2324 : QgsPanelWidget( parent )
2425 , mLayout( layout )
2526{
27+ Q_ASSERT ( mLayout );
28+
2629 setupUi ( this );
2730 setPanelTitle ( tr ( " Layout properties" ) );
2831 blockSignals ( true );
@@ -42,6 +45,30 @@ QgsLayoutPropertiesWidget::QgsLayoutPropertiesWidget( QWidget *parent, QgsLayout
4245 connect ( mGridResolutionSpinBox , static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::gridResolutionChanged );
4346 connect ( mOffsetXSpinBox , static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::gridOffsetXChanged );
4447 connect ( mOffsetYSpinBox , static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::gridOffsetYChanged );
48+
49+ double leftMargin = mLayout ->customProperty ( QStringLiteral ( " resizeToContentsLeftMargin" ) ).toDouble ();
50+ double topMargin = mLayout ->customProperty ( QStringLiteral ( " resizeToContentsTopMargin" ) ).toDouble ();
51+ double bottomMargin = mLayout ->customProperty ( QStringLiteral ( " resizeToContentsBottomMargin" ) ).toDouble ();
52+ double rightMargin = mLayout ->customProperty ( QStringLiteral ( " resizeToContentsRightMargin" ) ).toDouble ();
53+ QgsUnitTypes::LayoutUnit marginUnit = static_cast < QgsUnitTypes::LayoutUnit >(
54+ mLayout ->customProperty ( QStringLiteral ( " imageCropMarginUnit" ), QgsUnitTypes::LayoutMillimeters ).toInt () );
55+
56+ mTopMarginSpinBox ->setValue ( topMargin );
57+ mMarginUnitsComboBox ->linkToWidget ( mTopMarginSpinBox );
58+ mRightMarginSpinBox ->setValue ( rightMargin );
59+ mMarginUnitsComboBox ->linkToWidget ( mRightMarginSpinBox );
60+ mBottomMarginSpinBox ->setValue ( bottomMargin );
61+ mMarginUnitsComboBox ->linkToWidget ( mBottomMarginSpinBox );
62+ mLeftMarginSpinBox ->setValue ( leftMargin );
63+ mMarginUnitsComboBox ->linkToWidget ( mLeftMarginSpinBox );
64+ mMarginUnitsComboBox ->setUnit ( marginUnit );
65+ mMarginUnitsComboBox ->setConverter ( &mLayout ->context ().measurementConverter () );
66+
67+ connect ( mTopMarginSpinBox , static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::resizeMarginsChanged );
68+ connect ( mRightMarginSpinBox , static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::resizeMarginsChanged );
69+ connect ( mBottomMarginSpinBox , static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::resizeMarginsChanged );
70+ connect ( mLeftMarginSpinBox , static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this , &QgsLayoutPropertiesWidget::resizeMarginsChanged );
71+ connect ( mResizePageButton , &QPushButton::clicked, this , &QgsLayoutPropertiesWidget::resizeToContents );
4572}
4673
4774void QgsLayoutPropertiesWidget::updateSnappingElements ()
@@ -101,6 +128,28 @@ void QgsLayoutPropertiesWidget::snapToleranceChanged( int tolerance )
101128 mLayout ->snapper ().setSnapTolerance ( tolerance );
102129}
103130
131+ void QgsLayoutPropertiesWidget::resizeMarginsChanged ()
132+ {
133+ mLayout ->setCustomProperty ( QStringLiteral ( " resizeToContentsLeftMargin" ), mLeftMarginSpinBox ->value () );
134+ mLayout ->setCustomProperty ( QStringLiteral ( " resizeToContentsTopMargin" ), mTopMarginSpinBox ->value () );
135+ mLayout ->setCustomProperty ( QStringLiteral ( " resizeToContentsBottomMargin" ), mBottomMarginSpinBox ->value () );
136+ mLayout ->setCustomProperty ( QStringLiteral ( " resizeToContentsRightMargin" ), mRightMarginSpinBox ->value () );
137+ mLayout ->setCustomProperty ( QStringLiteral ( " imageCropMarginUnit" ), mMarginUnitsComboBox ->unit () );
138+ }
139+
140+ void QgsLayoutPropertiesWidget::resizeToContents ()
141+ {
142+ mLayout ->undoStack ()->beginMacro ( tr ( " Resize to Contents" ) );
143+
144+ mLayout ->pageCollection ()->resizeToContents ( QgsMargins ( mLeftMarginSpinBox ->value (),
145+ mTopMarginSpinBox ->value (),
146+ mRightMarginSpinBox ->value (),
147+ mBottomMarginSpinBox ->value () ),
148+ mMarginUnitsComboBox ->unit () );
149+
150+ mLayout ->undoStack ()->endMacro ();
151+ }
152+
104153void QgsLayoutPropertiesWidget::blockSignals ( bool block )
105154{
106155 mGridResolutionSpinBox ->blockSignals ( block );
0 commit comments