@@ -5816,30 +5816,36 @@ void QgisApp::openTemplate( const QString &fileName )
5816
5816
return ;
5817
5817
}
5818
5818
5819
- QString title ;
5820
- if ( !uniqueLayoutTitle ( this , title, true , QgsMasterLayoutInterface::PrintLayout ) )
5819
+ QDomDocument templateDoc ;
5820
+ if ( !templateDoc. setContent ( &templateFile, false ) )
5821
5821
{
5822
+ messageBar ()->pushMessage ( tr ( " Load template" ), tr ( " Could not load template file" ), Qgis::Warning );
5822
5823
return ;
5823
5824
}
5824
5825
5825
- QgsLayoutDesignerDialog *designer = createNewPrintLayout ( title );
5826
- if ( !designer )
5826
+ QString title;
5827
+ QDomElement layoutElem = templateDoc.documentElement ();
5828
+ if ( !layoutElem.isNull () )
5829
+ title = layoutElem.attribute ( QStringLiteral ( " name" ) );
5830
+
5831
+ if ( !uniqueLayoutTitle ( this , title, true , QgsMasterLayoutInterface::PrintLayout, title ) )
5827
5832
{
5828
- messageBar ()->pushMessage ( tr ( " Load template" ), tr ( " Could not create print layout" ), Qgis::Warning );
5829
5833
return ;
5830
5834
}
5831
5835
5836
+ // create new layout object
5837
+ std::unique_ptr< QgsPrintLayout > layout = qgis::make_unique< QgsPrintLayout >( QgsProject::instance () );
5832
5838
bool loadedOk = false ;
5833
- QDomDocument templateDoc;
5834
- if ( templateDoc. setContent ( &templateFile, false ) )
5839
+ layout-> loadFromTemplate ( templateDoc, QgsReadWriteContext (), true , &loadedOk ) ;
5840
+ if ( loadedOk )
5835
5841
{
5836
- designer->currentLayout ()->loadFromTemplate ( templateDoc, QgsReadWriteContext (), true , &loadedOk );
5837
- designer->activate ();
5838
- }
5842
+ layout->setName ( title );
5839
5843
5840
- if ( !loadedOk )
5844
+ openLayoutDesignerDialog ( layout.get () );
5845
+ QgsProject::instance ()->layoutManager ()->addLayout ( layout.release () );
5846
+ }
5847
+ else
5841
5848
{
5842
- designer->close ();
5843
5849
messageBar ()->pushMessage ( tr ( " Load template" ), tr ( " Could not load template file" ), Qgis::Warning );
5844
5850
}
5845
5851
}
0 commit comments