Skip to content

Commit

Permalink
Restore paste in place functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent b7e338b commit 6a78d48
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
13 changes: 5 additions & 8 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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 );
}
}

Expand All @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -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 );
Expand Down
41 changes: 37 additions & 4 deletions tests/src/python/test_qgslayout.py
Expand Up @@ -22,6 +22,7 @@
QgsLayoutObject,
QgsProject,
QgsLayoutItemGroup,
QgsLayoutItem,
QgsProperty,
QgsLayoutPageCollection,
QgsLayoutMeasurement,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 6a78d48

Please sign in to comment.