|
14 | 14 | ***************************************************************************/
|
15 | 15 |
|
16 | 16 | #include "qgslayoutpagepropertieswidget.h"
|
| 17 | +#include "qgsapplication.h" |
| 18 | +#include "qgspagesizeregistry.h" |
17 | 19 | #include "qgslayoutitempage.h"
|
| 20 | +#include "qgslayout.h" |
18 | 21 |
|
19 | 22 | QgsLayoutPagePropertiesWidget::QgsLayoutPagePropertiesWidget( QWidget *parent, QgsLayoutItem *layoutItem )
|
20 | 23 | : QgsLayoutItemBaseWidget( parent, layoutItem )
|
21 | 24 | , mPage( static_cast< QgsLayoutItemPage *>( layoutItem ) )
|
22 | 25 | {
|
23 | 26 | setupUi( this );
|
24 | 27 |
|
| 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(); |
25 | 130 | }
|
0 commit comments