Skip to content

Commit

Permalink
Merge pull request qgis#55440 from nirvn/qgis_project_save
Browse files Browse the repository at this point in the history
[ui] Let typed QGIS project file extension dictate the type when saving projects
  • Loading branch information
lbartoletti authored Dec 15, 2023
2 parents b0bb44e + 3a0f6d8 commit cef1363
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 58 deletions.
115 changes: 59 additions & 56 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6386,10 +6386,9 @@ void QgisApp::fileOpen()
QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastProjectDir" ), QDir::homePath() ).toString();


QStringList fileFilters;
QStringList extensions;
fileFilters << tr( "QGIS files" ) + QStringLiteral( " (*.qgs *.qgz *.QGS *.QGZ)" );
fileFilters << tr( "QGIS Project Formats" ) + QStringLiteral( " (*.qgz *.QGZ *.qgs *.QGS)" );
extensions << QStringLiteral( "qgs" ) << QStringLiteral( "qgz" );
for ( QgsCustomProjectOpenHandler *handler : std::as_const( mCustomProjectOpenHandlers ) )
{
Expand Down Expand Up @@ -6636,45 +6635,47 @@ bool QgisApp::fileSave()
QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastProjectDir" ), QDir::homePath() ).toString();

const QString qgsExt = tr( "QGIS files" ) + " (*.qgs)";
const QString zipExt = tr( "QGZ files" ) + " (*.qgz)";

QString exts;
Qgis::ProjectFileFormat defaultProjectFileFormat = settings.enumValue( QStringLiteral( "/qgis/defaultProjectFileFormat" ), Qgis::ProjectFileFormat::Qgz );
switch ( defaultProjectFileFormat )
{
case Qgis::ProjectFileFormat::Qgs:
{
exts = qgsExt + QStringLiteral( ";;" ) + zipExt;
break;
}
case Qgis::ProjectFileFormat::Qgz:
{
exts = zipExt + QStringLiteral( ";;" ) + qgsExt;
}
}
const QString qgisProjectExt = tr( "QGIS Project Formats" ) + ( defaultProjectFileFormat == Qgis::ProjectFileFormat::Qgz ? " (*.qgz *.QGZ *.qgs *.QGS)" : " (*.qgs *.QGS *.qgz *.QGZ)" );
const QString qgzProjectExt = tr( "QGIS Bundled Project Format" ) + " (*.qgz *.QGZ)";
const QString qgsProjectExt = tr( "QGIS XML Project Format" ) + " (*.qgs *.QGS)";

QString filter;
QString path = QFileDialog::getSaveFileName(
this,
tr( "Choose a QGIS project file" ),
lastUsedDir + '/' + QgsProject::instance()->title(),
exts, &filter );
qgisProjectExt + QStringLiteral( ";;" ) + qgzProjectExt + QStringLiteral( ";;" ) + qgsProjectExt, &filter );
if ( path.isEmpty() )
return false;

QFileInfo fullPath;
fullPath.setFile( path );
QFileInfo fullPath( path );
QgsSettings().setValue( QStringLiteral( "UI/lastProjectDir" ), fullPath.path() );

// make sure we have the .qgs extension in the file name
if ( filter == zipExt )
const QString ext = fullPath.suffix().toLower();
if ( filter == qgisProjectExt && ext != QLatin1String( "qgz" ) && ext != QLatin1String( "qgs" ) )
{
if ( fullPath.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) != 0 )
fullPath.setFile( fullPath.filePath() + ".qgz" );
switch ( defaultProjectFileFormat )
{
case Qgis::ProjectFileFormat::Qgs:
{
fullPath.setFile( fullPath.filePath() + ".qgs" );
break;
}
case Qgis::ProjectFileFormat::Qgz:
{
fullPath.setFile( fullPath.filePath() + ".qgz" );
break;
}
}
}
else
else if ( filter == qgzProjectExt && ext != QLatin1String( "qgz" ) )
{
if ( fullPath.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) != 0 )
fullPath.setFile( fullPath.filePath() + ".qgs" );
fullPath.setFile( fullPath.filePath() + ".qgz" );
}
else if ( filter == qgsProjectExt && ext != QLatin1String( "qgs" ) )
{
fullPath.setFile( fullPath.filePath() + ".qgs" );
}

QgsProject::instance()->setFileName( fullPath.filePath() );
Expand Down Expand Up @@ -6750,45 +6751,47 @@ void QgisApp::fileSaveAs()
defaultPath += QString( '/' + QgsProject::instance()->title() );
}

const QString qgsExt = tr( "QGIS files" ) + " (*.qgs *.QGS)";
const QString zipExt = tr( "QGZ files" ) + " (*.qgz)";

QString exts;
Qgis::ProjectFileFormat defaultProjectFileFormat = settings.enumValue( QStringLiteral( "/qgis/defaultProjectFileFormat" ), Qgis::ProjectFileFormat::Qgz );
switch ( defaultProjectFileFormat )
{
case Qgis::ProjectFileFormat::Qgs:
{
exts = qgsExt + QStringLiteral( ";;" ) + zipExt;
break;
}
case Qgis::ProjectFileFormat::Qgz:
{
exts = zipExt + QStringLiteral( ";;" ) + qgsExt;
break;
}
}
const QString qgisProjectExt = tr( "QGIS Project Formats" ) + ( defaultProjectFileFormat == Qgis::ProjectFileFormat::Qgz ? " (*.qgz *.QGZ *.qgs *.QGS)" : " (*.qgs *.QGS *.qgz *.QGZ)" );
const QString qgzProjectExt = tr( "QGIS Bundled Project Format" ) + " (*.qgz *.QGZ)";
const QString qgsProjectExt = tr( "QGIS XML Project Format" ) + " (*.qgs *.QGS)";

QString filter;
QString path = QFileDialog::getSaveFileName( this,
tr( "Save Project As" ),
defaultPath,
exts, &filter );
QString path = QFileDialog::getSaveFileName(
this,
tr( "Save Project As" ),
defaultPath,
qgisProjectExt + QStringLiteral( ";;" ) + qgzProjectExt + QStringLiteral( ";;" ) + qgsProjectExt, &filter );
if ( path.isEmpty() )
return;

QFileInfo fullPath( path );

QgsSettings().setValue( QStringLiteral( "UI/lastProjectDir" ), fullPath.path() );

if ( filter == zipExt )
const QString ext = fullPath.suffix().toLower();
if ( filter == qgisProjectExt && ext != QLatin1String( "qgz" ) && ext != QLatin1String( "qgs" ) )
{
if ( fullPath.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) != 0 )
fullPath.setFile( fullPath.filePath() + ".qgz" );
switch ( defaultProjectFileFormat )
{
case Qgis::ProjectFileFormat::Qgs:
{
fullPath.setFile( fullPath.filePath() + ".qgs" );
break;
}
case Qgis::ProjectFileFormat::Qgz:
{
fullPath.setFile( fullPath.filePath() + ".qgz" );
break;
}
}
}
else // .qgs
else if ( filter == qgzProjectExt && ext != QLatin1String( "qgz" ) )
{
if ( fullPath.suffix().compare( QLatin1String( "qgs" ), Qt::CaseInsensitive ) != 0 )
fullPath.setFile( fullPath.filePath() + ".qgs" );
fullPath.setFile( fullPath.filePath() + ".qgz" );
}
else if ( filter == qgsProjectExt && ext != QLatin1String( "qgs" ) )
{
fullPath.setFile( fullPath.filePath() + ".qgs" );
}

QgsProject::instance()->setFileName( fullPath.filePath() );
Expand Down
4 changes: 2 additions & 2 deletions src/ui/qgsoptionsbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@
<item row="0" column="1">
<widget class="QRadioButton" name="mFileFormatQgzButton">
<property name="text">
<string>QGZ Archive file format, embeds auxiliary data</string>
<string>QGZ Bundled project format, compressed into a single zip file, embeds auxiliary data</string>
</property>
<attribute name="buttonGroup">
<string notr="true">mDefaultProjectFileFormatButtonGroup</string>
Expand All @@ -879,7 +879,7 @@
<string>The auxiliary data will be kept in a separate .qgd data file which must be distributed along with the .qgs project file.</string>
</property>
<property name="text">
<string>QGS Project saved in a clear text, does not embed auxiliary data</string>
<string>QGS XML project format, saved in a clear text, does not embed auxiliary data</string>
</property>
<attribute name="buttonGroup">
<string notr="true">mDefaultProjectFileFormatButtonGroup</string>
Expand Down

0 comments on commit cef1363

Please sign in to comment.