Merge pull request #134 from mpusz/remember_last_path

QFileDialog last used paths storage improved
This commit is contained in:
Daniel Marjamäki 2013-02-19 11:17:05 -08:00
commit 8779eb90e4
7 changed files with 94 additions and 14 deletions

View File

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

44
gui/common.cpp Normal file
View File

@ -0,0 +1,44 @@
/*
* 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;
QString path = settings.value(type, "").toString();
if (path.isEmpty()) {
// if not set, fallback to last check path hoping that it will be close enough
path = settings.value(SETTINGS_LAST_CHECK_PATH, "").toString();
if (path.isEmpty())
// if not set, return user's home directory as the best we can do for now
return QDir::homePath();
}
return path;
}
void SetPath(const QString &type, const QString &value)
{
QSettings settings;
settings.setValue(type, value);
}

View File

@ -19,6 +19,8 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <QString>
/// @addtogroup GUI /// @addtogroup GUI
/// @{ /// @{
@ -59,7 +61,6 @@
#define SETTINGS_STD_POSIX "Platform Posix" #define SETTINGS_STD_POSIX "Platform Posix"
// Other settings // Other settings
#define SETTINGS_CHECK_PATH "Check path"
#define SETTINGS_CHECK_FORCE "Check force" #define SETTINGS_CHECK_FORCE "Check force"
#define SETTINGS_CHECK_THREADS "Check threads" #define SETTINGS_CHECK_THREADS "Check threads"
#define SETTINGS_SHOW_FULL_PATH "Show full path" #define SETTINGS_SHOW_FULL_PATH "Show full path"
@ -82,7 +83,31 @@
#define PROGRESS_MAX 1024.0 #define PROGRESS_MAX 1024.0
#define SETTINGS_CHECKED_PLATFORM "Checked platform" #define SETTINGS_CHECKED_PLATFORM "Checked platform"
#define SETTINGS_LAST_CHECK_PATH "Last check path"
#define SETTINGS_LAST_PROJECT_PATH "Last project 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"
/**
* @brief Obtains the path of specified type
* Returns the path of specified type if not empty. Otherwise returns last check
* path if valid or user's home directory.
* @param type Type of path to obtain
* @return Best path fo provided type
*/
QString GetPath(const QString &type);
/**
* @brief Stores last used path of specified type
* Stores provided path as last used path for specified type.
* @param type Type of the path to store
* @param value Path to store
*/
void SetPath(const QString &type, const QString &value);
/// @} /// @}
#endif #endif

View File

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

View File

@ -332,7 +332,7 @@ void MainWindow::DoCheckFiles(const QStringList &files)
mThread->SetFiles(fileNames); mThread->SetFiles(fileNames);
QDir inf(mCurrentDirectory); QDir inf(mCurrentDirectory);
const QString checkPath = inf.canonicalPath(); const QString checkPath = inf.canonicalPath();
mSettings->setValue(SETTINGS_CHECK_PATH, checkPath); SetPath(SETTINGS_LAST_CHECK_PATH, checkPath);
CheckLockDownUI(); // lock UI while checking CheckLockDownUI(); // lock UI while checking
@ -391,7 +391,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
if (mode == QFileDialog::ExistingFiles) { if (mode == QFileDialog::ExistingFiles) {
selected = QFileDialog::getOpenFileNames(this, selected = QFileDialog::getOpenFileNames(this,
tr("Select files to check"), tr("Select files to check"),
mSettings->value(SETTINGS_CHECK_PATH, "").toString()); GetPath(SETTINGS_LAST_CHECK_PATH));
if (selected.isEmpty()) if (selected.isEmpty())
mCurrentDirectory.clear(); mCurrentDirectory.clear();
else { else {
@ -402,7 +402,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
} else if (mode == QFileDialog::DirectoryOnly) { } else if (mode == QFileDialog::DirectoryOnly) {
QString dir = QFileDialog::getExistingDirectory(this, QString dir = QFileDialog::getExistingDirectory(this,
tr("Select directory to check"), tr("Select directory to check"),
mSettings->value(SETTINGS_CHECK_PATH, "").toString()); GetPath(SETTINGS_LAST_CHECK_PATH));
if (!dir.isEmpty()) { if (!dir.isEmpty()) {
qDebug() << "Setting current directory to: " << dir; qDebug() << "Setting current directory to: " << dir;
mCurrentDirectory = dir; mCurrentDirectory = dir;
@ -411,7 +411,9 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
FormatAndSetTitle(dir); FormatAndSetTitle(dir);
} }
} }
SetPath(SETTINGS_LAST_CHECK_PATH, mCurrentDirectory);
return selected; return selected;
} }
@ -670,7 +672,7 @@ void MainWindow::OpenResults()
const QString filter(tr("XML files (*.xml)")); const QString filter(tr("XML files (*.xml)"));
QString selectedFile = QFileDialog::getOpenFileName(this, QString selectedFile = QFileDialog::getOpenFileName(this,
tr("Open the report file"), tr("Open the report file"),
QString(), GetPath(SETTINGS_LAST_RESULT_PATH),
filter, filter,
&selectedFilter); &selectedFilter);
@ -684,6 +686,7 @@ void MainWindow::LoadResults(const QString selectedFile)
if (!selectedFile.isEmpty()) { if (!selectedFile.isEmpty()) {
mUI.mResults->Clear(true); mUI.mResults->Clear(true);
mUI.mResults->ReadErrorsXml(selectedFile); 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)")); 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, QString selectedFile = QFileDialog::getSaveFileName(this,
tr("Save the report file"), tr("Save the report file"),
QString(), GetPath(SETTINGS_LAST_RESULT_PATH),
filter, filter,
&selectedFilter); &selectedFilter);
@ -847,6 +850,7 @@ void MainWindow::Save()
} }
mUI.mResults->Save(selectedFile, type); 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 filter = tr("Project files (*.cppcheck);;All files(*.*)");
const QString filepath = QFileDialog::getOpenFileName(this, const QString filepath = QFileDialog::getOpenFileName(this,
tr("Select Project File"), tr("Select Project File"),
lastPath, GetPath(SETTINGS_LAST_PROJECT_PATH),
filter); filter);
if (!filepath.isEmpty()) { if (!filepath.isEmpty()) {
const QFileInfo fi(filepath); const QFileInfo fi(filepath);
if (fi.exists() && fi.isFile() && fi.isReadable()) { if (fi.exists() && fi.isFile() && fi.isReadable()) {
mSettings->setValue(SETTINGS_LAST_PROJECT_PATH, fi.path()); SetPath(SETTINGS_LAST_PROJECT_PATH, filepath);
LoadProjectFile(filepath); LoadProjectFile(filepath);
} }
} }
@ -1018,12 +1022,14 @@ void MainWindow::NewProjectFile()
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)"); const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
QString filepath = QFileDialog::getSaveFileName(this, QString filepath = QFileDialog::getSaveFileName(this,
tr("Select Project Filename"), tr("Select Project Filename"),
QString(), GetPath(SETTINGS_LAST_PROJECT_PATH),
filter); filter);
if (filepath.isEmpty()) if (filepath.isEmpty())
return; return;
SetPath(SETTINGS_LAST_PROJECT_PATH, filepath);
EnableProjectActions(true); EnableProjectActions(true);
QFileInfo inf(filepath); QFileInfo inf(filepath);
const QString filename = inf.fileName(); const QString filename = inf.fileName();

View File

@ -694,11 +694,12 @@ QString ResultsTree::AskFileDir(const QString &file)
msgbox.setText(text); msgbox.setText(text);
msgbox.setIcon(QMessageBox::Warning); msgbox.setIcon(QMessageBox::Warning);
msgbox.exec(); msgbox.exec();
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"), QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"),
"", GetPath(SETTINGS_LAST_SOURCE_PATH),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
mCheckPath = dir; mCheckPath = dir;
SetPath(SETTINGS_LAST_SOURCE_PATH, dir);
return dir; return dir;
} }

View File

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