Skip to content

Commit 3fd2e09

Browse files
committedAug 7, 2017
Fix ownership issue with layout guides
1 parent 94362fe commit 3fd2e09

File tree

9 files changed

+171
-99
lines changed

9 files changed

+171
-99
lines changed
 

‎python/core/layout/qgslayout.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
3939
called on the new layout.
4040
%End
4141

42+
~QgsLayout();
43+
4244
void initializeDefaults();
4345
%Docstring
4446
Initializes an empty layout, e.g. by adding a default page to the layout. This should be called after creating

‎python/core/layout/qgslayoutguidecollection.sip

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class QgsLayoutGuide : QObject
2626
Vertical,
2727
};
2828

29-
QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position );
29+
QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position, QgsLayoutItemPage *page );
3030
%Docstring
3131
Constructor for a new guide with the specified ``orientation`` and
3232
initial ``position``.
@@ -36,6 +36,8 @@ class QgsLayoutGuide : QObject
3636
the corresponding layout for you.
3737
%End
3838

39+
~QgsLayoutGuide();
40+
3941
QgsLayout *layout() const;
4042
%Docstring
4143
Returns the layout the guide belongs to.
@@ -82,21 +84,17 @@ class QgsLayoutGuide : QObject
8284
.. seealso:: position()
8385
%End
8486

85-
int page() const;
87+
QgsLayoutItemPage *page();
8688
%Docstring
87-
Returns the page number of the guide.
88-
89-
Page numbering begins at 0.
89+
Returns the page the guide is contained within.
9090

9191
.. seealso:: setPage()
92-
:rtype: int
92+
:rtype: QgsLayoutItemPage
9393
%End
9494

95-
void setPage( int page );
95+
void setPage( QgsLayoutItemPage *page );
9696
%Docstring
97-
Sets the ``page`` number of the guide.
98-
99-
Page numbering begins at 0.
97+
Sets the ``page`` the guide is contained within.
10098

10199
.. seealso:: page()
102100
%End
@@ -155,9 +153,10 @@ class QgsLayoutGuideCollection : QAbstractTableModel
155153
LayoutPositionRole,
156154
};
157155

158-
QgsLayoutGuideCollection( QgsLayout *layout );
156+
QgsLayoutGuideCollection( QgsLayout *layout, QgsLayoutPageCollection *pageCollection );
159157
%Docstring
160-
Constructor for QgsLayoutGuideCollection belonging to the specified layout.
158+
Constructor for QgsLayoutGuideCollection belonging to the specified layout,
159+
and linked to the specified ``pageCollection``.
161160
%End
162161
~QgsLayoutGuideCollection();
163162

@@ -210,9 +209,16 @@ class QgsLayoutGuideCollection : QAbstractTableModel
210209
Returns the list of guides contained in the collection with the specified
211210
``orientation`` and on a matching ``page``.
212211
If ``page`` is -1, guides from all pages will be returned.
212+
.. seealso:: guidesOnPage()
213213
:rtype: list of QgsLayoutGuide
214214
%End
215215

216+
QList< QgsLayoutGuide * > guidesOnPage( int page );
217+
%Docstring
218+
Returns the list of guides contained on a matching ``page``.
219+
.. seealso:: guides()
220+
:rtype: list of QgsLayoutGuide
221+
%End
216222

217223
bool visible() const;
218224
%Docstring

‎src/app/layout/qgslayoutguidewidget.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ QgsLayoutGuideWidget::QgsLayoutGuideWidget( QWidget *parent, QgsLayout *layout,
6060

6161
void QgsLayoutGuideWidget::addHorizontalGuide()
6262
{
63-
std::unique_ptr< QgsLayoutGuide > newGuide( new QgsLayoutGuide( QgsLayoutGuide::Horizontal, QgsLayoutMeasurement( 0 ) ) );
64-
newGuide->setPage( mPage );
63+
std::unique_ptr< QgsLayoutGuide > newGuide( new QgsLayoutGuide( QgsLayoutGuide::Horizontal, QgsLayoutMeasurement( 0 ), mLayout->pageCollection()->page( mPage ) ) );
6564
mLayout->guides().addGuide( newGuide.release() );
6665
}
6766

6867
void QgsLayoutGuideWidget::addVerticalGuide()
6968
{
70-
std::unique_ptr< QgsLayoutGuide > newGuide( new QgsLayoutGuide( QgsLayoutGuide::Vertical, QgsLayoutMeasurement( 0 ) ) );
71-
newGuide->setPage( mPage );
69+
std::unique_ptr< QgsLayoutGuide > newGuide( new QgsLayoutGuide( QgsLayoutGuide::Vertical, QgsLayoutMeasurement( 0 ), mLayout->pageCollection()->page( mPage ) ) );
7270
mLayout->guides().addGuide( newGuide.release() );
7371
}
7472

‎src/core/layout/qgslayout.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ QgsLayout::QgsLayout( QgsProject *project )
2222
: QGraphicsScene()
2323
, mProject( project )
2424
, mSnapper( QgsLayoutSnapper( this ) )
25-
, mGuideCollection( new QgsLayoutGuideCollection( this ) )
2625
, mPageCollection( new QgsLayoutPageCollection( this ) )
26+
, mGuideCollection( new QgsLayoutGuideCollection( this, mPageCollection.get() ) )
2727
{
2828
// just to make sure - this should be the default, but maybe it'll change in some future Qt version...
2929
setBackgroundBrush( Qt::NoBrush );
3030
}
3131

32+
QgsLayout::~QgsLayout()
33+
{
34+
// delete guide collection FIRST, since it depends on the page collection
35+
mGuideCollection.reset();
36+
}
37+
3238
void QgsLayout::initializeDefaults()
3339
{
3440
// default to a A4 landscape page

‎src/core/layout/qgslayout.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
5959
*/
6060
QgsLayout( QgsProject *project );
6161

62+
~QgsLayout();
63+
6264
/**
6365
* Initializes an empty layout, e.g. by adding a default page to the layout. This should be called after creating
6466
* a new layout.
@@ -309,10 +311,11 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
309311
QgsLayoutContext mContext;
310312
QgsLayoutSnapper mSnapper;
311313
QgsLayoutGridSettings mGridSettings;
312-
std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;
313314

314315
std::unique_ptr< QgsLayoutPageCollection > mPageCollection;
315316

317+
std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;
318+
316319
};
317320

318321
#endif //QGSLAYOUT_H

‎src/core/layout/qgslayoutguidecollection.cpp

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
// QgsLayoutGuide
2424
//
2525

26-
QgsLayoutGuide::QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position )
26+
QgsLayoutGuide::QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position, QgsLayoutItemPage *page )
2727
: QObject( nullptr )
2828
, mOrientation( orientation )
2929
, mPosition( position )
30-
, mLineItem( new QGraphicsLineItem() )
30+
, mPage( page )
31+
{}
32+
33+
QgsLayoutGuide::~QgsLayoutGuide()
3134
{
32-
mLineItem->hide();
33-
mLineItem->setZValue( QgsLayout::ZGuide );
34-
QPen linePen( Qt::DotLine );
35-
linePen.setColor( Qt::red );
36-
// use a pen width of 0, since this activates a cosmetic pen
37-
// which doesn't scale with the composer and keeps a constant size
38-
linePen.setWidthF( 0 );
39-
mLineItem->setPen( linePen );
35+
if ( mLayout && mLineItem )
36+
{
37+
mLayout->removeItem( mLineItem );
38+
delete mLineItem;
39+
}
4040
}
4141

4242
QgsLayoutMeasurement QgsLayoutGuide::position() const
@@ -51,56 +51,58 @@ void QgsLayoutGuide::setPosition( const QgsLayoutMeasurement &position )
5151
emit positionChanged();
5252
}
5353

54-
int QgsLayoutGuide::page() const
54+
QgsLayoutItemPage *QgsLayoutGuide::page()
5555
{
5656
return mPage;
5757
}
5858

59-
void QgsLayoutGuide::setPage( int page )
59+
void QgsLayoutGuide::setPage( QgsLayoutItemPage *page )
6060
{
6161
mPage = page;
6262
update();
6363
}
6464

6565
void QgsLayoutGuide::update()
6666
{
67-
if ( !mLayout )
67+
if ( !mLayout || !mLineItem )
6868
return;
6969

7070
// first find matching page
71-
if ( mPage >= mLayout->pageCollection()->pageCount() )
71+
if ( !mPage )
7272
{
7373
mLineItem->hide();
7474
return;
7575
}
7676

77-
QgsLayoutItemPage *page = mLayout->pageCollection()->page( mPage );
78-
mLineItem->setParentItem( page );
77+
if ( mLineItem->parentItem() != mPage )
78+
{
79+
mLineItem->setParentItem( mPage );
80+
}
7981
double layoutPos = mLayout->convertToLayoutUnits( mPosition );
8082
bool showGuide = mLayout->guides().visible();
8183
switch ( mOrientation )
8284
{
8385
case Horizontal:
84-
if ( layoutPos > page->rect().height() )
86+
if ( layoutPos > mPage->rect().height() )
8587
{
8688
mLineItem->hide();
8789
}
8890
else
8991
{
90-
mLineItem->setLine( 0, layoutPos, page->rect().width(), layoutPos );
92+
mLineItem->setLine( 0, layoutPos, mPage->rect().width(), layoutPos );
9193
mLineItem->setVisible( showGuide );
9294
}
9395

9496
break;
9597

9698
case Vertical:
97-
if ( layoutPos > page->rect().width() )
99+
if ( layoutPos > mPage->rect().width() )
98100
{
99101
mLineItem->hide();
100102
}
101103
else
102104
{
103-
mLineItem->setLine( layoutPos, 0, layoutPos, page->rect().height() );
105+
mLineItem->setLine( layoutPos, 0, layoutPos, mPage->rect().height() );
104106
mLineItem->setVisible( showGuide );
105107
}
106108

@@ -110,11 +112,14 @@ void QgsLayoutGuide::update()
110112

111113
QGraphicsLineItem *QgsLayoutGuide::item()
112114
{
113-
return mLineItem.get();
115+
return mLineItem;
114116
}
115117

116118
double QgsLayoutGuide::layoutPosition() const
117119
{
120+
if ( !mLineItem )
121+
return -999;
122+
118123
switch ( mOrientation )
119124
{
120125
case Horizontal:
@@ -128,6 +133,9 @@ double QgsLayoutGuide::layoutPosition() const
128133

129134
void QgsLayoutGuide::setLayoutPosition( double position )
130135
{
136+
if ( !mLayout )
137+
return;
138+
131139
double p = 0;
132140
switch ( mOrientation )
133141
{
@@ -152,7 +160,21 @@ QgsLayout *QgsLayoutGuide::layout() const
152160
void QgsLayoutGuide::setLayout( QgsLayout *layout )
153161
{
154162
mLayout = layout;
155-
mLayout->addItem( mLineItem.get() );
163+
164+
if ( !mLineItem )
165+
{
166+
mLineItem = new QGraphicsLineItem();
167+
mLineItem->hide();
168+
mLineItem->setZValue( QgsLayout::ZGuide );
169+
QPen linePen( Qt::DotLine );
170+
linePen.setColor( Qt::red );
171+
// use a pen width of 0, since this activates a cosmetic pen
172+
// which doesn't scale with the composer and keeps a constant size
173+
linePen.setWidthF( 0 );
174+
mLineItem->setPen( linePen );
175+
}
176+
177+
mLayout->addItem( mLineItem );
156178
update();
157179
}
158180

@@ -167,12 +189,15 @@ QgsLayoutGuide::Orientation QgsLayoutGuide::orientation() const
167189
// QgsLayoutGuideCollection
168190
//
169191

170-
QgsLayoutGuideCollection::QgsLayoutGuideCollection( QgsLayout *layout )
192+
QgsLayoutGuideCollection::QgsLayoutGuideCollection( QgsLayout *layout, QgsLayoutPageCollection *pageCollection )
171193
: QAbstractTableModel( layout )
172194
, mLayout( layout )
195+
, mPageCollection( pageCollection )
173196
{
174197
QFont f;
175198
mHeaderSize = QFontMetrics( f ).width( "XX" );
199+
200+
connect( mPageCollection, &QgsLayoutPageCollection::pageAboutToBeRemoved, this, &QgsLayoutGuideCollection::pageAboutToBeRemoved );
176201
}
177202

178203
QgsLayoutGuideCollection::~QgsLayoutGuideCollection()
@@ -223,7 +248,7 @@ QVariant QgsLayoutGuideCollection::data( const QModelIndex &index, int role ) co
223248
return guide->position().units();
224249

225250
case PageRole:
226-
return guide->page();
251+
return mPageCollection->pageNumber( guide->page() );
227252

228253
case LayoutPositionRole:
229254
return guide->layoutPosition();
@@ -358,23 +383,23 @@ void QgsLayoutGuideCollection::clear()
358383

359384
void QgsLayoutGuideCollection::applyGuidesToAllOtherPages( int sourcePage )
360385
{
386+
QgsLayoutItemPage *page = mPageCollection->page( sourcePage );
361387
// remove other page's guides
362388
Q_FOREACH ( QgsLayoutGuide *guide, mGuides )
363389
{
364-
if ( guide->page() != sourcePage )
390+
if ( guide->page() != page )
365391
removeGuide( guide );
366392
}
367393

368394
// remaining guides belong to source page - clone them to other pages
369395
Q_FOREACH ( QgsLayoutGuide *guide, mGuides )
370396
{
371-
for ( int p = 0; p < mLayout->pageCollection()->pageCount(); ++p )
397+
for ( int p = 0; p < mPageCollection->pageCount(); ++p )
372398
{
373399
if ( p == sourcePage )
374400
continue;
375401

376-
std::unique_ptr< QgsLayoutGuide> newGuide( new QgsLayoutGuide( guide->orientation(), guide->position() ) );
377-
newGuide->setPage( p );
402+
std::unique_ptr< QgsLayoutGuide> newGuide( new QgsLayoutGuide( guide->orientation(), guide->position(), mPageCollection->page( p ) ) );
378403
newGuide->setLayout( mLayout );
379404
if ( newGuide->item()->isVisible() )
380405
{
@@ -399,7 +424,18 @@ QList<QgsLayoutGuide *> QgsLayoutGuideCollection::guides( QgsLayoutGuide::Orient
399424
Q_FOREACH ( QgsLayoutGuide *guide, mGuides )
400425
{
401426
if ( guide->orientation() == orientation && guide->item()->isVisible() &&
402-
( page < 0 || page == guide->page() ) )
427+
( page < 0 || mPageCollection->page( page ) == guide->page() ) )
428+
res << guide;
429+
}
430+
return res;
431+
}
432+
433+
QList<QgsLayoutGuide *> QgsLayoutGuideCollection::guidesOnPage( int page )
434+
{
435+
QList<QgsLayoutGuide *> res;
436+
Q_FOREACH ( QgsLayoutGuide *guide, mGuides )
437+
{
438+
if ( mPageCollection->page( page ) == guide->page() )
403439
res << guide;
404440
}
405441
return res;
@@ -416,6 +452,14 @@ void QgsLayoutGuideCollection::setVisible( bool visible )
416452
update();
417453
}
418454

455+
void QgsLayoutGuideCollection::pageAboutToBeRemoved( int pageNumber )
456+
{
457+
Q_FOREACH ( QgsLayoutGuide *guide, guidesOnPage( pageNumber ) )
458+
{
459+
removeGuide( guide );
460+
}
461+
}
462+
419463

420464

421465
//

‎src/core/layout/qgslayoutguidecollection.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
#include "qgis_core.h"
2020
#include "qgslayoutmeasurement.h"
2121
#include "qgslayoutpoint.h"
22+
#include "qgslayoutitempage.h"
2223
#include <QPen>
2324
#include <QAbstractListModel>
2425
#include <QSortFilterProxyModel>
2526
#include <QGraphicsLineItem>
2627
#include <memory>
2728

2829
class QgsLayout;
30+
class QgsLayoutPageCollection;
2931

3032
/**
3133
* \ingroup core
@@ -55,7 +57,9 @@ class CORE_EXPORT QgsLayoutGuide : public QObject
5557
* Adding the guide to a QgsLayoutGuideCollection will automatically set
5658
* the corresponding layout for you.
5759
*/
58-
QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position );
60+
QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position, QgsLayoutItemPage *page );
61+
62+
~QgsLayoutGuide();
5963

6064
/**
6165
* Returns the layout the guide belongs to.
@@ -99,22 +103,18 @@ class CORE_EXPORT QgsLayoutGuide : public QObject
99103
void setPosition( const QgsLayoutMeasurement &position );
100104

101105
/**
102-
* Returns the page number of the guide.
103-
*
104-
* Page numbering begins at 0.
106+
* Returns the page the guide is contained within.
105107
*
106108
* \see setPage()
107109
*/
108-
int page() const;
110+
QgsLayoutItemPage *page();
109111

110112
/**
111-
* Sets the \a page number of the guide.
112-
*
113-
* Page numbering begins at 0.
113+
* Sets the \a page the guide is contained within.
114114
*
115115
* \see page()
116116
*/
117-
void setPage( int page );
117+
void setPage( QgsLayoutItemPage *page );
118118

119119
/**
120120
* Updates the position of the guide's line item.
@@ -152,13 +152,13 @@ class CORE_EXPORT QgsLayoutGuide : public QObject
152152
//! Horizontal/vertical position of guide on page
153153
QgsLayoutMeasurement mPosition;
154154

155-
//! Page number, 0 index based
156-
int mPage = 0;
155+
//! Page
156+
QPointer< QgsLayoutItemPage > mPage;
157157

158-
QgsLayout *mLayout = nullptr;
158+
QPointer< QgsLayout > mLayout;
159159

160160
//! Line item used in scene for guide
161-
std::unique_ptr< QGraphicsLineItem > mLineItem;
161+
QGraphicsLineItem *mLineItem = nullptr;
162162

163163
};
164164

@@ -186,9 +186,10 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
186186
};
187187

188188
/**
189-
* Constructor for QgsLayoutGuideCollection belonging to the specified layout.
189+
* Constructor for QgsLayoutGuideCollection belonging to the specified layout,
190+
* and linked to the specified \a pageCollection.
190191
*/
191-
QgsLayoutGuideCollection( QgsLayout *layout );
192+
QgsLayoutGuideCollection( QgsLayout *layout, QgsLayoutPageCollection *pageCollection );
192193
~QgsLayoutGuideCollection();
193194

194195
int rowCount( const QModelIndex & ) const override;
@@ -233,9 +234,15 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
233234
* Returns the list of guides contained in the collection with the specified
234235
* \a orientation and on a matching \a page.
235236
* If \a page is -1, guides from all pages will be returned.
237+
* \see guidesOnPage()
236238
*/
237239
QList< QgsLayoutGuide * > guides( QgsLayoutGuide::Orientation orientation, int page = -1 );
238240

241+
/**
242+
* Returns the list of guides contained on a matching \a page.
243+
* \see guides()
244+
*/
245+
QList< QgsLayoutGuide * > guidesOnPage( int page );
239246

240247
/**
241248
* Returns true if the guide lines should be drawn.
@@ -249,9 +256,14 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
249256
*/
250257
void setVisible( bool visible );
251258

259+
private slots:
260+
261+
void pageAboutToBeRemoved( int pageNumber );
262+
252263
private:
253264

254265
QgsLayout *mLayout = nullptr;
266+
QgsLayoutPageCollection *mPageCollection = nullptr;
255267

256268
QList< QgsLayoutGuide * > mGuides;
257269
int mHeaderSize = 0;

‎src/gui/layout/qgslayoutruler.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ void QgsLayoutRuler::drawMarkerPos( QPainter *painter )
281281

282282
void QgsLayoutRuler::drawGuideMarkers( QPainter *p, QgsLayout *layout )
283283
{
284-
QList< int > visiblePageNumbers = mView->visiblePageNumbers();
284+
QList< QgsLayoutItemPage * > visiblePages = mView->visiblePages();
285285
QList< QgsLayoutGuide * > guides = layout->guides().guides( mOrientation == Qt::Horizontal ? QgsLayoutGuide::Vertical : QgsLayoutGuide::Horizontal );
286286
p->save();
287287
p->setRenderHint( QPainter::Antialiasing, true );
288288
p->setPen( Qt::NoPen );
289289
Q_FOREACH ( QgsLayoutGuide *guide, guides )
290290
{
291-
if ( visiblePageNumbers.contains( guide->page() ) )
291+
if ( visiblePages.contains( guide->page() ) )
292292
{
293293
if ( guide == mHoverGuide )
294294
{
@@ -364,13 +364,13 @@ QPoint QgsLayoutRuler::convertLayoutPointToLocal( QPointF layoutPoint ) const
364364
QgsLayoutGuide *QgsLayoutRuler::guideAtPoint( QPoint localPoint ) const
365365
{
366366
QPointF layoutPoint = convertLocalPointToLayout( localPoint );
367-
QList< int > visiblePageNumbers = mView->visiblePageNumbers();
367+
QList< QgsLayoutItemPage * > visiblePages = mView->visiblePages();
368368
QList< QgsLayoutGuide * > guides = mView->currentLayout()->guides().guides( mOrientation == Qt::Horizontal ? QgsLayoutGuide::Vertical : QgsLayoutGuide::Horizontal );
369369
QgsLayoutGuide *closestGuide = nullptr;
370370
double minDelta = DBL_MAX;
371371
Q_FOREACH ( QgsLayoutGuide *guide, guides )
372372
{
373-
if ( visiblePageNumbers.contains( guide->page() ) )
373+
if ( visiblePages.contains( guide->page() ) )
374374
{
375375
double currentDelta = 0;
376376
switch ( mOrientation )
@@ -707,7 +707,7 @@ void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
707707
QPointF layoutPoint = convertLocalPointToLayout( event->pos() );
708708

709709
// delete guide if it ends outside of page
710-
QgsLayoutItemPage *page = mView->currentLayout()->pageCollection()->page( mDraggingGuide->page() );
710+
QgsLayoutItemPage *page = mDraggingGuide->page();
711711
bool deleteGuide = false;
712712
switch ( mDraggingGuide->orientation() )
713713
{
@@ -759,25 +759,23 @@ void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
759759
if ( !page )
760760
return; // dragged outside of a page
761761

762-
int pageNumber = layout->pageCollection()->pageNumber( page );
763762
std::unique_ptr< QgsLayoutGuide > guide;
764763
switch ( mOrientation )
765764
{
766765
case Qt::Horizontal:
767766
{
768767
//mouse is creating a horizontal guide
769768
double posOnPage = layout->pageCollection()->positionOnPage( scenePos ).y();
770-
guide.reset( new QgsLayoutGuide( QgsLayoutGuide::Horizontal, QgsLayoutMeasurement( posOnPage, layout->units() ) ) );
769+
guide.reset( new QgsLayoutGuide( QgsLayoutGuide::Horizontal, QgsLayoutMeasurement( posOnPage, layout->units() ), page ) );
771770
break;
772771
}
773772
case Qt::Vertical:
774773
{
775774
//mouse is creating a vertical guide
776-
guide.reset( new QgsLayoutGuide( QgsLayoutGuide::Vertical, QgsLayoutMeasurement( scenePos.x(), layout->units() ) ) );
775+
guide.reset( new QgsLayoutGuide( QgsLayoutGuide::Vertical, QgsLayoutMeasurement( scenePos.x(), layout->units() ), page ) );
777776
break;
778777
}
779778
}
780-
guide->setPage( pageNumber );
781779
mView->currentLayout()->guides().addGuide( guide.release() );
782780
}
783781
}

‎tests/src/python/test_qgslayoutguides.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def testGuideGettersSetters(self):
4141
p = QgsProject()
4242
l = QgsLayout(p)
4343
l.initializeDefaults() # add a page
44-
g = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
44+
g = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), None)
4545
self.assertEqual(g.orientation(), QgsLayoutGuide.Horizontal)
4646
self.assertEqual(g.position().length(), 5.0)
4747
self.assertEqual(g.position().units(), QgsUnitTypes.LayoutCentimeters)
@@ -55,14 +55,15 @@ def testGuideGettersSetters(self):
5555
self.assertEqual(g.position().units(), QgsUnitTypes.LayoutInches)
5656
self.assertEqual(len(position_changed_spy), 1)
5757

58-
g.setPage(1)
59-
self.assertEqual(g.page(), 1)
58+
page = l.pageCollection().page(0)
59+
g.setPage(page)
60+
self.assertEqual(g.page(), page)
6061

6162
def testUpdateGuide(self):
6263
p = QgsProject()
6364
l = QgsLayout(p)
6465
l.initializeDefaults() # add a page
65-
g = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
66+
g = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
6667
g.setLayout(l)
6768
g.update()
6869

@@ -83,7 +84,7 @@ def testUpdateGuide(self):
8384
self.assertEqual(g.layoutPosition(), 15)
8485

8586
# vertical guide
86-
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
87+
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
8788
g2.setLayout(l)
8889
g2.update()
8990
self.assertTrue(g2.item().isVisible())
@@ -93,11 +94,11 @@ def testUpdateGuide(self):
9394
self.assertEqual(g2.item().line().y2(), 210)
9495
self.assertEqual(g2.layoutPosition(), 50)
9596

96-
g.setPage(10)
97+
g.setPage(None)
9798
g.update()
9899
self.assertFalse(g.item().isVisible())
99100

100-
g.setPage(0)
101+
g.setPage(l.pageCollection().page(0))
101102
g.update()
102103
self.assertTrue(g.item().isVisible())
103104

@@ -119,7 +120,7 @@ def testCollection(self):
119120
self.assertFalse(guides.guides(QgsLayoutGuide.Vertical))
120121

121122
# add a guide
122-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
123+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
123124
guides.addGuide(g1)
124125
self.assertEqual(guides.rowCount(QModelIndex()), 1)
125126
self.assertEqual(guides.data(guides.index(0, 0), QgsLayoutGuideCollection.OrientationRole), QgsLayoutGuide.Horizontal)
@@ -128,8 +129,10 @@ def testCollection(self):
128129
self.assertEqual(guides.data(guides.index(0, 0), QgsLayoutGuideCollection.PageRole), 0)
129130
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
130131
self.assertFalse(guides.guides(QgsLayoutGuide.Vertical))
132+
self.assertEqual(guides.guidesOnPage(0), [g1])
133+
self.assertEqual(guides.guidesOnPage(1), [])
131134

132-
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15))
135+
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15), l.pageCollection().page(0))
133136
guides.addGuide(g2)
134137
self.assertEqual(guides.rowCount(QModelIndex()), 2)
135138
self.assertEqual(guides.data(guides.index(1, 0), QgsLayoutGuideCollection.OrientationRole), QgsLayoutGuide.Horizontal)
@@ -138,12 +141,12 @@ def testCollection(self):
138141
self.assertEqual(guides.data(guides.index(1, 0), QgsLayoutGuideCollection.PageRole), 0)
139142
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1, g2])
140143
self.assertFalse(guides.guides(QgsLayoutGuide.Vertical))
144+
self.assertEqual(guides.guidesOnPage(0), [g1, g2])
141145

142146
page2 = QgsLayoutItemPage(l)
143147
page2.setPageSize('A3')
144148
l.pageCollection().addPage(page2)
145-
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35))
146-
g3.setPage(1)
149+
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35), l.pageCollection().page(1))
147150
guides.addGuide(g3)
148151
self.assertEqual(guides.rowCount(QModelIndex()), 3)
149152
self.assertEqual(guides.data(guides.index(2, 0), QgsLayoutGuideCollection.OrientationRole), QgsLayoutGuide.Vertical)
@@ -157,18 +160,20 @@ def testCollection(self):
157160
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical, 0), [])
158161
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical, 1), [g3])
159162
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical, 2), [])
163+
self.assertEqual(guides.guidesOnPage(0), [g1, g2])
164+
self.assertEqual(guides.guidesOnPage(1), [g3])
160165

161166
def testDeleteRows(self):
162167
p = QgsProject()
163168
l = QgsLayout(p)
164169
l.initializeDefaults()
165170
guides = l.guides()
166171

167-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
172+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
168173
guides.addGuide(g1)
169-
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15))
174+
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15), l.pageCollection().page(0))
170175
guides.addGuide(g2)
171-
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35))
176+
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35), l.pageCollection().page(0))
172177
guides.addGuide(g3)
173178

174179
self.assertTrue(guides.removeRows(1, 1))
@@ -201,12 +206,11 @@ def testQgsLayoutGuideProxyModel(self):
201206
self.assertEqual(vert_filter.rowCount(QModelIndex()), 0)
202207

203208
# add some guides
204-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
209+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
205210
guides.addGuide(g1)
206-
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15))
207-
g2.setPage(1)
211+
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15), l.pageCollection().page(1))
208212
guides.addGuide(g2)
209-
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35))
213+
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35), l.pageCollection().page(0))
210214
guides.addGuide(g3)
211215

212216
self.assertEqual(hoz_filter.rowCount(QModelIndex()), 1)
@@ -228,7 +232,7 @@ def testRemoveGuide(self):
228232
guides = l.guides()
229233

230234
# add a guide
231-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
235+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
232236
guides.addGuide(g1)
233237
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
234238
guides.removeGuide(None)
@@ -243,9 +247,9 @@ def testClear(self):
243247
guides = l.guides()
244248

245249
# add a guide
246-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
250+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
247251
guides.addGuide(g1)
248-
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
252+
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
249253
guides.addGuide(g2)
250254
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1, g2])
251255
guides.clear()
@@ -261,14 +265,13 @@ def testApplyToOtherPages(self):
261265
guides = l.guides()
262266

263267
# add some guides
264-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5))
268+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5), l.pageCollection().page(0))
265269
guides.addGuide(g1)
266-
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(6))
270+
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(6), l.pageCollection().page(0))
267271
guides.addGuide(g2)
268-
g3 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(190))
272+
g3 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(190), l.pageCollection().page(0))
269273
guides.addGuide(g3)
270-
g4 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(1))
271-
g4.setPage(1)
274+
g4 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(1), l.pageCollection().page(1))
272275
guides.addGuide(g4)
273276

274277
# apply guides from page 0 - should delete g4
@@ -300,9 +303,9 @@ def testSetVisible(self):
300303
guides = l.guides()
301304

302305
# add some guides
303-
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5))
306+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5), l.pageCollection().page(0))
304307
guides.addGuide(g1)
305-
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(6))
308+
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(6), l.pageCollection().page(0))
306309
guides.addGuide(g2)
307310

308311
guides.setVisible(False)

0 commit comments

Comments
 (0)
Please sign in to comment.