Skip to content
Permalink
Browse files

Restore paste in place functionality

  • Loading branch information
nyalldawson committed Dec 4, 2017
1 parent b7e338b commit 6a78d489c0c4cb50513714650459e52e1f9eba7d
Showing with 43 additions and 12 deletions.
  1. +5 −8 src/core/layout/qgslayout.cpp
  2. +1 −0 src/core/layout/qgslayoutitem.cpp
  3. +37 −4 tests/src/python/test_qgslayout.py
@@ -719,7 +719,6 @@ bool QgsLayout::readXml( const QDomElement &layoutElement, const QDomDocument &d

QList< QgsLayoutItem * > QgsLayout::addItemsFromXml( const QDomElement &parentElement, const QDomDocument &document, const QgsReadWriteContext &context, QPointF *position, bool pasteInPlace )
{
std::unique_ptr< QPointF > pasteInPlacePt;
QList< QgsLayoutItem * > newItems;
QList< QgsLayoutMultiFrame * > newMultiFrames;

@@ -729,6 +728,7 @@ QList< QgsLayoutItem * > QgsLayout::addItemsFromXml( const QDomElement &parentEl
int zOrderOffset = mItemsModel->zOrderListSize();

QPointF pasteShiftPos;
int pageNumber = -1;
if ( position )
{
//If we are placing items relative to a certain point, then calculate how much we need
@@ -740,8 +740,7 @@ QList< QgsLayoutItem * > QgsLayout::addItemsFromXml( const QDomElement &parentEl
pasteShiftPos = *position - minItemPos;
if ( pasteInPlace )
{
int pageNumber = mPageCollection->pageNumberForPoint( *position );
pasteInPlacePt = qgis::make_unique< QPointF >( 0, mPageCollection->page( pageNumber )->pos().y() );
pageNumber = mPageCollection->pageNumberForPoint( *position );
}
}

@@ -760,12 +759,10 @@ QList< QgsLayoutItem * > QgsLayout::addItemsFromXml( const QDomElement &parentEl
item->readXml( currentItemElem, document, context );
if ( position )
{
if ( pasteInPlacePt )
if ( pasteInPlace )
{
#if 0 //TODO
item->setItemPosition( newLabel->pos().x(), std::fmod( newLabel->pos().y(), ( paperHeight() + spaceBetweenPages() ) ) );
item->move( pasteInPlacePt->x(), pasteInPlacePt->y() );
#endif
QgsLayoutPoint posOnPage = QgsLayoutPoint::decodePoint( currentItemElem.attribute( QStringLiteral( "positionOnPage" ) ) );
item->attemptMove( posOnPage, true, false, pageNumber );
}
else
{
@@ -530,6 +530,7 @@ bool QgsLayoutItem::writeXml( QDomElement &parentElement, QDomDocument &doc, con
element.setAttribute( QStringLiteral( "id" ), mId );
element.setAttribute( QStringLiteral( "referencePoint" ), QString::number( static_cast< int >( mReferencePoint ) ) );
element.setAttribute( QStringLiteral( "position" ), mItemPosition.encodePoint() );
element.setAttribute( QStringLiteral( "positionOnPage" ), pagePositionWithUnits().encodePoint() );
element.setAttribute( QStringLiteral( "size" ), mItemSize.encodeSize() );
element.setAttribute( QStringLiteral( "itemRotation" ), QString::number( mItemRotation ) );
element.setAttribute( QStringLiteral( "groupUuid" ), mParentGroupUuid );
@@ -22,6 +22,7 @@
QgsLayoutObject,
QgsProject,
QgsLayoutItemGroup,
QgsLayoutItem,
QgsProperty,
QgsLayoutPageCollection,
QgsLayoutMeasurement,
@@ -130,9 +131,9 @@ def testAddItemsFromXml(self):
new_item1 = [i for i in items if i.id() == 'xxyyxx'][0]
new_item2 = [i for i in items if i.id() == 'zzyyzz'][0]
self.assertEqual(new_item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.sizeWithUnits(),QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item2.positionWithUnits(), QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.sizeWithUnits(),QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))

# test with a group
group = QgsLayoutItemGroup(l)
@@ -165,9 +166,41 @@ def testAddItemsFromXml(self):
new_item1 = [i for i in items if i.id() == 'xxyyxx'][0]
new_item2 = [i for i in items if i.id() == 'zzyyzz'][0]
self.assertEqual(new_item1.positionWithUnits(), QgsLayoutPoint(10, 30, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.sizeWithUnits(),QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item2.positionWithUnits(), QgsLayoutPoint(2.0, 4.0, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.sizeWithUnits(),QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))

# paste in place
l4 = QgsLayout(p)
page = QgsLayoutItemPage(l)
page.setPageSize('A3')
l4.pageCollection().addPage(page)
page = QgsLayoutItemPage(l)
page.setPageSize('A6')
l4.pageCollection().addPage(page)

new_items = l4.addItemsFromXml(elem, doc, QgsReadWriteContext(), QPointF(10, 30), True)
self.assertEqual(len(new_items), 3)
new_item1 = [i for i in new_items if i.id() == 'xxyyxx'][0]
new_item2 = [i for i in new_items if i.id() == 'zzyyzz'][0]
self.assertEqual(new_item1.pagePositionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.page(), 0)
self.assertEqual(new_item2.pagePositionWithUnits(), QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.page(), 0)

# paste in place, page 2
new_items = l4.addItemsFromXml(elem, doc, QgsReadWriteContext(), QPointF(10, 550), True)
self.assertEqual(len(new_items), 3)
new_item1 = [i for i in new_items if i.id() == 'xxyyxx'][0]
new_item2 = [i for i in new_items if i.id() == 'zzyyzz'][0]
self.assertEqual(new_item1.pagePositionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item1.page(), 1)
self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
self.assertEqual(new_item2.pagePositionWithUnits(), QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters))
self.assertEqual(new_item2.page(), 1)
self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))

#TODO - test restoring multiframe

0 comments on commit 6a78d48

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