Skip to content

Commit

Permalink
Use title from template as default in layout manager
Browse files Browse the repository at this point in the history
use the template name (if empty or not) as default value in the dialog. user can edit it.
if it is already existing, the user has to change it.
  • Loading branch information
signedav authored and nyalldawson committed Dec 12, 2017
1 parent 253b9d9 commit 1838831
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
14 changes: 12 additions & 2 deletions src/app/composer/qgscomposermanager.cpp
Expand Up @@ -192,6 +192,10 @@ void QgsComposerManager::mAddButton_clicked()
{
QFile templateFile;
bool loadingTemplate = ( mTemplate->currentIndex() > 0 );
QDomDocument templateDoc;

QString currentTitle;

if ( loadingTemplate )
{
if ( mTemplate->currentIndex() == 1 )
Expand All @@ -213,13 +217,20 @@ void QgsComposerManager::mAddButton_clicked()
QMessageBox::warning( this, tr( "Template error" ), tr( "Error, could not read file" ) );
return;
}

if ( templateDoc.setContent( &templateFile, false ) )
{
QDomElement compositionElem = templateDoc.documentElement().firstChildElement( QStringLiteral( "Composition" ) );
if ( !compositionElem.isNull() )
currentTitle = compositionElem.attribute( "name" );
}
}

QgsComposer *newComposer = nullptr;
bool loadedOK = false;

QString title;
if ( !QgisApp::instance()->uniqueComposerTitle( this, title, true ) )
if ( !QgisApp::instance()->uniqueComposerTitle( this, title, true, currentTitle ) )
{
return;
}
Expand All @@ -237,7 +248,6 @@ void QgsComposerManager::mAddButton_clicked()

if ( loadingTemplate )
{
QDomDocument templateDoc;
if ( templateDoc.setContent( &templateFile, false ) )
{
loadedOK = newComposer->loadFromTemplate( templateDoc, true );
Expand Down
26 changes: 16 additions & 10 deletions src/app/layout/qgslayoutmanagerdialog.cpp
Expand Up @@ -201,6 +201,8 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
{
QFile templateFile;
bool loadingTemplate = ( mTemplate->currentIndex() > 0 );
QDomDocument templateDoc;
QString storedTitle;
if ( loadingTemplate )
{
if ( mTemplate->currentIndex() == 1 )
Expand All @@ -222,10 +224,18 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
QMessageBox::warning( this, tr( "Create layout" ), tr( "Could not read template file “%1”." ).arg( templateFile.fileName() ) );
return;
}


if ( templateDoc.setContent( &templateFile, false ) )
{
QDomElement layoutElem = templateDoc.documentElement();
if ( !layoutElem.isNull() )
storedTitle = layoutElem.attribute( "name" );
}
}

QString title;
if ( !QgisApp::instance()->uniqueLayoutTitle( this, title, true ) )
if ( !QgisApp::instance()->uniqueLayoutTitle( this, title, true, storedTitle ) )
{
return;
}
Expand All @@ -238,16 +248,12 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
std::unique_ptr< QgsLayout > layout = qgis::make_unique< QgsLayout >( QgsProject::instance() );
if ( loadingTemplate )
{
QDomDocument templateDoc;
if ( templateDoc.setContent( &templateFile, false ) )
bool loadedOK = false;
( void )layout->loadFromTemplate( templateDoc, QgsReadWriteContext(), true, &loadedOK );
if ( !loadedOK )
{
bool loadedOK = false;
( void )layout->loadFromTemplate( templateDoc, QgsReadWriteContext(), true, &loadedOK );
if ( !loadedOK )
{
QMessageBox::warning( this, tr( "Create layout" ), tr( "Invalid template file “%1”." ).arg( templateFile.fileName() ) );
layout.reset();
}
QMessageBox::warning( this, tr( "Create layout" ), tr( "Invalid template file “%1”." ).arg( templateFile.fileName() ) );
layout.reset();
}
}
else
Expand Down

0 comments on commit 1838831

Please sign in to comment.