Skip to content

Commit

Permalink
Port item frame related code from composer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent 0b18829 commit 51efa19
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
25 changes: 25 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -344,6 +344,31 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
.. seealso:: setBackgroundEnabled()
%End

virtual double estimatedFrameBleed() const;
%Docstring
Returns the estimated amount the item's frame bleeds outside the item's
actual rectangle. For instance, if the item has a 2mm frame stroke, then
1mm of this frame is drawn outside the item's rect. In this case the
return value will be 1.0.

Returned values are in layout units.

.. seealso:: rectWithFrame()
:rtype: float
%End

virtual QRectF rectWithFrame() const;
%Docstring
Returns the item's rectangular bounds, including any bleed caused by the item's frame.
The bounds are returned in the item's coordinate system (see Qt's QGraphicsItem docs for
more details about QGraphicsItem coordinate systems). The results differ from Qt's rect()
function, as rect() makes no allowances for the portion of outlines which are drawn
outside of the item.

.. seealso:: estimatedFrameBleed()
:rtype: QRectF
%End

public slots:

virtual void refresh();
Expand Down
16 changes: 16 additions & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -359,6 +359,22 @@ void QgsLayoutItem::setBackgroundColor( const QColor &color )
refreshBackgroundColor( true );
}

double QgsLayoutItem::estimatedFrameBleed() const
{
if ( !hasFrame() )
{
return 0;
}

return pen().widthF() / 2.0;
}

QRectF QgsLayoutItem::rectWithFrame() const
{
double frameBleed = estimatedFrameBleed();
return rect().adjusted( -frameBleed, -frameBleed, frameBleed, frameBleed );
}

QgsLayoutPoint QgsLayoutItem::applyDataDefinedPosition( const QgsLayoutPoint &position )
{
if ( !mLayout )
Expand Down
23 changes: 23 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -347,6 +347,29 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
*/
void setBackgroundColor( const QColor &color );

/**
* Returns the estimated amount the item's frame bleeds outside the item's
* actual rectangle. For instance, if the item has a 2mm frame stroke, then
* 1mm of this frame is drawn outside the item's rect. In this case the
* return value will be 1.0.
*
* Returned values are in layout units.
* \see rectWithFrame()
*/
virtual double estimatedFrameBleed() const;

/**
* Returns the item's rectangular bounds, including any bleed caused by the item's frame.
* The bounds are returned in the item's coordinate system (see Qt's QGraphicsItem docs for
* more details about QGraphicsItem coordinate systems). The results differ from Qt's rect()
* function, as rect() makes no allowances for the portion of outlines which are drawn
* outside of the item.
*
* \see estimatedFrameBleed()
*/
virtual QRectF rectWithFrame() const;

public slots:

/**
Expand Down
36 changes: 35 additions & 1 deletion tests/src/python/test_qgslayoutitem.py
Expand Up @@ -20,7 +20,10 @@
QgsLayoutObject,
QgsProperty,
QgsLayoutMeasurement,
QgsUnitTypes)
QgsUnitTypes,
QgsLayoutPoint,
QgsLayoutSize)
from qgis.PyQt.QtCore import QRectF
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtTest import QSignalSpy

Expand Down Expand Up @@ -101,6 +104,37 @@ def testLocked(self):
item.setLocked(False)
self.assertEqual(len(lock_changed_spy), 2)

def testFrameBleed(self):
layout = QgsLayout(QgsProject.instance())
item = QgsLayoutItemMap(layout)
item.setFrameEnabled(False)
self.assertEqual(item.estimatedFrameBleed(), 0)

item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutMillimeters))
item.setFrameEnabled(False)
self.assertEqual(item.estimatedFrameBleed(), 0)
item.setFrameEnabled(True)
self.assertEqual(item.estimatedFrameBleed(), 5) # only half bleeds out!

item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(item.estimatedFrameBleed(), 50) # only half bleeds out!

def testRectWithFrame(self):
layout = QgsLayout(QgsProject.instance())
item = QgsLayoutItemMap(layout)
item.attemptMove(QgsLayoutPoint(6, 10, QgsUnitTypes.LayoutMillimeters))
item.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))

item.setFrameEnabled(False)
self.assertEqual(item.rectWithFrame(), QRectF(0, 0, 18, 12))
item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutMillimeters))
item.setFrameEnabled(False)
self.assertEqual(item.rectWithFrame(), QRectF(0, 0, 18, 12))
item.setFrameEnabled(True)
self.assertEqual(item.rectWithFrame(), QRectF(-5.0, -5.0, 28.0, 22.0))
item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(item.rectWithFrame(), QRectF(-50.0, -50.0, 118.0, 112.0))


if __name__ == '__main__':
unittest.main()

0 comments on commit 51efa19

Please sign in to comment.