Skip to content

Commit 1838831

Browse files
signedavnyalldawson
authored andcommitted
Use title from template as default in layout manager
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.
1 parent 253b9d9 commit 1838831

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/app/composer/qgscomposermanager.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ void QgsComposerManager::mAddButton_clicked()
192192
{
193193
QFile templateFile;
194194
bool loadingTemplate = ( mTemplate->currentIndex() > 0 );
195+
QDomDocument templateDoc;
196+
197+
QString currentTitle;
198+
195199
if ( loadingTemplate )
196200
{
197201
if ( mTemplate->currentIndex() == 1 )
@@ -213,13 +217,20 @@ void QgsComposerManager::mAddButton_clicked()
213217
QMessageBox::warning( this, tr( "Template error" ), tr( "Error, could not read file" ) );
214218
return;
215219
}
220+
221+
if ( templateDoc.setContent( &templateFile, false ) )
222+
{
223+
QDomElement compositionElem = templateDoc.documentElement().firstChildElement( QStringLiteral( "Composition" ) );
224+
if ( !compositionElem.isNull() )
225+
currentTitle = compositionElem.attribute( "name" );
226+
}
216227
}
217228

218229
QgsComposer *newComposer = nullptr;
219230
bool loadedOK = false;
220231

221232
QString title;
222-
if ( !QgisApp::instance()->uniqueComposerTitle( this, title, true ) )
233+
if ( !QgisApp::instance()->uniqueComposerTitle( this, title, true, currentTitle ) )
223234
{
224235
return;
225236
}
@@ -237,7 +248,6 @@ void QgsComposerManager::mAddButton_clicked()
237248

238249
if ( loadingTemplate )
239250
{
240-
QDomDocument templateDoc;
241251
if ( templateDoc.setContent( &templateFile, false ) )
242252
{
243253
loadedOK = newComposer->loadFromTemplate( templateDoc, true );

src/app/layout/qgslayoutmanagerdialog.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
201201
{
202202
QFile templateFile;
203203
bool loadingTemplate = ( mTemplate->currentIndex() > 0 );
204+
QDomDocument templateDoc;
205+
QString storedTitle;
204206
if ( loadingTemplate )
205207
{
206208
if ( mTemplate->currentIndex() == 1 )
@@ -222,10 +224,18 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
222224
QMessageBox::warning( this, tr( "Create layout" ), tr( "Could not read template file “%1”." ).arg( templateFile.fileName() ) );
223225
return;
224226
}
227+
228+
229+
if ( templateDoc.setContent( &templateFile, false ) )
230+
{
231+
QDomElement layoutElem = templateDoc.documentElement();
232+
if ( !layoutElem.isNull() )
233+
storedTitle = layoutElem.attribute( "name" );
234+
}
225235
}
226236

227237
QString title;
228-
if ( !QgisApp::instance()->uniqueLayoutTitle( this, title, true ) )
238+
if ( !QgisApp::instance()->uniqueLayoutTitle( this, title, true, storedTitle ) )
229239
{
230240
return;
231241
}
@@ -238,16 +248,12 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
238248
std::unique_ptr< QgsLayout > layout = qgis::make_unique< QgsLayout >( QgsProject::instance() );
239249
if ( loadingTemplate )
240250
{
241-
QDomDocument templateDoc;
242-
if ( templateDoc.setContent( &templateFile, false ) )
251+
bool loadedOK = false;
252+
( void )layout->loadFromTemplate( templateDoc, QgsReadWriteContext(), true, &loadedOK );
253+
if ( !loadedOK )
243254
{
244-
bool loadedOK = false;
245-
( void )layout->loadFromTemplate( templateDoc, QgsReadWriteContext(), true, &loadedOK );
246-
if ( !loadedOK )
247-
{
248-
QMessageBox::warning( this, tr( "Create layout" ), tr( "Invalid template file “%1”." ).arg( templateFile.fileName() ) );
249-
layout.reset();
250-
}
255+
QMessageBox::warning( this, tr( "Create layout" ), tr( "Invalid template file “%1”." ).arg( templateFile.fileName() ) );
256+
layout.reset();
251257
}
252258
}
253259
else

0 commit comments

Comments
 (0)