@@ -4254,6 +4254,13 @@ void QgisApp::saveRecentProjectPath( bool savePreviewImage )
4254
4254
// Get canonical absolute path
4255
4255
QgsWelcomePageItemsModel::RecentProjectData projectData;
4256
4256
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
+
4257
4264
if ( projectData.path.isEmpty() ) // in case of custom project storage
4258
4265
projectData.path = QgsProject::instance()->fileName();
4259
4266
projectData.title = QgsProject::instance()->title();
@@ -14430,6 +14437,36 @@ void QgisApp::populateProjectStorageMenu( QMenu *menu, bool saving )
14430
14437
{
14431
14438
menu->clear();
14432
14439
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
+ } );
14433
14470
for ( QgsProjectStorage *storage : storages )
14434
14471
{
14435
14472
QString name = storage->visibleName();
0 commit comments