Skip to content

Commit

Permalink
Fix guide positioning when multiple pages are present
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 18, 2017
1 parent a1128a5 commit 200669a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/layout/qgslayoutguidecollection.cpp
Expand Up @@ -89,7 +89,7 @@ void QgsLayoutGuide::update()
}
else
{
mLineItem->setLine( 0, layoutPos, mPage->rect().width(), layoutPos );
mLineItem->setLine( 0, layoutPos + mPage->y(), mPage->rect().width(), layoutPos + mPage->y() );
mLineItem->setVisible( showGuide );
}

Expand All @@ -102,7 +102,7 @@ void QgsLayoutGuide::update()
}
else
{
mLineItem->setLine( layoutPos, 0, layoutPos, mPage->rect().height() );
mLineItem->setLine( layoutPos, mPage->y(), layoutPos, mPage->y() + mPage->rect().height() );
mLineItem->setVisible( showGuide );
}

Expand Down
4 changes: 3 additions & 1 deletion src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -95,6 +95,7 @@ void QgsLayoutPageCollection::reflow()
currentY += mLayout->convertToLayoutUnits( page->pageSize() ).height() + spaceBetweenPages();
p.setY( currentY );
}
mLayout->guides().update();
mLayout->updateBounds();
emit changed();
}
Expand Down Expand Up @@ -193,7 +194,8 @@ int QgsLayoutPageCollection::predictPageNumberForPoint( QPointF point ) const

QgsLayoutItemPage *QgsLayoutPageCollection::pageAtPoint( QPointF point ) const
{
Q_FOREACH ( QGraphicsItem *item, mLayout->items( point ) )
const QList< QGraphicsItem * > items = mLayout->items( point );
for ( QGraphicsItem *item : items )
{
if ( item->type() == QgsLayoutItemRegistry::LayoutPage )
{
Expand Down
29 changes: 29 additions & 0 deletions tests/src/python/test_qgslayoutguides.py
Expand Up @@ -65,6 +65,11 @@ def testUpdateGuide(self):
p = QgsProject()
l = QgsLayout(p)
l.initializeDefaults() # add a page
# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize('A5')
l.pageCollection().addPage(page2)

g = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
g.setLayout(l)
g.update()
Expand All @@ -85,6 +90,19 @@ def testUpdateGuide(self):
self.assertEqual(g.item().line().y2(), 15)
self.assertEqual(g.layoutPosition(), 15)

# guide on page2
g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(1))
g1.setLayout(l)
g1.update()
g1.setPosition(QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters))
g1.update()
self.assertTrue(g1.item().isVisible())
self.assertEqual(g1.item().line().x1(), 0)
self.assertEqual(g1.item().line().y1(), 235)
self.assertEqual(g1.item().line().x2(), 148)
self.assertEqual(g1.item().line().y2(), 235)
self.assertEqual(g1.layoutPosition(), 235)

# vertical guide
g2 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
g2.setLayout(l)
Expand All @@ -109,6 +127,17 @@ def testUpdateGuide(self):
g.update()
self.assertFalse(g.item().isVisible())

# guide on page2
g3 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(1))
g3.setLayout(l)
g3.update()
self.assertTrue(g3.item().isVisible())
self.assertEqual(g3.item().line().x1(), 50)
self.assertEqual(g3.item().line().y1(), 220)
self.assertEqual(g3.item().line().x2(), 50)
self.assertEqual(g3.item().line().y2(), 430)
self.assertEqual(g3.layoutPosition(), 50)

def testCollection(self):
p = QgsProject()
l = QgsLayout(p)
Expand Down

0 comments on commit 200669a

Please sign in to comment.