Skip to content
Permalink
Browse files

Move guide collection under page collection

  • Loading branch information
nyalldawson committed Sep 4, 2017
1 parent 62dcd0d commit 44a8a514fa329cbb134f7a1ed6c93b611460fe0c
@@ -49,7 +49,7 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
, mMarginX( 1.0 )
, mMarginY( 1.0 )
, mFontColor( QColor( 0, 0, 0 ) )
, mHAlignment( Qt::AlignLeft )
, mHAlignment( Qt::AlignJustify )
, mVAlignment( Qt::AlignTop )
, mDistanceArea( nullptr )
{
@@ -26,19 +26,12 @@ QgsLayout::QgsLayout( QgsProject *project )
, mSnapper( QgsLayoutSnapper( this ) )
, mGridSettings( this )
, mPageCollection( new QgsLayoutPageCollection( this ) )
, mGuideCollection( new QgsLayoutGuideCollection( this, mPageCollection.get() ) )
, mUndoStack( new QgsLayoutUndoStack( this ) )
{
// just to make sure - this should be the default, but maybe it'll change in some future Qt version...
setBackgroundBrush( Qt::NoBrush );
}

QgsLayout::~QgsLayout()
{
// delete guide collection FIRST, since it depends on the page collection
mGuideCollection.reset();
}

void QgsLayout::initializeDefaults()
{
// default to a A4 landscape page
@@ -100,12 +93,12 @@ QgsLayoutPoint QgsLayout::convertFromLayoutUnits( const QPointF &point, const Qg

QgsLayoutGuideCollection &QgsLayout::guides()
{
return *mGuideCollection;
return mPageCollection->guides();
}

const QgsLayoutGuideCollection &QgsLayout::guides() const
{
return *mGuideCollection;
return mPageCollection->guides();
}

QgsExpressionContext QgsLayout::createExpressionContext() const
@@ -285,7 +278,6 @@ QDomElement QgsLayout::writeXml( QDomDocument &document, const QgsReadWriteConte
save( &mSnapper );
save( &mGridSettings );
save( mPageCollection.get() );
save( mGuideCollection.get() );

writeXmlLayoutSettings( element, document, context );
return element;
@@ -314,7 +306,6 @@ bool QgsLayout::readXml( const QDomElement &layoutElement, const QDomDocument &d
readXmlLayoutSettings( layoutElement, document, context );

restore( mPageCollection.get() );
restore( mGuideCollection.get() );
restore( &mSnapper );
restore( &mGridSettings );

@@ -60,8 +60,6 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/
QgsLayout( QgsProject *project );

~QgsLayout();

/**
* Initializes an empty layout, e.g. by adding a default page to the layout. This should be called after creating
* a new layout.
@@ -366,7 +364,6 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
QgsLayoutGridSettings mGridSettings;

std::unique_ptr< QgsLayoutPageCollection > mPageCollection;
std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;
std::unique_ptr< QgsLayoutUndoStack > mUndoStack;

//! Writes only the layout settings (not member settings like grid settings, etc) to XML
@@ -286,7 +286,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
mLayout->undoStack()->beginCommand( this, tr( "Guide moved" ), Move + index.row() );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide moved" ), Move + index.row() );
whileBlocking( guide )->setPosition( m );
guide->update();
mLayout->undoStack()->endCommand();
@@ -302,7 +302,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
mLayout->undoStack()->beginCommand( this, tr( "Guide moved" ), Move + index.row() );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide moved" ), Move + index.row() );
whileBlocking( guide )->setPosition( m );
guide->update();
mLayout->undoStack()->endCommand();
@@ -317,7 +317,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant
if ( !ok )
return false;

mLayout->undoStack()->beginCommand( this, tr( "Guide moved" ), Move + index.row() );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide moved" ), Move + index.row() );
whileBlocking( guide )->setLayoutPosition( newPos );
mLayout->undoStack()->endCommand();
emit dataChanged( index, index, QVector<int>() << role );
@@ -333,7 +333,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant

QgsLayoutMeasurement m = guide->position();
m.setUnits( static_cast< QgsUnitTypes::LayoutUnit >( units ) );
mLayout->undoStack()->beginCommand( this, tr( "Guide moved" ), Move + index.row() );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide moved" ), Move + index.row() );
whileBlocking( guide )->setPosition( m );
guide->update();
mLayout->undoStack()->endCommand();
@@ -369,7 +369,7 @@ bool QgsLayoutGuideCollection::removeRows( int row, int count, const QModelIndex
return false;

if ( !mBlockUndoCommands )
mLayout->undoStack()->beginCommand( this, tr( "Guide(s) removed" ), Remove + row );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide(s) removed" ), Remove + row );
beginRemoveRows( parent, row, row + count - 1 );
for ( int i = 0; i < count; ++ i )
{
@@ -386,7 +386,7 @@ void QgsLayoutGuideCollection::addGuide( QgsLayoutGuide *guide )
guide->setLayout( mLayout );

if ( !mBlockUndoCommands )
mLayout->undoStack()->beginCommand( this, tr( "Guide created" ) );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide created" ) );
beginInsertRows( QModelIndex(), mGuides.count(), mGuides.count() );
mGuides.append( guide );
endInsertRows();
@@ -420,7 +420,7 @@ void QgsLayoutGuideCollection::setGuideLayoutPosition( QgsLayoutGuide *guide, do

void QgsLayoutGuideCollection::clear()
{
mLayout->undoStack()->beginCommand( this, tr( "Guides cleared" ) );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guides cleared" ) );
beginResetModel();
qDeleteAll( mGuides );
mGuides.clear();
@@ -430,7 +430,7 @@ void QgsLayoutGuideCollection::clear()

void QgsLayoutGuideCollection::applyGuidesToAllOtherPages( int sourcePage )
{
mLayout->undoStack()->beginCommand( this, tr( "Guides applied" ) );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guides applied" ) );
mBlockUndoCommands = true;
QgsLayoutItemPage *page = mPageCollection->page( sourcePage );
// remove other page's guides
@@ -499,7 +499,7 @@ bool QgsLayoutGuideCollection::visible() const

void QgsLayoutGuideCollection::setVisible( bool visible )
{
mLayout->undoStack()->beginCommand( this, tr( "Guide visibility changed" ) );
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide visibility changed" ) );
mGuidesVisible = visible;
mLayout->undoStack()->endCommand();
update();
@@ -528,6 +528,7 @@ bool QgsLayoutGuideCollection::writeXml( QDomElement &parentElement, QDomDocumen
guideElement.setAttribute( QStringLiteral( "units" ), QgsUnitTypes::encodeUnit( guide->position().units() ) );
element.appendChild( guideElement );
}

parentElement.appendChild( element );
return true;
}
@@ -24,6 +24,7 @@
QgsLayoutPageCollection::QgsLayoutPageCollection( QgsLayout *layout )
: QObject( layout )
, mLayout( layout )
, mGuideCollection( new QgsLayoutGuideCollection( layout, this ) )
{
createDefaultPageStyleSymbol();
}
@@ -151,6 +152,8 @@ bool QgsLayoutPageCollection::writeXml( QDomElement &parentElement, QDomDocument
page->writeXml( element, document, context );
}

mGuideCollection->writeXml( element, document, context );

parentElement.appendChild( element );
return true;
}
@@ -168,6 +171,8 @@ bool QgsLayoutPageCollection::readXml( const QDomElement &e, const QDomDocument
return false;
}

mBlockUndoCommands = true;

int i = 0;
for ( QgsLayoutItemPage *page : qgsAsConst( mPages ) )
{
@@ -195,9 +200,23 @@ bool QgsLayoutPageCollection::readXml( const QDomElement &e, const QDomDocument
}

reflow();

mGuideCollection->readXml( element, document, context );

mBlockUndoCommands = false;
return true;
}

QgsLayoutGuideCollection &QgsLayoutPageCollection::guides()
{
return *mGuideCollection;
}

const QgsLayoutGuideCollection &QgsLayoutPageCollection::guides() const
{
return *mGuideCollection;
}

void QgsLayoutPageCollection::redraw()
{
Q_FOREACH ( QgsLayoutItemPage *page, mPages )
@@ -257,16 +276,19 @@ QList<int> QgsLayoutPageCollection::visiblePageNumbers( QRectF region ) const

void QgsLayoutPageCollection::addPage( QgsLayoutItemPage *page )
{
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
if ( !mBlockUndoCommands )
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
mPages.append( page );
mLayout->addItem( page );
reflow();
mLayout->undoStack()->endCommand();
if ( !mBlockUndoCommands )
mLayout->undoStack()->endCommand();
}

void QgsLayoutPageCollection::insertPage( QgsLayoutItemPage *page, int beforePage )
{
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
if ( !mBlockUndoCommands )
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );

if ( beforePage < 0 )
beforePage = 0;
@@ -281,83 +303,51 @@ void QgsLayoutPageCollection::insertPage( QgsLayoutItemPage *page, int beforePag
}
mLayout->addItem( page );
reflow();
mLayout->undoStack()->endCommand();
if ( ! mBlockUndoCommands )
mLayout->undoStack()->endCommand();
}

///@cond PRIVATE

class QgsLayoutItemDeletePageUndoCommand: public QgsLayoutItemDeleteUndoCommand
{
public:

QgsLayoutItemDeletePageUndoCommand( QgsLayoutItemPage *page, const QString &text, int id = 0, QUndoCommand *parent SIP_TRANSFERTHIS = nullptr )
: QgsLayoutItemDeleteUndoCommand( page, text, id, parent )
{}

void redo() override
{
if ( mFirstRun )
{
mFirstRun = false;
return;
}

// remove from page collection
QgsLayoutItemPage *page = dynamic_cast< QgsLayoutItemPage *>( layout()->itemByUuid( itemUuid() ) );
layout()->pageCollection()->takePage( page );

QgsLayoutItemDeleteUndoCommand::redo();
layout()->pageCollection()->reflow();
}

void undo() override
{
QgsLayoutItemDeleteUndoCommand::undo();
layout()->pageCollection()->reflow();
}

QgsLayoutItem *recreateItem( int, QgsLayout *layout ) override
{
QgsLayoutItemPage *page = new QgsLayoutItemPage( layout );
layout->pageCollection()->addPage( page );
return page;
}

};

///@endcond

void QgsLayoutPageCollection::deletePage( int pageNumber )
{
if ( pageNumber < 0 || pageNumber >= mPages.count() )
return;

mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
if ( !mBlockUndoCommands )
{
mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
}
emit pageAboutToBeRemoved( pageNumber );
QgsLayoutItemPage *page = mPages.takeAt( pageNumber );
//mLayout->undoStack()->stack()->push( new QgsLayoutItemDeletePageUndoCommand( page, tr( "Remove page" ) ) );
mLayout->removeItem( page );
page->deleteLater();
reflow();
mLayout->undoStack()->endCommand();
mLayout->undoStack()->endMacro();
if ( ! mBlockUndoCommands )
{
mLayout->undoStack()->endCommand();
mLayout->undoStack()->endMacro();
}
}

void QgsLayoutPageCollection::deletePage( QgsLayoutItemPage *page )
{
if ( !mPages.contains( page ) )
return;

mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
if ( !mBlockUndoCommands )
{
mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
}
emit pageAboutToBeRemoved( mPages.indexOf( page ) );
//mLayout->undoStack()->stack()->push( new QgsLayoutItemDeletePageUndoCommand( page, tr( "Remove page" ) ) );
mPages.removeAll( page );
page->deleteLater();
reflow();
mLayout->undoStack()->endCommand();
mLayout->undoStack()->endMacro();
if ( !mBlockUndoCommands )
{
mLayout->undoStack()->endCommand();
mLayout->undoStack()->endMacro();
}
}

QgsLayoutItemPage *QgsLayoutPageCollection::takePage( QgsLayoutItemPage *page )
@@ -26,6 +26,7 @@
#include <memory>

class QgsLayout;
class QgsLayoutGuideCollection;

/**
* \ingroup core
@@ -227,6 +228,16 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
*/
bool readXml( const QDomElement &collectionElement, const QDomDocument &document, const QgsReadWriteContext &context ) override;

/**
* Returns a reference to the collection's guide collection, which manages page snap guides.
*/
QgsLayoutGuideCollection &guides();

/**
* Returns a reference to the collection's guide collection, which manages page snap guides.
*/
SIP_SKIP const QgsLayoutGuideCollection &guides() const;

public slots:

/**
@@ -253,11 +264,15 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri

QgsLayout *mLayout = nullptr;

std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;

//! Symbol for drawing pages
std::unique_ptr< QgsFillSymbol > mPageStyleSymbol;

QList< QgsLayoutItemPage * > mPages;

bool mBlockUndoCommands = false;

void createDefaultPageStyleSymbol();

friend class QgsLayoutPageCollectionUndoCommand;

0 comments on commit 44a8a51

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