Skip to content

Commit 7301a3e

Browse files
committed
QFileDialog last used paths storage improved
1 parent 73e2a8f commit 7301a3e

7 files changed

Lines changed: 85 additions & 13 deletions

File tree

gui/applicationdialog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QMessageBox>
2424
#include "applicationdialog.h"
2525
#include "application.h"
26+
#include "common.h"
2627

2728

2829
ApplicationDialog::ApplicationDialog(const QString &title,
@@ -59,10 +60,11 @@ void ApplicationDialog::Browse()
5960
#endif // Q_WS_WIN
6061
QString selectedFile = QFileDialog::getOpenFileName(this,
6162
tr("Select viewer application"),
62-
QString(),
63+
GetPath(SETTINGS_LAST_APP_PATH),
6364
filter);
6465

6566
if (!selectedFile.isEmpty()) {
67+
SetPath(SETTINGS_LAST_APP_PATH, selectedFile, false);
6668
QString path(QDir::toNativeSeparators(selectedFile));
6769
mUI.mPath->setText(path);
6870
}

gui/common.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
#include "common.h"
21+
#include <QSettings>
22+
#include <QFileInfo>
23+
#include <QDir>
24+
25+
26+
QString GetPath(const QString &type)
27+
{
28+
QSettings settings;
29+
const QString path = settings.value(type, "").toString();
30+
if (path.isEmpty())
31+
return settings.value(SETTINGS_LAST_USED_PATH, "").toString();
32+
return path;
33+
}
34+
35+
void SetPath(const QString &type, const QString &value, bool storeAsLastUsed /* = true */)
36+
{
37+
QSettings settings;
38+
settings.setValue(type, value);
39+
if (storeAsLastUsed) {
40+
// file name and especially its extension is not portable between types so strip it
41+
const QFileInfo fi(value);
42+
if (fi.isFile()) {
43+
settings.setValue(SETTINGS_LAST_USED_PATH, fi.dir().path());
44+
}
45+
else {
46+
settings.setValue(SETTINGS_LAST_USED_PATH, value);
47+
}
48+
}
49+
}

gui/common.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef COMMON_H
2020
#define COMMON_H
2121

22+
#include <QString>
23+
2224
/// @addtogroup GUI
2325
/// @{
2426

@@ -59,7 +61,6 @@
5961
#define SETTINGS_STD_POSIX "Platform Posix"
6062

6163
// Other settings
62-
#define SETTINGS_CHECK_PATH "Check path"
6364
#define SETTINGS_CHECK_FORCE "Check force"
6465
#define SETTINGS_CHECK_THREADS "Check threads"
6566
#define SETTINGS_SHOW_FULL_PATH "Show full path"
@@ -82,7 +83,18 @@
8283
#define PROGRESS_MAX 1024.0
8384

8485
#define SETTINGS_CHECKED_PLATFORM "Checked platform"
86+
87+
#define SETTINGS_LAST_USED_PATH "Last used path"
88+
#define SETTINGS_LAST_CHECK_PATH "Last check path"
8589
#define SETTINGS_LAST_PROJECT_PATH "Last project path"
90+
#define SETTINGS_LAST_RESULT_PATH "Last result path"
91+
#define SETTINGS_LAST_SOURCE_PATH "Last source path"
92+
#define SETTINGS_LAST_INCLUDE_PATH "Last include path"
93+
#define SETTINGS_LAST_APP_PATH "Last application path"
94+
95+
96+
QString GetPath(const QString &type);
97+
void SetPath(const QString &type, const QString &value, bool storeAsLastUsed = true);
8698

8799
/// @}
88100
#endif

gui/gui.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ SOURCES += aboutdialog.cpp \
106106
applicationlist.cpp \
107107
checkstatistics.cpp \
108108
checkthread.cpp \
109+
common.cpp \
109110
csvreport.cpp \
110111
erroritem.cpp \
111112
filelist.cpp \

gui/mainwindow.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
391391
if (mode == QFileDialog::ExistingFiles) {
392392
selected = QFileDialog::getOpenFileNames(this,
393393
tr("Select files to check"),
394-
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
394+
GetPath(SETTINGS_LAST_CHECK_PATH));
395395
if (selected.isEmpty())
396396
mCurrentDirectory.clear();
397397
else {
@@ -402,7 +402,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
402402
} else if (mode == QFileDialog::DirectoryOnly) {
403403
QString dir = QFileDialog::getExistingDirectory(this,
404404
tr("Select directory to check"),
405-
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
405+
GetPath(SETTINGS_LAST_CHECK_PATH));
406406
if (!dir.isEmpty()) {
407407
qDebug() << "Setting current directory to: " << dir;
408408
mCurrentDirectory = dir;
@@ -411,7 +411,9 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
411411
FormatAndSetTitle(dir);
412412
}
413413
}
414-
414+
415+
SetPath(SETTINGS_LAST_CHECK_PATH, mCurrentDirectory);
416+
415417
return selected;
416418
}
417419

@@ -670,7 +672,7 @@ void MainWindow::OpenResults()
670672
const QString filter(tr("XML files (*.xml)"));
671673
QString selectedFile = QFileDialog::getOpenFileName(this,
672674
tr("Open the report file"),
673-
QString(),
675+
GetPath(SETTINGS_LAST_RESULT_PATH),
674676
filter,
675677
&selectedFilter);
676678

@@ -684,6 +686,7 @@ void MainWindow::LoadResults(const QString selectedFile)
684686
if (!selectedFile.isEmpty()) {
685687
mUI.mResults->Clear(true);
686688
mUI.mResults->ReadErrorsXml(selectedFile);
689+
SetPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
687690
}
688691
}
689692

@@ -815,7 +818,7 @@ void MainWindow::Save()
815818
const QString filter(tr("XML files version 2 (*.xml);;XML files version 1 (*.xml);;Text files (*.txt);;CSV files (*.csv)"));
816819
QString selectedFile = QFileDialog::getSaveFileName(this,
817820
tr("Save the report file"),
818-
QString(),
821+
GetPath(SETTINGS_LAST_RESULT_PATH),
819822
filter,
820823
&selectedFilter);
821824

@@ -847,6 +850,7 @@ void MainWindow::Save()
847850
}
848851

849852
mUI.mResults->Save(selectedFile, type);
853+
SetPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
850854
}
851855
}
852856

@@ -935,13 +939,13 @@ void MainWindow::OpenProjectFile()
935939
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
936940
const QString filepath = QFileDialog::getOpenFileName(this,
937941
tr("Select Project File"),
938-
lastPath,
942+
GetPath(SETTINGS_LAST_PROJECT_PATH),
939943
filter);
940944

941945
if (!filepath.isEmpty()) {
942946
const QFileInfo fi(filepath);
943947
if (fi.exists() && fi.isFile() && fi.isReadable()) {
944-
mSettings->setValue(SETTINGS_LAST_PROJECT_PATH, fi.path());
948+
SetPath(SETTINGS_LAST_PROJECT_PATH, filepath);
945949
LoadProjectFile(filepath);
946950
}
947951
}
@@ -1018,12 +1022,14 @@ void MainWindow::NewProjectFile()
10181022
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
10191023
QString filepath = QFileDialog::getSaveFileName(this,
10201024
tr("Select Project Filename"),
1021-
QString(),
1025+
GetPath(SETTINGS_LAST_PROJECT_PATH),
10221026
filter);
10231027

10241028
if (filepath.isEmpty())
10251029
return;
10261030

1031+
SetPath(SETTINGS_LAST_PROJECT_PATH, filepath);
1032+
10271033
EnableProjectActions(true);
10281034
QFileInfo inf(filepath);
10291035
const QString filename = inf.fileName();

gui/resultstree.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,12 @@ QString ResultsTree::AskFileDir(const QString &file)
694694
msgbox.setText(text);
695695
msgbox.setIcon(QMessageBox::Warning);
696696
msgbox.exec();
697-
697+
698698
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"),
699-
"",
699+
GetPath(SETTINGS_LAST_SOURCE_PATH),
700700
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
701701
mCheckPath = dir;
702+
SetPath(SETTINGS_LAST_SOURCE_PATH, dir);
702703
return dir;
703704
}
704705

gui/settingsdialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,11 @@ void SettingsDialog::AddIncludePath()
310310
{
311311
QString selectedDir = QFileDialog::getExistingDirectory(this,
312312
tr("Select include directory"),
313-
QString());
313+
GetPath(SETTINGS_LAST_INCLUDE_PATH));
314314

315315
if (!selectedDir.isEmpty()) {
316316
AddIncludePath(selectedDir);
317+
SetPath(SETTINGS_LAST_INCLUDE_PATH, selectedDir);
317318
}
318319
}
319320

0 commit comments

Comments
 (0)