Skip to content

Commit 8a24d14

Browse files
committedMay 15, 2019
Add save to templates button
1 parent 5844a0f commit 8a24d14

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,6 +4254,13 @@ void QgisApp::saveRecentProjectPath( bool savePreviewImage )
42544254
// Get canonical absolute path
42554255
QgsWelcomePageItemsModel::RecentProjectData projectData;
42564256
projectData.path = QgsProject::instance()->absoluteFilePath();
4257+
QString templateDirName = QgsSettings().value( QStringLiteral( "qgis/projectTemplateDir" ),
4258+
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
4259+
4260+
// We don't want the template path to appear in the recent projects list. Never.
4261+
if ( projectData.path.startsWith( templateDirName ) )
4262+
return;
4263+
42574264
if ( projectData.path.isEmpty() ) // in case of custom project storage
42584265
projectData.path = QgsProject::instance()->fileName();
42594266
projectData.title = QgsProject::instance()->title();
@@ -14430,6 +14437,36 @@ void QgisApp::populateProjectStorageMenu( QMenu *menu, bool saving )
1443014437
{
1443114438
menu->clear();
1443214439
const QList<QgsProjectStorage *> storages = QgsApplication::projectStorageRegistry()->projectStorages();
14440+
QAction *action = menu->addAction( tr( "Templates" ) + QChar( 0x2026 ) ); // 0x2026 = ellipsis character
14441+
connect( action, &QAction::triggered, this, [ this ]
14442+
{
14443+
QgsSettings settings;
14444+
QString templateDirName = settings.value( QStringLiteral( "qgis/projectTemplateDir" ),
14445+
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
14446+
14447+
const QString originalFilename = QgsProject::instance()->fileName();
14448+
const QString templateName = QFileInfo( originalFilename ).baseName();
14449+
const QString filePath = templateDirName + QDir::separator() + templateName + QStringLiteral( ".qgz" );
14450+
if ( QFileInfo( filePath ).exists() )
14451+
{
14452+
QMessageBox msgBox( this );
14453+
msgBox.setWindowTitle( tr( "Overwrite template" ) );
14454+
msgBox.setText( tr( "The template %1 already exists, do you want to replace it?" ).arg( templateName ) );
14455+
msgBox.addButton( tr( "Overwrite" ), QMessageBox::YesRole );
14456+
auto cancelButton = msgBox.addButton( QMessageBox::Cancel );
14457+
msgBox.setIcon( QMessageBox::Question );
14458+
msgBox.exec();
14459+
if ( msgBox.clickedButton() == cancelButton )
14460+
{
14461+
return;
14462+
}
14463+
}
14464+
14465+
QgsProject::instance()->write( filePath );
14466+
QgsProject::instance()->setFileName( originalFilename );
14467+
messageBar()->pushInfo( tr( "Template saved" ), tr( "Template %1 was saved" ).arg( templateName ) );
14468+
14469+
} );
1443314470
for ( QgsProjectStorage *storage : storages )
1443414471
{
1443514472
QString name = storage->visibleName();

0 commit comments

Comments
 (0)
Please sign in to comment.