Skip to content

Commit

Permalink
Move guide collection under page collection
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 4, 2017
1 parent 62dcd0d commit 44a8a51
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
13 changes: 2 additions & 11 deletions src/core/layout/qgslayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );

Expand Down
3 changes: 0 additions & 3 deletions src/core/layout/qgslayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions src/core/layout/qgslayoutguidecollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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 );
Expand All @@ -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();
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
104 changes: 47 additions & 57 deletions src/core/layout/qgslayoutpagecollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
QgsLayoutPageCollection::QgsLayoutPageCollection( QgsLayout *layout )
: QObject( layout )
, mLayout( layout )
, mGuideCollection( new QgsLayoutGuideCollection( layout, this ) )
{
createDefaultPageStyleSymbol();
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -168,6 +171,8 @@ bool QgsLayoutPageCollection::readXml( const QDomElement &e, const QDomDocument
return false;
}

mBlockUndoCommands = true;

int i = 0;
for ( QgsLayoutItemPage *page : qgsAsConst( mPages ) )
{
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand All @@ -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 )
Expand Down
15 changes: 15 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <memory>

class QgsLayout;
class QgsLayoutGuideCollection;

/**
* \ingroup core
Expand Down Expand Up @@ -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:

/**
Expand All @@ -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;
Expand Down

0 comments on commit 44a8a51

Please sign in to comment.