Skip to content

Commit

Permalink
Add a dialog for inserting new pages into a layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 3648308 commit 5cfc9cc
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 33 deletions.
7 changes: 6 additions & 1 deletion python/core/layout/qgspagesizeregistry.sip
Expand Up @@ -23,7 +23,7 @@ class QgsPageSize

QgsPageSize();

QgsPageSize( const QString &name, const QgsLayoutSize &size );
QgsPageSize( const QString &name, const QgsLayoutSize &size, const QString &displayName = QString() );
%Docstring
Constructor for QgsPageSize, accepting the ``name`` of the page size and
page ``size``.
Expand All @@ -42,6 +42,11 @@ Name of page size
QgsLayoutSize size;
%Docstring
Page size
%End

QString displayName;
%Docstring
Translated page name
%End

bool operator==( const QgsPageSize &other ) const;
Expand Down
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -155,6 +155,7 @@ SET(QGIS_APP_SRCS
composer/qgscompositionwidget.cpp
composer/qgsatlascompositionwidget.cpp

layout/qgslayoutaddpagesdialog.cpp
layout/qgslayoutdesignerdialog.cpp

locator/qgsinbuiltlocatorfilters.cpp
Expand Down Expand Up @@ -332,6 +333,7 @@ SET (QGIS_APP_MOC_HDRS
composer/qgscompositionwidget.h
composer/qgsatlascompositionwidget.h

layout/qgslayoutaddpagesdialog.h
layout/qgslayoutdesignerdialog.h

locator/qgsinbuiltlocatorfilters.h
Expand Down
145 changes: 145 additions & 0 deletions src/app/layout/qgslayoutaddpagesdialog.cpp
@@ -0,0 +1,145 @@
/***************************************************************************
qgslayoutaddpagesdialog.cpp
---------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayoutaddpagesdialog.h"
#include "qgspagesizeregistry.h"
#include "qgssettings.h"
#include "qgslayout.h"


QgsLayoutAddPagesDialog::QgsLayoutAddPagesDialog( QWidget *parent, Qt::WindowFlags flags )
: QDialog( parent, flags )
{
setupUi( this );

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

Q_FOREACH ( const QgsPageSize &size, QgsApplication::pageSizeRegistry()->entries() )
{
mPageSizeComboBox->addItem( size.displayName, size.name );
}
mPageSizeComboBox->addItem( tr( "Custom" ) );
mPageSizeComboBox->setCurrentIndex( mPageSizeComboBox->findData( QStringLiteral( "A4" ) ) );
pageSizeChanged( mPageSizeComboBox->currentIndex() );
orientationChanged( 1 );

mSizeUnitsComboBox->linkToWidget( mWidthSpin );
mSizeUnitsComboBox->linkToWidget( mHeightSpin );

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

connect( mPositionComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutAddPagesDialog::positionChanged );
mExistingPageSpinBox->setEnabled( false );

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

void QgsLayoutAddPagesDialog::setLayout( QgsLayout *layout )
{
mSizeUnitsComboBox->setConverter( &layout->context().measurementConverter() );
mExistingPageSpinBox->setMaximum( layout->pageCollection()->pageCount() );
}

int QgsLayoutAddPagesDialog::numberPages() const
{
return mPagesSpinBox->value();
}

QgsLayoutAddPagesDialog::PagePosition QgsLayoutAddPagesDialog::pagePosition() const
{
return static_cast< PagePosition >( mPositionComboBox->currentIndex() );
}

int QgsLayoutAddPagesDialog::beforePage() const
{
return mExistingPageSpinBox->value();
}

QgsLayoutSize QgsLayoutAddPagesDialog::pageSize() const
{
return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
}

void QgsLayoutAddPagesDialog::positionChanged( int index )
{
mExistingPageSpinBox->setEnabled( index != 2 );
}

void QgsLayoutAddPagesDialog::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 );
switch ( mPageOrientationComboBox->currentData().toInt() )
{
case QgsLayoutItemPage::Landscape:
mWidthSpin->setValue( size.size.height() );
mHeightSpin->setValue( size.size.width() );
break;

case QgsLayoutItemPage::Portrait:
mWidthSpin->setValue( size.size.width() );
mHeightSpin->setValue( size.size.height() );
break;
}
mSizeUnitsComboBox->setUnit( size.size.units() );
}
}

void QgsLayoutAddPagesDialog::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;
}
}
83 changes: 83 additions & 0 deletions src/app/layout/qgslayoutaddpagesdialog.h
@@ -0,0 +1,83 @@
/***************************************************************************
qgslayoutaddpagesdialog.h
-------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYOUTADDPAGESDIALOG_H
#define QGSLAYOUTADDPAGESDIALOG_H

#include "qgis.h"
#include "qgis_gui.h"
#include "ui_qgslayoutnewpagedialog.h"

#include "qgslayoutsize.h"
#include "qgslayoutpoint.h"
#include "qgslayoutitem.h"

/**
* A dialog for configuring properties of new pages to be added to a layout
*/
class QgsLayoutAddPagesDialog : public QDialog, private Ui::QgsLayoutNewPageDialog
{
Q_OBJECT

public:

enum PagePosition
{
BeforePage,
AfterPage,
AtEnd
};

/**
* Constructor for QgsLayoutAddPagesDialog.
*/
QgsLayoutAddPagesDialog( QWidget *parent = nullptr, Qt::WindowFlags flags = 0 );

/**
* Sets the \a layout associated with the dialog. This allows the dialog
* to retrieve properties from the layout and perform tasks like automatic
* conversion of units.
*/
void setLayout( QgsLayout *layout );

/**
* Returns the number of pages to insert.
*/
int numberPages() const;

/**
* Returns the position at which to insert the new pages.
*/
PagePosition pagePosition() const;

/**
* Returns the page number for which new pages should be inserted before/after.
*/
int beforePage() const;

/**
* Returns the desired page size.
*/
QgsLayoutSize pageSize() const;

private slots:

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

};

#endif // QGSLAYOUTADDPAGESDIALOG_H
35 changes: 35 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -29,6 +29,7 @@
#include "qgsgui.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutruler.h"
#include "qgslayoutaddpagesdialog.h"
#include <QShortcut>
#include <QComboBox>
#include <QLineEdit>
Expand Down Expand Up @@ -155,6 +156,8 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
connect( mActionZoomActual, &QAction::triggered, mView, &QgsLayoutView::zoomActual );
connect( mActionZoomToWidth, &QAction::triggered, mView, &QgsLayoutView::zoomWidth );

connect( mActionAddPages, &QAction::triggered, this, &QgsLayoutDesignerDialog::addPages );

//create status bar labels
mStatusCursorXLabel = new QLabel( mStatusBar );
mStatusCursorXLabel->setMinimumWidth( 100 );
Expand Down Expand Up @@ -454,6 +457,38 @@ void QgsLayoutDesignerDialog::toggleFullScreen( bool enabled )
}
}

void QgsLayoutDesignerDialog::addPages()
{
QgsLayoutAddPagesDialog dlg( this );
dlg.setLayout( mLayout );

if ( dlg.exec() )
{
int firstPagePosition = dlg.beforePage() - 1;
switch ( dlg.pagePosition() )
{
case QgsLayoutAddPagesDialog::BeforePage:
break;

case QgsLayoutAddPagesDialog::AfterPage:
firstPagePosition = firstPagePosition + 1;
break;

case QgsLayoutAddPagesDialog::AtEnd:
firstPagePosition = mLayout->pageCollection()->pageCount();
break;

}

for ( int i = 0; i < dlg.numberPages(); ++i )
{
QgsLayoutItemPage *page = new QgsLayoutItemPage( mLayout );
page->setPageSize( dlg.pageSize() );
mLayout->pageCollection()->insertPage( page, firstPagePosition + i );
}
}
}

QgsLayoutView *QgsLayoutDesignerDialog::view()
{
return mView;
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -133,6 +133,8 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner

void toggleFullScreen( bool enabled );

void addPages();

private:

QgsAppLayoutDesignerInterface *mInterface = nullptr;
Expand Down

0 comments on commit 5cfc9cc

Please sign in to comment.