Skip to content

Commit 44a8a51

Browse files
committedSep 4, 2017
Move guide collection under page collection
1 parent 62dcd0d commit 44a8a51

File tree

6 files changed

+75
-81
lines changed

6 files changed

+75
-81
lines changed
 

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
4949
, mMarginX( 1.0 )
5050
, mMarginY( 1.0 )
5151
, mFontColor( QColor( 0, 0, 0 ) )
52-
, mHAlignment( Qt::AlignLeft )
52+
, mHAlignment( Qt::AlignJustify )
5353
, mVAlignment( Qt::AlignTop )
5454
, mDistanceArea( nullptr )
5555
{

‎src/core/layout/qgslayout.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,12 @@ QgsLayout::QgsLayout( QgsProject *project )
2626
, mSnapper( QgsLayoutSnapper( this ) )
2727
, mGridSettings( this )
2828
, mPageCollection( new QgsLayoutPageCollection( this ) )
29-
, mGuideCollection( new QgsLayoutGuideCollection( this, mPageCollection.get() ) )
3029
, mUndoStack( new QgsLayoutUndoStack( this ) )
3130
{
3231
// just to make sure - this should be the default, but maybe it'll change in some future Qt version...
3332
setBackgroundBrush( Qt::NoBrush );
3433
}
3534

36-
QgsLayout::~QgsLayout()
37-
{
38-
// delete guide collection FIRST, since it depends on the page collection
39-
mGuideCollection.reset();
40-
}
41-
4235
void QgsLayout::initializeDefaults()
4336
{
4437
// default to a A4 landscape page
@@ -100,12 +93,12 @@ QgsLayoutPoint QgsLayout::convertFromLayoutUnits( const QPointF &point, const Qg
10093

10194
QgsLayoutGuideCollection &QgsLayout::guides()
10295
{
103-
return *mGuideCollection;
96+
return mPageCollection->guides();
10497
}
10598

10699
const QgsLayoutGuideCollection &QgsLayout::guides() const
107100
{
108-
return *mGuideCollection;
101+
return mPageCollection->guides();
109102
}
110103

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

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

316308
restore( mPageCollection.get() );
317-
restore( mGuideCollection.get() );
318309
restore( &mSnapper );
319310
restore( &mGridSettings );
320311

‎src/core/layout/qgslayout.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
6060
*/
6161
QgsLayout( QgsProject *project );
6262

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

368366
std::unique_ptr< QgsLayoutPageCollection > mPageCollection;
369-
std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;
370367
std::unique_ptr< QgsLayoutUndoStack > mUndoStack;
371368

372369
//! Writes only the layout settings (not member settings like grid settings, etc) to XML

‎src/core/layout/qgslayoutguidecollection.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant
286286

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

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

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

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

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

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

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

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

500500
void QgsLayoutGuideCollection::setVisible( bool visible )
501501
{
502-
mLayout->undoStack()->beginCommand( this, tr( "Guide visibility changed" ) );
502+
mLayout->undoStack()->beginCommand( mPageCollection, tr( "Guide visibility changed" ) );
503503
mGuidesVisible = visible;
504504
mLayout->undoStack()->endCommand();
505505
update();
@@ -528,6 +528,7 @@ bool QgsLayoutGuideCollection::writeXml( QDomElement &parentElement, QDomDocumen
528528
guideElement.setAttribute( QStringLiteral( "units" ), QgsUnitTypes::encodeUnit( guide->position().units() ) );
529529
element.appendChild( guideElement );
530530
}
531+
531532
parentElement.appendChild( element );
532533
return true;
533534
}

‎src/core/layout/qgslayoutpagecollection.cpp

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
QgsLayoutPageCollection::QgsLayoutPageCollection( QgsLayout *layout )
2525
: QObject( layout )
2626
, mLayout( layout )
27+
, mGuideCollection( new QgsLayoutGuideCollection( layout, this ) )
2728
{
2829
createDefaultPageStyleSymbol();
2930
}
@@ -151,6 +152,8 @@ bool QgsLayoutPageCollection::writeXml( QDomElement &parentElement, QDomDocument
151152
page->writeXml( element, document, context );
152153
}
153154

155+
mGuideCollection->writeXml( element, document, context );
156+
154157
parentElement.appendChild( element );
155158
return true;
156159
}
@@ -168,6 +171,8 @@ bool QgsLayoutPageCollection::readXml( const QDomElement &e, const QDomDocument
168171
return false;
169172
}
170173

174+
mBlockUndoCommands = true;
175+
171176
int i = 0;
172177
for ( QgsLayoutItemPage *page : qgsAsConst( mPages ) )
173178
{
@@ -195,9 +200,23 @@ bool QgsLayoutPageCollection::readXml( const QDomElement &e, const QDomDocument
195200
}
196201

197202
reflow();
203+
204+
mGuideCollection->readXml( element, document, context );
205+
206+
mBlockUndoCommands = false;
198207
return true;
199208
}
200209

210+
QgsLayoutGuideCollection &QgsLayoutPageCollection::guides()
211+
{
212+
return *mGuideCollection;
213+
}
214+
215+
const QgsLayoutGuideCollection &QgsLayoutPageCollection::guides() const
216+
{
217+
return *mGuideCollection;
218+
}
219+
201220
void QgsLayoutPageCollection::redraw()
202221
{
203222
Q_FOREACH ( QgsLayoutItemPage *page, mPages )
@@ -257,16 +276,19 @@ QList<int> QgsLayoutPageCollection::visiblePageNumbers( QRectF region ) const
257276

258277
void QgsLayoutPageCollection::addPage( QgsLayoutItemPage *page )
259278
{
260-
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
279+
if ( !mBlockUndoCommands )
280+
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
261281
mPages.append( page );
262282
mLayout->addItem( page );
263283
reflow();
264-
mLayout->undoStack()->endCommand();
284+
if ( !mBlockUndoCommands )
285+
mLayout->undoStack()->endCommand();
265286
}
266287

267288
void QgsLayoutPageCollection::insertPage( QgsLayoutItemPage *page, int beforePage )
268289
{
269-
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
290+
if ( !mBlockUndoCommands )
291+
mLayout->undoStack()->beginCommand( this, tr( "Add page" ) );
270292

271293
if ( beforePage < 0 )
272294
beforePage = 0;
@@ -281,83 +303,51 @@ void QgsLayoutPageCollection::insertPage( QgsLayoutItemPage *page, int beforePag
281303
}
282304
mLayout->addItem( page );
283305
reflow();
284-
mLayout->undoStack()->endCommand();
306+
if ( ! mBlockUndoCommands )
307+
mLayout->undoStack()->endCommand();
285308
}
286309

287-
///@cond PRIVATE
288-
289-
class QgsLayoutItemDeletePageUndoCommand: public QgsLayoutItemDeleteUndoCommand
290-
{
291-
public:
292-
293-
QgsLayoutItemDeletePageUndoCommand( QgsLayoutItemPage *page, const QString &text, int id = 0, QUndoCommand *parent SIP_TRANSFERTHIS = nullptr )
294-
: QgsLayoutItemDeleteUndoCommand( page, text, id, parent )
295-
{}
296-
297-
void redo() override
298-
{
299-
if ( mFirstRun )
300-
{
301-
mFirstRun = false;
302-
return;
303-
}
304-
305-
// remove from page collection
306-
QgsLayoutItemPage *page = dynamic_cast< QgsLayoutItemPage *>( layout()->itemByUuid( itemUuid() ) );
307-
layout()->pageCollection()->takePage( page );
308-
309-
QgsLayoutItemDeleteUndoCommand::redo();
310-
layout()->pageCollection()->reflow();
311-
}
312-
313-
void undo() override
314-
{
315-
QgsLayoutItemDeleteUndoCommand::undo();
316-
layout()->pageCollection()->reflow();
317-
}
318-
319-
QgsLayoutItem *recreateItem( int, QgsLayout *layout ) override
320-
{
321-
QgsLayoutItemPage *page = new QgsLayoutItemPage( layout );
322-
layout->pageCollection()->addPage( page );
323-
return page;
324-
}
325-
326-
};
327-
328-
///@endcond
329-
330310
void QgsLayoutPageCollection::deletePage( int pageNumber )
331311
{
332312
if ( pageNumber < 0 || pageNumber >= mPages.count() )
333313
return;
334314

335-
mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
336-
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
315+
if ( !mBlockUndoCommands )
316+
{
317+
mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
318+
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
319+
}
337320
emit pageAboutToBeRemoved( pageNumber );
338321
QgsLayoutItemPage *page = mPages.takeAt( pageNumber );
339-
//mLayout->undoStack()->stack()->push( new QgsLayoutItemDeletePageUndoCommand( page, tr( "Remove page" ) ) );
340322
mLayout->removeItem( page );
341323
page->deleteLater();
342324
reflow();
343-
mLayout->undoStack()->endCommand();
344-
mLayout->undoStack()->endMacro();
325+
if ( ! mBlockUndoCommands )
326+
{
327+
mLayout->undoStack()->endCommand();
328+
mLayout->undoStack()->endMacro();
329+
}
345330
}
346331

347332
void QgsLayoutPageCollection::deletePage( QgsLayoutItemPage *page )
348333
{
349334
if ( !mPages.contains( page ) )
350335
return;
351336

352-
mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
353-
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
337+
if ( !mBlockUndoCommands )
338+
{
339+
mLayout->undoStack()->beginMacro( tr( "Remove page" ) );
340+
mLayout->undoStack()->beginCommand( this, tr( "Remove page" ) );
341+
}
354342
emit pageAboutToBeRemoved( mPages.indexOf( page ) );
355-
//mLayout->undoStack()->stack()->push( new QgsLayoutItemDeletePageUndoCommand( page, tr( "Remove page" ) ) );
356343
mPages.removeAll( page );
357344
page->deleteLater();
358345
reflow();
359-
mLayout->undoStack()->endCommand();
360-
mLayout->undoStack()->endMacro();
346+
if ( !mBlockUndoCommands )
347+
{
348+
mLayout->undoStack()->endCommand();
349+
mLayout->undoStack()->endMacro();
350+
}
361351
}
362352

363353
QgsLayoutItemPage *QgsLayoutPageCollection::takePage( QgsLayoutItemPage *page )

‎src/core/layout/qgslayoutpagecollection.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <memory>
2727

2828
class QgsLayout;
29+
class QgsLayoutGuideCollection;
2930

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

231+
/**
232+
* Returns a reference to the collection's guide collection, which manages page snap guides.
233+
*/
234+
QgsLayoutGuideCollection &guides();
235+
236+
/**
237+
* Returns a reference to the collection's guide collection, which manages page snap guides.
238+
*/
239+
SIP_SKIP const QgsLayoutGuideCollection &guides() const;
240+
230241
public slots:
231242

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

254265
QgsLayout *mLayout = nullptr;
255266

267+
std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;
268+
256269
//! Symbol for drawing pages
257270
std::unique_ptr< QgsFillSymbol > mPageStyleSymbol;
258271

259272
QList< QgsLayoutItemPage * > mPages;
260273

274+
bool mBlockUndoCommands = false;
275+
261276
void createDefaultPageStyleSymbol();
262277

263278
friend class QgsLayoutPageCollectionUndoCommand;

0 commit comments

Comments
 (0)
Please sign in to comment.