Skip to content

Commit

Permalink
Refine QgsLayoutGuide API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent db36440 commit fa5eb4e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutguidecollection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@



class QgsLayoutGuide
class QgsLayoutGuide : QObject
{
%Docstring
Contains the configuration for a single snap guide used by a layout.
Expand Down
17 changes: 16 additions & 1 deletion src/core/layout/qgslayoutguidecollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
//

QgsLayoutGuide::QgsLayoutGuide( QgsLayout *layout, Orientation orientation, const QgsLayoutMeasurement &position )
: mOrientation( orientation )
: QObject( layout )
, mOrientation( orientation )
, mPosition( position )
, mLayout( layout )
, mLineItem( new QGraphicsLineItem() )
Expand All @@ -50,6 +51,7 @@ void QgsLayoutGuide::setPosition( const QgsLayoutMeasurement &position )
{
mPosition = position;
update();
emit positionChanged();
}

int QgsLayoutGuide::page() const
Expand Down Expand Up @@ -110,6 +112,19 @@ QGraphicsLineItem *QgsLayoutGuide::item()
return mLineItem.get();
}

double QgsLayoutGuide::layoutPosition() const
{
switch ( mOrientation )
{
case Horizontal:
return mLineItem->line().y1();

case Vertical:
return mLineItem->line().x1();
}
return -999; // avoid warning
}

QgsLayoutGuide::Orientation QgsLayoutGuide::orientation() const
{
return mOrientation;
Expand Down
16 changes: 15 additions & 1 deletion src/core/layout/qgslayoutguidecollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class QGraphicsLineItem;
* \brief Contains the configuration for a single snap guide used by a layout.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsLayoutGuide
class CORE_EXPORT QgsLayoutGuide : public QObject
{

Q_OBJECT

public:

//! Guide orientation
Expand Down Expand Up @@ -98,6 +100,18 @@ class CORE_EXPORT QgsLayoutGuide
*/
QGraphicsLineItem *item();

/**
* Returns the guide's position in absolute layout units.
*/
double layoutPosition() const;

signals:

/**
* Emitted when the guide's position is changed.
*/
void positionChanged();

private:

Orientation mOrientation = Vertical;
Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgslayoutguides.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
QgsLayoutItemPage)
from qgis.PyQt.QtGui import (QPen,
QColor)
from qgis.PyQt.QtTest import QSignalSpy

from qgis.testing import start_app, unittest

Expand All @@ -40,9 +41,11 @@ def testGuideGettersSetters(self):
self.assertEqual(g.position().length(), 5.0)
self.assertEqual(g.position().units(), QgsUnitTypes.LayoutCentimeters)

position_changed_spy = QSignalSpy(g.positionChanged)
g.setPosition(QgsLayoutMeasurement(15, QgsUnitTypes.LayoutInches))
self.assertEqual(g.position().length(), 15.0)
self.assertEqual(g.position().units(), QgsUnitTypes.LayoutInches)
self.assertEqual(len(position_changed_spy), 1)

g.setPage(1)
self.assertEqual(g.page(), 1)
Expand All @@ -59,6 +62,7 @@ def testUpdateGuide(self):
self.assertEqual(g.item().line().y1(), 50)
self.assertEqual(g.item().line().x2(), 297)
self.assertEqual(g.item().line().y2(), 50)
self.assertEqual(g.layoutPosition(), 50)

g.setPosition(QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters))
g.update()
Expand All @@ -67,6 +71,17 @@ def testUpdateGuide(self):
self.assertEqual(g.item().line().y1(), 15)
self.assertEqual(g.item().line().x2(), 297)
self.assertEqual(g.item().line().y2(), 15)
self.assertEqual(g.layoutPosition(), 15)

# vertical guide
g2 = QgsLayoutGuide(l, QgsLayoutGuide.Vertical, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
g2.update()
self.assertTrue(g2.item().isVisible())
self.assertEqual(g2.item().line().x1(), 50)
self.assertEqual(g2.item().line().y1(), 0)
self.assertEqual(g2.item().line().x2(), 50)
self.assertEqual(g2.item().line().y2(), 210)
self.assertEqual(g2.layoutPosition(), 50)

g.setPage(10)
g.update()
Expand Down

0 comments on commit fa5eb4e

Please sign in to comment.