mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-28 00:17:30 -05:00
Save and restore layouts with project
This commit is contained in:
parent
e5e37fcafa
commit
61c57bc20e
@ -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 @@ class TestQgsLayoutManager(unittest.TestCase):
|
||||
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 @@ class TestQgsLayoutManager(unittest.TestCase):
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user