QFileDialog last used paths storage improved

This commit is contained in:
Mateusz Pusz 2013-02-08 16:18:05 +01:00
parent 73e2a8fdb5
commit 7301a3e64d
7 changed files with 85 additions and 13 deletions

View File

@ -23,6 +23,7 @@
#include <QMessageBox>
#include "applicationdialog.h"
#include "application.h"
#include "common.h"
ApplicationDialog::ApplicationDialog(const QString &title,
@ -59,10 +60,11 @@ void ApplicationDialog::Browse()
#endif // Q_WS_WIN
QString selectedFile = QFileDialog::getOpenFileName(this,
tr("Select viewer application"),
QString(),
GetPath(SETTINGS_LAST_APP_PATH),
filter);
if (!selectedFile.isEmpty()) {
SetPath(SETTINGS_LAST_APP_PATH, selectedFile, false);
QString path(QDir::toNativeSeparators(selectedFile));
mUI.mPath->setText(path);
}

49
gui/common.cpp Normal file
View File

@ -0,0 +1,49 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include <QSettings>
#include <QFileInfo>
#include <QDir>
QString GetPath(const QString &type)
{
QSettings settings;
const QString path = settings.value(type, "").toString();
if (path.isEmpty())
return settings.value(SETTINGS_LAST_USED_PATH, "").toString();
return path;
}
void SetPath(const QString &type, const QString &value, bool storeAsLastUsed /* = true */)
{
QSettings settings;
settings.setValue(type, value);
if (storeAsLastUsed) {
// file name and especially its extension is not portable between types so strip it
const QFileInfo fi(value);
if (fi.isFile()) {
settings.setValue(SETTINGS_LAST_USED_PATH, fi.dir().path());
}
else {
settings.setValue(SETTINGS_LAST_USED_PATH, value);
}
}
}

View File

@ -19,6 +19,8 @@
#ifndef COMMON_H
#define COMMON_H
#include <QString>
/// @addtogroup GUI
/// @{
@ -59,7 +61,6 @@
#define SETTINGS_STD_POSIX "Platform Posix"
// Other settings
#define SETTINGS_CHECK_PATH "Check path"
#define SETTINGS_CHECK_FORCE "Check force"
#define SETTINGS_CHECK_THREADS "Check threads"
#define SETTINGS_SHOW_FULL_PATH "Show full path"
@ -82,7 +83,18 @@
#define PROGRESS_MAX 1024.0
#define SETTINGS_CHECKED_PLATFORM "Checked platform"
#define SETTINGS_LAST_USED_PATH "Last used path"
#define SETTINGS_LAST_CHECK_PATH "Last check path"
#define SETTINGS_LAST_PROJECT_PATH "Last project path"
#define SETTINGS_LAST_RESULT_PATH "Last result path"
#define SETTINGS_LAST_SOURCE_PATH "Last source path"
#define SETTINGS_LAST_INCLUDE_PATH "Last include path"
#define SETTINGS_LAST_APP_PATH "Last application path"
QString GetPath(const QString &type);
void SetPath(const QString &type, const QString &value, bool storeAsLastUsed = true);
/// @}
#endif

View File

@ -106,6 +106,7 @@ SOURCES += aboutdialog.cpp \
applicationlist.cpp \
checkstatistics.cpp \
checkthread.cpp \
common.cpp \
csvreport.cpp \
erroritem.cpp \
filelist.cpp \

View File

@ -391,7 +391,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
if (mode == QFileDialog::ExistingFiles) {
selected = QFileDialog::getOpenFileNames(this,
tr("Select files to check"),
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
GetPath(SETTINGS_LAST_CHECK_PATH));
if (selected.isEmpty())
mCurrentDirectory.clear();
else {
@ -402,7 +402,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
} else if (mode == QFileDialog::DirectoryOnly) {
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select directory to check"),
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
GetPath(SETTINGS_LAST_CHECK_PATH));
if (!dir.isEmpty()) {
qDebug() << "Setting current directory to: " << dir;
mCurrentDirectory = dir;
@ -412,6 +412,8 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
}
}
SetPath(SETTINGS_LAST_CHECK_PATH, mCurrentDirectory);
return selected;
}
@ -670,7 +672,7 @@ void MainWindow::OpenResults()
const QString filter(tr("XML files (*.xml)"));
QString selectedFile = QFileDialog::getOpenFileName(this,
tr("Open the report file"),
QString(),
GetPath(SETTINGS_LAST_RESULT_PATH),
filter,
&selectedFilter);
@ -684,6 +686,7 @@ void MainWindow::LoadResults(const QString selectedFile)
if (!selectedFile.isEmpty()) {
mUI.mResults->Clear(true);
mUI.mResults->ReadErrorsXml(selectedFile);
SetPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
}
}
@ -815,7 +818,7 @@ void MainWindow::Save()
const QString filter(tr("XML files version 2 (*.xml);;XML files version 1 (*.xml);;Text files (*.txt);;CSV files (*.csv)"));
QString selectedFile = QFileDialog::getSaveFileName(this,
tr("Save the report file"),
QString(),
GetPath(SETTINGS_LAST_RESULT_PATH),
filter,
&selectedFilter);
@ -847,6 +850,7 @@ void MainWindow::Save()
}
mUI.mResults->Save(selectedFile, type);
SetPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
}
}
@ -935,13 +939,13 @@ void MainWindow::OpenProjectFile()
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
const QString filepath = QFileDialog::getOpenFileName(this,
tr("Select Project File"),
lastPath,
GetPath(SETTINGS_LAST_PROJECT_PATH),
filter);
if (!filepath.isEmpty()) {
const QFileInfo fi(filepath);
if (fi.exists() && fi.isFile() && fi.isReadable()) {
mSettings->setValue(SETTINGS_LAST_PROJECT_PATH, fi.path());
SetPath(SETTINGS_LAST_PROJECT_PATH, filepath);
LoadProjectFile(filepath);
}
}
@ -1018,12 +1022,14 @@ void MainWindow::NewProjectFile()
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
QString filepath = QFileDialog::getSaveFileName(this,
tr("Select Project Filename"),
QString(),
GetPath(SETTINGS_LAST_PROJECT_PATH),
filter);
if (filepath.isEmpty())
return;
SetPath(SETTINGS_LAST_PROJECT_PATH, filepath);
EnableProjectActions(true);
QFileInfo inf(filepath);
const QString filename = inf.fileName();

View File

@ -696,9 +696,10 @@ QString ResultsTree::AskFileDir(const QString &file)
msgbox.exec();
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"),
"",
GetPath(SETTINGS_LAST_SOURCE_PATH),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
mCheckPath = dir;
SetPath(SETTINGS_LAST_SOURCE_PATH, dir);
return dir;
}

View File

@ -310,10 +310,11 @@ void SettingsDialog::AddIncludePath()
{
QString selectedDir = QFileDialog::getExistingDirectory(this,
tr("Select include directory"),
QString());
GetPath(SETTINGS_LAST_INCLUDE_PATH));
if (!selectedDir.isEmpty()) {
AddIncludePath(selectedDir);
SetPath(SETTINGS_LAST_INCLUDE_PATH, selectedDir);
}
}