Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Functional page properties widget
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 20029c2 commit a4113fe
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/app/layout/qgslayoutpagepropertieswidget.cpp
Expand Up @@ -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();
}
6 changes: 6 additions & 0 deletions src/app/layout/qgslayoutpagepropertieswidget.h
Expand Up @@ -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;
Expand Down

0 comments on commit a4113fe

Please sign in to comment.