Skip to content

Commit 861ecec

Browse files
committed
Add option to remember last location for file dialogs
Add an option to remember the location of the last opened or saved file and use it as the default location for the next file dialog because always going back to the default location set in the preferences dialog can be a real hastle. See issues sqlitebrowser#224, sqlitebrowser#276 and sqlitebrowser#281.
1 parent f21ff61 commit 861ecec

File tree

11 files changed

+182
-79
lines changed

11 files changed

+182
-79
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ set(SQLB_MOC_HDR
7373
src/CipherDialog.h
7474
src/ExportSqlDialog.h
7575
src/SqlUiLexer.h
76+
src/FileDialog.h
7677
)
7778

7879
set(SQLB_SRC
@@ -101,6 +102,7 @@ set(SQLB_SRC
101102
src/CipherDialog.cpp
102103
src/ExportSqlDialog.cpp
103104
src/SqlUiLexer.cpp
105+
src/FileDialog.cpp
104106
)
105107

106108
set(SQLB_FORMS

src/EditDialog.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include "sqlitedb.h"
44
#include "PreferencesDialog.h"
55
#include "src/qhexedit.h"
6+
#include "FileDialog.h"
67

7-
#include <QFileDialog>
88
#include <QKeySequence>
99
#include <QShortcut>
1010

@@ -75,10 +75,9 @@ void EditDialog::importData()
7575
for(int i=0;i<image_formats_list.size();++i)
7676
image_formats.append(QString("*.%1 ").arg(QString::fromUtf8(image_formats_list.at(i))));
7777

78-
QString fileName = QFileDialog::getOpenFileName(
78+
QString fileName = FileDialog::getOpenFileName(
7979
this,
80-
tr("Choose a file"),
81-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString()
80+
tr("Choose a file")
8281
#ifndef Q_OS_MAC // Filters on OS X are buggy
8382
, tr("Text files(*.txt);;Image files(%1);;All files(*)").arg(image_formats)
8483
#endif
@@ -99,10 +98,9 @@ void EditDialog::importData()
9998

10099
void EditDialog::exportData()
101100
{
102-
QString fileName = QFileDialog::getSaveFileName(
101+
QString fileName = FileDialog::getSaveFileName(
103102
this,
104103
tr("Choose a filename to export data"),
105-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
106104
tr("Text files(*.txt);;All files(*)"));
107105

108106
if(fileName.size() > 0)

src/ExportCsvDialog.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include "PreferencesDialog.h"
55
#include "sqlitetablemodel.h"
66
#include "sqlite.h"
7+
#include "FileDialog.h"
78

89
#include <QFile>
910
#include <QTextStream>
1011
#include <QMessageBox>
11-
#include <QFileDialog>
1212
#include <QSettings>
1313

1414
ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent, const QString& query, const QString& selection)
@@ -145,10 +145,9 @@ void ExportCsvDialog::accept()
145145
if(!m_sQuery.isEmpty())
146146
{
147147
// called from sqlexecute query tab
148-
QString sFilename = QFileDialog::getSaveFileName(
148+
QString sFilename = FileDialog::getSaveFileName(
149149
this,
150150
tr("Choose a filename to export data"),
151-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
152151
tr("Text files(*.csv *.txt)"));
153152
if(sFilename.isEmpty())
154153
{
@@ -174,10 +173,9 @@ void ExportCsvDialog::accept()
174173
QStringList filenames;
175174
if(selectedItems.size() == 1)
176175
{
177-
QString fileName = QFileDialog::getSaveFileName(
176+
QString fileName = FileDialog::getSaveFileName(
178177
this,
179178
tr("Choose a filename to export data"),
180-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
181179
tr("Text files(*.csv *.txt)"));
182180
if(fileName.isEmpty())
183181
{
@@ -190,10 +188,9 @@ void ExportCsvDialog::accept()
190188
else
191189
{
192190
// ask for folder
193-
QString csvfolder = QFileDialog::getExistingDirectory(
191+
QString csvfolder = FileDialog::getExistingDirectory(
194192
this,
195193
tr("Choose a directory"),
196-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
197194
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
198195

199196
if(csvfolder.isEmpty())

src/ExportSqlDialog.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include "PreferencesDialog.h"
55
#include "sqlitetablemodel.h"
66
#include "sqlite.h"
7+
#include "FileDialog.h"
78

89
#include <QFile>
910
#include <QMessageBox>
10-
#include <QFileDialog>
1111
#include <QSettings>
1212

1313
static QString sSettingsGroup("exportsql");
@@ -62,10 +62,9 @@ void ExportSqlDialog::accept()
6262
tr("Please select at least 1 table."));
6363
return;
6464
}
65-
QString fileName = QFileDialog::getSaveFileName(
65+
QString fileName = FileDialog::getSaveFileName(
6666
this,
6767
tr("Choose a filename to export"),
68-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
6968
tr("Text files(*.sql *.txt)"));
7069

7170
if(fileName.isEmpty())

src/FileDialog.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "FileDialog.h"
2+
#include "PreferencesDialog.h"
3+
4+
QString FileDialog::getOpenFileName(QWidget* parent, const QString& caption, const QString &filter, QString *selectedFilter, Options options)
5+
{
6+
QString result = QFileDialog::getOpenFileName(parent, caption, getFileDialogPath(), filter, selectedFilter, options);
7+
if(!result.isEmpty())
8+
setFileDialogPath(result);
9+
return result;
10+
}
11+
12+
QString FileDialog::getSaveFileName(QWidget* parent, const QString& caption, const QString& filter, QString* selectedFilter, Options options)
13+
{
14+
QString result = QFileDialog::getSaveFileName(parent, caption, getFileDialogPath(), filter, selectedFilter, options);
15+
if(!result.isEmpty())
16+
setFileDialogPath(result);
17+
return result;
18+
}
19+
20+
QString FileDialog::getExistingDirectory(QWidget* parent, const QString& caption, Options options)
21+
{
22+
QString result = QFileDialog::getExistingDirectory(parent, caption, getFileDialogPath(), options);
23+
if(!result.isEmpty())
24+
setFileDialogPath(result);
25+
return result;
26+
}
27+
28+
QString FileDialog::getFileDialogPath()
29+
{
30+
switch(PreferencesDialog::getSettingsValue("db", "savedefaultlocation").toInt())
31+
{
32+
case 0: // Remember last location
33+
case 2: // Remember last location for current session only
34+
return PreferencesDialog::getSettingsValue("db", "lastlocation").toString();
35+
case 1: // Always use this locations
36+
return PreferencesDialog::getSettingsValue("db", "defaultlocation").toString();
37+
default:
38+
return "";
39+
}
40+
}
41+
42+
void FileDialog::setFileDialogPath(const QString& new_path)
43+
{
44+
QString dir = QFileInfo(new_path).absolutePath();
45+
46+
switch(PreferencesDialog::getSettingsValue("db", "savedefaultlocation").toInt())
47+
{
48+
case 0: // Remember last location
49+
PreferencesDialog::setSettingsValue("db", "lastlocation", dir);
50+
break;
51+
case 2: // Remember last location for current session only
52+
PreferencesDialog::setSettingsValue("db", "lastlocation", dir, true);
53+
break;
54+
case 1: // Always use this locations
55+
break; // Do nothing
56+
}
57+
}

src/FileDialog.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef FILEDIALOG_H
2+
#define FILEDIALOG_H
3+
4+
#include <QFileDialog>
5+
6+
class FileDialog : public QFileDialog
7+
{
8+
public:
9+
static QString getOpenFileName(QWidget* parent = 0, const QString& caption = QString(),
10+
const QString& filter = QString(), QString* selectedFilter = 0,
11+
Options options = 0);
12+
static QString getSaveFileName(QWidget* parent = 0, const QString& caption = QString(),
13+
const QString& filter = QString(), QString* selectedFilter = 0,
14+
Options options = 0);
15+
static QString getExistingDirectory(QWidget* parent = 0, const QString& caption = QString(),
16+
Options options = 0);
17+
18+
private:
19+
static QString getFileDialogPath();
20+
static void setFileDialogPath(const QString& new_path);
21+
};
22+
23+
#endif

src/MainWindow.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "CipherDialog.h"
1818
#include "ExportSqlDialog.h"
1919
#include "SqlUiLexer.h"
20+
#include "FileDialog.h"
2021

21-
#include <QFileDialog>
2222
#include <QFile>
2323
#include <QApplication>
2424
#include <QTextStream>
@@ -198,10 +198,9 @@ bool MainWindow::fileOpen(const QString& fileName, bool dontAddToRecentFiles)
198198
QString wFile = fileName;
199199
if (!QFile::exists(wFile))
200200
{
201-
wFile = QFileDialog::getOpenFileName(
201+
wFile = FileDialog::getOpenFileName(
202202
this,
203-
tr("Choose a database file"),
204-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString()
203+
tr("Choose a database file")
205204
#ifndef Q_OS_MAC // Filters on OS X are buggy
206205
, tr("SQLite database files (*.db *.sqlite *.sqlite3 *.db3);;All files (*)")
207206
#endif
@@ -243,7 +242,7 @@ bool MainWindow::fileOpen(const QString& fileName, bool dontAddToRecentFiles)
243242

244243
void MainWindow::fileNew()
245244
{
246-
QString fileName = QFileDialog::getSaveFileName(this, tr("Choose a filename to save under"), PreferencesDialog::getSettingsValue("db", "defaultlocation").toString());
245+
QString fileName = FileDialog::getSaveFileName(this, tr("Choose a filename to save under"));
247246
if(!fileName.isEmpty())
248247
{
249248
if(QFile::exists(fileName))
@@ -826,10 +825,9 @@ void MainWindow::mainTabSelected(int tabindex)
826825

827826
void MainWindow::importTableFromCSV()
828827
{
829-
QString wFile = QFileDialog::getOpenFileName(
828+
QString wFile = FileDialog::getOpenFileName(
830829
this,
831830
tr("Choose a text file"),
832-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
833831
tr("Text files(*.csv *.txt);;All files(*)"));
834832

835833
if (QFile::exists(wFile) )
@@ -898,10 +896,9 @@ void MainWindow::exportDatabaseToSQL()
898896
void MainWindow::importDatabaseFromSQL()
899897
{
900898
// Get file name to import
901-
QString fileName = QFileDialog::getOpenFileName(
899+
QString fileName = FileDialog::getOpenFileName(
902900
this,
903901
tr("Choose a file to import"),
904-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
905902
tr("Text files(*.sql *.txt);;All files(*)"));
906903

907904
// Cancel when file doesn't exist
@@ -916,10 +913,9 @@ void MainWindow::importDatabaseFromSQL()
916913
"If you answer no we will attempt to import the data in the SQL file to the current database."),
917914
QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) || !db.isOpen())
918915
{
919-
newDbFile = QFileDialog::getSaveFileName(
916+
newDbFile = FileDialog::getSaveFileName(
920917
this,
921-
tr("Choose a filename to save under"),
922-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString());
918+
tr("Choose a filename to save under"));
923919
if(QFile::exists(newDbFile))
924920
{
925921
QMessageBox::information(this, QApplication::applicationName(), tr("File %1 already exists. Please choose a different name.").arg(newDbFile));
@@ -1250,10 +1246,9 @@ unsigned int MainWindow::openSqlTab(bool resetCounter)
12501246

12511247
void MainWindow::openSqlFile()
12521248
{
1253-
QString file = QFileDialog::getOpenFileName(
1249+
QString file = FileDialog::getOpenFileName(
12541250
this,
12551251
tr("Select SQL file to open"),
1256-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
12571252
tr("Text files(*.sql *.txt);;All files(*)"));
12581253

12591254
if(QFile::exists(file))
@@ -1297,10 +1292,9 @@ void MainWindow::saveSqlFile()
12971292

12981293
void MainWindow::saveSqlFileAs()
12991294
{
1300-
QString file = QFileDialog::getSaveFileName(
1295+
QString file = FileDialog::getSaveFileName(
13011296
this,
13021297
tr("Select file name"),
1303-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
13041298
tr("Text files(*.sql *.txt);;All files(*)"));
13051299

13061300
if(!file.isEmpty())
@@ -1313,10 +1307,9 @@ void MainWindow::saveSqlFileAs()
13131307

13141308
void MainWindow::loadExtension()
13151309
{
1316-
QString file = QFileDialog::getOpenFileName(
1310+
QString file = FileDialog::getOpenFileName(
13171311
this,
13181312
tr("Select extension file"),
1319-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
13201313
tr("Extensions(*.so *.dll);;All files(*)"));
13211314

13221315
if(file.isEmpty())
@@ -1719,9 +1712,8 @@ void MainWindow::on_treePlotColumns_itemDoubleClicked(QTreeWidgetItem *item, int
17191712

17201713
void MainWindow::on_butSavePlot_clicked()
17211714
{
1722-
QString fileName = QFileDialog::getSaveFileName(this,
1715+
QString fileName = FileDialog::getSaveFileName(this,
17231716
tr("Choose a filename to save under"),
1724-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
17251717
tr("PNG(*.png);;JPG(*.jpg);;PDF(*.pdf);;BMP(*.bmp);;All Files(*)")
17261718
);
17271719
if(!fileName.isEmpty())
@@ -1790,9 +1782,8 @@ bool MainWindow::loadProject(QString filename)
17901782
// Show the open file dialog when no filename was passed as parameter
17911783
if(filename.isEmpty())
17921784
{
1793-
filename = QFileDialog::getOpenFileName(this,
1785+
filename = FileDialog::getOpenFileName(this,
17941786
tr("Choose a file to open"),
1795-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
17961787
tr("DB Browser for SQLite project file (*.sqbpro)"));
17971788
}
17981789

@@ -1930,9 +1921,8 @@ static void saveDbTreeState(const QTreeView* tree, QXmlStreamWriter& xml, QModel
19301921

19311922
void MainWindow::saveProject()
19321923
{
1933-
QString filename = QFileDialog::getSaveFileName(this,
1924+
QString filename = FileDialog::getSaveFileName(this,
19341925
tr("Choose a filename to save under"),
1935-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
19361926
tr("DB Browser for SQLite project file (*.sqbpro)")
19371927
);
19381928
if(!filename.isEmpty())
@@ -2014,10 +2004,9 @@ void MainWindow::saveProject()
20142004
void MainWindow::fileAttach()
20152005
{
20162006
// Get file name of database to attach
2017-
QString file = QFileDialog::getOpenFileName(
2007+
QString file = FileDialog::getOpenFileName(
20182008
this,
2019-
tr("Choose a database file"),
2020-
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString());
2009+
tr("Choose a database file"));
20212010
if(!QFile::exists(file))
20222011
return;
20232012

0 commit comments

Comments
 (0)