Port item frame related code from composer

This commit is contained in:
Nyall Dawson 2017-09-29 14:51:14 +10:00
parent 0b188295bc
commit 51efa19f75
4 changed files with 99 additions and 1 deletions

View File

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

View File

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

View File

@ -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:
/**

View File

@ -20,7 +20,10 @@ from qgis.core import (QgsProject,
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
@ -101,6 +104,37 @@ class TestQgsLayoutItem(unittest.TestCase):
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()