Skip to content
Permalink
Browse files

Save and restore layouts with project

  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent e5e37fc commit 61c57bc20e69e035a31ce390fa365bf3e14a8774
Showing with 46 additions and 0 deletions.
  1. +30 −0 src/core/composer/qgslayoutmanager.cpp
  2. +16 −0 tests/src/python/test_qgslayoutmanager.py
@@ -184,6 +184,29 @@ bool QgsLayoutManager::readXml( const QDomElement &element, const QDomDocument &
c->setName( legacyTitle );
result = result && addComposition( c );
}

QgsReadWriteContext context;
context.setPathResolver( mProject->pathResolver() );

// restore layouts
const QDomNodeList layoutNodes = element.elementsByTagName( QStringLiteral( "Layout" ) );
for ( int i = 0; i < layoutNodes.size(); ++i )
{
std::unique_ptr< QgsLayout > l = qgis::make_unique< QgsLayout >( mProject );
if ( !l->readXml( layoutNodes.at( i ).toElement(), doc, context ) )
{
result = false;
continue;
}
if ( addLayout( l.get() ) )
{
( void )l.release(); // ownership was transferred successfully
}
else
{
result = false;
}
}
return result;
}

@@ -199,6 +222,13 @@ QDomElement QgsLayoutManager::writeXml( QDomDocument &doc ) const
c->writeXml( composerElem, doc );
c->atlasComposition().writeXml( composerElem, doc );
}
QgsReadWriteContext context;
context.setPathResolver( mProject->pathResolver() );
for ( QgsLayout *l : mLayouts )
{
QDomElement layoutElem = l->writeXml( doc, context );
layoutsElem.appendChild( layoutElem );
}
return layoutsElem;
}

@@ -304,6 +304,18 @@ def testReadWriteXml(self):
manager.addComposition(composition2)
manager.addComposition(composition3)

# add a bunch of layouts
layout = QgsLayout(project)
layout.setName('test layout')
layout2 = QgsLayout(project)
layout2.setName('test layout2')
layout3 = QgsLayout(project)
layout3.setName('test layout3')

manager.addLayout(layout)
manager.addLayout(layout2)
manager.addLayout(layout3)

# save to xml
doc = QDomDocument("testdoc")
elem = manager.writeXml(doc)
@@ -318,6 +330,10 @@ def testReadWriteXml(self):
names = [c.name() for c in manager2.compositions()]
self.assertEqual(set(names), {'test composition', 'test composition2', 'test composition3'})

self.assertEqual(len(manager2.layouts()), 3)
names = [c.name() for c in manager2.layouts()]
self.assertCountEqual(names, ['test layout', 'test layout2', 'test layout3'])

def testSaveAsTemplate(self):
"""
Test saving composition as template

0 comments on commit 61c57bc

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