Skip to content

Commit 58548e0

Browse files
committedJun 22, 2018
[layouts] Save last used export folder in project
And use it in preference to the global last used export folder. Because GIS use is typically specific project based, and it makes little sense to default to an output folder related to a different project.
1 parent feea121 commit 58548e0

File tree

3 files changed

+69
-40
lines changed

3 files changed

+69
-40
lines changed
 

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,14 +1831,17 @@ void QgsLayoutDesignerDialog::exportToRaster()
18311831
if ( !showFileSizeWarning() )
18321832
return;
18331833

1834-
QgsSettings s;
1835-
QString outputFileName = QgsFileUtils::stringToSafeFilename( mMasterLayout->name() );
1834+
QString outputFileName;
18361835
QgsLayoutAtlas *printAtlas = atlas();
1836+
QString lastUsedDir = defaultExportPath();
18371837
if ( printAtlas && printAtlas->enabled() && mActionAtlasPreview->isChecked() )
18381838
{
1839-
QString lastUsedDir = s.value( QStringLiteral( "lastSaveAsImageDir" ), QDir::homePath(), QgsSettings::App ).toString();
18401839
outputFileName = QDir( lastUsedDir ).filePath( QgsFileUtils::stringToSafeFilename( printAtlas->currentFilename() ) );
18411840
}
1841+
else
1842+
{
1843+
outputFileName = QDir( lastUsedDir ).filePath( QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) );
1844+
}
18421845

18431846
#ifdef Q_OS_MAC
18441847
QgisApp::instance()->activateWindow();
@@ -1852,6 +1855,8 @@ void QgsLayoutDesignerDialog::exportToRaster()
18521855
return;
18531856
}
18541857

1858+
setLastExportPath( fileNExt.first );
1859+
18551860
QgsLayoutExporter::ImageExportSettings settings;
18561861
QSize imageSize;
18571862
if ( !getRasterExportSettings( settings, imageSize ) )
@@ -1921,19 +1926,17 @@ void QgsLayoutDesignerDialog::exportToPdf()
19211926
showForceVectorWarning();
19221927
}
19231928

1924-
QgsSettings settings;
1925-
QString lastUsedFile = settings.value( QStringLiteral( "lastSaveAsPdfFile" ), QStringLiteral( "qgis.pdf" ), QgsSettings::App ).toString();
1926-
QFileInfo file( lastUsedFile );
1929+
const QString exportPath = defaultExportPath();
19271930
QString outputFileName;
19281931

19291932
QgsLayoutAtlas *printAtlas = atlas();
19301933
if ( printAtlas && printAtlas->enabled() && mActionAtlasPreview->isChecked() )
19311934
{
1932-
outputFileName = QDir( file.path() ).filePath( QgsFileUtils::stringToSafeFilename( printAtlas->currentFilename() ) + QStringLiteral( ".pdf" ) );
1935+
outputFileName = QDir( exportPath ).filePath( QgsFileUtils::stringToSafeFilename( printAtlas->currentFilename() ) + QStringLiteral( ".pdf" ) );
19331936
}
19341937
else
19351938
{
1936-
outputFileName = file.path() + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".pdf" );
1939+
outputFileName = exportPath + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".pdf" );
19371940
}
19381941

19391942
#ifdef Q_OS_MAC
@@ -1956,7 +1959,7 @@ void QgsLayoutDesignerDialog::exportToPdf()
19561959
outputFileName += QLatin1String( ".pdf" );
19571960
}
19581961

1959-
settings.setValue( QStringLiteral( "lastSaveAsPdfFile" ), outputFileName, QgsSettings::App );
1962+
setLastExportPath( outputFileName );
19601963

19611964
mView->setPaintingEnabled( false );
19621965
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );
@@ -2025,19 +2028,17 @@ void QgsLayoutDesignerDialog::exportToSvg()
20252028

20262029
showSvgExportWarning();
20272030

2028-
QgsSettings settings;
2029-
QString lastUsedFile = settings.value( QStringLiteral( "lastSaveAsSvgFile" ), QStringLiteral( "qgis.svg" ), QgsSettings::App ).toString();
2030-
QFileInfo file( lastUsedFile );
2031+
const QString defaultPath = defaultExportPath();
20312032
QString outputFileName = QgsFileUtils::stringToSafeFilename( mMasterLayout->name() );
20322033

20332034
QgsLayoutAtlas *printAtlas = atlas();
20342035
if ( printAtlas && printAtlas->enabled() && mActionAtlasPreview->isChecked() )
20352036
{
2036-
outputFileName = QDir( file.path() ).filePath( QgsFileUtils::stringToSafeFilename( printAtlas->currentFilename() + QStringLiteral( ".svg" ) ) );
2037+
outputFileName = QDir( defaultPath ).filePath( QgsFileUtils::stringToSafeFilename( printAtlas->currentFilename() + QStringLiteral( ".svg" ) ) );
20372038
}
20382039
else
20392040
{
2040-
outputFileName = file.path() + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".svg" );
2041+
outputFileName = defaultPath + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".svg" );
20412042
}
20422043

20432044
#ifdef Q_OS_MAC
@@ -2061,7 +2062,7 @@ void QgsLayoutDesignerDialog::exportToSvg()
20612062
}
20622063

20632064
bool prevSettingLabelsAsOutlines = mLayout->project()->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), true );
2064-
settings.setValue( QStringLiteral( "lastSaveAsSvgFile" ), outputFileName, QgsSettings::App );
2065+
setLastExportPath( outputFileName );
20652066

20662067
QgsLayoutExporter::SvgExportSettings svgSettings;
20672068
bool exportAsText = false;
@@ -2437,8 +2438,7 @@ void QgsLayoutDesignerDialog::exportAtlasToRaster()
24372438
printAtlas->setFilenameExpression( QStringLiteral( "'output_'||@atlas_featurenumber" ), error );
24382439
}
24392440

2440-
QgsSettings s;
2441-
QString lastUsedDir = s.value( QStringLiteral( "lastSaveAtlasAsImagesDir" ), QDir::homePath(), QgsSettings::App ).toString();
2441+
QString lastUsedDir = defaultExportPath();
24422442

24432443
QFileDialog dlg( this, tr( "Export Atlas to Directory" ) );
24442444
dlg.setFileMode( QFileDialog::Directory );
@@ -2461,7 +2461,7 @@ void QgsLayoutDesignerDialog::exportAtlasToRaster()
24612461
{
24622462
return;
24632463
}
2464-
s.setValue( QStringLiteral( "lastSaveAtlasAsImagesDir" ), dir, QgsSettings::App );
2464+
setLastExportPath( dir );
24652465

24662466
// test directory (if it exists and is writable)
24672467
if ( !QDir( dir ).exists() || !QFileInfo( dir ).isWritable() )
@@ -2592,8 +2592,7 @@ void QgsLayoutDesignerDialog::exportAtlasToSvg()
25922592
printAtlas->setFilenameExpression( QStringLiteral( "'output_'||@atlas_featurenumber" ), error );
25932593
}
25942594

2595-
QgsSettings s;
2596-
QString lastUsedDir = s.value( QStringLiteral( "lastSaveAtlasAsSvgDir" ), QDir::homePath(), QgsSettings::App ).toString();
2595+
QString lastUsedDir = defaultExportPath();
25972596

25982597
QFileDialog dlg( this, tr( "Export Atlas to Directory" ) );
25992598
dlg.setFileMode( QFileDialog::Directory );
@@ -2619,7 +2618,7 @@ void QgsLayoutDesignerDialog::exportAtlasToSvg()
26192618
{
26202619
return;
26212620
}
2622-
s.setValue( QStringLiteral( "lastSaveAtlasAsSvgDir" ), dir, QgsSettings::App );
2621+
setLastExportPath( dir );
26232622

26242623
// test directory (if it exists and is writable)
26252624
if ( !QDir( dir ).exists() || !QFileInfo( dir ).isWritable() )
@@ -2754,13 +2753,11 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
27542753
bool singleFile = mLayout->customProperty( QStringLiteral( "singleFile" ), true ).toBool();
27552754

27562755
QString outputFileName;
2757-
QgsSettings settings;
27582756
QString dir;
27592757
if ( singleFile )
27602758
{
2761-
QString lastUsedFile = settings.value( QStringLiteral( "lastSaveAsPdfFile" ), QStringLiteral( "qgis.pdf" ), QgsSettings::App ).toString();
2762-
QFileInfo file( lastUsedFile );
2763-
outputFileName = file.path() + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".pdf" );
2759+
const QString defaultPath = defaultExportPath();
2760+
outputFileName = defaultPath + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".pdf" );
27642761

27652762
#ifdef Q_OS_MAC
27662763
QgisApp::instance()->activateWindow();
@@ -2781,7 +2778,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
27812778
{
27822779
outputFileName += QLatin1String( ".pdf" );
27832780
}
2784-
settings.setValue( QStringLiteral( "lastSaveAsPdfFile" ), outputFileName, QgsSettings::App );
2781+
setLastExportPath( outputFileName );
27852782
}
27862783
else
27872784
{
@@ -2800,7 +2797,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
28002797
}
28012798

28022799

2803-
QString lastUsedDir = settings.value( QStringLiteral( "lastSaveAtlasAsPdfDir" ), QDir::homePath(), QgsSettings::App ).toString();
2800+
const QString lastUsedDir = defaultExportPath();
28042801

28052802
QFileDialog dlg( this, tr( "Export Atlas to Directory" ) );
28062803
dlg.setFileMode( QFileDialog::Directory );
@@ -2826,7 +2823,7 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
28262823
{
28272824
return;
28282825
}
2829-
settings.setValue( QStringLiteral( "lastSaveAtlasAsPdfDir" ), dir, QgsSettings::App );
2826+
setLastExportPath( dir );
28302827

28312828
// test directory (if it exists and is writable)
28322829
if ( !QDir( dir ).exists() || !QFileInfo( dir ).isWritable() )
@@ -2950,7 +2947,6 @@ void QgsLayoutDesignerDialog::exportAtlasToPdf()
29502947

29512948
void QgsLayoutDesignerDialog::exportReportToRaster()
29522949
{
2953-
QgsSettings s;
29542950
QString outputFileName = QgsFileUtils::stringToSafeFilename( mMasterLayout->name() );
29552951

29562952
QPair<QString, QString> fileNExt = QgsGuiUtils::getSaveAsImageName( this, tr( "Save Report As" ), outputFileName );
@@ -2961,6 +2957,8 @@ void QgsLayoutDesignerDialog::exportReportToRaster()
29612957
return;
29622958
}
29632959

2960+
setLastExportPath( fileNExt.first );
2961+
29642962
#ifdef Q_OS_MAC
29652963
QgisApp::instance()->activateWindow();
29662964
this->raise();
@@ -3051,10 +3049,8 @@ void QgsLayoutDesignerDialog::exportReportToSvg()
30513049
{
30523050
showSvgExportWarning();
30533051

3054-
QgsSettings settings;
3055-
QString lastUsedFile = settings.value( QStringLiteral( "lastSaveAsSvgFile" ), QStringLiteral( "qgis.svg" ), QgsSettings::App ).toString();
3056-
QFileInfo file( lastUsedFile );
3057-
QString outputFileName = file.path() + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".svg" );
3052+
const QString defaultPath = defaultExportPath();
3053+
QString outputFileName = defaultPath + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".svg" );
30583054

30593055
outputFileName = QFileDialog::getSaveFileName(
30603056
this,
@@ -3076,7 +3072,7 @@ void QgsLayoutDesignerDialog::exportReportToSvg()
30763072
this->raise();
30773073
#endif
30783074
bool prevSettingLabelsAsOutlines = mMasterLayout->layoutProject()->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), true );
3079-
settings.setValue( QStringLiteral( "lastSaveAsSvgFile" ), outputFileName, QgsSettings::App );
3075+
setLastExportPath( outputFileName );
30803076

30813077
QgsLayoutExporter::SvgExportSettings svgSettings;
30823078
bool exportAsText = false;
@@ -3179,12 +3175,9 @@ void QgsLayoutDesignerDialog::exportReportToSvg()
31793175

31803176
void QgsLayoutDesignerDialog::exportReportToPdf()
31813177
{
3182-
QgsSettings settings;
3178+
const QString defaultPath = defaultExportPath();
31833179

3184-
QString lastUsedFile = settings.value( QStringLiteral( "lastSaveAsPdfFile" ), QStringLiteral( "qgis.pdf" ), QgsSettings::App ).toString();
3185-
QFileInfo file( lastUsedFile );
3186-
3187-
QString outputFileName = file.path() + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".pdf" );
3180+
QString outputFileName = defaultPath + '/' + QgsFileUtils::stringToSafeFilename( mMasterLayout->name() ) + QStringLiteral( ".pdf" );
31883181

31893182
#ifdef Q_OS_MAC
31903183
QgisApp::instance()->activateWindow();
@@ -3205,7 +3198,7 @@ void QgsLayoutDesignerDialog::exportReportToPdf()
32053198
{
32063199
outputFileName += QLatin1String( ".pdf" );
32073200
}
3208-
settings.setValue( QStringLiteral( "lastSaveAsPdfFile" ), outputFileName, QgsSettings::App );
3201+
setLastExportPath( outputFileName );
32093202

32103203
mView->setPaintingEnabled( false );
32113204
QgsTemporaryCursorOverride cursorOverride( Qt::BusyCursor );
@@ -4151,6 +4144,36 @@ void QgsLayoutDesignerDialog::updateActionNames( QgsMasterLayoutInterface::Type
41514144
}
41524145
}
41534146

4147+
QString QgsLayoutDesignerDialog::defaultExportPath() const
4148+
{
4149+
// first priority - last export folder saved in project
4150+
const QString projectLastExportPath = QgsFileUtils::findClosestExistingPath( QgsProject::instance()->readEntry( QStringLiteral( "Layouts" ), QStringLiteral( "/lastLayoutExportDir" ), QString() ) );
4151+
if ( !projectLastExportPath.isEmpty() )
4152+
return projectLastExportPath;
4153+
4154+
// second priority - project home path
4155+
const QString projectHome = QgsFileUtils::findClosestExistingPath( QgsProject::instance()->homePath() );
4156+
if ( !projectHome.isEmpty() )
4157+
return projectHome;
4158+
4159+
// last priority - app setting last export folder, with homepath as backup
4160+
QgsSettings s;
4161+
return QgsFileUtils::findClosestExistingPath( s.value( QStringLiteral( "lastLayoutExportDir" ), QDir::homePath(), QgsSettings::App ).toString() );
4162+
}
4163+
4164+
void QgsLayoutDesignerDialog::setLastExportPath( const QString &path ) const
4165+
{
4166+
QFileInfo fi( path );
4167+
QString savePath;
4168+
if ( fi.isFile() )
4169+
savePath = fi.path();
4170+
else
4171+
savePath = path;
4172+
4173+
QgsProject::instance()->writeEntry( QStringLiteral( "Layouts" ), QStringLiteral( "/lastLayoutExportDir" ), savePath );
4174+
QgsSettings().setValue( QStringLiteral( "lastLayoutExportDir" ), savePath, QgsSettings::App );
4175+
}
4176+
41544177
void QgsLayoutDesignerDialog::updateWindowTitle()
41554178
{
41564179
QString title;

‎src/app/layout/qgslayoutdesignerdialog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
502502
QString reportTypeString();
503503
void updateActionNames( QgsMasterLayoutInterface::Type type );
504504

505+
QString defaultExportPath() const;
506+
void setLastExportPath( const QString &path ) const;
507+
505508
};
506509

507510
#endif // QGSLAYOUTDESIGNERDIALOG_H

‎src/core/qgsfileutils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ QString QgsFileUtils::stringToSafeFilename( const QString &string )
9696

9797
QString QgsFileUtils::findClosestExistingPath( const QString &path )
9898
{
99+
if ( path.isEmpty() )
100+
return QString();
101+
99102
QDir currentPath;
100103
QFileInfo fi( path );
101104
if ( fi.isFile() )

0 commit comments

Comments
 (0)
Please sign in to comment.