Skip to content
Permalink
Browse files

Refine QgsLayoutGuide API

  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent db36440 commit fa5eb4e5adf56834daa81dcdc98cd0b76fc3d628
@@ -8,7 +8,7 @@



class QgsLayoutGuide
class QgsLayoutGuide : QObject
{
%Docstring
Contains the configuration for a single snap guide used by a layout.
@@ -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() )
@@ -50,6 +51,7 @@ void QgsLayoutGuide::setPosition( const QgsLayoutMeasurement &position )
{
mPosition = position;
update();
emit positionChanged();
}

int QgsLayoutGuide::page() const
@@ -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;
@@ -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
@@ -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;
@@ -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

@@ -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)
@@ -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()
@@ -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()

0 comments on commit fa5eb4e

Please sign in to comment.
You can’t perform that action at this time.