Skip to content
Permalink
Browse files

Functional page properties widget

  • Loading branch information
nyalldawson committed Jul 22, 2017
1 parent 20029c2 commit a4113fe51dec41dc51b16fa8c99100467ec5f751
Showing with 111 additions and 0 deletions.
  1. +105 −0 src/app/layout/qgslayoutpagepropertieswidget.cpp
  2. +6 −0 src/app/layout/qgslayoutpagepropertieswidget.h
@@ -14,12 +14,117 @@
***************************************************************************/

#include "qgslayoutpagepropertieswidget.h"
#include "qgsapplication.h"
#include "qgspagesizeregistry.h"
#include "qgslayoutitempage.h"
#include "qgslayout.h"

QgsLayoutPagePropertiesWidget::QgsLayoutPagePropertiesWidget( QWidget *parent, QgsLayoutItem *layoutItem )
: QgsLayoutItemBaseWidget( parent, layoutItem )
, mPage( static_cast< QgsLayoutItemPage *>( layoutItem ) )
{
setupUi( this );

mPageOrientationComboBox->addItem( tr( "Portrait" ), QgsLayoutItemPage::Portrait );
mPageOrientationComboBox->addItem( tr( "Landscape" ), QgsLayoutItemPage::Landscape );

Q_FOREACH ( const QgsPageSize &size, QgsApplication::pageSizeRegistry()->entries() )
{
mPageSizeComboBox->addItem( size.displayName, size.name );
}
mPageSizeComboBox->addItem( tr( "Custom" ) );
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->count() - 1 );
//TODO - match to preset page sizes

mWidthSpin->setValue( mPage->pageSize().width() );
mHeightSpin->setValue( mPage->pageSize().height() );
mSizeUnitsComboBox->setUnit( mPage->pageSize().units() );

mPageOrientationComboBox->setCurrentIndex( mPageOrientationComboBox->findData( mPage->orientation() ) );

mSizeUnitsComboBox->linkToWidget( mWidthSpin );
mSizeUnitsComboBox->linkToWidget( mHeightSpin );
mSizeUnitsComboBox->setConverter( &mPage->layout()->context().measurementConverter() );

mLockAspectRatio->setWidthSpinBox( mWidthSpin );
mLockAspectRatio->setHeightSpinBox( mHeightSpin );

connect( mPageSizeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::pageSizeChanged );
connect( mPageOrientationComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutPagePropertiesWidget::orientationChanged );

connect( mWidthSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );
connect( mHeightSpin, static_cast< void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutPagePropertiesWidget::updatePageSize );

}

void QgsLayoutPagePropertiesWidget::pageSizeChanged( int )
{
if ( mPageSizeComboBox->currentData().toString().isEmpty() )
{
//custom size
mWidthSpin->setEnabled( true );
mHeightSpin->setEnabled( true );
mLockAspectRatio->setEnabled( true );
mSizeUnitsComboBox->setEnabled( true );
mPageOrientationComboBox->setEnabled( false );
}
else
{
mWidthSpin->setEnabled( false );
mHeightSpin->setEnabled( false );
mLockAspectRatio->setEnabled( false );
mLockAspectRatio->setLocked( false );
mSizeUnitsComboBox->setEnabled( false );
mPageOrientationComboBox->setEnabled( true );
QgsPageSize size = QgsApplication::pageSizeRegistry()->find( mPageSizeComboBox->currentData().toString() ).value( 0 );
QgsLayoutSize convertedSize = mConverter.convert( size.size, mSizeUnitsComboBox->unit() );
switch ( mPageOrientationComboBox->currentData().toInt() )
{
case QgsLayoutItemPage::Landscape:
mWidthSpin->setValue( convertedSize.height() );
mHeightSpin->setValue( convertedSize.width() );
break;

case QgsLayoutItemPage::Portrait:
mWidthSpin->setValue( convertedSize.width() );
mHeightSpin->setValue( convertedSize.height() );
break;
}
}
updatePageSize();
}

void QgsLayoutPagePropertiesWidget::orientationChanged( int )
{
if ( mPageSizeComboBox->currentData().toString().isEmpty() )
return;

double width = mWidthSpin->value();
double height = mHeightSpin->value();
switch ( mPageOrientationComboBox->currentData().toInt() )
{
case QgsLayoutItemPage::Landscape:
if ( width < height )
{
whileBlocking( mWidthSpin )->setValue( height );
whileBlocking( mHeightSpin )->setValue( width );
}
break;

case QgsLayoutItemPage::Portrait:
if ( width > height )
{
whileBlocking( mWidthSpin )->setValue( height );
whileBlocking( mHeightSpin )->setValue( width );
}
break;
}

updatePageSize();
}

void QgsLayoutPagePropertiesWidget::updatePageSize()
{
mPage->setPageSize( QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() ) );
mPage->layout()->pageCollection()->reflow();
}
@@ -41,6 +41,12 @@ class QgsLayoutPagePropertiesWidget : public QgsLayoutItemBaseWidget, private Ui
*/
QgsLayoutPagePropertiesWidget( QWidget *parent, QgsLayoutItem *page );

private slots:

void pageSizeChanged( int index );
void orientationChanged( int index );
void updatePageSize();

private:

QgsLayoutItemPage *mPage = nullptr;

0 comments on commit a4113fe

Please sign in to comment.
You can’t perform that action at this time.