Skip to content
Permalink
Browse files

Merge pull request #10011 from m-kuhn/save_to_templates

Add save to templates button
  • Loading branch information
m-kuhn committed May 16, 2019
2 parents a2aaf60 + 81771dd commit e07741f00b7d69cada35521f10d3703ff404083f
Showing with 52 additions and 0 deletions.
  1. +52 −0 src/app/qgisapp.cpp
@@ -4254,6 +4254,13 @@ void QgisApp::saveRecentProjectPath( bool savePreviewImage )
// Get canonical absolute path
QgsWelcomePageItemsModel::RecentProjectData projectData;
projectData.path = QgsProject::instance()->absoluteFilePath();
QString templateDirName = QgsSettings().value( QStringLiteral( "qgis/projectTemplateDir" ),
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();

// We don't want the template path to appear in the recent projects list. Never.
if ( projectData.path.startsWith( templateDirName ) )
return;

if ( projectData.path.isEmpty() ) // in case of custom project storage
projectData.path = QgsProject::instance()->fileName();
projectData.title = QgsProject::instance()->title();
{
menu->clear();
const QList<QgsProjectStorage *> storages = QgsApplication::projectStorageRegistry()->projectStorages();
QAction *action = menu->addAction( tr( "Templates" ) + QChar( 0x2026 ) ); // 0x2026 = ellipsis character
connect( action, &QAction::triggered, this, [ this ]
{
QgsSettings settings;
QString templateDirName = settings.value( QStringLiteral( "qgis/projectTemplateDir" ),
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();

const QString originalFilename = QgsProject::instance()->fileName();
QString templateName = QFileInfo( originalFilename ).baseName();

if ( templateName.isEmpty() )
{
bool ok;
templateName = QInputDialog::getText( this, tr( "Template Name" ),
tr( "Name for the template" ), QLineEdit::Normal,
QString(), &ok );

if ( !ok )
return;
if ( templateName.isEmpty() )
{
messageBar()->pushInfo( tr( "Template not saved" ), tr( "The template can not have an empty name." ) );
}
}
const QString filePath = templateDirName + QDir::separator() + templateName + QStringLiteral( ".qgz" );
if ( QFileInfo( filePath ).exists() )
{
QMessageBox msgBox( this );
msgBox.setWindowTitle( tr( "Overwrite template" ) );
msgBox.setText( tr( "The template %1 already exists, do you want to replace it?" ).arg( templateName ) );
msgBox.addButton( tr( "Overwrite" ), QMessageBox::YesRole );
auto cancelButton = msgBox.addButton( QMessageBox::Cancel );
msgBox.setIcon( QMessageBox::Question );
msgBox.exec();
if ( msgBox.clickedButton() == cancelButton )
{
return;
}
}

QgsProject::instance()->write( filePath );
QgsProject::instance()->setFileName( originalFilename );
messageBar()->pushInfo( tr( "Template saved" ), tr( "Template %1 was saved" ).arg( templateName ) );

} );
for ( QgsProjectStorage *storage : storages )
{
QString name = storage->visibleName();

0 comments on commit e07741f

Please sign in to comment.
You can’t perform that action at this time.