Skip to content

Commit a4113fe

Browse files
committed
Functional page properties widget
1 parent 20029c2 commit a4113fe

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/app/layout/qgslayoutpagepropertieswidget.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,117 @@
1414
***************************************************************************/
1515

1616
#include "qgslayoutpagepropertieswidget.h"
17+
#include "qgsapplication.h"
18+
#include "qgspagesizeregistry.h"
1719
#include "qgslayoutitempage.h"
20+
#include "qgslayout.h"
1821

1922
QgsLayoutPagePropertiesWidget::QgsLayoutPagePropertiesWidget( QWidget *parent, QgsLayoutItem *layoutItem )
2023
: QgsLayoutItemBaseWidget( parent, layoutItem )
2124
, mPage( static_cast< QgsLayoutItemPage *>( layoutItem ) )
2225
{
2326
setupUi( this );
2427

28+
mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
29+
mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );
30+
31+
Q_FOREACH ( const QgsPageSize &size, QgsApplication::pageSizeRegistry()->entries() )
32+
{
33+
mPageSizeComboBox->addItem( size.displayName, size.name );
34+
}
35+
mPageSizeComboBox->addItem( tr( "Custom" ) );
36+
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->count() - 1 );
37+
//TODO - match to preset page sizes
38+
39+
mWidthSpin->setValue( mPage->pageSize().width() );
40+
mHeightSpin->setValue( mPage->pageSize().height() );
41+
mSizeUnitsComboBox->setUnit( mPage->pageSize().units() );
42+
43+
mPageOrientationComboBox->setCurrentIndex( mPageOrientationComboBox->findData( mPage->orientation() ) );
44+
45+
mSizeUnitsComboBox->linkToWidget( mWidthSpin );
46+
mSizeUnitsComboBox->linkToWidget( mHeightSpin );
47+
mSizeUnitsComboBox->setConverter( &mPage->layout()->context().measurementConverter() );
48+
49+
mLockAspectRatio->setWidthSpinBox( mWidthSpin );
50+
mLockAspectRatio->setHeightSpinBox( mHeightSpin );
51+
52+
connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::pageSizeChanged );
53+
connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::orientationChanged );
54+
55+
connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
56+
connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
57+
58+
}
59+
60+
void QgsLayoutPagePropertiesWidget::pageSizeChanged( int )
61+
{
62+
if ( mPageSizeComboBox->currentData().toString().isEmpty() )
63+
{
64+
//custom size
65+
mWidthSpin->setEnabled( true );
66+
mHeightSpin->setEnabled( true );
67+
mLockAspectRatio->setEnabled( true );
68+
mSizeUnitsComboBox->setEnabled( true );
69+
mPageOrientationComboBox->setEnabled( false );
70+
}
71+
else
72+
{
73+
mWidthSpin->setEnabled( false );
74+
mHeightSpin->setEnabled( false );
75+
mLockAspectRatio->setEnabled( false );
76+
mLockAspectRatio->setLocked( false );
77+
mSizeUnitsComboBox->setEnabled( false );
78+
mPageOrientationComboBox->setEnabled( true );
79+
QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
80+
QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
81+
switch ( mPageOrientationComboBox->currentData().toInt() )
82+
{
83+
case QgsLayoutItemPage::Landscape:
84+
mWidthSpin->setValue( convertedSize.height() );
85+
mHeightSpin->setValue( convertedSize.width() );
86+
break;
87+
88+
case QgsLayoutItemPage::Portrait:
89+
mWidthSpin->setValue( convertedSize.width() );
90+
mHeightSpin->setValue( convertedSize.height() );
91+
break;
92+
}
93+
}
94+
updatePageSize();
95+
}
96+
97+
void QgsLayoutPagePropertiesWidget::orientationChanged( int )
98+
{
99+
if ( mPageSizeComboBox->currentData().toString().isEmpty() )
100+
return;
101+
102+
double width = mWidthSpin->value();
103+
double height = mHeightSpin->value();
104+
switch ( mPageOrientationComboBox->currentData().toInt() )
105+
{
106+
case QgsLayoutItemPage::Landscape:
107+
if ( width < height )
108+
{
109+
whileBlocking( mWidthSpin )->setValue( height );
110+
whileBlocking( mHeightSpin )->setValue( width );
111+
}
112+
break;
113+
114+
case QgsLayoutItemPage::Portrait:
115+
if ( width > height )
116+
{
117+
whileBlocking( mWidthSpin )->setValue( height );
118+
whileBlocking( mHeightSpin )->setValue( width );
119+
}
120+
break;
121+
}
122+
123+
updatePageSize();
124+
}
125+
126+
void QgsLayoutPagePropertiesWidget::updatePageSize()
127+
{
128+
mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
129+
mPage->layout()->pageCollection()->reflow();
25130
}

src/app/layout/qgslayoutpagepropertieswidget.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class QgsLayoutPagePropertiesWidget : public QgsLayoutItemBaseWidget, private Ui
4141
*/
4242
QgsLayoutPagePropertiesWidget( QWidget *parent, QgsLayoutItem *page );
4343

44+
private slots:
45+
46+
void pageSizeChanged( int index );
47+
void orientationChanged( int index );
48+
void updatePageSize();
49+
4450
private:
4551

4652
QgsLayoutItemPage *mPage = nullptr;

0 commit comments

Comments
 (0)